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:
parent
c66363cba5
commit
475e9d6189
|
@ -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.
|
// Build retrieves all the fields specified in the table and constructs the RTable.
|
||||||
func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
|
func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
|
||||||
rows := map[string]RTableRow{}
|
rows := map[string]RTableRow{}
|
||||||
|
emptyTags := map[string]int{}
|
||||||
|
|
||||||
tagCount := 0
|
tagCount := 0
|
||||||
for _, f := range t.Fields {
|
for _, f := range t.Fields {
|
||||||
|
@ -475,9 +476,7 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, Errorf(err, "converting %q", ent.Value)
|
return nil, Errorf(err, "converting %q", ent.Value)
|
||||||
}
|
}
|
||||||
if fvs, ok := fv.(string); !ok || fvs != "" {
|
ifv[""] = fv
|
||||||
ifv[""] = fv
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err := gs.Walk(oid, func(ent gosnmp.SnmpPDU) error {
|
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 {
|
if err != nil {
|
||||||
return Errorf(err, "converting %q", ent.Value)
|
return Errorf(err, "converting %q", ent.Value)
|
||||||
}
|
}
|
||||||
if fvs, ok := fv.(string); !ok || fvs != "" {
|
ifv[idx] = fv
|
||||||
ifv[idx] = fv
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -518,14 +515,20 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
|
||||||
rtr.Fields = map[string]interface{}{}
|
rtr.Fields = map[string]interface{}{}
|
||||||
rows[i] = rtr
|
rows[i] = rtr
|
||||||
}
|
}
|
||||||
if f.IsTag {
|
// don't add an empty string
|
||||||
if vs, ok := v.(string); ok {
|
if vs, ok := v.(string); !ok || vs != "" {
|
||||||
rtr.Tags[f.Name] = vs
|
if f.IsTag {
|
||||||
|
if ok {
|
||||||
|
rtr.Tags[f.Name] = vs
|
||||||
|
} else {
|
||||||
|
rtr.Tags[f.Name] = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
rtr.Tags[f.Name] = fmt.Sprintf("%v", v)
|
rtr.Fields[f.Name] = v
|
||||||
}
|
}
|
||||||
} else {
|
} else if f.IsTag {
|
||||||
rtr.Fields[f.Name] = v
|
// 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
|
Time: time.Now(), //TODO record time at start
|
||||||
Rows: make([]RTableRow, 0, len(rows)),
|
Rows: make([]RTableRow, 0, len(rows)),
|
||||||
}
|
}
|
||||||
for _, r := range rows {
|
for i, r := range rows {
|
||||||
if len(r.Tags) < tagCount {
|
if len(r.Tags)+emptyTags[i] < tagCount {
|
||||||
// don't add rows which are missing tags, as without tags you can't filter
|
// don't add rows which are missing tags, as without tags you can't filter
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -406,9 +406,17 @@ func TestTableBuild_walk(t *testing.T) {
|
||||||
"myfield4": 22,
|
"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, rtr1)
|
||||||
assert.Contains(t, tb.Rows, rtr2)
|
assert.Contains(t, tb.Rows, rtr2)
|
||||||
|
assert.Contains(t, tb.Rows, rtr3)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTableBuild_noWalk(t *testing.T) {
|
func TestTableBuild_noWalk(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue