#!/bin/bash
# script that checks the 3ware disks arrays, needs the tw_cli installed and have sudo for it.
TW_CLI=/usr/sbin/tw_cli
# lets find out how many controllers are installed on this system
sudo "$TW_CLI" info | sed -n 's/^Controller \([0-9]\).*/\1/p' > /tmp/tw_contr
# now request info from each controller
while read contr_num ; do
	sudo "$TW_CLI" info c"$contr_num" > /tmp/tw_stat
		while read line ; do
			unitstat=`echo "$line" | sed -n 's/ *\(Unit [0-9]\+\): .*: \([^OV].*\)$/\1 \2/p'`
			portstat=`echo "$line" | sed -n 's/ *\(Port [0-9]\+\): .*: \([^O].*\)$/\1 \2/p'`
			unitmsg=`echo "$line" | sed -n 's/ *\(Unit [0-9]\+\): .*: \([OV].*\)$/\1 \2/p'`
			if [ -n "$unitstat" ] ; then
        			unit_status="$unit_status c$contr_num $unitstat"
			fi
			if [ -n "$portstat" ] ; then
				port_status="$port_status c$contr_num $portstat"
			fi
			if [ -n "$unitmsg" ] ; then
                                unit_msg="$unit_msg c$contr_num $unitmsg"
                        fi
		done < /tmp/tw_stat
done < /tmp/tw_contr 
if [ -n "$port_status" ] || [ -n "$unit_status" ] ; then
	echo Problem with "$port_status" "$unit_status"
	exit 2
elif [ -z "$port_status" ] && [ -z "$unit_status" ] && [ -n "$unit_msg" ] && [ -f /tmp/tw_stat ] && [ -f "$TW_CLI" ] && [ -f /tmp/tw_contr ] ; then 
        echo "$unit_msg"
	exit 0
else
	echo "$port_status" "$unit_status" 
	echo "check on 3ware controller failed!"
	exit 3
fi
