From 475e9d6189946b6bd0bae90ff26276938336ea9d Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld Date: Tue, 25 Oct 2016 21:46:55 +0200 Subject: [PATCH] snmp: Don't drop lines with an empty tag The changes in #1848 resulted in lines being dropped if they had an empty tag. So let's count empty tags as valid to take the right decision! --- plugins/inputs/snmp/snmp.go | 31 +++++++++++++++++-------------- plugins/inputs/snmp/snmp_test.go | 10 +++++++++- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go index cc750e769..2a8dd6548 100644 --- a/plugins/inputs/snmp/snmp.go +++ b/plugins/inputs/snmp/snmp.go @@ -440,6 +440,7 @@ func (s *Snmp) gatherTable(acc telegraf.Accumulator, gs snmpConnection, t Table, // Build retrieves all the fields specified in the table and constructs the RTable. func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { rows := map[string]RTableRow{} + emptyTags := map[string]int{} tagCount := 0 for _, f := range t.Fields { @@ -475,9 +476,7 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { if err != nil { return nil, Errorf(err, "converting %q", ent.Value) } - if fvs, ok := fv.(string); !ok || fvs != "" { - ifv[""] = fv - } + ifv[""] = fv } } else { err := gs.Walk(oid, func(ent gosnmp.SnmpPDU) error { @@ -498,9 +497,7 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { if err != nil { return Errorf(err, "converting %q", ent.Value) } - if fvs, ok := fv.(string); !ok || fvs != "" { - ifv[idx] = fv - } + ifv[idx] = fv return nil }) if err != nil { @@ -518,14 +515,20 @@ 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 + } else if f.IsTag { + // count that we had an empty tag so that we don't reject the row for wrong reasons + emptyTags[i]++ } } } @@ -535,8 +538,8 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { Time: time.Now(), //TODO record time at start Rows: make([]RTableRow, 0, len(rows)), } - for _, r := range rows { - if len(r.Tags) < tagCount { + for i, r := range rows { + if len(r.Tags)+emptyTags[i] < tagCount { // don't add rows which are missing tags, as without tags you can't filter continue } diff --git a/plugins/inputs/snmp/snmp_test.go b/plugins/inputs/snmp/snmp_test.go index 6839fdd8f..7a8efdf7b 100644 --- a/plugins/inputs/snmp/snmp_test.go +++ b/plugins/inputs/snmp/snmp_test.go @@ -406,9 +406,17 @@ 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), + }, + } + assert.Len(t, tb.Rows, 3) assert.Contains(t, tb.Rows, rtr1) assert.Contains(t, tb.Rows, rtr2) + assert.Contains(t, tb.Rows, rtr3) } func TestTableBuild_noWalk(t *testing.T) {