
Monitoring Raspberry Pi with Telegraf
I had been waiting for a long time to publish some posts about Telegraf and some wonders we can do. In this simple document we will see how we can collect performance metrics from a Raspberry Pi and display them in real time with Grafana!
Assuming we already have Influxdb mounted and functional on a server, as well as Grafana (otherwise, Check this), Well, it's time to collect metrics, We start with something simple, a Linux machine (based on Debian), and what better than a Raspberry Pi, where in addition to obtaining the common metrics, we will also get those of the CPU and GPU temperatures.
¡Started! We'll install the Telegraf agent on the Raspberry Pi to tell you the metrics we want to send to the Influxdb, The first thing, we download the Influx repo keys:
curl -sL https://repos.influxdata.com/influxdb.key | Sudo apt-key add -
And we add the repository with (eye, We will select our correct version of Debian (Bullseye, Buster, stretch…) :
ECHO "Deb https://repos.influxdata.com/debian buster stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
And now it is, We installed Telegraf:
sudo wget -O - https://repos.influxdata.com/influxdb.key | Sudo apt-key add - Sudo apt-get update sudo apt install telegraf -y
Next, we have to tap on the Telegraf configuration file (/etc/telegraf/telegraf.conf), So we open it up and edit it, We will change several things, according to our needs. We start with 'interval’ and with 'flush_interval', The first indicates how often you will check, and the second one every so often he sends it to the BD, if you want at the level of 1 second would be like this, it is not necessary perhaps so aggressive, but then it is very impressive in Grafana, See how it refreshes the second…:
... [agent] interval = "1s" flush_interval = "1s" ...
In the Inputs section, We will uncomment on the metrics we want to receive, example:
... # Input Plugins [[inputs.cpu]] Percpu = True TotalCPU = True collect_cpu_time = False report_active = False [[inputs.disk]] ignore_fs = ["Also", "Please.", "Devfs"] [[inputs.io]] [[inputs.mem]] [[inputs.net]] [[inputs.system]] [[inputs.swap]] [[inputs.netstat]] [[inputs.processes]] [[inputs.kernel]] ...
And finally, in the Outputs, at the exit, Where do we send the metrics, we uncomment and correctly configure the Influxdb section:
... [[outputs.influxdb]] URLs = ["HTTP://DIRECCION_IP:8086"] # required database = "NAMEBD" username = "user" password = "XXXXXXXX" skip_database_creation = true ...
After saving the file, We can test if the configuration is correct with:
Telegraf --Test
We restarted Telegraf services so that it loads the newly modified configuration, we see that it started and we instruct Telegraf to start with the system:
Sudo SystemCTL Start Telegraf sudo SystemCTL Status Telegraf sudo SystemCTL Enable Telegraf
If in addition to these standard metrics we also want to add metrics that give us the temperature of the CPU and/or the temperature of the GPU of the Raspberry Pi, We continue with, adding the Telegraf user to the video group so that they can read such data:
sudo usermod -G telegraf video
Then we can create a configuration file for this purpose, For example we call it '/etc/telegraf/telegraf-raspberrypitemp.conf’ and with the following content:
[[inputs.file]] files = ["/sys/class/thermal/thermal_zone0/temp"] name_override = "cpu_temperature" data_format = "value" data_type = "Integer" [[inputs.exec]] commands = ["/opt/vc/bin/vcgencmd measure_temp"] name_override = "gpu_temperature" data_format = "Grok" grok_patterns = ["%{NUMBER:value:float}"]
We reload Telegraf to read this new configuration and we would have it ready:
sudo systemctl restart telegraf
With this, the data would already be stored in Influxdb, now to view it from Grafana because the usual, if we don't have the Data Source against Influxdb we create it, and the next and most comfortable thing will be to download a Dashboard that we like and has made the community 🙂 I have tried with this Dashboard, which as you can see is as simple as when creating a new Dashboard in our Grafana, import your ID, in this case the '10578', but come on, there are many other dashboards or you will modify it in a jiffy. By the way, this Dashboard is by Jorge de la Cruz, So thanks for your little time!!
And I'm sure that in a moment you'll have something as cool as this! Or wow, Much better!!! We can now visualize in real time the performance of our Raspberry Pi, we see the different CPUs, Memoirs, Disk usage, Network Traffic, load, Temperatures…
Well, I say goodbye as always thanking all of you who read these badges, to those who want to learn, to whom you share informative content on social networks!
Hugs!