Telegraf

From Leo's Notes
Last edited on 25 April 2023, at 22:46.

Telegraf is an agent that pushes metrics to a server, such as InfluxDB.

Installation

The install instructions can be found at https://docs.influxdata.com/telegraf/v1.22/install/. Distro-specific repos are listed on this page and this is probably the preferred method to install Telegraf.

Alternatively, you can download and extract the binaries directly to your system. You will have to set up the systemd service script manually.

# wget https://dl.influxdata.com/telegraf/releases/telegraf-1.26.0_linux_amd64.tar.gz
# tar -C / --strip-components=2 -xzvf telegraf-1.26.0_linux_amd64.tar.gz

Tasks

Monitoring CPU frequency

Simplest way is to use an exec input:

[[inputs.exec]]
  commands = ["bash -c \"grep MHz /proc/cpuinfo | /bin/awk -F: '{freq += \\$2; n++} END{printf(\\\"%f\\\", freq/n); }'\" "]
  name_override = "cpu_freq"
  data_format = "value"
  data_type = "float"

This will just retrieve the CPU speeds which awk will average out as a single value.

$ grep MHz /proc/cpuinfo | awk -F: '{freq+=$2; n++} END {printf("%f", freq/n);}'
3099.926000

Monitoring SNMP

You can use the SNMP input plugin to poll remote SNMP agents for metrics. You will have to install the net-snmp-utils package as the plugin uses the snmptranslate program.

Polling switch port metrics from a Cisco switch

Here's an example config which I used to poll the port counters from a Cisco switch. The ifXTable OID is used because the extended (X) table uses 64-bit counters which doesn't roll over as quickly. Port speeds greater than 4Gb/s will also be reported properly (as it doesn't overflow the Counter32 type). I've also added some additional fields as tags so that I can graph port counters by specific interface names, labels, or MAC address.

[[inputs.snmp]]
  agents = ["udp://10.x.x.x:161"]
  timeout = "5s"
  # path = ["/usr/share/snmp/mibs"]
  
  version = 3
  sec_name = "username"
  auth_protocol = "SHA"
  auth_password = "********"
  sec_level = "authPriv"
  priv_protocol = "AES"
  priv_password = "********"

  [[inputs.snmp.field]]
    oid = "RFC1213-MIB::sysUpTime.0"
    name = "uptime"

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

  [[inputs.snmp.table]]
    oid = "IF-MIB::ifXTable"
    name = "interface"
    inherit_tags = ["source"]

    ## Additional fields, not included in ifXTable
    ## Link status
    [[inputs.snmp.table.field]]
      oid = "IF-MIB::ifOperStatus"
      name = "ifOperStatus"
      is_tag = false
    [[inputs.snmp.table.field]]
      oid = "IF-MIB::ifMtu"
      name = "ifMtu"
      is_tag = false

    ## These fields have to be set as tags
    [[inputs.snmp.table.field]]
      oid = "IF-MIB::ifDescr"
      name = "ifDescr"
      is_tag = true
    [[inputs.snmp.table.field]]
      oid = "IF-MIB::ifName"
      name = "ifName"
      is_tag = true
    [[inputs.snmp.table.field]]
      oid = "IF-MIB::ifAlias"
      name = "ifAlias"
      is_tag = true
    [[inputs.snmp.table.field]]
      oid = "IF-MIB::ifPhysAddress"
      name = "ifPhysAddress"
      is_tag = true