Translate snmp field values (#8466)
This commit is contained in:
parent
d312a5e6a8
commit
a7096c8128
|
|
@ -184,6 +184,10 @@ One [metric][] is created for each row of the SNMP table.
|
|||
## path segments). Truncates the index after this point to remove non-fixed
|
||||
## value or length index suffixes.
|
||||
# oid_index_length = 0
|
||||
|
||||
## Specifies if the value of given field should be snmptranslated
|
||||
## by default no field values are translated
|
||||
# translate = true
|
||||
```
|
||||
|
||||
### Troubleshooting
|
||||
|
|
|
|||
|
|
@ -237,6 +237,8 @@ type Field struct {
|
|||
// "hwaddr" will convert a 6-byte string to a MAC address.
|
||||
// "ipaddr" will convert the value to an IPv4 or IPv6 address.
|
||||
Conversion string
|
||||
// Translate tells if the value of the field should be snmptranslated
|
||||
Translate bool
|
||||
|
||||
initialized bool
|
||||
}
|
||||
|
|
@ -460,6 +462,17 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
|
|||
}, idx)
|
||||
}
|
||||
|
||||
// snmptranslate table field value here
|
||||
if f.Translate {
|
||||
if entOid, ok := ent.Value.(string); ok {
|
||||
_, _, oidText, _, err := SnmpTranslate(entOid)
|
||||
if err == nil {
|
||||
// If no error translating, the original value for ent.Value should be replaced
|
||||
ent.Value = oidText
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fv, err := fieldConvert(f.Conversion, ent.Value)
|
||||
if err != nil {
|
||||
return &walkError{
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ var mockedCommands = [][]string{
|
|||
{"snmptranslate", "-Td", "-Ob", "-m", "all", ".1.0.0.0.1.1.0"},
|
||||
{"snmptranslate", "-Td", "-Ob", "-m", "all", ".1.0.0.0.1.5"},
|
||||
{"snmptranslate", "-Td", "-Ob", "-m", "all", ".1.2.3"},
|
||||
{"snmptranslate", "-Td", "-Ob", "-m", "all", ".1.0.0.0.1.7"},
|
||||
{"snmptranslate", "-Td", "-Ob", ".iso.2.3"},
|
||||
{"snmptranslate", "-Td", "-Ob", "-m", "all", ".999"},
|
||||
{"snmptranslate", "-Td", "-Ob", "TEST::server"},
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ var mockedCommandResults = map[string]mockedCommandResult{
|
|||
"snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.1.0.0.0.1.1.0": {stdout: "TEST::server.0\nserver OBJECT-TYPE\n -- FROM\tTEST\n SYNTAX\tOCTET STRING\n MAX-ACCESS\tread-only\n STATUS\tcurrent\n::= { iso(1) 0 testOID(0) testTable(0) testTableEntry(1) server(1) 0 }\n", stderr: "", exitError: false},
|
||||
"snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.1.0.0.0.1.5": {stdout: "TEST::testTableEntry.5\ntestTableEntry OBJECT-TYPE\n -- FROM\tTEST\n MAX-ACCESS\tnot-accessible\n STATUS\tcurrent\n INDEX\t\t{ server }\n::= { iso(1) 0 testOID(0) testTable(0) testTableEntry(1) 5 }\n", stderr: "", exitError: false},
|
||||
"snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.1.2.3": {stdout: "iso.2.3\niso OBJECT-TYPE\n -- FROM\t#-1\n::= { iso(1) 2 3 }\n", stderr: "", exitError: false},
|
||||
"snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.1.0.0.0.1.7": {stdout: "TEST::testTableEntry.7\ntestTableEntry OBJECT-TYPE\n -- FROM\tTEST\n MAX-ACCESS\tnot-accessible\n STATUS\tcurrent\n INDEX\t\t{ server }\n::= { iso(1) std(0) testOID(0) testTable(0) testTableEntry(1) 7 }\n", stderr: "", exitError: false},
|
||||
"snmptranslate\x00-Td\x00-Ob\x00.iso.2.3": {stdout: "iso.2.3\niso OBJECT-TYPE\n -- FROM\t#-1\n::= { iso(1) 2 3 }\n", stderr: "", exitError: false},
|
||||
"snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.999": {stdout: ".999\n [TRUNCATED]\n", stderr: "", exitError: false},
|
||||
"snmptranslate\x00-Td\x00-Ob\x00TEST::server": {stdout: "TEST::server\nserver OBJECT-TYPE\n -- FROM\tTEST\n SYNTAX\tOCTET STRING\n MAX-ACCESS\tread-only\n STATUS\tcurrent\n::= { iso(1) 0 testOID(0) testTable(0) testTableEntry(1) 1 }\n", stderr: "", exitError: false},
|
||||
|
|
|
|||
|
|
@ -81,6 +81,7 @@ var tsc = &testSNMPConnection{
|
|||
".1.0.0.1.3": []byte("byte slice"),
|
||||
".1.0.0.2.1.5.0.9.9": 11,
|
||||
".1.0.0.2.1.5.1.9.9": 22,
|
||||
".1.0.0.0.1.6.0": ".1.0.0.0.1.7",
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -493,6 +494,16 @@ func TestTableBuild_walk(t *testing.T) {
|
|||
Oid: ".1.0.0.2.1.5",
|
||||
OidIndexLength: 1,
|
||||
},
|
||||
{
|
||||
Name: "myfield6",
|
||||
Oid: ".1.0.0.0.1.6",
|
||||
Translate: true,
|
||||
},
|
||||
{
|
||||
Name: "myfield7",
|
||||
Oid: ".1.0.0.0.1.6",
|
||||
Translate: false,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -510,6 +521,8 @@ func TestTableBuild_walk(t *testing.T) {
|
|||
"myfield3": float64(0.123),
|
||||
"myfield4": 11,
|
||||
"myfield5": 11,
|
||||
"myfield6": "testTableEntry.7",
|
||||
"myfield7": ".1.0.0.0.1.7",
|
||||
},
|
||||
}
|
||||
rtr2 := RTableRow{
|
||||
|
|
|
|||
Loading…
Reference in New Issue