2016-01-20 20:11:28 +00:00
|
|
|
package snmp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
|
2016-04-18 02:08:56 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-01-20 20:11:28 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSNMPErrorGet1(t *testing.T) {
|
|
|
|
get1 := Data{
|
|
|
|
Name: "oid1",
|
|
|
|
Unit: "octets",
|
|
|
|
Oid: ".1.3.6.1.2.1.2.2.1.16.1",
|
|
|
|
}
|
|
|
|
h := Host{
|
|
|
|
Collect: []string{"oid1"},
|
|
|
|
}
|
|
|
|
s := Snmp{
|
|
|
|
SnmptranslateFile: "bad_oid.txt",
|
|
|
|
Host: []Host{h},
|
|
|
|
Get: []Data{get1},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := s.Gather(&acc)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSNMPErrorGet2(t *testing.T) {
|
|
|
|
get1 := Data{
|
|
|
|
Name: "oid1",
|
|
|
|
Unit: "octets",
|
|
|
|
Oid: ".1.3.6.1.2.1.2.2.1.16.1",
|
|
|
|
}
|
|
|
|
h := Host{
|
|
|
|
Collect: []string{"oid1"},
|
|
|
|
}
|
|
|
|
s := Snmp{
|
|
|
|
Host: []Host{h},
|
|
|
|
Get: []Data{get1},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := s.Gather(&acc)
|
2016-04-18 02:08:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, len(acc.Metrics))
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSNMPErrorBulk(t *testing.T) {
|
|
|
|
bulk1 := Data{
|
|
|
|
Name: "oid1",
|
|
|
|
Unit: "octets",
|
|
|
|
Oid: ".1.3.6.1.2.1.2.2.1.16",
|
|
|
|
}
|
|
|
|
h := Host{
|
2016-01-27 18:09:14 +00:00
|
|
|
Address: testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
Collect: []string{"oid1"},
|
|
|
|
}
|
|
|
|
s := Snmp{
|
|
|
|
Host: []Host{h},
|
|
|
|
Bulk: []Data{bulk1},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := s.Gather(&acc)
|
2016-04-18 02:08:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, len(acc.Metrics))
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSNMPGet1(t *testing.T) {
|
2016-02-19 23:15:14 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
get1 := Data{
|
|
|
|
Name: "oid1",
|
|
|
|
Unit: "octets",
|
|
|
|
Oid: ".1.3.6.1.2.1.2.2.1.16.1",
|
|
|
|
}
|
|
|
|
h := Host{
|
2016-01-27 18:09:14 +00:00
|
|
|
Address: testutil.GetLocalHost() + ":31161",
|
2016-01-20 20:11:28 +00:00
|
|
|
Community: "telegraf",
|
|
|
|
Version: 2,
|
|
|
|
Timeout: 2.0,
|
|
|
|
Retries: 2,
|
|
|
|
Collect: []string{"oid1"},
|
|
|
|
}
|
|
|
|
s := Snmp{
|
|
|
|
Host: []Host{h},
|
|
|
|
Get: []Data{get1},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := s.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"oid1",
|
|
|
|
map[string]interface{}{
|
|
|
|
"oid1": uint(543846),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"unit": "octets",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSNMPGet2(t *testing.T) {
|
2016-02-19 23:15:14 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
get1 := Data{
|
|
|
|
Name: "oid1",
|
|
|
|
Oid: "ifNumber",
|
|
|
|
}
|
|
|
|
h := Host{
|
2016-01-27 18:09:14 +00:00
|
|
|
Address: testutil.GetLocalHost() + ":31161",
|
2016-01-20 20:11:28 +00:00
|
|
|
Community: "telegraf",
|
|
|
|
Version: 2,
|
|
|
|
Timeout: 2.0,
|
|
|
|
Retries: 2,
|
|
|
|
Collect: []string{"oid1"},
|
|
|
|
}
|
|
|
|
s := Snmp{
|
|
|
|
SnmptranslateFile: "./testdata/oids.txt",
|
|
|
|
Host: []Host{h},
|
|
|
|
Get: []Data{get1},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := s.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifNumber",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifNumber": int(4),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"instance": "0",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSNMPGet3(t *testing.T) {
|
2016-02-19 23:15:14 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
get1 := Data{
|
|
|
|
Name: "oid1",
|
|
|
|
Unit: "octets",
|
|
|
|
Oid: "ifSpeed",
|
|
|
|
Instance: "1",
|
|
|
|
}
|
|
|
|
h := Host{
|
2016-01-27 18:09:14 +00:00
|
|
|
Address: testutil.GetLocalHost() + ":31161",
|
2016-01-20 20:11:28 +00:00
|
|
|
Community: "telegraf",
|
|
|
|
Version: 2,
|
|
|
|
Timeout: 2.0,
|
|
|
|
Retries: 2,
|
|
|
|
Collect: []string{"oid1"},
|
|
|
|
}
|
|
|
|
s := Snmp{
|
|
|
|
SnmptranslateFile: "./testdata/oids.txt",
|
|
|
|
Host: []Host{h},
|
|
|
|
Get: []Data{get1},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := s.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifSpeed",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifSpeed": uint(10000000),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"unit": "octets",
|
|
|
|
"instance": "1",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSNMPEasyGet4(t *testing.T) {
|
2016-02-19 23:15:14 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
get1 := Data{
|
|
|
|
Name: "oid1",
|
|
|
|
Unit: "octets",
|
|
|
|
Oid: "ifSpeed",
|
|
|
|
Instance: "1",
|
|
|
|
}
|
|
|
|
h := Host{
|
2016-01-27 18:09:14 +00:00
|
|
|
Address: testutil.GetLocalHost() + ":31161",
|
2016-01-20 20:11:28 +00:00
|
|
|
Community: "telegraf",
|
|
|
|
Version: 2,
|
|
|
|
Timeout: 2.0,
|
|
|
|
Retries: 2,
|
|
|
|
Collect: []string{"oid1"},
|
|
|
|
GetOids: []string{"ifNumber"},
|
|
|
|
}
|
|
|
|
s := Snmp{
|
|
|
|
SnmptranslateFile: "./testdata/oids.txt",
|
|
|
|
Host: []Host{h},
|
|
|
|
Get: []Data{get1},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := s.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifSpeed",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifSpeed": uint(10000000),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"unit": "octets",
|
|
|
|
"instance": "1",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifNumber",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifNumber": int(4),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"instance": "0",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSNMPEasyGet5(t *testing.T) {
|
2016-02-19 23:15:14 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
get1 := Data{
|
|
|
|
Name: "oid1",
|
|
|
|
Unit: "octets",
|
|
|
|
Oid: "ifSpeed",
|
|
|
|
Instance: "1",
|
|
|
|
}
|
|
|
|
h := Host{
|
2016-01-27 18:09:14 +00:00
|
|
|
Address: testutil.GetLocalHost() + ":31161",
|
2016-01-20 20:11:28 +00:00
|
|
|
Community: "telegraf",
|
|
|
|
Version: 2,
|
|
|
|
Timeout: 2.0,
|
|
|
|
Retries: 2,
|
|
|
|
Collect: []string{"oid1"},
|
|
|
|
GetOids: []string{".1.3.6.1.2.1.2.1.0"},
|
|
|
|
}
|
|
|
|
s := Snmp{
|
|
|
|
SnmptranslateFile: "./testdata/oids.txt",
|
|
|
|
Host: []Host{h},
|
|
|
|
Get: []Data{get1},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := s.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifSpeed",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifSpeed": uint(10000000),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"unit": "octets",
|
|
|
|
"instance": "1",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifNumber",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifNumber": int(4),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"instance": "0",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSNMPEasyGet6(t *testing.T) {
|
2016-02-19 23:15:14 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
h := Host{
|
2016-01-27 18:09:14 +00:00
|
|
|
Address: testutil.GetLocalHost() + ":31161",
|
2016-01-20 20:11:28 +00:00
|
|
|
Community: "telegraf",
|
|
|
|
Version: 2,
|
|
|
|
Timeout: 2.0,
|
|
|
|
Retries: 2,
|
|
|
|
GetOids: []string{"1.3.6.1.2.1.2.1.0"},
|
|
|
|
}
|
|
|
|
s := Snmp{
|
|
|
|
SnmptranslateFile: "./testdata/oids.txt",
|
|
|
|
Host: []Host{h},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := s.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifNumber",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifNumber": int(4),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"instance": "0",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSNMPBulk1(t *testing.T) {
|
2016-02-19 23:15:14 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
bulk1 := Data{
|
|
|
|
Name: "oid1",
|
|
|
|
Unit: "octets",
|
|
|
|
Oid: ".1.3.6.1.2.1.2.2.1.16",
|
|
|
|
MaxRepetition: 2,
|
|
|
|
}
|
|
|
|
h := Host{
|
2016-01-27 18:09:14 +00:00
|
|
|
Address: testutil.GetLocalHost() + ":31161",
|
2016-01-20 20:11:28 +00:00
|
|
|
Community: "telegraf",
|
|
|
|
Version: 2,
|
|
|
|
Timeout: 2.0,
|
|
|
|
Retries: 2,
|
|
|
|
Collect: []string{"oid1"},
|
|
|
|
}
|
|
|
|
s := Snmp{
|
|
|
|
SnmptranslateFile: "./testdata/oids.txt",
|
|
|
|
Host: []Host{h},
|
|
|
|
Bulk: []Data{bulk1},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := s.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifOutOctets",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifOutOctets": uint(543846),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"unit": "octets",
|
|
|
|
"instance": "1",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifOutOctets",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifOutOctets": uint(26475179),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"unit": "octets",
|
|
|
|
"instance": "2",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifOutOctets",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifOutOctets": uint(108963968),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"unit": "octets",
|
|
|
|
"instance": "3",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifOutOctets",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifOutOctets": uint(12991453),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"unit": "octets",
|
|
|
|
"instance": "36",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO find why, if this test is active
|
|
|
|
// Circle CI stops with the following error...
|
|
|
|
// bash scripts/circle-test.sh died unexpectedly
|
|
|
|
// Maybe the test is too long ??
|
|
|
|
func dTestSNMPBulk2(t *testing.T) {
|
|
|
|
bulk1 := Data{
|
|
|
|
Name: "oid1",
|
|
|
|
Unit: "octets",
|
|
|
|
Oid: "ifOutOctets",
|
|
|
|
MaxRepetition: 2,
|
|
|
|
}
|
|
|
|
h := Host{
|
2016-01-27 18:09:14 +00:00
|
|
|
Address: testutil.GetLocalHost() + ":31161",
|
2016-01-20 20:11:28 +00:00
|
|
|
Community: "telegraf",
|
|
|
|
Version: 2,
|
|
|
|
Timeout: 2.0,
|
|
|
|
Retries: 2,
|
|
|
|
Collect: []string{"oid1"},
|
|
|
|
}
|
|
|
|
s := Snmp{
|
|
|
|
SnmptranslateFile: "./testdata/oids.txt",
|
|
|
|
Host: []Host{h},
|
|
|
|
Bulk: []Data{bulk1},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := s.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifOutOctets",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifOutOctets": uint(543846),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"unit": "octets",
|
|
|
|
"instance": "1",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifOutOctets",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifOutOctets": uint(26475179),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"unit": "octets",
|
|
|
|
"instance": "2",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifOutOctets",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifOutOctets": uint(108963968),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"unit": "octets",
|
|
|
|
"instance": "3",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"ifOutOctets",
|
|
|
|
map[string]interface{}{
|
|
|
|
"ifOutOctets": uint(12991453),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-05-10 20:27:18 +00:00
|
|
|
"unit": "octets",
|
|
|
|
"instance": "36",
|
|
|
|
"snmp_host": testutil.GetLocalHost(),
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|