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!
This commit is contained in:
Jonas Hahnfeld 2016-10-25 21:46:55 +02:00
parent c66363cba5
commit 475e9d6189
2 changed files with 26 additions and 15 deletions

View File

@ -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
}

View File

@ -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) {