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!
This commit is contained in:
Jonas Hahnfeld 2017-01-24 23:57:43 +01:00 committed by Cameron Sparr
parent d4d3f61673
commit b0c721491d
2 changed files with 27 additions and 17 deletions

View File

@ -433,10 +433,8 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
if err != nil { if err != nil {
return nil, Errorf(err, "converting %q (OID %s) for field %s", ent.Value, ent.Name, f.Name) 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 { } else {
err := gs.Walk(oid, func(ent gosnmp.SnmpPDU) error { err := gs.Walk(oid, func(ent gosnmp.SnmpPDU) error {
if len(ent.Name) <= len(oid) || ent.Name[:len(oid)+1] != oid+"." { if len(ent.Name) <= len(oid) || ent.Name[:len(oid)+1] != oid+"." {
@ -456,9 +454,7 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
if err != nil { if err != nil {
return Errorf(err, "converting %q (OID %s) for field %s", ent.Value, ent.Name, f.Name) 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 return nil
}) })
if err != nil { if err != nil {
@ -476,8 +472,10 @@ 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
} }
// don't add an empty string
if vs, ok := v.(string); !ok || vs != "" {
if f.IsTag { if f.IsTag {
if vs, ok := v.(string); ok { if ok {
rtr.Tags[f.Name] = vs rtr.Tags[f.Name] = vs
} else { } else {
rtr.Tags[f.Name] = fmt.Sprintf("%v", v) rtr.Tags[f.Name] = fmt.Sprintf("%v", v)
@ -487,6 +485,7 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
} }
} }
} }
}
rt := RTable{ rt := RTable{
Name: t.Name, Name: t.Name,
@ -494,10 +493,6 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
Rows: make([]RTableRow, 0, len(rows)), Rows: make([]RTableRow, 0, len(rows)),
} }
for _, r := range 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) rt.Rows = append(rt.Rows, r)
} }
return &rt, nil return &rt, nil

View File

@ -457,9 +457,24 @@ 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),
},
}
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, rtr1)
assert.Contains(t, tb.Rows, rtr2) assert.Contains(t, tb.Rows, rtr2)
assert.Contains(t, tb.Rows, rtr3)
assert.Contains(t, tb.Rows, rtr4)
} }
func TestTableBuild_noWalk(t *testing.T) { func TestTableBuild_noWalk(t *testing.T) {