#!/bin/bash 

fault="No"

# Use omreport output for RAID testing
omenable=1
omreport=/usr/sbin/omreport

# Use MegaCLI for RAID testing
megaenable=1
megacli=/opt/MegaRAID/MegaCli/MegaCli

sudo=/usr/bin/sudo

if [ $omenable -eq 1 ]
then
    for status in $($sudo $omreport storage battery | grep Status | grep -v "Capacity Status" | awk '{print $NF}')
    do
        if [ "$status" != "Ok" ]
        then
            fault="Battery has status: $status"
        fi
    done

    for state in $($sudo $omreport storage battery | grep State | grep -v "Learn State" | awk '{print $NF}')
    do
        if [ "$state" != "Ready" ]
        then
            fault="Battery has state: $status"
        fi
    done
fi

if [ $megaenable -eq 1 ]
then
    output=/tmp/$RANDOM
    /usr/bin/sudo /opt/MegaRAID/MegaCli/MegaCli -AdpBbuCmd -aALL > $output

    # Battery Replacement required            : No
    batreplace=$(grep "Battery Replacement required" $output | awk '{print $5}')
    # Pack is about to fail & should be replaced : No
    batabout=$(grep "Pack is about to fail & should be replaced" $output | awk '{print $11}')
    # Remaining Time Alarm    : No
    battime=$(grep "Remaining Time Alarm" $output | awk '{print $5}')
    # Remaining Capacity Alarm: No
    batcapac=$(grep -m 1 "Remaining Capacity Alarm" $output | awk '{print $4}')

    if [ "$batreplace" != "No" ] || [ "$batabout" != "No" ] || [ "$battime" != "No" ] || [ "$batcapac" != "No" ] ; then
        fault="Battery Replacement required: $batreplace, Pack is about to fail and should be replaced: $batabout, Remaining Time Alarm: $battime, Remaining Capacity Alarm: $batcapac"
    fi

    rm $output
fi

if [ "$fault" != "No" ]
then
    echo -n "FAULT: $fault"
else
    echo -n "OK"
fi
