Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for SNMP over TCP #5870

Merged
merged 4 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@

[[constraint]]
name = "github.com/soniah/gosnmp"
branch = "master"
version = "1.22.0"

[[constraint]]
name = "github.com/StackExchange/wmi"
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/snmp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Resulting output:
### Config parameters

* `agents`: Default: `[]`
List of SNMP agents to connect to in the form of `IP[:PORT]`. If `:PORT` is unspecified, it defaults to `161`.
List of SNMP agents to connect to in the form of `[tcp://]IP[:PORT]`. If `:PORT` is unspecified, it defaults to `161`. When using the optional prefix `tcp://`, SNMP over TCP will be used. Otherwise UDP is used as the transport protocol.

* `version`: Default: `2`
SNMP protocol version to use.
Expand Down
4 changes: 4 additions & 0 deletions plugins/inputs/snmp/snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,10 @@ func (s *Snmp) getConnection(idx int) (snmpConnection, error) {
gs := gosnmpWrapper{&gosnmp.GoSNMP{}}
s.connectionCache[idx] = gs

if strings.HasPrefix(agent, "tcp://") {
agent = strings.TrimPrefix(agent, "tcp://")
gs.Transport = "tcp"
}
host, portStr, err := net.SplitHostPort(agent)
if err != nil {
if err, ok := err.(*net.AddrError); !ok || err.Err != "missing port in address" {
Expand Down
34 changes: 34 additions & 0 deletions plugins/inputs/snmp/snmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,46 @@ func TestGetSNMPConnection_v2(t *testing.T) {
assert.EqualValues(t, 567, gs.Port)
assert.Equal(t, gosnmp.Version2c, gs.Version)
assert.Equal(t, "foo", gs.Community)
assert.Equal(t, "udp", gs.Transport)

gsc, err = s.getConnection(1)
require.NoError(t, err)
gs = gsc.(gosnmpWrapper)
assert.Equal(t, "1.2.3.4", gs.Target)
assert.EqualValues(t, 161, gs.Port)
assert.Equal(t, "udp", gs.Transport)
}

func TestGetSNMPConnectionTCP(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
go stubTCPServer(&wg)
wg.Wait()

s := &Snmp{
Agents: []string{"tcp://127.0.0.1:56789"},
}
err := s.init()
require.NoError(t, err)

wg.Add(1)
gsc, err := s.getConnection(0)
require.NoError(t, err)
gs := gsc.(gosnmpWrapper)
assert.Equal(t, "127.0.0.1", gs.Target)
assert.EqualValues(t, 56789, gs.Port)
assert.Equal(t, "tcp", gs.Transport)
wg.Wait()
}

func stubTCPServer(wg *sync.WaitGroup) {
defer wg.Done()
tcpAddr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:56789")
tcpServer, _ := net.ListenTCP("tcp", tcpAddr)
defer tcpServer.Close()
wg.Done()
conn, _ := tcpServer.AcceptTCP()
defer conn.Close()
}

func TestGetSNMPConnection_v3(t *testing.T) {
Expand Down