Skip to content

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:

  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

Setup

  1. Create the folders needed by the container

    Run the following commands inside your home folder:

    # Open your terminal application
    cd ~
    mkdir -p telegraf/config
    cd telegraf
  2. 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 application
    sudo docker run --rm telegraf telegraf config > config/telegraf.conf
  3. 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 Nano
    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
    # IMPORTANT: Please read the instructions below
    Instructions:
    • Optional Replace docker with podman if needed
    • Required Replace port number 8125 and 8092 and 8094 (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
  4. Run the script to create the container

    Run the following command:

    # Open your terminal application
    sudo sh telegraf_run.sh

    The image telegraf is automatically pulled and the container is created.

  5. 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.

Favorites

Comments

    No comments found for this note.

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

    Copyright 2021- Fiction Becomes Fact