Monitoring Docker Containers
Overview
Step 1: Enable Mist.io monitoring
Step 2: Add a simple collectd script to monitor container uptime
#!/bin/sh list_of_containers="db-dev web-staging" HOSTNAME="${COLLECTD_HOSTNAME:-localhost}" INTERVAL="${COLLECTD_INTERVAL:-60}" containers=`sudo docker ps -f status=running --format "{{.Names}}"` for container in $list_of_containers do if echo $containers |grep -q $container then echo "PUTVAL \"$HOSTNAME/container-$container/gauge-status\" interval=60 N:1" else echo "PUTVAL \"$HOSTNAME/container-$container/gauge-status\" interval=60 N:0" fi done
LoadPlugin exec <Plugin exec> Exec "ubuntu" "/opt/mistio-collectd/docker.sh" </Plugin>
You cannot put root here since collectd doesn't allow it, instead, you have to use a sudo user that can sudo without password (ubuntu in our case).
Make sure /etc/sudoers contains line
ubuntu ALL=(ALL) NOPASSWD:ALL
Where you replace ubuntu with the user you have. If you don't want to allow passwordless sudo access for this user, make sure this user can run the docker command with passwordless sudo. This need the full command path. Example
root@ip-172-31-21-199:~# which docker /usr/bin/docker
So in our case the entry will be
mistio ALL=(ALL) NOPASSWD:/usr/bin/docker
Now make the script executable
root@user:~# chmod +x /opt/mistio-collectd/docker.sh
and restart collectd
root@user:~# /opt/mistio-collectd/collectd.sh restart
Step 3: View the container uptime diagram
Now visit the Machine page for the Docker host. You will see two new diagrams, one for each of the containers.
If the container is running, then values will be 1. If the container becomes unavailable, then values becomes 0 .
Step 4: Add rules to get alerted when containers are unresponsive
Now add a rule to get alerted in case the container becomes unresponsive. Click ADD RULE, select the container (name will be container NAME gauge statues where NAME is the name of our container) and set the condition as < 1
As soon as a container becomes unavailable an email alert is sent.
Step 5: Add more containers
If you want to monitor uptime for new containers, just edit file /opt/mistio-collectd/collectd.sh and add the container name on variable list_of_containers of the script (no need to restart collectd).