Created
November 18, 2017 17:58
-
-
Save NFarrington/62e60e1b6f3a0b235fd7633a423dd0e5 to your computer and use it in GitHub Desktop.
Nagios Plugins
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
#!/bin/bash | |
# An updated memory plugin to use MemAvailable rather than MemFree | |
# TODO: Error check for kernel version (check MemAvailable is available in /proc/meminfo) | |
# TODO: Add swap checks | |
# TODO: Improve rounding | |
command -v bc > /dev/null 2>&1 || { | |
echo "UNKNOWN - Missing Command: bc" | |
exit 3 | |
} | |
USAGE="`basename $0` [-w|--warning] <percent used> [-c|--critical] <percent used>" | |
if [[ $# -lt 4 ]]; then | |
echo -e "UNKNOWN - Incorrect Syntax: `basename $0` $*\nUsage: $USAGE" | |
exit 3 | |
fi | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
-w|--warning) | |
shift | |
WARN=$1 | |
;; | |
-c|--critical) | |
shift | |
CRIT=$1 | |
;; | |
esac | |
shift | |
done | |
if [[ $CRIT -le $WARN ]]; then | |
THRESHOLD_USAGE="Critical threshold must be greater than warning threshold: `basename $0` $*" | |
echo -e "UNKNOWN - $THRESHOLD_USAGE" | |
exit 3 | |
fi | |
TOTAL=`grep -o '^MemTotal:.*kB$' /proc/meminfo | gawk '{print "scale=4; "$2"/1024"}' | bc` | |
AVAILABLE=`grep -o '^MemAvailable:.*kB$' /proc/meminfo | gawk '{print "scale=4; "$2"/1024"}' | bc` | |
USED=`echo "scale=4; $TOTAL-$AVAILABLE" | bc` | |
USED_WARN=`echo "scale=0; $TOTAL*$WARN/100" | bc` | |
USED_CRIT=`echo "scale=0; $TOTAL*$CRIT/100" | bc` | |
PERCENT=`echo "scale=2; 100-100*$AVAILABLE/$TOTAL" | bc` | |
if [[ "`echo "$PERCENT >= $CRIT" | bc`" -eq 1 ]]; then | |
echo "CRITICAL - Memory $PERCENT% used - ${AVAILABLE::-3}MB memory left | 'Memory Used'=${USED}MB;$USED_WARN;$USED_CRIT;0;${TOTAL::-5}" | |
exit 2 | |
elif [[ "`echo "$PERCENT >= $WARN" | bc`" -eq 1 ]]; then | |
echo "WARNING - Memory $PERCENT% used - ${AVAILABLE::-3}MB memory left | 'Memory Used'=${USED}MB;$USED_WARN;$USED_CRIT;0;${TOTAL::-5}" | |
exit 1 | |
elif [[ "`echo "$PERCENT < $WARN" | bc`" -eq 1 ]]; then | |
echo "OK - Memory $PERCENT% used - ${AVAILABLE::-3}MB memory left | 'Memory Used'=${USED}MB;$USED_WARN;$USED_CRIT;0;${TOTAL::-5}" | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment