Skip to content

Instantly share code, notes, and snippets.

@psi-4ward
Created August 25, 2015 15:36
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 psi-4ward/e280f9b19bed3b04d91d to your computer and use it in GitHub Desktop.
Save psi-4ward/e280f9b19bed3b04d91d to your computer and use it in GitHub Desktop.
check_docker_cpu.sh
#!/usr/bin/env bash
#############################################################
# Check the cpu 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
TMPDIR=/run/check_docker_cpu
# Set help
print_help () {
echo "Usage: $SCRIPTNAME -n <containerName> -i <containerID> [-w <warning>] [-c <critical>]"
echo ""
echo "This plugin checks cpu consumption for a running docker container."
echo ""
echo "Provide either a -n OR -i"
echo "Warn and crit thresholds are relative values in percent."
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 percentage"
echo " -c <critical> Critical threshold in percentage"
echo " -ignoreCores Ignore the vCPU number"
echo ""
}
# Parse CLI args
while [[ $# > 0 ]]; do
key=$1
case "$key" in
-h|--help)
print_help
exit 0
;;
-v|--version)
echo $VERSION
exit 0
;;
-ignoreCores)
echo IGNORE
IGNORE_CORES="1"
;;
-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
mkdir -p $TMPDIR
if [ ! -w $TMPDIR ]; then
>&2 echo "Error: $TMPDIR not writeable"
exit $STATE_UNKNOWN
fi
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
NOW=$(expr `date +"%s"` \* 1000000000 + `date +"%N"`)
USAGE=`cat /sys/fs/cgroup/cpu,cpuacct/system.slice/docker-$CONTAINER_ID*.scope/cpuacct.usage`
if [ "$USAGE" == "" ] ; then
>&2 echo "Error: Could not read cgroup values for ContainerID $CONTAINER_ID. Is the container running?"
exit $STATE_UNKNOWN
fi
# Load values from last run
[ -e "$TMPDIR/cid-$CONTAINER_ID" ] && source $TMPDIR/cid-$CONTAINER_ID
# Save current values
echo "LAST_NOW=$NOW" > $TMPDIR/cid-$CONTAINER_ID
echo "LAST_USAGE=$USAGE" >> $TMPDIR/cid-$CONTAINER_ID
if [ "$LAST_NOW" == "" ]; then
# This is the first run for this container, we cant do anything
echo "First run"
exit $STATE_OK
fi
# Caluclation
ELAPSED_TIME=$(expr $NOW - $LAST_NOW)
DELTA=$(expr $USAGE - $LAST_USAGE)
PERCENT=$(expr $DELTA \* 100 / $ELAPSED_TIME)
if [ "$PERCENT" == "" ]; then
>&2 echo "Error: Unknown error"
exit $STATE_UNKNOWN
fi
MAX=100
if [ "$IGNORE_CORES" != "1" ]; then
PERCENT=$(expr $PERCENT / `nproc`)
else
MAX=$(expr `nproc` \* 100)
fi
echo "CPU usage: ${PERCENT}% | CpuUsage=${PERCENT}%;$WARN;$CRIT;0;$MAX"
[ "$DONTWARN" == "1" ] && exit $STATE_OK
[ $PERCENT -gt $CRIT ] && exit $STATE_CRITICAL
[ $PERCENT -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