# Do a relearn when BBU charge falls low
if [ "$ARCH" == "x86_64" ]; then
MEGAPATH="/opt/MegaRAID/MegaCli/MegaCli64"
MEGAPATH="/opt/MegaRAID/MegaCli/MegaCli"
# If we already have something in progress, don't do anything
STATUS=$($MEGAPATH -AdpBbuCmd -GetBbuStatus -aALL | grep "Charger Status")
if echo "$STATUS" | grep -q -i "In Progress"; then
echo "Relearn already in progress... exiting"
# If we already have a progress file, remove it (because we're not in progress anymore)
[ -f /home/nagios/bbu_relearn.progress ] && rm -f /home/nagios/bbu_relearn.progress
[ -f /home/nagios/bbu_relearn.error ] && rm -f /home/nagios/bbu_relearn.error
# See if our BBU is undercharged
if [ -f $MEGAPATH ]; then
BBU_OK=$(sudo $MEGAPATH AdpBbuCmd -GetBbuStatus -aALL | grep '^isSOHGood' |awk '{print $2}')
if [ "$BBU_OK" == "Yes" ]; then
# BBU_OK=$($MEGAPATH -AdpBbuCmd -GetBbuStatus -aALL | awk '$0~/Relative State of Charge/ {print $(NF-1)}')
BBU_OK=$(cat /home/nagios/raid_battery_level)
if [ "$BBU_OK" -lt 80 ]; then
# The BBU charge is too low, try to force a relearn (the attempt will be logged in the raid controller logs)
if ! $MEGAPATH -AdpBbuCmd -BbuLearn -a0; then
touch /home/nagios/bbu_relearn.error
chown nagios:nagios /home/nagios/bbu_relearn.error
[ -f /home/nagios/bbu_relearn.error ] && rm -f /home/nagios/bbu_relearn.error
touch /home/nagios/bbu_relearn.progress
chown nagios:nagios /home/nagios/bbu_relearn.progress
# Touch a file to show when this relearn occurred
touch "/home/nagios/.relearn.$(date +%s)"
chown nagios:nagios "/home/nagios/.relearn.$(date +%s)"