Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save harshadyeola/e731ff744b87535abfb20b2a2d2dbcc0 to your computer and use it in GitHub Desktop.
Save harshadyeola/e731ff744b87535abfb20b2a2d2dbcc0 to your computer and use it in GitHub Desktop.

Revisions

  1. @ekristen ekristen created this gist Apr 24, 2014.
    39 changes: 39 additions & 0 deletions check_docker_container.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/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"