From c4d4185fb5155d2d4298ef3e34500a5e5f4e341d Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld Date: Tue, 24 Jan 2017 23:57:43 +0100 Subject: [PATCH] snmp: Allow lines with empty or missing tags (#2172) The changes in #1848 resulted in lines being dropped if they had an empty tag. Let's allow all lines that have empty or missing tags! --- plugins/inputs/snmp/snmp.go | 27 +++++++++++---------------- plugins/inputs/snmp/snmp_test.go | 17 ++++++++++++++++- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go index 6f515e227..9296bc043 100644 --- a/plugins/inputs/snmp/snmp.go +++ b/plugins/inputs/snmp/snmp.go @@ -433,9 +433,7 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { if err != nil { return nil, Errorf(err, "converting %q (OID %s) for field %s", ent.Value, ent.Name, f.Name) } - if fvs, ok := fv.(string); !ok || fvs != "" { - ifv[""] = fv - } + ifv[""] = fv } } else { err := gs.Walk(oid, func(ent gosnmp.SnmpPDU) error { @@ -456,9 +454,7 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { if err != nil { return Errorf(err, "converting %q (OID %s) for field %s", ent.Value, ent.Name, f.Name) } - if fvs, ok := fv.(string); !ok || fvs != "" { - ifv[idx] = fv - } + ifv[idx] = fv return nil }) if err != nil { @@ -476,14 +472,17 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { rtr.Fields = map[string]interface{}{} rows[i] = rtr } - if f.IsTag { - if vs, ok := v.(string); ok { - rtr.Tags[f.Name] = vs + // don't add an empty string + if vs, ok := v.(string); !ok || vs != "" { + if f.IsTag { + if ok { + rtr.Tags[f.Name] = vs + } else { + rtr.Tags[f.Name] = fmt.Sprintf("%v", v) + } } else { - rtr.Tags[f.Name] = fmt.Sprintf("%v", v) + rtr.Fields[f.Name] = v } - } else { - rtr.Fields[f.Name] = v } } } @@ -494,10 +493,6 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { Rows: make([]RTableRow, 0, len(rows)), } for _, r := range rows { - if len(r.Tags) < tagCount { - // don't add rows which are missing tags, as without tags you can't filter - continue - } rt.Rows = append(rt.Rows, r) } return &rt, nil diff --git a/plugins/inputs/snmp/snmp_test.go b/plugins/inputs/snmp/snmp_test.go index ed01508f2..62b19fcea 100644 --- a/plugins/inputs/snmp/snmp_test.go +++ b/plugins/inputs/snmp/snmp_test.go @@ -457,9 +457,24 @@ func TestTableBuild_walk(t *testing.T) { "myfield4": 22, }, } - assert.Len(t, tb.Rows, 2) + rtr3 := RTableRow{ + Tags: map[string]string{}, + Fields: map[string]interface{}{ + "myfield2": 0, + "myfield3": float64(0.0), + }, + } + rtr4 := RTableRow{ + Tags: map[string]string{}, + Fields: map[string]interface{}{ + "myfield3": float64(9.999), + }, + } + assert.Len(t, tb.Rows, 4) assert.Contains(t, tb.Rows, rtr1) assert.Contains(t, tb.Rows, rtr2) + assert.Contains(t, tb.Rows, rtr3) + assert.Contains(t, tb.Rows, rtr4) } func TestTableBuild_noWalk(t *testing.T) {