diff --git a/check-md.sh b/check-md.sh new file mode 100644 index 0000000..ba960b7 --- /dev/null +++ b/check-md.sh @@ -0,0 +1,124 @@ +#!/bin/bash + +# +#/ +#/ Usage: +#/ check-md.sh --warning= --critical= +#/ +#/ Checks the raid-device status +#/ +#/ Options: +#/ -w, --warning= The events of when to trigger a warning (comma-separated) +#/ -c, --critical= 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}" \ No newline at end of file