Skip to content

Example use cases: Telegraf

Johannes edited this page Mar 4, 2020 · 3 revisions

Content


Example Use with Telegraf

Suppose, you want to visualize the overall Download Rate of your FritzBox: The way to go here is to use the Action IGDWAN with parameter STATE. It gives (for example) this output:

NewByteSendRate 265
NewByteReceiveRate 17
NewPacketSendRate 0
NewPacketReceiveRate 0
NewTotalBytesSent 0
NewTotalBytesReceived 0
NewAutoDisconnectTime 0
NewIdleDisconnectTime 0
NewDNSServer1 83.169.186.33
NewDNSServer2 83.169.186.97
NewVoipDNSServer1 83.169.186.33
NewVoipDNSServer2 83.169.186.97
NewUpnpControlEnabled 0
NewRoutedBridgedModeBoth 1

The important line here is the one with NewByteReceiveRate in it. If we augment the script execution with some pipe magic, we get exactly the Download Rate value:

BoxUSER=YourUser BoxPW=YourPassword /opt/telegraf/FritzBoxShell/fritzBoxShell.sh IGDWAN STATE|grep NewByteReceiveRate|cut -d ' ' -f 2

Now - how to get this measurement into Telegraf? Well, Telegraf has an exec input that allows us to inject one measurement with a specific value. It looks like this:

[[inputs.exec]]
  commands = ["<some command>"]
  name_override = "fritzbox_byte_receive_rate"
  data_format = "value"
  data_type = "integer"

Now for the ugly truth: we can not use the command shown earlier for where it says some command - we must instead use some simple bash logic:

[[inputs.exec]]
  commands = ["/bin/bash -c \"BoxUSER=YourUser BoxPW=YourPassword /opt/telegraf/FritzBoxShell/fritzBoxShell.sh IGDWAN STATE|grep NewByteReceiveRate|cut -d ' ' -f 2\"" ]
  name_override = "fritzbox_byte_receive_rate"
  data_format = "value"
  data_type = "integer"

Note

Data with huge absolute values probably dont fit into type integer - in this case, long is the way to go...


Pushing of multiple values to telegraf

Instead of pushing single values to telegraf, you can push all desired values:

fritzBoxShell.sh IGDWAN STATE | tr '\n' ',' | tr ' ' '=' | sed "s/,$//" | echo "fritz $(cat -)"

This creates a valid influx string (documented here)

In my setup, I also added a grep in the first place to filter only relevant lines.

I've put this into a shell file to circumvent the escape backslashes. My [[inputs.exec]] looks like this:

[[inputs.exec]]
  commands = ["/bin/bash /etc/telegraf/fritzBoxShell/to_influx.sh"]
  data_format = "influx"

And the to_influx.sh looks like this:

cd "$(dirname "$0")"
/bin/bash fritzBoxShell.sh IGDWAN STATE | grep Byte | tr '\n' ',' | tr ' ' '=' | sed "s/,$//" | echo "fritz $(cat -)"

Non-integer values

You can use sed -E 's/ ([A-Za-z]+)/ "\1"/' to surround value with double quotes. For instance:

/bin/bash /tmp/fritzBoxShell.sh IGDIP STATE | egrep "NewConnectionStatus|NewUptime" | sed -E 's/ ([A-Za-z]+)/ "\1"/' | tr '\n' ',' | tr ' ' '=' | sed "s/,$//" | echo "fritz $(cat -)"