Created
August 25, 2015 13:10
-
-
Save psi-4ward/d1104e06508ce7ef1c62 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
############################################################# | |
# Check the memory consumption of a running docker container | |
# by reading cgroup information | |
# | |
# Licence: LGPL | |
# Author: Christoph Wiechert <wio@psitrx.de> | |
############################################################# | |
# Set States | |
STATE_OK=0 | |
STATE_WARNING=1 | |
STATE_CRITICAL=2 | |
STATE_UNKNOWN=3 | |
SCRIPTNAME=$0 | |
VERSION=1.0.0 | |
# Set help | |
print_help () { | |
echo "Usage: $SCRIPTNAME -n <containerName> -i <containerID> [-w <warning>] [-c <critical>]" | |
echo "" | |
echo "This plugin checks memory consumption for a running docker container." | |
echo "" | |
echo "Provide either a -n OR -i" | |
echo "Warn and crit thresholds could be MB or %." | |
echo "To use a relative value append a % sign. The value is calculated using" | |
echo "the cgroup memory-limit or if its not set, the available system memory." | |
echo "Omit -w and -c to return OK for every value" | |
echo "" | |
echo "Options:" | |
echo " -h Prints this helpscreen" | |
echo " -v Prints the version" | |
echo " -n <containerName> Name of the running docker container" | |
echo " -i <containerId> CID" | |
echo " -w <warning> Warning threshold in MB. To use percentage append %" | |
echo " -c <critical> Critical threshold in MB. To use percentage append %" | |
echo "" | |
} | |
# Parse CLI args | |
while [[ $# > 1 ]]; do | |
key=$1 | |
case "$key" in | |
-h|--help) | |
print_help | |
exit 0 | |
;; | |
-v|--version) | |
echo $VERSION | |
exit 0 | |
;; | |
-n) | |
CONTAINER_NAME="$2" | |
shift | |
;; | |
-i) | |
CONTAINER_ID="$2" | |
shift | |
;; | |
-w) | |
WARN="$2" | |
shift | |
;; | |
-c) | |
CRIT="$2" | |
shift | |
;; | |
*) | |
>&2 echo "Ignoring unknowen option $key" | |
;; | |
esac | |
shift # past argument or value | |
done | |
if [ "$CONTAINER_NAME" = "" ] && [ "$CONTAINER_ID" = "" ]; then | |
>&2 echo "Error: Either a ContainerName (-n) or a ContainerID (-i) is mandatory." | |
print_help | |
exit $STATE_UNKNOWN | |
fi | |
# Resolve ContainerName to CID | |
if [ "$CONTAINER_ID" = "" ]; then | |
CONTAINER_ID=$(docker ps | grep -E "$CONTAINER_NAME[ ]*$" | cut -d " " -f 1) | |
if [ "$CONTAINER_ID" == "" ] ; then | |
>&2 echo "Error: Could not find CID for $CONTAINER_NAME. Is the container running?" | |
exit $STATE_UNKNOWN | |
fi | |
fi | |
# Read the values | |
LIMIT=`cat /sys/fs/cgroup/memory/system.slice/docker-$CONTAINER_ID*.scope/memory.limit_in_bytes` | |
USAGE=`cat /sys/fs/cgroup/memory/system.slice/docker-$CONTAINER_ID*.scope/memory.usage_in_bytes` | |
if [ "$LIMIT" == "" ] || [ "$USAGE" == "" ] ; then | |
>&2 echo "Error: Could not read cgroup values for ContainerID $CONTAINER_ID. Is the container running and cgroup_enable=memory?" | |
exit $STATE_UNKNOWN | |
fi | |
# Fix implausible limit_in_bytes value | |
if [ ${#LIMIT} -gt 12 ] ; then | |
LIMIT=`cat /proc/meminfo | grep MemTotal | awk '{ print $2 }'` | |
LIMIT=$(perl -e "printf('%u', $LIMIT*1024)") | |
fi | |
USAGE_MB=$(perl -e "printf('%u', $USAGE/1024/1024)") | |
LIMIT_MB=$(perl -e "printf('%u', $LIMIT/1024/1024)") | |
USAGE_PERC=$(perl -e "printf('%u', $USAGE_MB/$LIMIT_MB*100)") | |
[ "$WARN" == "" ] && [ "$CRIT" == "" ] && DONTWARN="1" | |
if [ "$WARN" != "" ]; then | |
if [ "${WARN: -1}" == "%" ] ; then | |
WARN=$(perl -e "printf('%u', $LIMIT_MB*${WARN:0:-1}/100)") | |
fi | |
else | |
WARN=$LIMIT_MB | |
fi | |
if [ "$CRIT" != "" ]; then | |
if [ "${CRIT: -1}" == "%" ] ; then | |
CRIT=$(perl -e "printf('%u', $LIMIT_MB*${CRIT:0:-1}/100)") | |
fi | |
else | |
CRIT=$LIMIT_MB | |
fi | |
echo "Memory usage: ${USAGE_MB}MB $PERCENT% | MemoryUsage=${USAGE_MB}MB;$WARN;$CRIT;0;$LIMIT_MB" | |
[ "$DONTWARN" == "1" ] && exit $STATE_OK | |
[ $USAGE_MB -gt $CRIT ] && exit $STATE_CRITICAL | |
[ $USAGE_MB -gt $WARN ] && exit $STATE_WARNING | |
exit $STATE_OK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment