123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #!/bin/sh
- # {{{ Variables
- DEVICE=
- SCAN=0
- TYPE=
- ZBX_DISCOVERY_OUTPUT=0
- # }}}
- # {{{ usage()
- usage() {
- cat <<EOH
- USAGE: $(basename "$0") -d DEVICE
- Get RAID status value for a device
- Options:
- -d Device path
- -t Raid type (mdraid, megaraid, etc.)
- -S Scan devices and display list
- -s Scan devices and display list (Zabbix discovery output)
- EOH
- }
- # }}}
- # {{{ scan_megaraid_devices()
- scan_megaraid_devices() {
- if which storcli >/dev/null 2>&1 ; then
- ctrlcount=$(storcli show ctrlcount| sed -n 's/^Controller Count = //p')
- if [ "${ctrlcount}" -gt 0 ]; then
- ctrlcount=$(( ${ctrlcount} - 1))
- for i in $(seq 0 ${ctrlcount}) ; do
- storcli /c${i}/vall show|sed -r -n 's/^[0-9]+\/([0-9+]+)\s+RAID[0-9]+\s+[a-z]+\s+.*$/\1/pi' | \
- while read vd ; do
- printf "megaraid /c%s/v%s\n" "${i}" "${vd}"
- done
- done
- fi
- fi
- }
- # }}}
- # {{{ scan_mdraid_devices()
- scan_mdraid_devices() {
- if which mdadm >/dev/null 2>&1 ; then
- mdadm --detail --scan | sed -r -n 's/^ARRAY\s+(.*)\s+metadata=.*$/mdraid \1/p'
- fi
- }
- # }}}
- # {{{ scan_devices()
- scan_devices() {
- scan_megaraid_devices
- scan_mdraid_devices
- }
- # }}}
- # {{{ list_raid_devices()
- list_raid_devices() {
- ZBX="${1}"
- if [ "${ZBX}" = 1 ]; then
- printf '{"data":['
- fi
- scan_devices | \
- while read type device ; do
- if [ "${ZBX}" = 1 ]; then
- printf '{'
- printf '"{#DEVICE}":"%s",' "${device}"
- printf '"{#TYPE}":"%s"' "${type}"
- printf '},'
- else
- printf "${device}|${type}\n"
- fi
- done | sed 's/},$/}/'
- if [ "${ZBX}" = 1 ]; then
- printf ']}'
- fi
- }
- # }}}
- # {{{ main()
- while getopts "d:t:Ss" OPT ; do
- case "$OPT" in
- \?|h)
- usage
- exit 0
- ;;
- d)
- DEVICE="$OPTARG"
- ;;
- t)
- TYPE="$OPTARG"
- ;;
- S)
- SCAN=1
- ;;
- s)
- SCAN=1
- ZBX_DISCOVERY_OUTPUT=1
- ;;
- esac
- done
- if [ "${SCAN}" = 1 ]; then
- list_raid_devices "${ZBX_DISCOVERY_OUTPUT}"
- exit 0
- fi
- if [ -z "${DEVICE}" ] ; then
- printf "[ERR] device is not defined\n"
- usage
- exit 1
- fi
- if [ -z "${TYPE}" ] ; then
- printf "[ERR] raid type is not defined\n"
- usage
- exit 1
- fi
- result=0
- case "${TYPE}" in
- mdraid)
- status="$(mdadm --detail "${DEVICE}" 2>/dev/null | sed -r -n 's/^\s+State\s*:\s*([^ ]+)\s*$/\1/p')"
- if [ "${status}" != 'clean' ] && [ "${status}" != 'active' ]; then
- result=1
- fi
- ;;
- megaraid)
- status="$(storcli "${DEVICE}" show 2>/dev/null | sed -r -n 's/^.*RAID[0-9]+\s+([a-z]+)\s+.*$/\1/pi')"
- if [ "${status}" != 'Optl' ]; then
- result=1
- fi
- ;;
- *)
- printf "[ERR] unsupported raid type '%s'\n" "${TYPE}"
- usage
- exit 1
- esac
- printf "${result}\n"
- exit 0
- # }}}
- # vim: foldmethod=marker foldlevel=0 foldenable
|