feat: Add disk, load and memory check scripts

Added three new scripts to check the status of disk usage, system load and memory usage. Each script takes in options for warning and critical levels, as well as an option to choose which average to use for the load level. The output includes information on used, available and total space/usage along with percentage values.
This commit is contained in:
Jeroen De Meerleer 2023-05-26 12:32:12 +02:00
parent 1dfaa2479b
commit 84d97e3ca0
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
3 changed files with 345 additions and 0 deletions

117
check-disk.sh Normal file
View File

@ -0,0 +1,117 @@
#!/bin/bash
#
#/
#/ Usage:
#/ music-sync <options> -s|--source <source> -d|--dest <destination>
#/ music-sync <options> <source> <destination>
#/
#/ Syncronises music from one folder to another.
#/
#/ 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)
#/
#/ 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:
LONGOPTS=warning:,critical:
# -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
;;
--)
shift
break
;;
*)
echo "DISK UNKNOWN - ${1} is not a valid parameter"
exit 3
;;
esac
done
if [ -z ${1+x} ]; then
echo "DISK UNKNOWN - Mountpoint not given"
exit 3
elif [ ! -d ${1} ]; then
echo "DISK UNKNOWN - Mountpoint is not a directory"
exit 3
else
disk=${1}
fi
}
LC_NUMERIC="C"
warn=0.7
crit=0.9
disk='/'
script_name=$(basename "${0}")
script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
possibleavarages=("1" "5" "15")
GetOptions "$@"
totalfull=$(df --output=size -B 1024 "${disk}" | tail -n 1 | xargs)
availfull=$(df --output=avail -B 1024 "${disk}" | tail -n 1 | xargs)
usedfull=$(df --output=used -B 1024 "${disk}" | tail -n 1 | xargs)
val=$(echo "${usedfull}/${totalfull}" | 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
critfull=$(echo ${totalfull}\*${crit} | bc -l | awk '{ print int($1+0.5) }')
warnfull=$(echo ${totalfull}\*${warn} | bc -l | awk '{ print int($1+0.5) }')
minfull=0
maxfull=${totalfull}
unitfull="KiB"
used100=$(printf %.1f $(echo ${val}\*100 | bc -l))
crit100=$(printf %.1f $(echo ${crit}\*100 | bc -l))
warn100=$(printf %.1f $(echo ${warn}\*100 | bc -l))
min100=0
max100=100
unit100="%"
echo "DISK ${rmsg} - Used: ${usedfull} Available: ${availfull} Total: ${totalfull}|disk=${usedfull}${unitfull};$warnfull;$critfull;$minfull;$maxfull disk%=${used100}${unit100};$warn100;$crit100;$min100;$max100"
exit $rval

121
check-load.sh Normal file
View File

@ -0,0 +1,121 @@
#!/bin/bash
#
#/
#/ Usage:
#/ music-sync <options> -s|--source <source> -d|--dest <destination>
#/ music-sync <options> <source> <destination>
#/
#/ Syncronises music from one folder to another.
#/
#/ 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

107
check-mem.sh Normal file
View File

@ -0,0 +1,107 @@
#!/bin/bash
#
#/
#/ Usage:
#/ music-sync <options> -s|--source <source> -d|--dest <destination>
#/ music-sync <options> <source> <destination>
#/
#/ Syncronises music from one folder to another.
#/
#/ 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)
#/
#/ 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:
LONGOPTS=warning:,critical:
# -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
;;
--)
shift
break
;;
*)
echo "MEMORY UNKNOWN - ${1} is not a valid parameter"
exit 3
;;
esac
done
}
LC_NUMERIC="C"
warn=0.7
crit=0.9
script_name=$(basename "${0}")
script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
possibleavarages=("1" "5" "15")
GetOptions "$@"
totalfull=$(cat /proc/meminfo | grep MemTotal | awk '{ print $2; }')
availfull=$(cat /proc/meminfo | grep MemAvailable | awk '{ print $2; }')
usedfull=$(echo ${totalfull}-${availfull} | bc)
val=$(echo "${usedfull}/${totalfull}" | 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
critfull=$(echo ${totalfull}\*${crit} | bc -l | awk '{ print int($1+0.5) }')
warnfull=$(echo ${totalfull}\*${warn} | bc -l | awk '{ print int($1+0.5) }')
minfull=0
maxfull=${totalfull}
unitfull="KiB"
used100=$(printf %.1f $(echo ${val}\*100 | bc -l))
crit100=$(printf %.1f $(echo ${crit}\*100 | bc -l))
warn100=$(printf %.1f $(echo ${warn}\*100 | bc -l))
min100=0
max100=100
unit100="%"
echo "MEMORY ${rmsg} - Used: ${usedfull} Available: ${availfull} Total: ${totalfull}|mem=${usedfull}${unitfull};$warnfull;$critfull;$minfull;$maxfull mem%=${used100}${unit100};$warn100;$crit100;$min100;$max100"
exit $rval