#!/bin/bash
# ILO4 Nagios check script v0.1
# Last update 2016/03/09 tokaji.attila@gmail.com
#Requires: ipmitool - Tested with ipmitool-1.8.11-21 on CentOS
#License: CC BY-NC-SA 4.0
#Usage: check_ilo4 <ilouser> <ilouserpassword> <iloIP or iloHostname>


TMPFILE=$(mktemp)
ipmitool -I lanplus -U $1 -P $2 -H $3 chassis status>$TMPFILE


retcode=0

SystemPowerStatus=`cat $TMPFILE|grep "System Power"|awk -F ":" {'print $2'}`
PowerOverloadStatus=`cat $TMPFILE|grep "Power Overload"|awk -F ":" {'print $2'}`
MainPowerFaultStatus=`cat $TMPFILE|grep "Main Power Fault"|awk -F ":" {'print $2'}`
PowerControlFaultStatus=`cat $TMPFILE|grep "Power Control Fault"|awk -F ":" {'print $2'}`
LastPowerEventStatus=`cat $TMPFILE|grep "Last Power Event"|awk -F ":" {'print $2'}`
DriveFaultStatus=`cat $TMPFILE|grep "Drive Fault"|awk -F ":" {'print $2'}`
CoolingFanFaultStatus=`cat $TMPFILE|grep "Cooling/Fan Fault"|awk -F ":" {'print $2'}`

if [ $SystemPowerStatus != "on" ];then
POWER=`echo "[Power Status: not OK] "`
retcode=1
else
POWER=`echo "[Power Status: OK] "`
fi

if [ $PowerOverloadStatus != "false" ];then
POWEROVERLOAD=`echo "[Power Overload Status: not OK] "`
retcode=2
else
POWEROVERLOAD=`echo "[Power Overload Status: OK] "`
fi

if [ $MainPowerFaultStatus != "false" ];then
MAINPOWER=`echo "[Main Power Status: not OK] "`
retcode=2
else
MAINPOWER=`echo "[Main Power Status: OK] "`
fi

if [ $PowerControlFaultStatus != "false" ];then
POWERCONTROL=`echo "[Power Control Status: not OK] "`
retcode=2
else
POWERCONTROL=`echo "[Power Control Status: OK] "`
fi

if [ $DriveFaultStatus != "false" ];then
DRIVE=`echo "[Drive Status: not OK] "`
retcode=2
else
DRIVE=`echo "[Drive Status: OK] "`
fi

if [ $CoolingFanFaultStatus != "false" ];then
COOLING=`echo "[Cooling/Fan Status: not OK]"`
retcode=2
else
COOLING=`echo "[Cooling/Fan Status: OK]"`
fi

echo $POWER $POWEROVERLOAD $MAINPOWER $POWERCONTROL $DRIVE $COOLING

exit $retcode
rm $TMPFILE
