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:
- 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
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
Tags
Notes mentioning this note
- Home Assistant - Monitor Performance of Docker Containers
Within my Home Assistant dashboard I wanted to see information about my Docker containers. For example CPU use, memory use...
- Docker - Telegraf Container with Syslog Receiver Input Plugin
I found out that an IoT device (smart power strip) had the setting to communicatie with a syslog server and...
Comments
No comments found for this note.
Join the discussion for this note on this ticket. Comments appear on this page instantly.