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

Make SNMP agent_host configurable #8060

Closed
tallexer opened this issue Aug 31, 2020 · 6 comments
Closed

Make SNMP agent_host configurable #8060

tallexer opened this issue Aug 31, 2020 · 6 comments
Labels
area/snmp feature request Requests for new plugin and for new features to existing plugins

Comments

@tallexer
Copy link
Contributor

tallexer commented Aug 31, 2020

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.

@tallexer tallexer added the feature request Requests for new plugin and for new features to existing plugins label Aug 31, 2020
@Hipska
Copy link
Contributor

Hipska commented Sep 1, 2020

I don't think it would be used much.

In my case for example, there are multiple telegraf hosts that are polling snmp devices, so in that scenario it is useful to still know which one did this polling.

Anyway, I would suggest to rename the config setting to be more expressive like agent_host_tag.

@tallexer
Copy link
Contributor Author

tallexer commented Sep 2, 2020

In that particular use case you can always use global tag to define collector particularities including its hostname, location etc and those tags will always be set on each and every measurement generated by that collector. This would allow you to identify what measurement came from what telegraf collector.

This configuration option, thats the subject of this feature request, is meant to define the tag name that will store the SNMP agent being polled. This is mainly useful when using different inputs that references the source of the data with different tag identifiers.

For example if one is using a combination of SNMP, gNMI and jti_openconfig_telemetry inputs to collect data (and it might become necessary to do so if devices have different feature sets) than the same data is referenced with different tag names:

  1. SNMP - agent_host
  2. gNMI - source
  3. jti_openconfig_telemetry - device or system_id

You can always use extra processing to align those tags but hardcoding this value in the input phase just to rename it later on does not sound very efficient.

@Hipska
Copy link
Contributor

Hipska commented Sep 2, 2020

Ah, I solved that by setting a hostname tag which contains the name of the polled host, since agent_host contains only the IP address (because it's always a DNS problem 😉). So basically just the other way round like what you want.

I agree that multiple plugins should be more aligned regarding the use of tag name to specify the collected host/agent/device/source. That might be the better and higher goal.

What did you think of my suggestion of renaming the new option to be more clear?

@tallexer
Copy link
Contributor Author

tallexer commented Sep 3, 2020

I think is a good idea ... I will make the update.

@Hipska
Copy link
Contributor

Hipska commented Sep 3, 2020

I Just realised this is an issue and not a PR 🧐

@Hipska
Copy link
Contributor

Hipska commented Feb 10, 2021

#8082 is merged since october.

@Hipska Hipska closed this as completed Feb 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/snmp feature request Requests for new plugin and for new features to existing plugins
Projects
None yet
Development

No branches or pull requests

3 participants