From 4c5e0e73c8c444887fddfb1fccc220531c02b70b Mon Sep 17 00:00:00 2001 From: Alexandru Tudori <60925074+tallexer@users.noreply.github.com> Date: Fri, 4 Sep 2020 16:17:43 +0200 Subject: [PATCH 1/2] Add agent host tag configuration option The host of the device being monitored is hardcoded and it is stored under "agent_host" tag. Make this variable configurable in order to avoid post processing. --- plugins/inputs/snmp/README.md | 3 +++ plugins/inputs/snmp/snmp.go | 14 ++++++++++++-- plugins/inputs/snmp/snmp_test.go | 5 +++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/plugins/inputs/snmp/README.md b/plugins/inputs/snmp/README.md index 4e9ce8e50d929..a1ba930a9ad46 100644 --- a/plugins/inputs/snmp/README.md +++ b/plugins/inputs/snmp/README.md @@ -35,6 +35,9 @@ information. ## SNMP community string. # community = "public" + ## Agent host tag + # agent_host_tag = "agent_host" + ## Number of retries to attempt. # retries = 3 diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go index 737be06f67c58..103b23d214485 100644 --- a/plugins/inputs/snmp/snmp.go +++ b/plugins/inputs/snmp/snmp.go @@ -34,6 +34,9 @@ const sampleConfig = ` ## SNMP version; can be 1, 2, or 3. # version = 2 + ## Agent host tag; the tag used to reference the source host + # agent_host_tag = "agent_host" + ## SNMP community string. # community = "public" @@ -95,6 +98,9 @@ type Snmp struct { // udp://1.2.3.4:161). If the scheme is not specified then "udp" is used. Agents []string `toml:"agents"` + // The tag used to name the agent host + AgentHostTag string `toml:"agent_host_tag"` + snmp.ClientConfig Tables []Table `toml:"table"` @@ -128,6 +134,10 @@ func (s *Snmp) init() error { } } + if len(s.AgentHostTag) == 0 { + s.AgentHostTag = "agent_host" + } + s.initialized = true return nil } @@ -374,8 +384,8 @@ func (s *Snmp) gatherTable(acc telegraf.Accumulator, gs snmpConnection, t Table, } } } - if _, ok := tr.Tags["agent_host"]; !ok { - tr.Tags["agent_host"] = gs.Host() + if _, ok := tr.Tags[s.AgentHostTag]; !ok { + tr.Tags[s.AgentHostTag] = gs.Host() } acc.AddFields(rt.Name, tr.Fields, tr.Tags, rt.Time) } diff --git a/plugins/inputs/snmp/snmp_test.go b/plugins/inputs/snmp/snmp_test.go index 9991ff7413a9a..74ba7fed06d0a 100644 --- a/plugins/inputs/snmp/snmp_test.go +++ b/plugins/inputs/snmp/snmp_test.go @@ -91,6 +91,7 @@ func TestSampleConfig(t *testing.T) { expected := &Snmp{ Agents: []string{"udp://127.0.0.1:161"}, + AgentHostTag: "", ClientConfig: config.ClientConfig{ Timeout: internal.Duration{Duration: 5 * time.Second}, Version: 2, @@ -634,7 +635,7 @@ func TestGather(t *testing.T) { m := acc.Metrics[0] assert.Equal(t, "mytable", m.Measurement) - assert.Equal(t, "tsc", m.Tags["agent_host"]) + assert.Equal(t, "tsc", m.Tags[s.AgentHostTag]) assert.Equal(t, "baz", m.Tags["myfield1"]) assert.Len(t, m.Fields, 2) assert.Equal(t, 234, m.Fields["myfield2"]) @@ -644,7 +645,7 @@ func TestGather(t *testing.T) { m2 := acc.Metrics[1] assert.Equal(t, "myOtherTable", m2.Measurement) - assert.Equal(t, "tsc", m2.Tags["agent_host"]) + assert.Equal(t, "tsc", m2.Tags[s.AgentHostTag]) assert.Equal(t, "baz", m2.Tags["myfield1"]) assert.Len(t, m2.Fields, 1) assert.Equal(t, 123456, m2.Fields["myOtherField"]) From 1f3cf94b8937d363d19199770aec73f8cb3a30bb Mon Sep 17 00:00:00 2001 From: Alexandru Tudori <60925074+tallexer@users.noreply.github.com> Date: Mon, 14 Sep 2020 11:52:09 +0200 Subject: [PATCH 2/2] Update snmp input fmt --- plugins/inputs/snmp/snmp_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inputs/snmp/snmp_test.go b/plugins/inputs/snmp/snmp_test.go index 74ba7fed06d0a..583b2dc847282 100644 --- a/plugins/inputs/snmp/snmp_test.go +++ b/plugins/inputs/snmp/snmp_test.go @@ -90,7 +90,7 @@ func TestSampleConfig(t *testing.T) { require.NoError(t, err) expected := &Snmp{ - Agents: []string{"udp://127.0.0.1:161"}, + Agents: []string{"udp://127.0.0.1:161"}, AgentHostTag: "", ClientConfig: config.ClientConfig{ Timeout: internal.Duration{Duration: 5 * time.Second},