Skip to content

Make SNMP agent_host configurable #8060

@tallexer

Description

@tallexer

Feature Request

The agent hostname is hardcoded in agent_host tag. Expose this as a variable and make it configurable in order to avoid extra processing later on.

Proposal:

diff --git a/plugins/inputs/snmp/README.md b/plugins/inputs/snmp/README.md
index 4e9ce8e..2493607 100644
--- a/plugins/inputs/snmp/README.md
+++ b/plugins/inputs/snmp/README.md
@@ -32,6 +32,9 @@ information.
   ## SNMP version; can be 1, 2, or 3.
   # version = 2
 
+  ## Agent Host 
+  # agent_host = "agent_host"
+
   ## SNMP community string.
   # community = "public"
 
diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go
index 737be06..c3d0543 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
+  # agent_host = "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
+       AgentHost string `toml:"agent_host"`
+
        snmp.ClientConfig
 
        Tables []Table `toml:"table"`
@@ -128,6 +134,10 @@ func (s *Snmp) init() error {
                }
        }
 
+       if len(s.AgentHost) == 0 {
+               s.AgentHost = "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.AgentHost]; !ok {
+                       tr.Tags[s.AgentHost] = 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 9991ff7..932b212 100644
--- a/plugins/inputs/snmp/snmp_test.go
+++ b/plugins/inputs/snmp/snmp_test.go
@@ -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"},
+               AgentHost: "",
                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.AgentHost])
        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.AgentHost])
        assert.Equal(t, "baz", m2.Tags["myfield1"])
        assert.Len(t, m2.Fields, 1)
        assert.Equal(t, 123456, m2.Fields["myOtherField"])

Current behavior:

Agent_host is hardcoded and all measurements are exported with agent_host="hostname".

Desired behavior:

Introduce a new configuration option (ex: agent_host) in order to define what is the agent host tag name (ex: agent_host = "host") and then use that value to export the hostname tag in SNMP measurements like host="hostname".

Use case:

It gives an efficient option to align hostname tag without the need of extra processing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/snmpfeature requestRequests for new plugin and for new features to existing plugins

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions