Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add agent host tag configuration option #8082

Merged
merged 2 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions plugins/inputs/snmp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 12 additions & 2 deletions plugins/inputs/snmp/snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -128,6 +134,10 @@ func (s *Snmp) init() error {
}
}

if len(s.AgentHostTag) == 0 {
s.AgentHostTag = "agent_host"
}

s.initialized = true
return nil
}
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions plugins/inputs/snmp/snmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ 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},
Version: 2,
Expand Down Expand Up @@ -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"])
Expand All @@ -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"])
Expand Down