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 Bucketbutton - Give the Bucket a 
Nameand choose when toDelete Dataand click theCreatebutton 
 - Go to the tab 
 - Go to the tab 
Telegraf - Click the 
Create Configurationbutton - Choose the Bucket
 - Choose Docker to monitor
 - Under 
Pluginsclick ondockerand enterunix:///var/run/docker.sockas endpoint and click theDonebutton - Give your configuration a name. For example 
telegraf - Click the 
Create and Verifybutton - An API token has now been automatically created. Click the 
Copy to Clipboardbutton to copy the token - Click 
Finish - Now click on the name of your configuration. In my case this was 
telegraf - Change the agent 
intervalif needed. I have set an interval of 60 seconds - Scroll down to the 
outputs.influxdb_v2section and replace$INFLUX_TOKENwith 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 Configbutton to download your configuration - Click the 
Save Changesbutton and confirm this 
Setup
- 
Create the folders needed by the container
Run the following commands inside your home folder:
# Open your terminal applicationcd ~mkdir -p telegraf/configcd telegraf - 
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:# Open your terminal applicationsudo docker run --rm telegraf telegraf config > config/telegraf.conf - 
Create the script needed to run the container
Save the following script as
telegraf_run.sh:telegraf_run.sh # To create this script use your text editor application, for example Nanodocker run -d \--name=telegraf \--hostname=telegraf \--user telegraf:$(stat -c '%g' /var/run/docker.sock) \-p 8125:8125/udp \-p 8092:8092/udp \-p 8094:8094 \-v $PWD/config/telegraf.conf:/etc/telegraf/telegraf.conf:ro \-v /var/run/docker.sock:/var/run/docker.sock \-e TZ=Europe/Amsterdam \--restart unless-stopped \telegraf# IMPORTANT: Please read the instructions belowInstructions:
- Optional  Replace 
dockerwithpodmanif needed - Required  Replace port number 
8125and8092and8094(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.confwith the location of your Telegraf configuration if needed - Optional  The setting 
/var/run/docker.sockis needed if you want to monitor Docker - Required  Replace 
Europe/Amsterdamwith your own timezone 
 - Optional  Replace 
 - 
Run the script to create the container
Run the following command:
# Open your terminal applicationsudo sh telegraf_run.shThe image
telegrafis 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.