#!/bin/bash
#
# check_raid_all -- universal RAID health check for Nagios / NRPE.
#
# Checks in a single run whatever the host actually has:
#   * Linux software RAID (mdadm), parsed from /proc/mdstat
#   * LSI MegaRAID / Dell PERC, via storcli64, perccli64 or MegaCli64
#
# A host with no RAID card and no md arrays is reported OK (use -r to turn that
# into CRITICAL), so the very same check can be deployed on every server.
# Supersedes check_mdadm and check_megaraid_sas.
#
# Exit codes: 0 = OK, 1 = WARNING, 2 = CRITICAL, 3 = UNKNOWN
#
# The controller CLIs need root (/proc/mdstat does not) -- run this via sudo.

set -u
export LC_ALL=C
PATH='/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin'
export PATH

PROGNAME=${0##*/}

E_OK=0
E_WARNING=1
E_CRITICAL=2
E_UNKNOWN=3

# Return codes of the check_* functions below.
R_CHECKED=0   # controller/arrays found and evaluated
R_ABSENT=1    # nothing of this kind on the host
R_NOANSWER=2  # tool is installed but told us nothing

require_raid=0
min_arrays=0
want_hotspares=0
media_allow=0
pred_allow=0
other_allow=0
cmd_timeout=30
verbose=0

# Overridable only to make the parsers testable with recorded output. In
# production sudo drops the whole environment (Defaults env_reset, the sudoers
# default), so neither is reachable from the nrpe command line. If env_reset
# is disabled on a platform, add CHECK_RAID_MDSTAT and CHECK_RAID_TOOLDIR to
# env_delete in sudoers.
mdstat_path=${CHECK_RAID_MDSTAT:-/proc/mdstat}
extra_tooldir=${CHECK_RAID_TOOLDIR:-}
[ -n "$extra_tooldir" ] && PATH="$extra_tooldir:$PATH"

usage() {
	cat <<EOF
Usage: $PROGNAME [-r] [-a num] [-s num] [-m num] [-p num] [-o num] [-t sec] [-v]

  -r      report CRITICAL when no RAID at all is found (default: OK)
  -a num  minimum number of mdadm arrays expected (default: 0 = no check)
  -s num  number of hardware hotspares expected (default: 0 = no check)
  -m num  media errors to tolerate (default: 0)
  -p num  predictive failure count to tolerate (default: 0)
  -o num  other disk errors to tolerate (default: 0)
  -t sec  timeout for a single controller CLI call (default: 30)
  -v      append per-device detail to the output
  -h      this help
EOF
}

bail() {
	printf 'UNKNOWN - %s\n' "$1"
	exit $E_UNKNOWN
}

is_num() {
	case ${1:-} in
		'' | *[!0-9]*) return 1 ;;
		*) return 0 ;;
	esac
}

while getopts 'ra:s:m:p:o:t:vh' opt; do
	case $opt in
		r) require_raid=1 ;;
		a) min_arrays=$OPTARG; is_num "$min_arrays" || bail '-a needs a non-negative integer' ;;
		s) want_hotspares=$OPTARG; is_num "$want_hotspares" || bail '-s needs a non-negative integer' ;;
		m) media_allow=$OPTARG; is_num "$media_allow" || bail '-m needs a non-negative integer' ;;
		p) pred_allow=$OPTARG; is_num "$pred_allow" || bail '-p needs a non-negative integer' ;;
		o) other_allow=$OPTARG; is_num "$other_allow" || bail '-o needs a non-negative integer' ;;
		t) cmd_timeout=$OPTARG; is_num "$cmd_timeout" || bail '-t needs a non-negative integer' ;;
		v) verbose=1 ;;
		h) usage; exit $E_OK ;;
		*) usage >&2; exit $E_UNKNOWN ;;
	esac
done

# ---------------------------------------------------------------- aggregation

