From 18a51d54dc5b188dc98b1e14cebb6bba0f6ac827 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 17 Apr 2018 20:34:39 -0400 Subject: [PATCH] Add snmp input option to strip non fixed length index suffixes (#4025) --- plugins/inputs/snmp/README.md | 3 +++ plugins/inputs/snmp/snmp.go | 14 ++++++++++++++ plugins/inputs/snmp/snmp_test.go | 7 +++++++ 3 files changed, 24 insertions(+) diff --git a/plugins/inputs/snmp/README.md b/plugins/inputs/snmp/README.md index 42181d6a8..dab28e9b0 100644 --- a/plugins/inputs/snmp/README.md +++ b/plugins/inputs/snmp/README.md @@ -141,6 +141,9 @@ OID to get. May be a numeric or textual OID. * `oid_index_suffix`: 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`: 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. diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go index 7d3cb7298..9dcce12ad 100644 --- a/plugins/inputs/snmp/snmp.go +++ b/plugins/inputs/snmp/snmp.go @@ -237,6 +237,8 @@ type Field struct { 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 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 bool // 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)] } + 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) if err != nil { diff --git a/plugins/inputs/snmp/snmp_test.go b/plugins/inputs/snmp/snmp_test.go index 497461485..6ab28c335 100644 --- a/plugins/inputs/snmp/snmp_test.go +++ b/plugins/inputs/snmp/snmp_test.go @@ -453,6 +453,11 @@ func TestTableBuild_walk(t *testing.T) { Oid: ".1.0.0.2.1.5", 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, "myfield3": float64(0.123), "myfield4": 11, + "myfield5": 11, }, } rtr2 := RTableRow{ @@ -480,6 +486,7 @@ func TestTableBuild_walk(t *testing.T) { "myfield2": 2, "myfield3": float64(0.456), "myfield4": 22, + "myfield5": 22, }, } rtr3 := RTableRow{