SNMP inputs custom tag

Hello,

I am pretty much new to telegraf. Currently I have a bash script gathering some SNMP data from a remote host, and inject the results into influxDB with curl. This is working fine, but I wanted to migrate this script to telegraf instead.

My current script do something like :

UPTIME=$(snmpget -Oqv -v 2c -c community server sysptime)
/usr/bin/curl --silent --request POST 'http://127.0.0.1:8086/write?db=mydb' --data-binary 'uptime,host=server value='$UPTIME''

But with telegraf, I am able to get the data injected, but I’m not able to find a very simple way to add a tag with hostname of the server.

[[inputs.snmp]]
  agents = [ "server:161" ]
  community = "public"
[[inputs.snmp.field]]
    name = "uptime"
    oid = "sysuptime"

I tried different configuration like this one :

[[inputs.snmp]]
  agents = [ "server:161" ]
  community = "public"
[[inputs.snmp.field]]
    name = [ "uptime", "server" ]
    oid = "sysuptime"
    is_tag = true

but it doesn’t works.

Thank you for your help.

@romgo the host tag is usually automatically added if you didn’t set omit_hostname = true in the agent section of your config…

There’s also the agent_host tag by default. (run telegraf with --test to see what’s actually collected)

You can also retrieve the hostname from the snmp device using this example:

  [[inputs.snmp.field]]
    oid = "RFC1213-MIB::sysUpTime.0"
    name = "sysUptime"
    conversion = "float(2)"

  [[inputs.snmp.field]]
    oid = "RFC1213-MIB::sysName.0"
    name = "sysName"
    is_tag = true

Adding a fixed tag is possible by adding this:

  [inputs.snmp.tags]
    server = "my hostname"

I use telegraf 1.31.3, my conf :

root@server ~  cat /etc/telegraf/telegraf.conf | grep omit
  omit_hostname = false

but when I query th data I don’t see the hostname ?

> select gpu_core_temp from "snmp" limit 10;
name: snmp
time                gpu_core_temp
----                -------------
1726476285000000000 28
1726476290000000000 28
1726476300000000000 28
1726476310000000000 28
1726476320000000000 28
1726476330000000000 28
1726476340000000000 28
1726476350000000000 28
1726476360000000000 28
1726476370000000000 28

thanks !

Well if you only select one field you will only see that field… :smiley: Try

> select * from "snmp" limit 10;

thank you !
Indeed this was a PEBKAC issue ! Sorry :sweat_smile:
I have a field agent_host which do the job :

select gpu_fan_speed,agent_host from “snmp” limit 10

side question : is there any good practice with regards on how to organize data within databases ?
currently my snmp database mix various data coming from snmp, could this be an issue some how ?

Regards

That’s why I said to use --test, then you see what metrics actually would be sent to InfluxDB.

It’s not really a database like you know ‘relational’ databases, it’s a ‘time series’ database; so you have a bucket, with metrics (in this case you name it snmp) which can have different tags and fields. There’s no big issue with that, depending on how you query your data afterwards.

If you’re just beginning with snmp and telegraf, I advise to also look at this blog post: Telegraf Best Practices: SNMP Plugin | InfluxData

Thank you.
I did give a try with --test, but I got too much data, and I was note sure that this will no affect current running telegraf service.

I will review the blog post thanks.