nagios-scripts/check-load.sh

121 lines
3.2 KiB
Bash
Raw Normal View History

#!/bin/bash
#
#/
#/ Usage:
#/ check-load.sh --warning=<level> --critical=<level> --average=<level>
#/
#/ Checks the system load
#/
#/ Options:
#/ -w, --warning=<level> The level of when to trigger a warning (level=loadavg/nproc)
#/ -c, --critical=<level> The level of when to trigger a critical warning (level=loadavg/nproc)
#/ -a, --average=<average> Which avarage to use for the load level (possible values are 1, 5 and 15)
#/
#/ Exit Codes:
#/ 0 Everything OK
#/ 1 Warning level exceeded
#/ 2 Critical level exceeded
#/ 3 Unknown status
#/
Usage() {
grep '^#/' "${script_dir}/${script_name}" | sed 's/^#\/\w*//'
}
GetOptions() {
# https://stackoverflow.com/a/29754866
OPTIONS=w:c:a:
LONGOPTS=warning:,critical:,avarage:
# -use ! and PIPESTATUS to get exit code with errexit set
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
# -pass arguments only via -- "$@" to separate them correctly
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
Usage
exit 2
fi
# read getopt's output this way to handle the quoting right:
eval set -- "$PARSED"
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-w|--warning)
warn="$2"
shift 2
;;
-c|--critital)
crit="$2"
shift 2
;;
-a|--average)
if [[ ! " ${possibleavarages[*]} " =~ " ${2} " ]]; then
echo "LOAD UNKNOWN - ${2} is not a valid load avarage"
exit 3
fi
check="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "LOAD UNKNOWN - ${1} is not a valid parameter"
exit 3
;;
esac
done
}
warn=0.7
crit=1
check=5
script_name=$(basename "${0}")
script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
possibleavarages=("1" "5" "15")
GetOptions "$@"
nproc=$(nproc)
LOADVAL1=$(awk '{ print $1; }' < /proc/loadavg)
LOADVAL5=$(awk '{ print $2; }' < /proc/loadavg)
LOADVAL15=$(awk '{ print $3; }' < /proc/loadavg)
checkval=${LOADVAL5}
case "$check" in
"1")
checkval=${LOADVAL1}
;;
"5")
checkval=${LOADVAL5}
;;
"15")
checkval=${LOADVAL15}
;;
esac
val=$(echo ${checkval}/${nproc} | bc -l)
if (( $(echo "$val < ${warn}" | bc -l) )); then
rval=0
rmsg="OK"
elif (( $(echo "$val < ${crit}" | bc -l) )); then
rval=1
rmsg="WARNING"
else
rval=2
rmsg="CRITICAL"
fi
crit=${nproc}
warn=$(echo ${nproc}\*${warn} | bc -l)
min=0
unit=""
echo "LOAD ${rmsg} - ${LOADVAL1} ${LOADVAL5} ${LOADVAL15}|load1m=${LOADVAL1}${unit};$warn;$crit;$min load5m=${LOADVAL5}${unit};$warn;$crit;$min load15m=${LOADVAL15}${unit};$warn;$crit;$min"
exit $rval