#!/bin/bash
#
# $Id$
#
# checks if ipmi-sensors are satisfied with the monitored hardware.
# if IPMISENSORS != 0 that many sensors are expected to be reported and OK.
# if POWERSUPPLIES != 0 that many power supplies are expected to be there and OK.
#

# prepare

#echo "DEBUG: 0. CRITICALS='$CRITICALS'"

. /usr/lib/montools/mt-prepare

RESNAME="IPMI-Sensors"


# work

PATH="/etc/scripts:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

lsmod |grep ipmi_devintf >/dev/null || modprobe ipmi_devintf
lsmod |grep ipmi_si >/dev/null || modprobe ipmi_si

#echo "DEBUG: 1. CRITICALS='$CRITICALS'"

if [ "$IPMISENSORS" != "0" ] ; then
  export OKSENSORS=$(ipmi-sensors --quiet-cache --interpret-oem-data --ignore-not-available-sensors |egrep -iv "^ID|Power" |grep -c "'OK'")

  if [ "$OKSENSORS" != "$IPMISENSORS" ] ; then
    add2warnings \
      "$OKSENSORS instead of $IPMISENSORS ipmi-sensors are OK, re-trying once to eventually overcome freeimpi caching hickup"
    sleep 5
    # flush cache
    ipmi-sensors --quiet-cache -f
    sleep 5
    export OKSENSORS=$(ipmi-sensors --quiet-cache --interpret-oem-data --ignore-not-available-sensors |egrep -iv "^ID|Power" |grep -c "'OK'")
    if [ "$OKSENSORS" -lt "$IPMISENSORS" ] ; then
      add2criticals \
        "Only $OKSENSORS of $IPMISENSORS ipmi-sensors are OK, at 2nd call to impi-senors."
    elif [ "$OKSENSORS" -gt "$IPMISENSORS" ] ; then
      add2warnings \
        "After 2nd call more impi-sensors are OK, $OKSENSORS instead of just $IPMISENSORS."
    else
      add2warnings \
        "After 2nd call all impi-sensors all $IPMISENSORS impi-sensors seem OK."
    fi
  else
    add2infos "All $IPMISENSORS impi-sensors seem OK."
  fi
fi

#echo "DEBUG: 2. CRITICALS='$CRITICALS'"

if [ "$POWERSUPPLIES" != "0" ] ; then 
  PRESENTPOWERSUPPLIES=$(ipmi-sensors --quiet-cache --interpret-oem-data --ignore-not-available-sensors |egrep -i " (PS|Status).* Power Supply" |grep -c "'Presence detected'")
  OKPOWERSUPPLIES=$(ipmi-sensors --quiet-cache --interpret-oem-data --ignore-not-available-sensors |egrep -i " (PS|Status).* Power Supply" |egrep -vi "Fail|input lost" |wc -l)

  if [ "$PRESENTPOWERSUPPLIES" -lt "$POWERSUPPLIES" ] ; then
    add2criticals \
      "Only $PRESENTPOWERSUPPLIES of $POWERSUPPLIES power supplies are detected as 'present'."
  elif [ "$PRESENTPOWERSUPPLIES" -gt "$POWERSUPPLIES" ] ; then
    add2warnings \
      "Unexpectedly $OKPOWERSUPPLIES instead of just $POWERSUPPLIES power supplies are detected as 'present'."
  else
    add2infos "All $POWERSUPPLIES power suppplies are detected."
  fi
  if [ "$OKPOWERSUPPLIES" -lt "$POWERSUPPLIES" ] ; then
    add2criticals \
      "Only $OKPOWERSUPPLIES of $POWERSUPPLIES power supplies are OK."
  elif [ "$OKPOWERSUPPLIES" -gt "$POWERSUPPLIES" ] ; then
    add2warnings \
      "Unexpectedly $OKPOWERSUPPLIES instead of just $POWERSUPPLIES power supplies are OK."
  else
    add2infos "All $POWERSUPPLIES power suppplies seem OK."
  fi
fi

#echo "DEBUG: 3. CRITICALS='$CRITICALS'"


# notifications

. /usr/lib/montools/mt-notify-exit

