Add agent host tag configuration option (#8082)

This commit is contained in:
Alexandru Tudori 2020-10-08 17:43:28 +02:00 committed by GitHub
parent c8e69aca3c
commit 01eaa202d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 5 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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,
@ -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"])