Skip to content

Commit 18a51d5

Browse files
ragzilladanielnelson
authored andcommitted
Add snmp input option to strip non fixed length index suffixes (#4025)
1 parent 3c9498a commit 18a51d5

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

plugins/inputs/snmp/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ OID to get. May be a numeric or textual OID.
141141
* `oid_index_suffix`:
142142
The OID sub-identifier to strip off so that the index can be matched against other fields in the table.
143143

144+
* `oid_index_length`:
145+
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.
146+
144147
* `name`:
145148
Output field/tag name.
146149
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.

plugins/inputs/snmp/snmp.go

+14
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ type Field struct {
237237
Oid string
238238
// OidIndexSuffix is the trailing sub-identifier on a table record OID that will be stripped off to get the record's index.
239239
OidIndexSuffix string
240+
// 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.
241+
OidIndexLength int
240242
// IsTag controls whether this OID is output as a tag or a value.
241243
IsTag bool
242244
// 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) {
462464
}
463465
idx = idx[:len(idx)-len(f.OidIndexSuffix)]
464466
}
467+
if f.OidIndexLength != 0 {
468+
i := f.OidIndexLength + 1 // leading separator
469+
idx = strings.Map(func(r rune) rune {
470+
if r == '.' {
471+
i -= 1
472+
}
473+
if i < 1 {
474+
return -1
475+
}
476+
return r
477+
}, idx)
478+
}
465479

466480
fv, err := fieldConvert(f.Conversion, ent.Value)
467481
if err != nil {

plugins/inputs/snmp/snmp_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ func TestTableBuild_walk(t *testing.T) {
453453
Oid: ".1.0.0.2.1.5",
454454
OidIndexSuffix: ".9.9",
455455
},
456+
{
457+
Name: "myfield5",
458+
Oid: ".1.0.0.2.1.5",
459+
OidIndexLength: 1,
460+
},
456461
},
457462
}
458463

@@ -469,6 +474,7 @@ func TestTableBuild_walk(t *testing.T) {
469474
"myfield2": 1,
470475
"myfield3": float64(0.123),
471476
"myfield4": 11,
477+
"myfield5": 11,
472478
},
473479
}
474480
rtr2 := RTableRow{
@@ -480,6 +486,7 @@ func TestTableBuild_walk(t *testing.T) {
480486
"myfield2": 2,
481487
"myfield3": float64(0.456),
482488
"myfield4": 22,
489+
"myfield5": 22,
483490
},
484491
}
485492
rtr3 := RTableRow{

0 commit comments

Comments
 (0)