# Nagios severity order is not the exit-code order: OK < UNKNOWN < WARNING <
# CRITICAL. Track the worst as a rank and translate back at the very end.
worst_rank=0
summary=''
detail=''
perfdata=''

rank_of() {
	case $1 in
		0) printf 0 ;;
		3) printf 1 ;;
		1) printf 2 ;;
		2) printf 3 ;;
		*) printf 1 ;;
	esac
}

escalate() {
	local _rank
	_rank=$(rank_of "$1")
	if [ "$_rank" -gt "$worst_rank" ]; then
		worst_rank=$_rank
	fi
	return 0
}

add_summary() {
	if [ -n "$summary" ]; then
		summary="$summary; $1"
	else
		summary=$1
	fi
	return 0
}

add_detail() {
	if [ -n "$detail" ]; then
		detail="$detail $1"
	else
		detail=$1
	fi
	return 0
}

# ------------------------------------------------------------------- runners

if command -v timeout >/dev/null 2>&1; then
	TIMEOUT="timeout $cmd_timeout"
else
	TIMEOUT=''
fi

if [ "$(id -u)" -ne 0 ]; then
	SUDO='sudo -n'
else
	SUDO=''
fi

# Run a controller CLI: never let it hang, never let its noise reach stdout.
# stderr goes to a scratch file rather than /dev/null: run_tool runs inside
# command substitutions, so a variable could not carry the error text back,
# but a file can -- and it costs no second, separately timeout-bounded
# invocation when the tool is wedged.
rt_errfile="${TMPDIR:-/tmp}/check_raid_all.$$.stderr"
trap 'rm -f "$rt_errfile"' EXIT
run_tool() {
	# shellcheck disable=SC2086
	$TIMEOUT $SUDO "$@" 2>"$rt_errfile"
}

# First stderr line of the most recent run_tool call, for the UNKNOWN message.
mute_reason=''
save_tool_error() {
	mute_reason=$(head -1 "$rt_errfile" 2>/dev/null)
}

# Find the first existing binary out of the names given.
find_tool() {
	local _name _path _dir
	for _name in "$@"; do
		for _dir in "$extra_tooldir" /opt/MegaRAID/storcli /opt/MegaRAID/perccli /opt/MegaRAID/MegaCli \
			/opt/storcli /opt/perccli /opt/MegaCli /usr/local/sbin /usr/sbin /usr/bin; do
			[ -n "$_dir" ] || continue
			if [ -x "$_dir/$_name" ]; then
				printf '%s\n' "$_dir/$_name"
				return 0
			fi
		done
		_path=$(command -v "$_name" 2>/dev/null)
		if [ -n "$_path" ] && [ -x "$_path" ]; then
			printf '%s\n' "$_path"
			return 0
		fi
	done
	return 1
}

# -------------------------------------------------------------- software RAID

