Configure Telegraf to write metrics to InfluxDB

Following our journey through Kubernetes deployments, from deploying Grafana on Kubernetes using Helm to deploy InfluxDB on Kubernetes using HELM, we now embark on the next step: integrating Telegraf with InfluxDB for comprehensive monitoring. In this tutorial, we’ll extend our monitoring infrastructure by setting up a Telegraf server on a Raspberry Pi device, leveraging its GPIO capabilities to connect DS18B20 temperature sensors.

As we progress, we’ll delve into the intricacies of Telegraf, an agile agent designed for collecting and transmitting metrics. Our aim is to seamlessly channel these metrics into InfluxDB, where they’ll be efficiently stored and managed. Finally, we’ll utilize Grafana to visualize this data, completing our end-to-end monitoring solution.

Whether you’re a seasoned Kubernetes enthusiast or a curious hobbyist exploring IoT monitoring, this tutorial will serve as your guide. By the end, you’ll have a robust monitoring setup tailored to Raspberry Pi’s unique capabilities, ready to provide insights into temperature fluctuations and beyond.

Let’s go…

Why use Telegraf ?

Telegraf is a server-based agent for collecting and sending all metrics and events from databases, systems, and IoT sensors. Telegraf is written in Go and compiles into a single binary with no external dependencies, and requires a very minimal memory footprint.

Collect and store all kinds of data:

IoT sensors
Collect critical stateful data (pressure levels, temp levels, etc.) with popular protocols like MQTT, ModBus, OPC-UA, and Kafka.

DevOps Tools and frameworks
Gather metrics from cloud platforms, containers, and orchestrators like GitHub, Kubernetes, CloudWatch, Prometheus, and more.

System telemetry
Metrics from system telemetry like iptables, Netstat, NGINX, and HAProxy help provide a full stack view of your apps.

Prerequisites:

          Kubernetes cluster   –> kubernetes cluster installation tutorial: https://eli-bukin.com/projects/singlenode-rke-cluster-installation/

          InfluxDB server   –> deploy InfluxDB on Kubernetes: https://eli-bukin.com/projects/influxdb-helm-on-kubernetes/

          Grafana   –> deploy Grafana on Kubernetes: https://eli-bukin.com/projects/grafana-helm-on-kubernetes/

          RaspberryPI with DS18B20 sensor connected   –> https://randomnerdtutorials.com/raspberry-pi-ds18b20-python/

Install and configure Telegraf

first let’s install and configure Telegraf on your PI .

NOTE: “stretch” is the codename for your version of Raspbian.  which you can find on the Raspberry Pi org’s download page. To find out which version you’re running, you can check the /etc/os-releases file using the following command: “cat /etc/os-release”

	1. curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
	2. echo "deb https://repos.influxdata.com/debian stretch stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
	3. sudo apt-get update
    4. sudo apt-get install telegraf
    5. sudo systemctl start telegraf
	6. sudo  systemctl enable telegraf
	7. sudo systemctl is-enabled telegraf
	8. Edit the telegraf.conf file:     /etc/telegraf/telegraf.conf
	9. Add telegraf user to video group:    sudo usermod -G video telegraf
	10. Restart Talagref:   sudo systemctl restart telegraf.service

Now open “/etc/telegraf/telegraf.conf ” and get to the “outputs” section,
there you will find “[[outputs.influxdb]]”.

Uncomment and update ‘urls’ and ‘database’ with relevant values.
it supposed to look about like this:

Next proceed to “[[inputs.exec]]” section, there you will configure your execution of the python script that queries the DS18B20 sensor.

This is a typical block of inputs.exec.
command: is the python script to be executed.
timeout: is the timeout to complete the command.
name_suffix: is the name will be assigned to that command.
data_format: is the format that will be passed to InfluxDB.

[[inputs.exec]]
   ## Commands array
commands = [
      "python /home/pi/Desktop/sysconfig-sputnik2/ds18b20/ds18b20_28-01203abf8e02_bunker_window.py"
   ]

   ## Timeout for each command to complete.
timeout = "30s"

   ## measurement name suffix (for separating different commands)
name_suffix = "ds18b20_28-01203abf8e02_bunker_window"

   ## Data format to consume.
   ## Each data format has its own unique set of configuration options, read
   ## more about them here:
   ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
data_format = "value"

Then next step will be restarting telegraf and checking InfluxDB to see the new DB created.

sudo systemctl restart telegraf.service

Connect to InfluxDB and run “show databases”, it’ll be created in a few seconds.

Grafana configuration

Now let’s go to Grafana, configure a ‘data source’, create a dashboard and write a querry.

browse to your Grafana UI ‘home>connections>add new connection’ find InfluxDB data source.

Set your database details and test connection.

Now the last part, which is creating the dashboard and writing the query.

Navigate to ‘home>dashboards’ and create a new dashboard, add a panel and write the query as it is in the following images.

For the temperature proper display you should divide by 1000 and select temperature unit, i prefer Celsius.

if all configured correctly you should be able to see the graph.