Skip to content

Commit

Permalink
Add snmp input option to strip non fixed length index suffixes (#4025)
Browse files Browse the repository at this point in the history
  • Loading branch information
ragzilla authored and danielnelson committed Apr 18, 2018
1 parent 3c9498a commit 18a51d5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions plugins/inputs/snmp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions plugins/inputs/snmp/snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions plugins/inputs/snmp/snmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}

Expand All @@ -469,6 +474,7 @@ func TestTableBuild_walk(t *testing.T) {
"myfield2": 1,
"myfield3": float64(0.123),
"myfield4": 11,
"myfield5": 11,
},
}
rtr2 := RTableRow{
Expand All @@ -480,6 +486,7 @@ func TestTableBuild_walk(t *testing.T) {
"myfield2": 2,
"myfield3": float64(0.456),
"myfield4": 22,
"myfield5": 22,
},
}
rtr3 := RTableRow{
Expand Down

0 comments on commit 18a51d5

Please sign in to comment.