Docker - Telegraf Container Setup with InfluxDB to Monitor Docker


Here I describe my setup of the Docker Telegraf container. Telegraf from InfluxData is a server agent for collecting metrics from sensors and systems and writing them to InfluxDB or other outputs.

I initially chose Home Assistant to monitor Docker. In the end I chose Telegraf because it makes it very easy to have many metrics written to an InfluxDB bucket with a fairly short retention. You can then use this data again in Home Assistant by means of an InfluxDB sensor.

Dependencies

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 Installation chapter. Another option is to use the InfluxDB GUI to generate the configuration. The InfluxDB output is then already filled in:

  1. Go to the InfluxDB GUI
  2. Go to Data
  3. (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 to Delete Data and click the Create button
  4. Go to the tab Telegraf
  5. Click the Create Configuration button
  6. Choose the Bucket
  7. Choose Docker to monitor
  8. Under Plugins click on docker and enter unix:///var/run/docker.sock as endpoint and click the Done button
  9. Give your configuration a name. For example telegraf
  10. Click the Create and Verify button
  11. An API token has now been automatically created. Click the Copy to Clipboard button to copy the token
  12. Click Finish
  13. Now click on the name of your configuration. In my case this was telegraf
  14. Change the agent interval if needed. I have set an interval of 60 seconds
  15. 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
  16. Configure your input if needed
  17. (optional) Click the Download Config button to download your configuration
  18. Click the Save Changes button and confirm this

Installation

I run this Docker container on a Ubuntu VM that runs via [[ Proxmox VE ]]. I perform the following on the CLI.

Docker pull command:

docker pull telegraf

Within my home folder I have created a Docker folder where I create a subfolder for each container:

cd ~
mkdir -p docker/telegraf
cd docker/telegraf

Also create folders where the Telegraf config file can be stored:

mkdir config

Now copy the configuration to the config directory you just created. This can be the configuration you created under the Dependencies chapter. In my case I copied and pasted the configuration into the Nano text editor:

sudo nano config/telegraf.conf
# Exit Nano with CTRL-X and save the changes

Or if you do not have a configuration. Then generate a sample configuration and save it as telegraf.conf :

docker run --rm telegraf telegraf config > config/telegraf.conf

Now that the configuration has been created we create a shell script with Nano:

sudo nano telegraf_run.sh

With this shell script we are going to create the container. Copy the following into telegraf_run.sh:

docker 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

If necessary, adjust the following:

–user telegraf:$(stat -c ‘%g’ /var/run/docker.sock)
This is needed if you want to monitor Docker

-p 8125:8125/udp and -p 8092:8092/udp and 8094:8094
Choose a port that is still available. Check this with netstat

-v $PWD/config/telegraf.conf:/etc/telegraf/telegraf.conf:ro
Choose the location for the Telegraf configuration. In this example it is the config map we created

-v /var/run/docker.sock:/var/run/docker.sock
This is needed if you want to monitor Docker

-e TZ=Europe/Amsterdam
Pick the right timezone

Exit Nano (CTRL-X) and save the changes.

Make sure your Docker input and InfluxDB output is configured and then start the container:

sudo sh telegraf_run.sh

Check if the container is running properly.

See also my notes about updating containers with Portainer or via the CLI. With Synology’s Docker Application, updating a container is also very easy.

Using Telegraf

if everything went well you will see that Telegraf writes the measurements to InfluxDB.


Read other notes

Comments

    No comments found for this note.

    Join the discussion for this note on this ticket. Comments appear on this page instantly.

    Tags


    Notes mentioning this note


    Notes Graph