#!/bin/bash
#
# Noris - IPMI - ipv4/6
# 21307153
# written by dloeschmann@noris.de
#
# IPMI MUSS AUF ZIELSYSTEM AKTIVIERT SEIN



IPMITOOL=/usr/bin/ipmitool
EXIT=0;
STATE_OK=0;
STATE_WARNING=1;
STATE_CRITIAL=2;
STATE_UNKNOWN=3;
FILTER="fail|fault|lcr|failure|critcal|Lost|Error";

if [ -x "$IPMITOOL" ]; then
    IPMITOOL=/usr/bin/ipmitool;  
else
    echo "FEHLER: ipmitool-plugin wurde nicht gefunden! (/usr/bin/ipmitool)"
exit
fi

if [ $# -lt 1 ]; then
    echo "Kommando: ipmitool -I lanplus -H host -U ipmi_username -P ipmi_password -L user"
    exit $STATE_UNKNOWN
fi

while test -n "$1"; do
    case "$1" in
        -H)
            HOST=$2
            shift
            ;;
        -U)
            IPMIUSER=$2
            shift
            ;;
        -P)
            PASSWORD=$2
            shift
            ;; 
        -T)
            TYPE=$2
            shift
            ;;
        *)
        echo "Unbekanntes Argument: $1"
        exit $STATE_UNKNOWN
        ;;
        esac
    shift
done

if [ -z $HOST ]; then
    echo "FEHLER: Host/FQDN fehlt (-H)"
    exit $STATE_UNKNOWN
elif [ -z $IPMIUSER ]; then
    echo "FEHLER: BENUTZERNAME fehlt (-U)"
    exit $STATE_UNKNOWN
elif [ -z $PASSWORD ]; then
    echo "FEHLER: PASSWORT fehlt (-P)"
    exit $STATE_UNKNOWN
fi

# Abfrage, ob System erreichbar und eingeschalten ist!
IPMI_CHASSIS=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user chassis status );
rc=$?;

if [ $rc != 0 ]; then
    echo "Verbindungsfehler";
    exit $STATE_UNKNOWN;
fi

case "$IPMI_CHASSIS" in
	*"System Power         : off"* ) 
		echo "System ist ausgeschalten!"
		exit $STATE_WARNING;
	;;
	*"System Power         : on"* ) 
		: # System eingeschalten!
	;;
	* ) 
		echo "Systemstatus Unbekannt!" 
		exit $STATE_UNKNOWN;
	;;
esac

#echo "$IPMI_CHASSIS" | grep 'System Power' | awk '{print $4}';
#IPMI_PWR_OFF=$($IPMI_CHASSIS | grep 'System Power         : off');
#IPMI_PWR_ON=$($IPMI_CHASSIS | grep 'System Power         : on');

#POWER_ON=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user chassis status | grep "System Power         : off");
#if [[ -n $IPMI_PWR_OFF ]] && [[ -z $IPMI_PWR_ON ]]; then
#if [[ -n $POWER_ON ]]; then     
#     echo "System ist ausgeschalten!" ;
#     exit 1;
#     else
#        : # Host erreichbar
#fi

case "$TYPE" in
    FAN) # LUEFTER (GEHAEUSE)
            FAN=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user sdr type FAN | grep -iE 'fail|lcr|Error|critical' ); sleep 5;
            FAN_FAULT=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user chassis status | grep "Cooling/Fan Fault    : true");
            if [[ -n $FAN ]] || [[ -n $FAN_FAULT ]]; then
                echo $FAN $FAN_FAULT;
                EXIT=2;
            else
                echo "Luefter: OK";
            fi
            ;;
    PSU) # NETZTEIL(E) + AUSLASTUNG + BATTERIEN (RAID,CMOS)
            PSU=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user sdr type Power\ Supply | grep -iE 'fail|lcr|failure|critcal|Lost|Error' ); sleep 3;
            BATTERY=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user sdr type Battery | grep -iE 'fail|lcr|failure|critcal|Lost|Error' ); sleep 3;
            PWR=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user sdr type Current | grep -iE 'fail|lcr|failure|critcal|Lost|Error' );
    	    if [[ -n $PSU ]] || [[ -n $BATTERY ]] || [[ -n $PWR ]] ; then
                echo $PSU $BATTERY $PWR;
                EXIT=2;
            else
                echo "Netzteil(e): OK";
            fi
            ;;
    CPU) # PROZESSOR(EN)
            CPU=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user sdr type Processor | grep -iE $FILTER );
            if [[ -n $CPU ]]; then
                echo $CPU;
                EXIT=2;
            else
                echo "Prozessor(en): OK";
            fi
            ;;
    RAM) # ARBEITSSPEICHER
            RAM=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user sdr type Memory | grep -iE 'failed|fault|lcr|failure|critcal|Lost' );
            if [[ -n $RAM ]]; then
                echo $RAM;
                EXIT=2;
            else
                echo "Arbeitsspeicher: OK";
            fi
            ;; 
    TEMP) # TEMPERATUREN
            TEMP=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user sdr type Temp | grep -iE $FILTER );
            if [[ -n $TEMP ]]; then
                echo $TEMP;
                EXIT=2;
            else
                echo "Temperatur: OK";
            fi
            ;;
    HDD) # HDD + KABEL 
            HDD=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user sdr type Drive\ Slot\ /\ Bay | grep -iE $FILTER  ); sleep 3;
            HDD_FAULT=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user chassis status | grep "Drive Fault          : true"); sleep 3;
	    CABLE=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user sdr type Cable\ /\ Interconnect | grep -iE $FILTER );
            if [[ -n $HDD ]] || [[ -n $CABLE ]] || [[ -n $HDD_FAULT ]]; then
                echo $HDD $CABLE $HDD_FAULT;
    	    	EXIT=2;
            else
                echo "Festplatte(n): OK";
            fi
            ;;
    LOG) # ALARM/EVENT-LOG von MGMT-INTERFACE
            EVENTLOG=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user sel list | grep -iE $FILTER );
            if [[ -n $EVENTLOG ]]; then
                echo $EVENTLOG;
                EXIT=2;
                echo "LOG muss manuell gecleared werden, damit der Fehler verschwindet!"
            else
                echo "Log: OK";
            fi
            ;;
    OTHER) # SONSTIGES (VOLT, PCI-E...)
            OTHER=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user sdr elist full | grep -iE $FILTER );
            if [[ -n $OTHER ]]; then
                echo $OTHER;
                EXIT=2;
            else
                echo "OK";
            fi
            ;;
    VERBOSE) # ZEIGE ALLES (inkl. OK-Meldungen)
            VERBOSE=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user sdr );
	    VERBOSE2=$($IPMITOOL -I lanplus -H $HOST -U $IPMIUSER -P $PASSWORD -L user sdr elist full );
            echo $VERBOSE;
            echo $VERBOSE2
            ;;
    *)
       echo "Verfuegbare Parameter: -T {FAN,HDD,TEMP,CPU,RAM,PSU,LOG,OTHER,VERBOSE}"
       exit $STATE_UNKNOWN;
       ;;
esac


exit $EXIT;

