Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenseii/c9ed8ade925abe1bc8bbf74d20f2d946 to your computer and use it in GitHub Desktop.
Save kenseii/c9ed8ade925abe1bc8bbf74d20f2d946 to your computer and use it in GitHub Desktop.
Monitoring scripts for docker containers.
#!/bin/bash
# Author: Haukur Kristinsson / Erik Kristensen
# Email: haukur@hauxi.is / erik@erikkristensen.com
# 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.
# 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
STARTED=$(docker inspect --format="{{ .State.StartedAt }}" $CONTAINER)
NAME=$(docker inspect --format="{{ .Name }}" $CONTAINER)
NETWORKMODE=$(docker inspect --format="{{ .HostConfig.NetworkMode }}" $CONTAINER)
NETWORK=$(docker inspect --format="{{ .NetworkSettings.Networks."$NETWORKMODE".IPAddress }}" $CONTAINER)
echo "OK - $CONTAINER is running. IP: $NETWORK, StartedAt: $STARTED, Named: $NAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment