#!/bin/bash
#
# $Id$
#
# if alarm.pl is present and $CRITICALS resp. $WARNINGS is not empty, alerts are sent.
# otherwise the messages are just echoed.
# finally exits with 1 (CRITICAL not empty), 2 (WARNING not empty), or 0 (otherwise)
#
# has to be sourced!


# Messages empty?

#logDebug "Notify entered with criticals: '$CRITICALS'"
#logDebug "Notify entered with warnings: '$wARNINGS'"
#logDebug "Notify entered with infos: '$INFOS'"

export MESSAGE=$(echo -n "${CRITICALS}" ; echo -n "${WARNINGS}" ; echo -n "${INFOS}")

if [ -z "$MESSAGE" ] ; then 
  logNormal "No message set by $0 or whoever"
  WARNINGS="No messages set by $0 (or whoever failed to source me)"
  MESSAGE="${WARNINGS}"
fi 


# send notifications if warning or even critical

if [ -x "/usr/bin/alarm.pl" ] ; then
  if [ -n "$CRITICALS" ] ; then
    logDebug "Sending criticals: '$CRITICALS'"
    SUBJECT="[MonTools] ${RESNAME} CRITICAL on $(hostname -f)"
    echo "${MESSAGE}" |/usr/bin/alarm.pl -s "${SUBJECT}" ${CRITICALRECIPIENTS} ${WARNINGRECIPIENTS}
    exit 1
  else
    if [ -n "$WARNINGS" ] ; then
      logDebug "Sending warnings: '$WARNINGS'"
      SUBJECT="[MonTools] ${RESNAME} warning on $(hostname -f)"
      echo "${MESSAGE}" |/usr/bin/alarm.pl -s "${SUBJECT}" ${WARNINGRECIPIENTS}
      exit 2
    fi
  fi
fi

if [ -n "$CRITICALS" ] ; then
  RC=1
else
  if [ -n "$WARNINGS" ] ; then
    RC=2
  fi
fi

logVerbose "${MESSAGE}"

exit $RC

