snmp: support table indexes as tags (#2366)

This commit is contained in:
Patrick Hemmer 2017-03-24 15:06:52 -04:00 committed by Daniel Nelson
parent c65cfb6a6e
commit 36c1a39a09
4 changed files with 32 additions and 8 deletions

View File

@ -58,6 +58,7 @@ be deprecated eventually.
- [#1678](https://github.com/influxdata/telegraf/pull/1678): Add AMQP consumer input plugin - [#1678](https://github.com/influxdata/telegraf/pull/1678): Add AMQP consumer input plugin
- [#2501](https://github.com/influxdata/telegraf/pull/2501): Support DEAD(X) state in system input plugin. - [#2501](https://github.com/influxdata/telegraf/pull/2501): Support DEAD(X) state in system input plugin.
- [#2522](https://github.com/influxdata/telegraf/pull/2522): Add support for mongodb client certificates. - [#2522](https://github.com/influxdata/telegraf/pull/2522): Add support for mongodb client certificates.
- [#1948](https://github.com/influxdata/telegraf/pull/1948): Support adding SNMP table indexes as tags.
### Bugfixes ### Bugfixes

View File

@ -168,6 +168,9 @@ If not specified, it defaults to the value of `oid`. If `oid` is numeric, an at
* `inherit_tags`: * `inherit_tags`:
Which tags to inherit from the top-level config and to use in the output of this table's measurement. Which tags to inherit from the top-level config and to use in the output of this table's measurement.
* `index_as_tag`:
Adds each row's index within the table as a tag.
### MIB lookups ### MIB lookups
If the plugin is configured such that it needs to perform lookups from the MIB, it will use the net-snmp utilities `snmptranslate` and `snmptable`. If the plugin is configured such that it needs to perform lookups from the MIB, it will use the net-snmp utilities `snmptranslate` and `snmptable`.

View File

@ -168,6 +168,9 @@ type Table struct {
// Which tags to inherit from the top-level config. // Which tags to inherit from the top-level config.
InheritTags []string InheritTags []string
// Adds each row's table index as a tag.
IndexAsTag bool
// Fields is the tags and values to look up. // Fields is the tags and values to look up.
Fields []Field `toml:"field"` Fields []Field `toml:"field"`
@ -464,13 +467,19 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) {
} }
} }
for i, v := range ifv { for idx, v := range ifv {
rtr, ok := rows[i] rtr, ok := rows[idx]
if !ok { if !ok {
rtr = RTableRow{} rtr = RTableRow{}
rtr.Tags = map[string]string{} rtr.Tags = map[string]string{}
rtr.Fields = map[string]interface{}{} rtr.Fields = map[string]interface{}{}
rows[i] = rtr rows[idx] = rtr
}
if t.IndexAsTag && idx != "" {
if idx[0] == '.' {
idx = idx[1:]
}
rtr.Tags["index"] = idx
} }
// don't add an empty string // don't add an empty string
if vs, ok := v.(string); !ok || vs != "" { if vs, ok := v.(string); !ok || vs != "" {

View File

@ -414,6 +414,7 @@ func TestGosnmpWrapper_get_retry(t *testing.T) {
func TestTableBuild_walk(t *testing.T) { func TestTableBuild_walk(t *testing.T) {
tbl := Table{ tbl := Table{
Name: "mytable", Name: "mytable",
IndexAsTag: true,
Fields: []Field{ Fields: []Field{
{ {
Name: "myfield1", Name: "myfield1",
@ -442,7 +443,10 @@ func TestTableBuild_walk(t *testing.T) {
assert.Equal(t, tb.Name, "mytable") assert.Equal(t, tb.Name, "mytable")
rtr1 := RTableRow{ rtr1 := RTableRow{
Tags: map[string]string{"myfield1": "foo"}, Tags: map[string]string{
"myfield1": "foo",
"index": "0",
},
Fields: map[string]interface{}{ Fields: map[string]interface{}{
"myfield2": 1, "myfield2": 1,
"myfield3": float64(0.123), "myfield3": float64(0.123),
@ -450,7 +454,10 @@ func TestTableBuild_walk(t *testing.T) {
}, },
} }
rtr2 := RTableRow{ rtr2 := RTableRow{
Tags: map[string]string{"myfield1": "bar"}, Tags: map[string]string{
"myfield1": "bar",
"index": "1",
},
Fields: map[string]interface{}{ Fields: map[string]interface{}{
"myfield2": 2, "myfield2": 2,
"myfield3": float64(0.456), "myfield3": float64(0.456),
@ -458,14 +465,18 @@ func TestTableBuild_walk(t *testing.T) {
}, },
} }
rtr3 := RTableRow{ rtr3 := RTableRow{
Tags: map[string]string{}, Tags: map[string]string{
"index": "2",
},
Fields: map[string]interface{}{ Fields: map[string]interface{}{
"myfield2": 0, "myfield2": 0,
"myfield3": float64(0.0), "myfield3": float64(0.0),
}, },
} }
rtr4 := RTableRow{ rtr4 := RTableRow{
Tags: map[string]string{}, Tags: map[string]string{
"index": "3",
},
Fields: map[string]interface{}{ Fields: map[string]interface{}{
"myfield3": float64(9.999), "myfield3": float64(9.999),
}, },