fix snmp emitting empty fields

This commit is contained in:
Patrick Hemmer 2016-10-04 10:46:01 -04:00
parent 9feb639bbd
commit 681bc28613
3 changed files with 20 additions and 4 deletions

View File

@ -44,6 +44,7 @@
- [#1702](https://github.com/influxdata/telegraf/issues/1702): sqlserver, fix issue when case sensitive collation is activated.
- [#1823](https://github.com/influxdata/telegraf/issues/1823): Fix huge allocations in http_listener when dealing with huge payloads.
- [#1833](https://github.com/influxdata/telegraf/issues/1833): Fix translating SNMP fields not in MIB.
- [#1835](https://github.com/influxdata/telegraf/issues/1835): Fix SNMP emitting empty fields.
## v1.0.1 [unreleased]

View File

@ -460,13 +460,15 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
// index, and being added on the same row.
if pkt, err := gs.Get([]string{oid}); err != nil {
return nil, Errorf(err, "performing get")
} else if pkt != nil && len(pkt.Variables) > 0 && pkt.Variables[0].Type != gosnmp.NoSuchObject {
} else if pkt != nil && len(pkt.Variables) > 0 && pkt.Variables[0].Type != gosnmp.NoSuchObject && pkt.Variables[0].Type != gosnmp.NoSuchInstance {
ent := pkt.Variables[0]
fv, err := fieldConvert(f.Conversion, ent.Value)
if err != nil {
return nil, Errorf(err, "converting %q", ent.Value)
}
ifv[""] = fv
if fvs, ok := fv.(string); !ok || fvs != "" {
ifv[""] = fv
}
}
} else {
err := gs.Walk(oid, func(ent gosnmp.SnmpPDU) error {
@ -487,7 +489,9 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
if err != nil {
return Errorf(err, "converting %q", ent.Value)
}
ifv[idx] = fv
if fvs, ok := fv.(string); !ok || fvs != "" {
ifv[idx] = fv
}
return nil
})
if err != nil {

View File

@ -62,12 +62,15 @@ var tsc = &testSNMPConnection{
values: map[string]interface{}{
".1.0.0.0.1.1.0": "foo",
".1.0.0.0.1.1.1": []byte("bar"),
".1.0.0.0.1.1.2": []byte(""),
".1.0.0.0.1.102": "bad",
".1.0.0.0.1.2.0": 1,
".1.0.0.0.1.2.1": 2,
".1.0.0.0.1.2.2": 0,
".1.0.0.0.1.3.0": "0.123",
".1.0.0.0.1.3.1": "0.456",
".1.0.0.0.1.3.2": "9.999",
".1.0.0.0.1.3.2": "0.000",
".1.0.0.0.1.3.3": "9.999",
".1.0.0.0.1.4.0": 123456,
".1.0.0.1.1": "baz",
".1.0.0.1.2": 234,
@ -426,6 +429,14 @@ func TestTableBuild_noWalk(t *testing.T) {
Oid: ".1.0.0.1.2",
IsTag: true,
},
{
Name: "empty",
Oid: ".1.0.0.0.1.1.2",
},
{
Name: "noexist",
Oid: ".1.2.3.4.5",
},
},
}