# Flatten /proc/mdstat into one pipe-separated record per array:
#   name|state|personality|have|want|map|faulty|spare|action|percent
MDSTAT_AWK='
function emit() {
	if (name == "") return
	printf "%s|%s|%s|%s|%s|%s|%d|%d|%s|%s\n", name, state, pers, have, want, map, faulty, spare, action, pct
}
/^md[^ \t]*[ \t]*:/ {
	emit()
	name = $1
	state = $3
	pers = ($4 ~ /^\(/) ? $5 : $4
	if (pers ~ /\[/) pers = ""   # inactive array: no personality, just members
	have = ""; want = ""; map = ""; faulty = 0; spare = 0; action = ""; pct = ""
	for (i = 3; i <= NF; i++) {
		if ($i ~ /\(F\)/) faulty++
		if ($i ~ /\(S\)/) spare++
	}
	next
}
name != "" {
	if (match($0, /\[[0-9]+\/[0-9]+\]/)) {
		split(substr($0, RSTART + 1, RLENGTH - 2), a, "/")
		have = a[1]; want = a[2]
	}
	if (match($0, /\[[U_]+\]/)) map = substr($0, RSTART + 1, RLENGTH - 2)
	if ($0 ~ /=DELAYED/) {
		action = "delayed"
	} else if ($0 ~ /=PENDING/) {
		action = "pending"
	} else if ($0 ~ /(recovery|resync|reshape|check)[ \t]*=/) {
		if (match($0, /recovery|resync|reshape|check/)) action = substr($0, RSTART, RLENGTH)
		if (match($0, /[0-9]+\.[0-9]+%/)) pct = substr($0, RSTART, RLENGTH)
	}
}
END { emit() }
'

check_md() {
	local _records=''
	if [ -r "$mdstat_path" ]; then
		_records=$(awk "$MDSTAT_AWK" "$mdstat_path" 2>/dev/null)
	fi

	if [ -z "$_records" ]; then
		# No arrays at all. That is only a problem if we were told to expect some
		# -- which is exactly the case a bare mdstat parser reports as OK.
		if [ "$min_arrays" -gt 0 ]; then
			escalate $E_CRITICAL
			add_summary "mdadm: no arrays present, $min_arrays expected"
			perfdata="$perfdata md_arrays=0 md_failed=0 md_missing=$min_arrays md_degraded=0"
			return $R_CHECKED
		fi
		return $R_ABSENT
	fi

	local _total=0 _bad=0 _warn=0 _faulty=0 _spare=0 _msg=''
	local name state pers have want map faulty spare action pct
	local _state _note _desc

	while IFS='|' read -r name state pers have want map faulty spare action pct; do
		[ -n "$name" ] || continue
		_total=$((_total + 1))
		_faulty=$((_faulty + faulty))
		_spare=$((_spare + spare))
		_state=ok
		_note=''

		if [ "$state" != 'active' ]; then
			# inactive/unknown: the array is not serving data at all
			_state=crit
			_note="state=$state"
		elif [ "${map#*_}" != "$map" ] || { [ -n "$want" ] && [ "$have" -lt "$want" ]; }; then
			# missing member(s) -- only a WARNING while it is being rebuilt
			case $action in
				recovery | reshape | resync)
					_state=warn
					_note="degraded, $action ${pct:-in progress}"
					;;
				*)
					_state=crit
					_note='degraded'
					;;
			esac
		elif [ "$faulty" -gt 0 ]; then
			# optimal map but a member is still flagged faulty (spare took over)
			_state=warn
			_note="$faulty faulty member(s)"
		else
			case $action in
				recovery | reshape | resync)
					_state=warn
					_note="$action ${pct:-in progress}"
					;;
				delayed | pending)
					_state=warn
					_note="resync $action"
					;;
				check)
					# routine monthly scrub, not a fault
					_note="check ${pct:-in progress}"
					;;
			esac
		fi

		case $_state in
			crit)
				_bad=$((_bad + 1))
				escalate $E_CRITICAL
				;;
			warn)
				_warn=$((_warn + 1))
				escalate $E_WARNING
				;;
		esac

		_desc=$name
		[ -n "$pers" ] && _desc="$_desc $pers"
		[ -n "$want" ] && _desc="$_desc $have/$want"
		[ -n "$map" ] && _desc="$_desc [$map]"
		[ -n "$_note" ] && _desc="$_desc: $_note"

		if [ "$_state" != 'ok' ]; then
			_msg="$_msg ($_desc)"
		elif [ "$verbose" -eq 1 ]; then
			add_detail "($_desc)"
		fi
	done <<EOF
