-
-
Save Scifire/03995d793ecab02331d9025853e87dc5 to your computer and use it in GitHub Desktop.
Bash Script for PRTG to Check Status of Docker Container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Original Author: Erik Kristensen | |
# Email: erik@erikkristensen.com | |
# License: MIT | |
# Original script: https://gist.github.com/ekristen/11254304 | |
# PRTG Usage: place this in '/var/prtg/scripts/check_docker_container.sh' and create a new Sensore "SSH Script". | |
# The parameter have to be the container name. | |
# | |
# Depending on your docker configuration, root might be required or at least user have to be in docker group. If your prtg 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 - January 06, 2020 | |
# - 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 | |
# - Changed Return commands to make them readbale by PRTG | |
CONTAINER=$1 | |
if [ "x${CONTAINER}" == "x" ]; then | |
echo "1:$?:UNKNOWN - Container ID or Friendly Name Required" | |
exit 3 | |
fi | |
if [ "x$(which docker)" == "x" ]; then | |
echo "1:$?:UNKNOWN - Missing docker binary" | |
exit 3 | |
fi | |
docker info > /dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
echo "1:$?: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 "1:$?:UNKNOWN - $CONTAINER does not exist." | |
exit 3 | |
fi | |
if [ "$RUNNING" == "false" ]; then | |
echo "2:$?:Down - CRITICAL - $CONTAINER is not running." | |
exit 2 | |
fi | |
RESTARTING=$(docker inspect --format="{{.State.Restarting}}" $CONTAINER) | |
if [ "$RESTARTING" == "true" ]; then | |
echo "1:$?: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 "0:$?:OK - $CONTAINER is running. IP: $NETWORK, StartedAt: $STARTED" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment