Skip to content

Instantly share code, notes, and snippets.

@ehostunreach
Forked from ekristen/check_docker_container.sh
Last active August 19, 2016 09:45
Show Gist options
  • Select an option

  • Save ehostunreach/47d7e53c4b40a1e7ea3a to your computer and use it in GitHub Desktop.

Select an option

Save ehostunreach/47d7e53c4b40a1e7ea3a to your computer and use it in GitHub Desktop.
#!/bin/bash
# Author: Erik Kristensen
# Email: [email protected]
# 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
GHOST=$(docker inspect --format="{{ .State.Ghost }}" $CONTAINER)
if [ "$GHOST" == "true" ]; then
echo "WARNING - $CONTAINER has been ghosted."
exit 1
fi
STARTED=$(docker inspect --format="{{ .State.StartedAt }}" $CONTAINER)
NETWORK=$(docker inspect --format="{{ .NetworkSettings.IPAddress }}" $CONTAINER)
echo "OK - $CONTAINER is running. IP: $NETWORK, StartedAt: $STARTED"
@ktanriverdi
Copy link

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

@andrusstrockiy
Copy link

@ktanriverdi just add the following to sudoers for ubuntu host

nagios ALL=(ALL) NOPASSWD:/usr/lib/nagios/plugins/check_container

and change line of script by appending sudo i.e
RUNNING=$(sudo docker inspect --format="{{ .State.Running }}" $CONTAINER 2> /dev/null)

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