#!/bin/bash

COLUMN=raid    # Name of the column
COLOR=green     # By default, everything is OK

raise_colour() {
    local newcol=$1
    local oldcol=$2
    local thecolour=green


    case $oldcol in
        red)
           thecolour=red
           ;;
        yellow)
           if [ $newcol == red ]
           then
               thecolour=red
           else
               thecolour=yellow
           fi
           ;;
        green)
           thecolour=$newcol
           ;;
    esac
    echo $thecolour
}

check_adaptec() {
    
    local MYCOLOR=green
    MSG="${MSG} 
Adaptec Raid Status"
    RAIDTMP=$BBTMP/raid.$$
    sudo /usr/local/bin/arcconf getconfig 1 > $RAIDTMP
    if [ $? != "0" ]
    then
       MYCOLOR=`raise_color red $MYCOLOR`
       MSG="${MSG}
       exit $?"
    fi
    #FANSTAT=`grep "Fan 1 status" $RAIDTMP|cut -d\: -f2`
    #TMPSTAT=`grep "Temperature status" $RAIDTMP|cut -d\: -f2`
    LGCSTAT=`grep "Status of logical device" $RAIDTMP|cut -d\: -f2`
    CTLSTAT=`grep "Controller Status" $RAIDTMP|cut -d\: -f2`
    DFDSTAT=`grep "Logical devices/Failed/Degraded" $RAIDTMP|cut -d\: -f2`
    DFSTAT=`grep "Logical devices/Failed/Degraded" $RAIDTMP|cut -d\: -f2|cut -d/ -f2`
    DDSTAT=`grep "Logical devices/Failed/Degraded" $RAIDTMP|cut -d\: -f2|cut -d/ -f3`
    FSGSTAT=`grep "Failed segments" $RAIDTMP|cut -d\: -f2`
    FSTSTAT=`grep "Failed stripes" $RAIDTMP|cut -d\: -f2`
    DDCSTAT=`grep "Defunct disk drive count" $RAIDTMP|cut -d\: -f2`
    
    #if [ $FANSTAT != "Optimal" ]
    #then
    #       COLOR=red
    #       MSG="${MSG}
    #       `grep "Fan 1 status" $RAIDTMP`
    #       "
    #fi
    #if [ $TMPSTAT != "Normal" ]
    #then
    #        COLOR=red
    #        MSG="${MSG}
    #        `grep "Temperature status" $RAIDTMP`
    #        "
    #fi
    for STAT in $LGCSTAT
    do
    if [ $STAT != "Optimal" ]
    then
            MYCOLOR=`raise_color red $MYCOLOR`
            MSG="${MSG}
            `grep "Status of logical device" $RAIDTMP`
            "
    fi
    done
    if [ $CTLSTAT != "Optimal" ]
    then
            MYCOLOR=`raise_color red $MYCOLOR`
            MSG="${MSG}
            `grep "Controller Status" $RAIDTMP`
            "
    fi
    if [ $DFSTAT != "0" ]
    then
            MYCOLOR=`raise_color red $MYCOLOR`
            MSG="${MSG}
            `grep "Logical devices/Failed/Degraded" $RAIDTMP`
            "
    fi
    if [ $DDSTAT != "0" ]
    then
            MYCOLOR=`raise_color red $MYCOLOR`
            MSG="${MSG}
            `grep "Logical devices/Failed/Degraded" $RAIDTMP`
            "
    fi
    for STAT in $FSGSTAT
    do
    if [ $STAT != "No" ]
    then
            MYCOLOR=`raise_color red $MYCOLOR`
            MSG="${MSG}
            `grep "Failed segments" $RAIDTMP`
            "
    fi
    done
    for FSTAT in $FSTSTAT
    do
    if [ $FSTAT != "No" ]
    then
            MYCOLOR=`raise_color red $MYCOLOR`
            MSG="${MSG}
            `grep "Failed stripes" $RAIDTMP`
            "
    fi
    done
    if [ $DDCSTAT != "0" ]
    then
            MYCOLOR=`raise_color red $MYCOLOR`
            MSG="${MSG}
            `grep "Defunct disk drive count" $RAIDTMP`
            "
    fi
    
    COLOR=$MYCOLOR
    MSG="${MSG}
    `cat ${RAIDTMP}`"
    
    rm $RAIDTMP
}

check_3ware() {
    MSG="${MSG} 
3ware Raid Status"
    RAIDTMP=$BBTMP/raid.$$
    CONTROLLERS=`sudo /usr/local/sbin/tw_cli show | grep ^c | awk '{ print $1 }'`
    local MYCOLOR=green
    for cont in $CONTROLLERS
    do
        sudo /usr/local/sbin/tw_cli /${cont} show > ${RAIDTMP}
        units=`grep ^u ${RAIDTMP} | awk '{ print $3 }'`
        for unit in $units
        do
            case x${unit} in
                xVERIFYING)
                    MYCOLOR=`raise_colour yellow $MYCOLOR`
		;;
                xOK)
                    MYCOLOR=`raise_colour green $MYCOLOR`
                ;;
                *)
                MYCOLOR=`raise_colour red $MYCOLOR`
               ;;
            esac
        done
        ports=`grep ^p ${RAIDTMP} | awk '{ print $2 }'`
        for port in $ports
        do

            if echo $port | grep -q ERROR 
            then
                MYCOLOR=`raise_colour yellow $MYCOLOR `
            fi
        done

        
    done
    
    MSG="${MSG}
    `cat ${RAIDTMP}`"
    COLOR=$MYCOLOR
    rm $RAIDTMP
}

MSG="RAID status"

if [ -x /usr/local/bin/arcconf ]
then
   check_adaptec
fi

if [ -x /usr/local/sbin/tw_cli ]
then
   check_3ware 
fi


#echo $COLOR
# Tell Hobbit about it
$BB $BBDISP "status $MACHINE.$COLUMN $COLOR `date`

${MSG}
"
echo  $BB $BBDISP "status $MACHINE.$COLUMN $COLOR `date`"
echo $MSG

exit 0

