Skip to content

Instantly share code, notes, and snippets.

@ekristen
Last active January 16, 2024 16:15
Show Gist options
  • Save ekristen/11254304 to your computer and use it in GitHub Desktop.
Save ekristen/11254304 to your computer and use it in GitHub Desktop.
Bash Script for Nagios to Check Status of Docker Container
#!/bin/bash
# Author: Erik Kristensen
# Email: erik@erikkristensen.com
# License: MIT
# Nagios Usage: check_nrpe!check_docker_container!_container_id_
# Usage: ./check_docker_container.sh _container_id_
#
# Depending on your docker configuration, root might be required. If your nrpe user has rights
# to talk to the docker daemon, then root is not required. This is why root privileges are not
# checked.
#
# The script checks if a container is running.
# OK - running
# WARNING - restarting
# CRITICAL - stopped
# UNKNOWN - does not exist
#
# CHANGELOG - March 20, 2017
# - Removes Ghost State Check, Checks for Restarting State, Properly finds the Networking IP addresses
# - Returns unknown (exit code 3) if docker binary is missing, unable to talk to the daemon, or if container id is missing
CONTAINER=$1
if [ "x${CONTAINER}" == "x" ]; then
echo "UNKNOWN - Container ID or Friendly Name Required"
exit 3
fi
if [ "x$(which docker)" == "x" ]; then
echo "UNKNOWN - Missing docker binary"
exit 3
fi
docker info > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "UNKNOWN - Unable to talk to the docker daemon"
exit 3
fi
RUNNING=$(docker inspect --format="{{.State.Running}}" $CONTAINER 2> /dev/null)
if [ $? -eq 1 ]; then
echo "UNKNOWN - $CONTAINER does not exist."
exit 3
fi
if [ "$RUNNING" == "false" ]; then
echo "CRITICAL - $CONTAINER is not running."
exit 2
fi
RESTARTING=$(docker inspect --format="{{.State.Restarting}}" $CONTAINER)
if [ "$RESTARTING" == "true" ]; then
echo "WARNING - $CONTAINER state is restarting."
exit 1
fi
STARTED=$(docker inspect --format="{{.State.StartedAt}}" $CONTAINER)
NETWORK=$(docker inspect --format="{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" $CONTAINER)
echo "OK - $CONTAINER is running. IP: $NETWORK, StartedAt: $STARTED"
@ekristen
Copy link
Author

ekristen commented Mar 20, 2017

I wasn't getting notifications on this! My apologies.

I've updated the script with most of the suggestions in the comments.

Please note, I'm not using this script anymore, but if needed I'll move this to a git repo so pull requests can be accepted.

@wirtoo
Copy link

wirtoo commented Mar 26, 2018

Hello guys.
Added nagios user to docker group, so it has permissions to speak to docker daemon.
When I execute it as nagios:

nagios@nrpe-client-host:~$ sh /usr/lib/nagios/plugins/check_docker_container.sh redis
/usr/lib/nagios/plugins/check_docker_container.sh: 25: [: xredis: unexpected operator
/usr/lib/nagios/plugins/check_docker_container.sh: 30: [: x/usr/bin/docker: unexpected operator
/usr/lib/nagios/plugins/check_docker_container.sh: 48: [: true: unexpected operator
/usr/lib/nagios/plugins/check_docker_container.sh: 55: [: false: unexpected operator
OK - redis is running. IP: 172.17.0.2, StartedAt: 2018-03-01T08:07:42.857992735Z

So, it works.
But when I'm trying to execute it from nagios host:

user@nagios-host:~$ /usr/local/nagios/libexec/check_nrpe -H 123.45.67.89 -c check_docker_container redis
NRPE: Unable to read output

Nagios displays the same "UNKNOWN - NRPE: Unable to read output"

Here is nrpe.cfg
command[check_docker_container]=/usr/lib/nagios/plugins/check_docker_container.sh
and service definition

define service {
        use                             generic-service
        host_name                       host-example
        service_description             Redis Docker Container
        check_command                   check_nrpe!check_docker_container!redis
}

Am I missing something? What's wrong?
Thank you!

@wirtoo
Copy link

wirtoo commented Mar 27, 2018

Just solved my issue.
It was about permissions, sorry.
Great script, btw. Thanks.

@jjbursik
Copy link

@wirtoo can you share the permission issue fix you used?

@r3lik
Copy link

r3lik commented Aug 6, 2018

I'm running NRPE in a container. Do I need to add the nagios user to /etc/sudoers in the container itself?
From my Nagios host:

./check_nrpe -H 10.99.125.131 -c check_docker_container1
NRPE: Unable to read output

@vanokg
Copy link

vanokg commented Sep 18, 2018

What I do wrong?

Remote
/usr/local/nagios/libexec/check_nrpe -H hostip -c check_docker -a asterisk
UNKNOWN - Missing docker binary

Local
/usr/lib64/nagios/plugins/check_docker asterisk
OK - asterisk is running. IP: 172.19.0.2, StartedAt: 2018-09-14T06:44:09.174409454Z

@nexusguy59
Copy link

nexusguy59 commented Jan 2, 2019

I understand this might be outdated, I mean this thread not the script. Take a look here and it may help some of you who are having permission problems. Nagios and Docker Monitoring

@anjali-gour
Copy link

Awesome script, thank you, just what I needed!

@rairaghul
Copy link

HI,

i need docker stat output with mail alert shell script. Please help on this

@xFuture603
Copy link

Thank you for sharing this!

@grambharos
Copy link

Thank you Erik Kristensen

@Scifire
Copy link

Scifire commented Jan 6, 2021

Thanks a lot, this is also working for PRTG (with some small changes in the output).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment