#!/bin/bash
# This file is managed by Puppet - any manual edits will be lost.
#
# ChangeLog:
# When		Who			What
# 08-02-2016	Rudy Broersma		Installation in Puppet.
# 07-11-2016	Rudy Broersma		Add check for eventlog
# 27-05-2017	Rudy Broersma		Check all SEL entries; not only today
#                                       you can use "ipmitool sel clear" to clear entries
# 27-07-2017	Rudy Broersma		Remove PS check; not required since we read SEL
# 20-10-2017	Rudy Broersma		Fix voor Dell R410 die bij sel list "Event Logging Disabled #0x72 - Log area reset/cleared - Asserted" meldt
# 16-03-2018	Rudy Broersma		Ignore "Drive Slot / Bay" assert/deasserts. Bug.
# 22-10-2018	Rudy Broersma		Ignore "Watchdog" assert/deassert bug
# 26-11-2018	Ronald T. de Graaf	Ignore "Intrusion detection" bug
# 13-05-2022	Rudy Broersma		Fix voor utils.sh, komt in meer mappen voor. Fix tevens exit status. 
#
# HINT:
# Unknown #0x04 is CPU overheat
#
#####################################################################
#
#  Uses ipmi tool to get Supermicro power supply status.  This works
#  on the X8 class motherboards and may or may not require changes to
#  work on other motherboards and chassis.
#
#  Copyright (c) 2010, tummy.com, ltd., All Rights Reserved.
#  Written by Kyle Anderson, tummy.com, ltd.
#  Under the GPL v2 License 
#
#  Modified 2016 - Rudy Broersma <r.broersma@duocast.nl>
#  - Add compatibility with multiple boards
#  - Remove host/user/path and use local access
#  - Fix empty value(s) error
#  - Module checking
#  - Output IP to return to Nagios
#
#####################################################################
#
# check for plugin directory where utils.sh lives
[ -d /usr/lib/nagios/plugins ]   && UTILPATH=/usr/lib/nagios/plugins
[ -d /usr/lib64/nagios/plugins ] && UTILPATH=/usr/lib64/nagios/plugins
[ -d /usr/lib/icinga ]           && UTILPATH=/usr/lib/icinga/
[ -d /usr/local/nagios/libexec ] && UTILPATH=/usr/local/nagios/libexec/

#  load states and strings
if [ -x "$UTILPATH"/utils.sh ]; then
        . "$UTILPATH"/utils.sh
else
        echo "ERROR: Cannot find utils.sh"
        exit 3
fi

if ! which ipmitool >/dev/null 2>&1; then
        echo "ERROR: No ipmitool found in my path"
        exit $STATE_UNKNOWN
fi

# check wether required module is loaded.
IPMI_MODULES=`lsmod | grep ipmi_devintf | wc -l`
if [ $IPMI_MODULES -lt 1 ]; then
  echo "UNKNOWN: IPMI module not loaded"
  exit $STATE_UNKNOWN
fi

LAST_LINE=`ipmitool sel list 2> /dev/null | grep -v "Drive Slot / Bay" | grep -v "Watchdog2 #0xca" | tail --lines 1`

if [ "$LAST_LINE" != "" ]; then
  if [[ $LAST_LINE != *"Log area reset"* && $LAST_LINE != *"Chassis intrusion"* ]]; then
    echo "CRITICAL: $LAST_LINE" | sed "s/|/-/g"
    exit $STATE_CRITICAL
  fi
fi

IPADDR=`ipmitool lan print 1 | grep "IP Address  " | awk '{ print $4}'`

echo "OK: SEL OK ($IPADDR)"
exit $STATE_OK
