#!/bin/bash

#
# Checks the status of the hardware raid.
# Checkresults are returned as text or integer.
# The script expects one parameter: The check you want to perform .
# The second paramter, the value you want to grep, is optional.
# The third parameter, the adapter number, is optional. If adapter number is missing, 0 is set.
# Thanks to https://twiki.cern.ch/twiki/bin/view/FIOgroup/DiskRefPerc
# for the nice overview
#

MEGACLI_BIN=/opt/MegaRAID/MegaCli/MegaCli64
MEGACLI="$MEGACLI_BIN"

# Check if Megacli is installed and executable
if [ -e $MEGACLI_BIN ]
then
    true
else
    echo "Megacli missing";
    exit 1
fi

# Set the adapternumber
if [ $3 ]
then
    ADAPTER=$3
else
    ADAPTER='ALL'
fi

# Check if Megacli is compatible with the hardware
#COUNT=$(LD_PRELOAD=./libfakeuname.so $MEGACLI -adpCount|grep "Controller Count"|grep -o "[0-9]*")
COUNT=1

if [ $COUNT -lt 1 ]
then
    echo "No compatible adapter"
    exit 1
fi

case $1 in
    bbu)
        # Grep anything from the bbu info
        LD_PRELOAD=./libfakeuname.so $MEGACLI -AdpBbuCmd -a${ADAPTER}|grep "$2"|head -n1|cut -d ":" -f 2|tr -d " "
    ;;
    adapter)
        # Grep anything from the adapter info
        LD_PRELOAD=./libfakeuname.so $MEGACLI -AdpAllInfo -a${ADAPTER}
    ;;
    mediaerrors)
        # Return the total number of disk which has reported an media error.
        LD_PRELOAD=./libfakeuname.so $MEGACLI -PDList -a${ADAPTER} |grep "Media Error Count"|grep -cv "Media Error Count: 0"
    ;;
    raiderrors)
        # Return the number of arrays which do not have state "optimal"
#        LD_PRELOAD=./libfakeuname.so $MEGACLI -LDInfo -Lall -a${ADAPTER}|grep State|grep -vc Optimal
        $MEGACLI -LDInfo -Lall -a${ADAPTER}|grep State|grep -vc Optimal
    ;;
    alarm)
        # Return 0 if the adapter is reporting no alarm. Return 1 if an alarm is reported.
        # This check does not know what kind of alarm is reported.
        LD_PRELOAD=./libfakeuname.so $MEGACLI -AdpAllInfo -aALL|grep -i Alarm|head -n1|grep -cv "Alarm.*Absent"
    ;;
    adapters)
        # Return the number of installed adapters
        echo $COUNT
    ;;
esac
