add oid_index_suffix to snmp plugin

This commit is contained in:
Patrick Hemmer 2016-09-17 23:08:00 -04:00 committed by Cameron Sparr
parent 2778b7be30
commit 055ef168ae
4 changed files with 49 additions and 18 deletions

View File

@ -22,6 +22,7 @@
- [#1699](https://github.com/influxdata/telegraf/pull/1699): Add database blacklist option for Postgresql - [#1699](https://github.com/influxdata/telegraf/pull/1699): Add database blacklist option for Postgresql
- [#1791](https://github.com/influxdata/telegraf/pull/1791): Add Docker container state metrics to Docker input plugin output - [#1791](https://github.com/influxdata/telegraf/pull/1791): Add Docker container state metrics to Docker input plugin output
- [#1755](https://github.com/influxdata/telegraf/issues/1755): Add support to SNMP for IP & MAC address conversion. - [#1755](https://github.com/influxdata/telegraf/issues/1755): Add support to SNMP for IP & MAC address conversion.
- [#1729](https://github.com/influxdata/telegraf/issues/1729): Add support to SNMP for OID index suffixes.
### Bugfixes ### Bugfixes

View File

@ -136,6 +136,9 @@ Output measurement name.
* `oid`: * `oid`:
OID to get. May be a numeric or textual OID. OID to get. May be a numeric or textual OID.
* `oid_index_suffix`:
The OID sub-identifier to strip off so that the index can be matched against other fields in the table.
* `name`: * `name`:
Output field/tag name. Output field/tag name.
If not specified, it defaults to the value of `oid`. If `oid` is numeric, an attempt to translate the numeric OID into a texual OID will be made. If not specified, it defaults to the value of `oid`. If `oid` is numeric, an attempt to translate the numeric OID into a texual OID will be made.

View File

@ -258,6 +258,8 @@ type Field struct {
// off the OID prefix, and use the remainder as the index. For multiple fields // off the OID prefix, and use the remainder as the index. For multiple fields
// to show up in the same row, they must share the same index. // to show up in the same row, they must share the same index.
Oid string Oid string
// OidIndexSuffix is the trailing sub-identifier on a table record OID that will be stripped off to get the record's index.
OidIndexSuffix string
// IsTag controls whether this OID is output as a tag or a value. // IsTag controls whether this OID is output as a tag or a value.
IsTag bool IsTag bool
// Conversion controls any type conversion that is done on the value. // Conversion controls any type conversion that is done on the value.
@ -460,18 +462,28 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
if err != nil { if err != nil {
return nil, Errorf(err, "converting %q", ent.Value) return nil, Errorf(err, "converting %q", ent.Value)
} }
ifv[ent.Name[len(oid):]] = fv ifv[""] = fv
} }
} else { } else {
err := gs.Walk(oid, func(ent gosnmp.SnmpPDU) error { err := gs.Walk(oid, func(ent gosnmp.SnmpPDU) error {
if len(ent.Name) <= len(oid) || ent.Name[:len(oid)+1] != oid+"." { if len(ent.Name) <= len(oid) || ent.Name[:len(oid)+1] != oid+"." {
return NestedError{} // break the walk return NestedError{} // break the walk
} }
idx := ent.Name[len(oid):]
if f.OidIndexSuffix != "" {
if !strings.HasSuffix(idx, f.OidIndexSuffix) {
// this entry doesn't match our OidIndexSuffix. skip it
return nil
}
idx = idx[:len(idx)-len(f.OidIndexSuffix)]
}
fv, err := fieldConvert(f.Conversion, ent.Value) fv, err := fieldConvert(f.Conversion, ent.Value)
if err != nil { if err != nil {
return Errorf(err, "converting %q", ent.Value) return Errorf(err, "converting %q", ent.Value)
} }
ifv[ent.Name[len(oid):]] = fv ifv[idx] = fv
return nil return nil
}) })
if err != nil { if err != nil {

View File

@ -72,6 +72,8 @@ var tsc = &testSNMPConnection{
".1.0.0.1.1": "baz", ".1.0.0.1.1": "baz",
".1.0.0.1.2": 234, ".1.0.0.1.2": 234,
".1.0.0.1.3": []byte("byte slice"), ".1.0.0.1.3": []byte("byte slice"),
".1.0.0.2.1.5.0.9.9": 11,
".1.0.0.2.1.5.1.9.9": 22,
}, },
} }
@ -371,6 +373,11 @@ func TestTableBuild_walk(t *testing.T) {
Oid: ".1.0.0.0.1.3", Oid: ".1.0.0.0.1.3",
Conversion: "float", Conversion: "float",
}, },
{
Name: "myfield4",
Oid: ".1.0.0.2.1.5",
OidIndexSuffix: ".9.9",
},
}, },
} }
@ -380,11 +387,19 @@ func TestTableBuild_walk(t *testing.T) {
assert.Equal(t, tb.Name, "mytable") assert.Equal(t, tb.Name, "mytable")
rtr1 := RTableRow{ rtr1 := RTableRow{
Tags: map[string]string{"myfield1": "foo"}, Tags: map[string]string{"myfield1": "foo"},
Fields: map[string]interface{}{"myfield2": 1, "myfield3": float64(0.123)}, Fields: map[string]interface{}{
"myfield2": 1,
"myfield3": float64(0.123),
"myfield4": 11,
},
} }
rtr2 := RTableRow{ rtr2 := RTableRow{
Tags: map[string]string{"myfield1": "bar"}, Tags: map[string]string{"myfield1": "bar"},
Fields: map[string]interface{}{"myfield2": 2, "myfield3": float64(0.456)}, Fields: map[string]interface{}{
"myfield2": 2,
"myfield3": float64(0.456),
"myfield4": 22,
},
} }
assert.Len(t, tb.Rows, 2) assert.Len(t, tb.Rows, 2)
assert.Contains(t, tb.Rows, rtr1) assert.Contains(t, tb.Rows, rtr1)