$_records
EOF

	# Fewer arrays than expected means one did not assemble at all -- the case a
	# bare /proc/mdstat parser happily reports as OK. Missing (never assembled)
	# is counted apart from failed (was up, went down) in the perfdata.
	local _missing=0
	if [ "$min_arrays" -gt 0 ] && [ "$_total" -lt "$min_arrays" ]; then
		escalate $E_CRITICAL
		_missing=$((min_arrays - _total))
		_msg="$_msg (only $_total of $min_arrays expected arrays present)"
	fi

	local _text="mdadm: $_total array(s)"
	[ "$_bad" -gt 0 ] && _text="$_text, $_bad failed"
	[ "$_warn" -gt 0 ] && _text="$_text, $_warn need(s) attention"
	[ "$_faulty" -gt 0 ] && _text="$_text, $_faulty faulty disk(s)"
	[ "$_spare" -gt 0 ] && _text="$_text, $_spare spare(s)"
	[ -n "$_msg" ] && _text="$_text -$_msg"
	add_summary "$_text"

	perfdata="$perfdata md_arrays=$_total md_failed=$_bad md_missing=$_missing md_degraded=$_warn"
	return $R_CHECKED
}

# ------------------------------------------------------- shared state mapping

# storcli/perccli virtual-drive state -> exit code
vd_severity() {
	case $1 in
		Optl* | Optimal*) printf %s $E_OK ;;
		Pdgd* | Partially*) printf %s $E_WARNING ;;
		Dgrd* | Degraded* | OfLn* | Offln* | Offline* | Failed*) printf %s $E_CRITICAL ;;
		*) printf %s $E_UNKNOWN ;;
	esac
}

# storcli/perccli physical-drive state -> exit code
pd_severity() {
	case $1 in
		Onln | Online | JBOD | UGood | UGUnsp | GHS | DHS) printf %s $E_OK ;;
		Rbld | Cpybck | UBad | UBUnsp | Shld | SntAs) printf %s $E_WARNING ;;
		Offln | Failed | Msng | Missing | Bad) printf %s $E_CRITICAL ;;
		*) printf %s $E_UNKNOWN ;;
	esac
}

# MegaCli firmware state -> exit code
fw_severity() {
	case $1 in
		Online* | JBOD* | Hotspare* | 'Unconfigured(good)'*) printf %s $E_OK ;;
		Rebuild* | Copyback* | 'Unconfigured(bad)'* | Shield*) printf %s $E_WARNING ;;
		Failed* | Offline* | Missing*) printf %s $E_CRITICAL ;;
		*) printf %s $E_UNKNOWN ;;
	esac
}

# Common tail for both hardware paths: error counters and hotspare expectation.
report_hw_counters() {
	local _media=$1 _pred=$2 _other=$3 _smart=$4 _hotspares=$5
	local _text=''

	if [ $((_media + _pred + _other + _smart)) -gt 0 ]; then
		_text="errors: media=$_media predictive=$_pred other=$_other"
		[ "$_smart" -gt 0 ] && _text="$_text smart_alerts=$_smart"
		if [ "$_media" -gt "$media_allow" ] || [ "$_pred" -gt "$pred_allow" ] \
			|| [ "$_other" -gt "$other_allow" ] || [ "$_smart" -gt 0 ]; then
			escalate $E_WARNING
		fi
	fi

	if [ "$want_hotspares" -gt 0 ]; then
		[ -n "$_text" ] && _text="$_text, "
		_text="${_text}hotspares: $_hotspares of $want_hotspares"
		if [ "$_hotspares" -lt "$want_hotspares" ]; then
			escalate $E_WARNING
		fi
	fi

	[ -n "$_text" ] && add_summary "$_text"
	perfdata="$perfdata media_err=$_media pred_err=$_pred other_err=$_other hotspares=$_hotspares"
	return 0
}

# --------------------------------------------------------- storcli / perccli

