Add snmp input option to strip non fixed length index suffixes (#4025)

This commit is contained in:
Matt 2018-04-17 20:34:39 -04:00 committed by Daniel Nelson
parent 3c9498aae2
commit 18a51d54dc
3 changed files with 24 additions and 0 deletions

View File

@ -141,6 +141,9 @@ OID to get. May be a numeric or textual OID.
* `oid_index_suffix`: * `oid_index_suffix`:
The OID sub-identifier to strip off so that the index can be matched against other fields in the table. The OID sub-identifier to strip off so that the index can be matched against other fields in the table.
* `oid_index_length`:
Specifies the length of the index after the supplied table OID (in OID path segments). Truncates the index after this point to remove non-fixed value or length index suffixes.
* `name`: * `name`:
Output field/tag name. Output field/tag name.
If not specified, it defaults to the value of `oid`. If `oid` is numeric, an attempt to translate the numeric OID into a texual OID will be made. If not specified, it defaults to the value of `oid`. If `oid` is numeric, an attempt to translate the numeric OID into a texual OID will be made.

View File

@ -237,6 +237,8 @@ type Field struct {
Oid string Oid string
// OidIndexSuffix is the trailing sub-identifier on a table record OID that will be stripped off to get the record's index. // OidIndexSuffix is the trailing sub-identifier on a table record OID that will be stripped off to get the record's index.
OidIndexSuffix string OidIndexSuffix string
// OidIndexLength specifies the length of the index in OID path segments. It can be used to remove sub-identifiers that vary in content or length.
OidIndexLength int
// IsTag controls whether this OID is output as a tag or a value. // IsTag controls whether this OID is output as a tag or a value.
IsTag bool IsTag bool
// Conversion controls any type conversion that is done on the value. // Conversion controls any type conversion that is done on the value.
@ -462,6 +464,18 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
} }
idx = idx[:len(idx)-len(f.OidIndexSuffix)] idx = idx[:len(idx)-len(f.OidIndexSuffix)]
} }
if f.OidIndexLength != 0 {
i := f.OidIndexLength + 1 // leading separator
idx = strings.Map(func(r rune) rune {
if r == '.' {
i -= 1
}
if i < 1 {
return -1
}
return r
}, idx)
}
fv, err := fieldConvert(f.Conversion, ent.Value) fv, err := fieldConvert(f.Conversion, ent.Value)
if err != nil { if err != nil {

View File

@ -453,6 +453,11 @@ func TestTableBuild_walk(t *testing.T) {
Oid: ".1.0.0.2.1.5", Oid: ".1.0.0.2.1.5",
OidIndexSuffix: ".9.9", OidIndexSuffix: ".9.9",
}, },
{
Name: "myfield5",
Oid: ".1.0.0.2.1.5",
OidIndexLength: 1,
},
}, },
} }
@ -469,6 +474,7 @@ func TestTableBuild_walk(t *testing.T) {
"myfield2": 1, "myfield2": 1,
"myfield3": float64(0.123), "myfield3": float64(0.123),
"myfield4": 11, "myfield4": 11,
"myfield5": 11,
}, },
} }
rtr2 := RTableRow{ rtr2 := RTableRow{
@ -480,6 +486,7 @@ func TestTableBuild_walk(t *testing.T) {
"myfield2": 2, "myfield2": 2,
"myfield3": float64(0.456), "myfield3": float64(0.456),
"myfield4": 22, "myfield4": 22,
"myfield5": 22,
}, },
} }
rtr3 := RTableRow{ rtr3 := RTableRow{