Telegraf container with InfluxDB to monitor Docker setup
Introduction
Telegraf from InfluxData is a server agent designed to collect metrics from sensors and systems and write them to InfluxDB or other outputs.
Telegraf efficiently writes many metrics to an InfluxDB bucket with a short retention period. This data can be reused in Home Assistant through an InfluxDB sensor.
Requirements
First make sure InfluxDB is already installed.
To be able to use Telegraf, a configuration file must be created. This can easily be done by generating a sample configuration. How this works is explained in the Setup
chapter below.
Another option is to use the InfluxDB GUI to generate the configuration. The InfluxDB output is then already filled in:
- Go to the InfluxDB GUI
- Go to
Data
- (optional) If you don’t have a Bucket for the data:
- Go to the tab
Buckets
- Click the
Create Bucket
button - Give the Bucket a
Name
and choose when toDelete Data
and click theCreate
button
- Go to the tab
- Go to the tab
Telegraf
- Click the
Create Configuration
button - Choose the Bucket
- Choose Docker to monitor
- Under
Plugins
click ondocker
and enterunix:///var/run/docker.sock
as endpoint and click theDone
button - Give your configuration a name. For example
telegraf
- Click the
Create and Verify
button - An API token has now been automatically created. Click the
Copy to Clipboard
button to copy the token - Click
Finish
- Now click on the name of your configuration. In my case this was
telegraf
- Change the agent
interval
if needed. I have set an interval of 60 seconds - Scroll down to the
outputs.influxdb_v2
section and replace$INFLUX_TOKEN
with your token (with CTRL-V) and remove:export INFLUX_TOKEN=
. InfluxDB does this by default via an environment variable, make your own choice - Configure your input if needed
- (optional) Click the
Download Config
button to download your configuration - Click the
Save Changes
button and confirm this
Setup
-
Create the folders needed by the container
Run the following commands inside your home folder:
-
Copy the configuration to the config folder
Copy and paste the configuration you created earlier to
config/telegraf.conf
. Or if you do not have a configuration, then generate a sample configuration: -
Create the script needed to run the container
Save the following script as
telegraf_run.sh
:Instructions:
- Optional Replace
docker
withpodman
if needed - Required Replace port number
8125
and8092
and8094
(on the left side of:
) with a port number that is available if needed - Optional The setting
--user telegraf:$(stat -c '%g' /var/run/docker.sock)
is needed if you want to monitor Docker - Optional Replace
$PWD/config/telegraf.conf
with the location of your Telegraf configuration if needed - Optional The setting
/var/run/docker.sock
is needed if you want to monitor Docker - Required Replace
Europe/Amsterdam
with your own timezone
- Optional Replace
-
Run the script to create the container
Run the following command:
The image
telegraf
is automatically pulled and the container is created. -
Check the results
If needed you can check if the container is running properly. And if everything went well you will see that Telegraf writes the measurements to InfluxDB.
No comments found for this note.
Join the discussion for this note on Github. Comments appear on this page instantly.