diff --git a/CHANGELOG.md b/CHANGELOG.md index e297ae251..e566e6647 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go index f83a44408..b7c29b3cf 100644 --- a/plugins/inputs/snmp/snmp.go +++ b/plugins/inputs/snmp/snmp.go @@ -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 { diff --git a/plugins/inputs/snmp/snmp_test.go b/plugins/inputs/snmp/snmp_test.go index 9dbdb43e3..6839fdd8f 100644 --- a/plugins/inputs/snmp/snmp_test.go +++ b/plugins/inputs/snmp/snmp_test.go @@ -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", + }, }, }