nagios-scripts/check-md.sh

129 lines
3.2 KiB
Bash

#!/bin/bash
#
#/
#/ Usage:
#/ check-md.sh --warning=<events> --critical=<events> --help <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)
#/ -h, --help Display this help message
#/
#/ 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:h
LONGOPTS=warning:,critical:,help
# -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
;;
-h|--help)
Usage
exit 0
;;
--)
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}"