Skip to content

Instantly share code, notes, and snippets.

@lgaggini
Forked from ekristen/check_docker_container.sh
Last active May 10, 2017 08:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgaggini/826a26ce9a9e2c7a1fdc to your computer and use it in GitHub Desktop.
Save lgaggini/826a26ce9a9e2c7a1fdc to your computer and use it in GitHub Desktop.
Bash Script for Nagios to Check Status of Docker Container
#!/bin/bash
# Author: Erik Kristensen, Lorenzo Gaggini
# Email: erik@erikkristensen.com, lorenzo.gaggini@dada.eu
# License: MIT
# Nagios Usage: check_nrpe!check_docker_container!_container_id_
# Usage: ./check_docker_container.sh _container_id_
#
# The script checks if a container is running and grab performance data
# OK - running
# WARNING - container is ghosted
# CRITICAL - container is stopped
# UNKNOWN - does not exist
CONTAINER=$1
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
GHOST=$(docker inspect --format="{{ .State.Dead }}" $CONTAINER)
if [ "$GHOST" == "true" ]; then
echo "WARNING - $CONTAINER is dead."
exit 1
fi
STARTED=$(docker inspect --format="{{ .State.StartedAt }}" $CONTAINER)
NETWORK=$(docker inspect --format="{{ .NetworkSettings.IPAddress }}" $CONTAINER)
STATS=$(docker stats --no-stream $CONTAINER | tail -1)
CPU=$(echo $STATS | awk '{print $2}')
MEMUSAGE=$(echo $STATS | awk '{print $6}')
MEMUSED=$(echo $STATS | awk '{print $3$4}' | cut -d '/' -f1)
NETIN=$(echo $STATS | awk '{print $7$8}' | cut -d '/' -f1)
NETOUT=$(echo $STATS | awk '{print $8$9}' | cut -d'/' -f2)
echo "OK - $CONTAINER is running. IP: $NETWORK, StartedAt: $STARTED | cpu=$CPU mem=$MEMUSAGE memused=$MEMUSED netIn=$NETIN netOut=$NETOUT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment