#!/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" |
This comment has been minimized.
This comment has been minimized.
Thanks a lot ;) |
This comment has been minimized.
This comment has been minimized.
Nice gist, thanks. |
This comment has been minimized.
This comment has been minimized.
Thanks a lot, great job. |
This comment has been minimized.
This comment has been minimized.
cool, thank you |
This comment has been minimized.
This comment has been minimized.
Are ghosted containers still an issue? This thread seems to indicate they aren't |
This comment has been minimized.
This comment has been minimized.
Thanks, just what I needed! |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Awesome script, thank you :) docker inspect is a powerful command |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
How can i run this script without sudo?Because when i try to run this script via nagios user,it says does not exist and with sudo ./check_docker_container container_id command it works fine.Any suggestions?Thanks |
This comment has been minimized.
This comment has been minimized.
when doing the ghost command, i get:
is this an error or a normal msg ? thanks ! |
This comment has been minimized.
This comment has been minimized.
I get the same error as michabb, (Docker version 1.9.1, build a34a1d5, on Ubuntu, Trusty Tahr 14.04.4 LTS), but when I issue the docker inspect command for the container I am using, the State.Ghost field does not exist. So I expect this is: I thought off option c: the property is only added in specific circumstances, that would be strange though, imho. Ow the properties I do have are: A bit of a newbie in docker, but maybe Dead is a replacement for ghost, have to read more changelogs and manual pages. |
This comment has been minimized.
This comment has been minimized.
Awesome, it helped me to write my own script. Good job. |
This comment has been minimized.
This comment has been minimized.
since this comment, I've adopted this script with two modifications:
|
This comment has been minimized.
This comment has been minimized.
first of thanks for sharing. docker inspect command. Added something like to top # permissions if [ "$(whoami)" != "root" ]; then echo "Root privileges are required to run this, try running with sudo..." exit 2 fi |
This comment has been minimized.
This comment has been minimized.
Thanks man, after 3 years it's still not outdated! |
This comment has been minimized.
This comment has been minimized.
If you are using Docker 1.12+ and using the IP driver (direct,macvlan, etc) -- the way to get the IP address is:
That also works on the default networked containers, so it's probably a better way. |
This comment has been minimized.
This comment has been minimized.
A slight improvement:
other ps filter options: |
This comment has been minimized.
This comment has been minimized.
Thanks a lot!! |
This comment has been minimized.
This comment has been minimized.
2> /dev/null is what I needed after hours of searching. |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
Hello guys.
So, it works.
Nagios displays the same "UNKNOWN - NRPE: Unable to read output" Here is nrpe.cfg
Am I missing something? What's wrong? |
This comment has been minimized.
This comment has been minimized.
Just solved my issue. |
This comment has been minimized.
This comment has been minimized.
@wirtoo can you share the permission issue fix you used? |
This comment has been minimized.
This comment has been minimized.
I'm running NRPE in a container. Do I need to add the
|
This comment has been minimized.
This comment has been minimized.
What I do wrong? Remote Local |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
Awesome script, thank you, just what I needed! |
This comment has been minimized.
This comment has been minimized.
HI, i need docker stat output with mail alert shell script. Please help on this |
This comment has been minimized.
This comment has been minimized.
Thank you for sharing this! |
This comment has been minimized.
This comment has been minimized.
Thank you Erik Kristensen |
This comment has been minimized.
This comment has been minimized.
Thanks a lot, this is also working for PRTG (with some small changes in the output). |
This comment has been minimized.
Pro, this is exactly what I was looking for. Thanks!