#!/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 state in $($sudo $omreport storage pdisk controller=0 | grep State | awk '{print $NF}')
    do
        if [ "$state" != "Online" ] && [ "$state" != "Ready" ]
        then
            fault="Physical disk has state: $state"
        fi
    done

    for failure in $($sudo $omreport storage pdisk controller=0 | grep Failure | awk '{print $NF}')
    do
        if [ "$failure" != "No" ]
        then
            fault="Physical disk failure predicted: $failure"
        fi
    done

    for state in $($sudo $omreport storage vdisk | grep State | awk '{print $NF}')
    do
        if [ "$state" != "Ready" ]
        then
            fault="Virtual disk has state: $state"
        fi
    done
fi

if [ $megaenable -eq 1 ]
then
    for status in $($sudo $megacli -LdPdInfo -aALL | grep S.M.A.R.T | awk '{print $NF}')
    do
        if [ "$status" != "No" ]
        then
            fault="Drive has flagged a S.M.A.R.T alert: $status"
        fi
    done

    for state in $($sudo $megacli -LdPdInfo -aALL | grep -m 1 State | awk '{print $NF}')
    do
        if [ "$state" != "Optimal" ]
        then
            fault="Virtual Disk State: $state"
        fi
    done
fi

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