nagios-scripts/check-md.sh
Jeroen De Meerleer b2fccadfa1
Add check-md.sh script to monitor RAID device status
This commit adds a new bash script called check-md.sh that can be used to monitor the status of a RAID device. The script takes in options for warning and critical events, and checks the status of the specified MD-device. It then returns an exit code based on whether any warning or critical events were triggered, along with a message describing the current state of the array.
2023-06-01 14:34:36 +02:00

124 lines
3.0 KiB
Bash

#!/bin/bash
#
#/
#/ Usage:
#/ check-md.sh --warning=<events> --critical=<events> <md-device>
#/
#/ Checks the raid-device status
#/
#/ Options:
#/ -w, --warning=<events> The events of when to trigger a warning (comma-separated)
#/ -c, --critical=<events> The events of when to trigger a critical warning (comma-separated)
#/
#/ Possible events
#/ - clean
#/ - write-pending
#/ - active
#/ - degraded
#/
#/ Exit Codes:
#/ 0 Everything OK
#/ 1 Warning events happened
#/ 2 Critical events happened
#/ 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"
OLDIFS=$IFS
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-w|--warning)
IFS=","
warn=$(echo $2)
IFS=$OLDIFS
shift 2
;;
-c|--critical)
IFS=","
crit=$(echo $2)
IFS=$OLDIFS
shift 2
;;
--)
shift
break
;;
*)
echo "MD UNKNOWN - ${1} is not a valid parameter"
exit 3
;;
esac
done
if [ -z ${1+x} ]; then
echo "MD UNKNOWN - MD-device not given"
exit 3
elif [ ! -d /sys/block/${1} ]; then
echo "MD UNKNOWN - MD-device is not valid"
exit 3
else
device=${1}
fi
}
LC_NUMERIC="C"
warn='active'
crit='degraded'
device=''
script_name=$(basename "${0}")
script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
GetOptions "$@"
events=()
degradeddevs=$(cat /sys/block/${device}/md/degraded)
msg=()
if [[ $degradeddevs > 0 ]]; then
actevents+=('degraded')
msg+=("$degradeddevs devices degraded")
fi
state=$(cat /sys/block/${device}/md/array_state)
msg+=("Array ${device} is ${state}")
actevents+=($state)
rval=0
rmsg="OK"
for warnevent in $warn; do
if [[ " ${actevents[*]} " =~ " ${warnevent} " ]]; then
rval=1
rmsg="WARNING"
fi
done
for critevent in $crit; do
if [[ " ${actevents[*]} " =~ " ${critevent} " ]]; then
rval=2
rmsg="CRITICAL"
fi
done
msgprint=$( (IFS=$'\n'; echo "${msg[*]}") )
echo "MD ${rmsg} - ${msgprint}"