#!/bin/bash

# LSI RaidCard: Check BBU
# HP RaidCard: Check BBU, Controller, Cache Ratio

_install() {
    if [[ $VMFLAG = 1 ]];then
        return 0
    fi
}

_check() {
    _check="PASS"

    if [[ $VMFLAG = 1 ]] || [[ $RaidInfo =~ "noraid" ]];then
        return 0
    fi

    if [[ $RaidType = "LSI_RAID" ]];then
        local BBU_status=$(/opt/MegaRAID/MegaCli/MegaCli64 -adpbbucmd -getbbustatus -aAll|grep -ci 'Optimal')
        if [[ $BBU_status -eq 0 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] RaidCard Battery Status Not OK."
        fi
    elif [[ $RaidType = "HP_RAID" ]];then
        local BBU_count=$(ssacli ctrl all show config detail|awk '/Battery\/Capacitor Count:/{print $NF}')
        local BBU_status=$(ssacli ctrl all show config detail|awk '/Battery\/Capacitor Status:/{print $NF}'|grep -c 'OK')
        local Controller=$(ssacli ctrl all show config detail|awk '/Controller Status:/{print $NF}'|grep -c 'OK')
        local cache_ratio=$(ssacli ctrl all show config detail |grep -ci 'Cache Ratio: 10% Read / 90% Write')

        if [[ $BBU_count -eq 0 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] RaidCard Battery Not Found."
        fi

        if [[ $BBU_status -eq 0 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] RaidCard Battery Status Not OK."
        fi

        if [[ $Controller -eq 0 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] RaidCard Controller Status Not OK."
        fi

        if [[ $cache_ratio -ne 1 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] HP RaidCard Cache Ratio Not: 10% Read / 90% Write"
        fi
    elif [[ $RaidType = "PMC_RAID" ]];then
        local pmc_dev=`arcconf list|grep -i 'Controller [0-9]'|awk -F ':' '{print $1}'|awk '{print $2}'`
        local SupCap_status=$(arcconf getconfig $pmc_dev ad|awk '/Supercap Status|Backup Power Status/{print $NF}'|grep -c 'Ready\|Ok')
        local Controller=$(arcconf getconfig $pmc_dev ad|awk '/Controller Status/{print $NF}'|grep -c 'Optimal')
        local cache_policy=$(arcconf getconfig $pmc_dev ad |grep -P -ci 'Global Physical Device Write Cache Policy\s+:\s+Drive Specific|Cache Status.*OK')

        if [[ $SupCap_status -eq 0 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] RaidCard Supercap Status Not Ready."
        fi

        if [[ $Controller -eq 0 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] RaidCard Controller Status Not Optimal."
        fi

        if [[ $cache_policy -ne 1 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] PMC RaidCard Write Cache Policy Not Drive Specific."
        fi
    fi
}