check_storcli() {
	local _tool=$1
	local _label=${1##*/}
	local _out _ctrl

	_out=$(run_tool "$_tool" show ctrlcount nolog)
	if [ -z "$_out" ]; then
		save_tool_error
		return $R_NOANSWER
	fi
	# storcli changed the ctrlcount wording between releases: 007.16xx prints
	# "Number of Controllers = N", 007.37xx prints "Controller Count = N".
	_ctrl=$(printf '%s\n' "$_out" | awk -F= '/Number of Controllers|Controller Count/ { gsub(/[^0-9]/, "", $2); print $2; exit }')
	is_num "${_ctrl:-}" || return $R_NOANSWER
	[ "$_ctrl" -gt 0 ] || return $R_ABSENT

	_out=$(run_tool "$_tool" /call show nolog)
	if [ -z "$_out" ]; then
		save_tool_error
		return $R_NOANSWER
	fi

	local _vd_total=0 _vd_bad=0 _pd_total=0 _pd_bad=0 _hotspares=0 _msg=''
	local _ctl _id _state _sev

	# Virtual drives, e.g. "0/0   RAID1  Optl  RW  Yes  RWBD  -  ON  446.625 GB"
	while read -r _ctl _id _state; do
		[ -n "$_id" ] || continue
		_vd_total=$((_vd_total + 1))
		_sev=$(vd_severity "$_state")
		if [ "$_sev" -ne $E_OK ]; then
			_vd_bad=$((_vd_bad + 1))
			escalate "$_sev"
			_msg="$_msg [c$_ctl]VD$_id=$_state"
		elif [ "$verbose" -eq 1 ]; then
			add_detail "[c$_ctl]VD$_id=$_state"
		fi
	done <<EOF
$(printf '%s\n' "$_out" | awk '
	BEGIN { c = 0 }
	/^Controller = [0-9]+$/ { c = $3 + 0; next }
	$1 ~ /^[0-9]+\/[0-9]+$/ { print c, $1, $3 }')
EOF

	# Physical drives, e.g. "32:0   0 Onln   0 446.625 GB SATA SSD ..."
	while read -r _ctl _id _state; do
		[ -n "$_id" ] || continue
		case $_state in
			GHS | DHS) _hotspares=$((_hotspares + 1)) ;;
		esac
		case $_state in
			UGood | UGUnsp) continue ;; # spare capacity, not part of any array
		esac
		_pd_total=$((_pd_total + 1))
		_sev=$(pd_severity "$_state")
		if [ "$_sev" -ne $E_OK ]; then
			_pd_bad=$((_pd_bad + 1))
			escalate "$_sev"
			_msg="$_msg [c$_ctl]PD$_id=$_state"
		elif [ "$verbose" -eq 1 ]; then
			add_detail "[c$_ctl]PD$_id=$_state"
		fi
	done <<EOF
$(printf '%s\n' "$_out" | awk '
	BEGIN { c = 0 }
	/^Controller = [0-9]+$/ { c = $3 + 0; next }
	$1 ~ /^[0-9]+:[0-9]+$/ { print c, $1, $3 }')
EOF

	local _text="$_label: $_ctrl controller(s), $_vd_total VD, $_pd_total PD"
	[ "$_vd_bad" -gt 0 ] && _text="$_text, $_vd_bad VD not optimal"
	[ "$_pd_bad" -gt 0 ] && _text="$_text, $_pd_bad PD not healthy"
	[ -n "$_msg" ] && _text="$_text -$_msg"
	add_summary "$_text"
	perfdata="$perfdata hw_vd=$_vd_total hw_pd=$_pd_total hw_vd_bad=$_vd_bad hw_pd_bad=$_pd_bad"

	# Battery / cachevault: a dead one silently drops the cache to write-through.
	# An absent BBU is not a fault -- many cards ship without one, and the query
	# then answers "Battery is not present" or nothing at all. Escalate only when
	# a battery is actually reported and its state is unhealthy; empty output
	# therefore also passes as OK.
	local _bbu
	_bbu=$(run_tool "$_tool" /call/bbu show nolog | awk '
		/^(Status|Description|CLI Version|Operating system)/ { next }
		# A field whose value is Yes/No has to be judged by that value: a perfectly
		# healthy battery still reports "Battery Pack Missing = No".
		/(=|:)[ \t]*(Yes|No)[ \t]*$/ {
			if ($0 ~ /[Mm]issing|[Rr]eplace|[Ff]ail|Degraded/ && $0 ~ /Yes[ \t]*$/) bad = 1
			next
		}
		# No battery is not a fault, whatever the firmware calls it: older
		# releases say "Battery is not present" (Status = Failure), newer ones
		# print a Detailed Status row "Failed - Battery is absent!" whose
		# "Failed" would otherwise trip the state match below.
		/[Bb]attery is (absent|not present)/ { next }
		# Anything else is a state, where the bare word is the verdict.
		/[Ff]ailed|Degraded|Missing/ { bad = 1 }
		END { print (bad ? "bad" : "ok") }')
	if [ "$_bbu" = 'bad' ]; then
		escalate $E_WARNING
		add_summary "$_label: battery/cachevault needs attention"
	fi

	# Per-drive error counters live in the detailed view only. Same trap as the
	# battery: the awk END block prints zeros on empty input, so a failed tool
	# call must be told apart from clean counters. read (not set --) keeps the
	# fields away from the shell's glob expansion.
	local _detail _media _pred _other _smart
	_detail=$(run_tool "$_tool" /call/eall/sall show all nolog)
	if [ -z "$_detail" ]; then
		escalate $E_WARNING
		add_summary "$_label: cannot read per-drive error counters"
	else
		read -r _media _pred _other _smart <<EOF
$(printf '%s\n' "$_detail" | awk -F' = ' '
	/^Media Error Count/            { media += $2 }
	/^Other Error Count/            { other += $2 }
	/^Predictive Failure Count/     { pred  += $2 }
	/S\.M\.A\.R\.T alert flagged/   { if ($2 ~ /Yes/) smart++ }
	END { printf "%d %d %d %d", media + 0, pred + 0, other + 0, smart + 0 }')
EOF
		report_hw_counters "${_media:-0}" "${_pred:-0}" "${_other:-0}" "${_smart:-0}" "$_hotspares"
	fi
	return $R_CHECKED
}

# ------------------------------------------------------------------- MegaCli

check_megacli() {
	local _tool=$1
	local _label=${1##*/}
	local _out _adapters _ld _pd

	_out=$(run_tool "$_tool" -adpCount -NoLog)
	if [ -z "$_out" ]; then
		save_tool_error
		return $R_NOANSWER
	fi
	_adapters=$(printf '%s\n' "$_out" | awk '/Controller Count/ { gsub(/[^0-9]/, ""); print; exit }')
	is_num "${_adapters:-}" || return $R_NOANSWER
	[ "$_adapters" -gt 0 ] || return $R_ABSENT

	_ld=$(run_tool "$_tool" -LDInfo -Lall -aALL -NoLog)
	if [ -z "$_ld" ]; then
		save_tool_error
		return $R_NOANSWER
	fi

	local _vd_total=0 _vd_bad=0 _pd_total=0 _pd_bad=0 _hotspares=0 _msg=''
	local _adp _id _state _sev _badblocks
	local _media=0 _pred=0 _other=0 _smart=0

	while read -r _adp _id _state; do
		[ -n "$_id" ] || continue
		_vd_total=$((_vd_total + 1))
		_sev=$(vd_severity "$_state")
		if [ "$_sev" -ne $E_OK ]; then
			_vd_bad=$((_vd_bad + 1))
			escalate "$_sev"
			_msg="$_msg [a$_adp]VD$_id=$_state"
		elif [ "$verbose" -eq 1 ]; then
			add_detail "[a$_adp]VD$_id=$_state"
		fi
	done <<EOF
$(printf '%s\n' "$_ld" | awk -F': *' '
	BEGIN { adp = 0 }
	/^Adapter [0-9]+ --/ { split($1, t, " "); adp = t[2]; next }
	/^Virtual Drive/     { split($2, t, " "); id = t[1]; next }
	/^State/             { gsub(/[ \t]+$/, "", $2); print adp, (id == "" ? "?" : id), $2 }')
EOF

	# || true: grep -c exits 1 on zero matches, which is a valid count here --
	# keep the line safe should set -e ever be enabled.
	_badblocks=$(printf '%s\n' "$_ld" | grep -c '^Bad Blocks Exist: *Yes' || true)
	if [ "${_badblocks:-0}" -gt 0 ]; then
		escalate $E_WARNING
		add_summary "$_label: bad blocks on $_badblocks VD(s)"
	fi

	_pd=$(run_tool "$_tool" -PDList -aALL -NoLog)
	if [ -n "$_pd" ]; then
		while read -r _adp _id _state; do
			[ -n "$_id" ] || continue
			[ "$_id" = '255' ] && continue # enclosure processor, not a disk
			case $_state in
				Hotspare*) _hotspares=$((_hotspares + 1)) ;;
			esac
			case $_state in
				'Unconfigured(good)'*) continue ;; # not part of any array
			esac
			_pd_total=$((_pd_total + 1))
			_sev=$(fw_severity "$_state")
			if [ "$_sev" -ne $E_OK ]; then
				_pd_bad=$((_pd_bad + 1))
				escalate "$_sev"
				_msg="$_msg [a$_adp]slot$_id=$_state"
			elif [ "$verbose" -eq 1 ]; then
				add_detail "[a$_adp]slot$_id=$_state"
			fi
		done <<EOF
$(printf '%s\n' "$_pd" | awk -F': *' '
	BEGIN { adp = 0 }
	/^Adapter #[0-9]+/ { split($1, t, "#"); adp = t[2]; next }
	/^Slot Number/     { slot = $2; next }
	/^Firmware state/  { split($2, f, ","); print adp, (slot == "" ? "?" : slot), f[1] }')
EOF
		# read (not set --) keeps the fields away from the shell's glob expansion.
		read -r _media _pred _other _smart <<EOF
$(printf '%s\n' "$_pd" | awk -F': *' '
	/^Media Error Count/        { media += $2 }
	/^Other Error Count/        { other += $2 }
	/^Predictive Failure Count/ { pred  += $2 }
	/flagged a S\.M\.A\.R\.T/   { if ($0 ~ /Yes/) smart++ }
	END { printf "%d %d %d %d", media + 0, pred + 0, other + 0, smart + 0 }')
EOF
		_media=${_media:-0}
		_pred=${_pred:-0}
		_other=${_other:-0}
		_smart=${_smart:-0}
	fi

	local _text="$_label: $_adapters adapter(s), $_vd_total VD, $_pd_total PD"
	[ "$_vd_bad" -gt 0 ] && _text="$_text, $_vd_bad VD not optimal"
	[ "$_pd_bad" -gt 0 ] && _text="$_text, $_pd_bad PD not healthy"
	[ -n "$_msg" ] && _text="$_text -$_msg"
	add_summary "$_text"
	perfdata="$perfdata hw_vd=$_vd_total hw_pd=$_pd_total hw_vd_bad=$_vd_bad hw_pd_bad=$_pd_bad"

	# Battery backup unit, when the adapter has one. An absent BBU is not a
	# fault: MegaCli answers the query with "Get BBU Status Failed" (or nothing)
	# on such adapters. Escalate only when a battery is actually reported and
	# its state is unhealthy; empty output therefore also passes as OK.
	local _bbu
	_bbu=$(run_tool "$_tool" -AdpBbuCmd -GetBbuStatus -aALL -NoLog | awk '
		/^Battery State/ {
			s = $0
			sub(/^[^:]*:[ \t]*/, "", s)
			# Some firmwares pad the value with trailing blanks.
			sub(/[ \t]+$/, "", s)
			if (s !~ /^(Optimal|Operational)$/) bad = 1
		}
		/Battery Replacement required/ { if ($NF ~ /Yes/) bad = 1 }
		/isSOHGood/                    { if ($NF ~ /No/) bad = 1 }
		END { print (bad ? "bad" : "ok") }')
	if [ "$_bbu" = 'bad' ]; then
		escalate $E_WARNING
		add_summary "$_label: battery backup unit needs attention"
	fi

	report_hw_counters "$_media" "$_pred" "$_other" "$_smart" "$_hotspares"
	return $R_CHECKED
}

# ------------------------------------------- controllers we cannot talk to yet

# Name of a RAID controller that sits on the PCI bus but has no management tool
# installed here. Plain SAS/HBA controllers (class 0107) are skipped: in IT or
# JBOD mode they have no arrays to report on.
pci_raid_controller() {
	command -v lspci >/dev/null 2>&1 || return 1
	# Same hang guard as run_tool: a wedged PCI enumeration must end in this
	# check's own UNKNOWN, not in nrpe's command_timeout. All matching cards
	# are reported, joined with " + ", each stripped of PCI ids and revision.
	# shellcheck disable=SC2086
	$TIMEOUT lspci -nn 2>/dev/null | awk '
		/RAID bus controller \[0104\]/ {
			sub(/^[^:]*: /, "")
			sub(/ *\[[0-9a-f][0-9a-f][0-9a-f][0-9a-f]:[0-9a-f][0-9a-f][0-9a-f][0-9a-f]\].*/, "")
			sub(/ *\(rev .*/, "")
			cards = cards (cards ? " + " : "") $0
		}
		END { if (cards) print cards }'
}

# ---------------------------------------------------------------------- main

check_md

hw_rc=$R_ABSENT
mute_tool=''

hw_tool=$(find_tool storcli64 storcli perccli64 perccli)
if [ -n "${hw_tool:-}" ]; then
	check_storcli "$hw_tool"
	hw_rc=$?
	[ "$hw_rc" -eq $R_NOANSWER ] && mute_tool=${hw_tool##*/}
fi

# storcli may be present but unusable (old firmware, wrong flavour) -- MegaCli
# is still worth a try before giving up on the hardware side.
if [ "$hw_rc" -ne $R_CHECKED ]; then
	mega_tool=$(find_tool MegaCli64 MegaCli megacli)
	if [ -n "${mega_tool:-}" ]; then
		check_megacli "$mega_tool"
		case $? in
			"$R_CHECKED") hw_rc=$R_CHECKED; mute_tool='' ;;
			# Keep both names when storcli was mute too -- the message must not
			# hide that the first tool also failed.
			"$R_NOANSWER") mute_tool="${mute_tool:+$mute_tool+}${mega_tool##*/}" ;;
		esac
	fi
fi

if [ -n "$mute_tool" ]; then
	# The tool is installed but gave us nothing: missing driver, no permission
	# or a hung controller. Never silently pass this off as healthy.
	escalate $E_UNKNOWN
	add_summary "$mute_tool: no usable answer from the controller${mute_reason:+ [$mute_reason]} (root required? driver loaded?)"
fi

if [ -z "$summary" ]; then
	pci_card=$(pci_raid_controller)
	if [ -n "${pci_card:-}" ]; then
		escalate $E_UNKNOWN
		add_summary "RAID controller present (${pci_card}) but no storcli/perccli/MegaCli installed"
	elif [ "$require_raid" -eq 1 ]; then
		escalate $E_CRITICAL
		add_summary 'no RAID controller and no mdadm array found'
	else
		add_summary 'no RAID configured on this host'
	fi
fi

case $worst_rank in
	0) status='OK'; exitcode=$E_OK ;;
	1) status='UNKNOWN'; exitcode=$E_UNKNOWN ;;
	2) status='WARNING'; exitcode=$E_WARNING ;;
	*) status='CRITICAL'; exitcode=$E_CRITICAL ;;
esac

out="$status - $summary"
[ "$verbose" -eq 1 ] && [ -n "$detail" ] && out="$out [$detail]"
if [ -n "$perfdata" ]; then
	printf '%s |%s\n' "$out" "$perfdata"
else
	printf '%s\n' "$out"
fi
exit "$exitcode"
