#!/bin/sh
# ========================================================================================
#  New error reports in the `ipmi Service Event Log` plugin for Nagios 
# 
# Written by         	: Steve Bosek (sbosek@mac.com)
# Release               : 0.6
# Creation date			: 20 September 2007
# Revision date         : 23 octobre 2007
# Package               : DTB Nagios Plugin
# Description           : Nagios plugin (script) to check for new error reports since last time in the ipmi eventlog.
#						  This script has been designed and written on Linux
#                       
# Usage                 : ./check_ipmi_eventlog.sh -i <interface> -h <hostname> -u <user> -p <passwrd> 
# ----------------------------------------------------------------------------------------
#
# TODO:
#
# ========================================================================================

IPMITOOL=/usr/bin/ipmitool
# date "+%m/%d/%Y | %H:%M:%S"
# Nagios return codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

# Plugin default level
#INTERFACE=${INTERFACE:=lan}
LOGIN=${LOGIN:=admin}
PASS=${PASS:=changeme}

if [ ! -x $IPMITOOL ]; then
	echo "UNKNOWN: ipmitool not found or is not executable by the nagios user."
	exit $STATE_UNKNOWN
fi

# Plugin variable description
PROGNAME=$(basename $0)
RELEASE="Revision 1.O"
AUTHOR="(c) 2007 Steve Bosek (sbosek@mac.com)"

# Functions plugin usage 
print_release() {
    echo "$RELEASE $AUTHOR"
}

print_usage() {
		echo ""
        echo "$PROGNAME $RELEASE - New error reports in the IPMI Service Event Log plugin for Nagios  "
		echo ""
		echo "		-h  Show this page"
		echo "		-v  Show script version"
		echo ""
        echo "Usage: $PROGNAME --help"
		echo "Usage: $PROGNAME --version"
		echo ""
}

print_help() {
		print_usage
        echo ""
        print_release $PROGNAME $RELEASE
        echo ""
        echo "This plugin will check the IPMI Service Event Log and report new error"
		echo "-i is for ipmi interface, default lan"
		echo "-H is for hostname or IP on IPMI interface"
		echo "-u is user IPMI login, default admin"
		echo "-p is password IPMI user, default changeme"
        echo ""
		exit 0
}

# Make sure the correct number of command line arguments have been supplied
#if [ $# -gt 0 ]; then
#    print_usage
#   exit $STATE_UNKNOWN
#fi

# Parse parameters
while [ $# -ne 0 ]; do
    case "$1" in
        -h | --help)
            print_help
            exit $STATE_OK
            ;;
        -v | --version)
                print_release
                exit $STATE_OK
                ;;
        -i | --interface)
                shift
                INTERFACE=$1
                ;;
        -H | --hostname)
               shift
                HOSTNAME=$1
                ;;
	-u | --user)
                shift
                LOGIN=$1
                ;;
	-p | --password)
                shift
                PASS=$1
                ;;
        *)  echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
        esac
shift
done
DIR_IPMI=/tmp/ipmi_tmp
if [ ! -d $DIR_IPMI ]; then
        mkdir -p $DIR_IPMI
fi
IPMI_LAST=$DIR_IPMI/ipmilast.$HOSTNAME
IPMI_TMP=$DIR_IPMI/ipmitmp.$HOSTNAME
IPMI_ERROR=$DIR_IPMI/ipmierr.$HOSTNAME

# check if first run
if [ ! -f ${IPMI_LAST} ] ; then
	# first TIME INITILIZED ...
	if [ $LOCAL -ge 1 ]; then
	$IPMITOOL -U $LOGIN -P $PASS sel elist | head -n 1 | tr / : > $IPMI_LAST	
	else
	$IPMITOOL -I $INTERFACE -H $HOSTNAME -U $LOGIN -P $PASS sel elist | head -n 1 | tr / : > $IPMI_LAST
	fi
	echo "Timer Initialization ... coming soon !"
        exit $STATE_UNKNOWN
fi

if [ ! -s $IPMI_LAST ]; then 
		if [ $LOCAL -ge 1 ]; then
			$IPMITOOL -U $LOGIN -P $PASS sel elist | tail -1 | tr / : > $IPMI_LAST
		else
			$IPMITOOL -I $INTERFACE -H $HOSTNAME -U $LOGIN -P $PASS sel elist | tail -1 | tr / : > $IPMI_LAST	
		fi
   		echo "Timer is empty new Initialization ... coming soon !"
	exit $STATE_UNKNOWN
fi


LAST_TIME=$(cat ${IPMI_LAST})
if [ $LOCAL -ge 1 ]; then
CMD=$($IPMITOOL -U $LOGIN -P $PASS sel elist | tr / : > $IPMI_TMP)
else
CMD=$($IPMITOOL -I $INTERFACE -H $HOSTNAME -U $LOGIN -P $PASS sel elist | tr / : > $IPMI_TMP)
	ERROR=$(cat $IPMI_TMP | grep "${LAST_TIME}" -A 1000 | grep -v "$LAST_TIME" | grep -Ei '(error|failure|failed)' > $IPMI_ERROR)
fi
COUNT_ERROR=$(cat $IPMI_ERROR | wc -l)
tail -1 $IPMI_TMP  > $IPMI_LAST
TIME_IPMI_LAST=$(cat $IPMI_LAST | awk '{print $3 " " $5}')
if [ ${COUNT_ERROR} -ge 1 ]; then
echo "HARDWARE WARNING : Error number (${COUNT_ERROR}) : `cat $IPMI_ERROR | tr "|" " "`"
: > $IPMI_ERROR 		# Fichier maintenant vide
    exit $STATE_WARNING
else
	
	echo "HARDWARE OK : no new error in IPMI eventlog since ${TIME_IPMI_LAST}"
	exit $STATE_OK
fi

