#!/bin/bash
#
# $Id$
#
# if alarm.pl is present and $CRITICALS resp. $WARNINGS is not empty, alerts are sent.
# otherwise the messages are just echoed.
#
# also sets RC to something usable as exit code
#
# has to be sourced!


# Messages empty?

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

RC=0

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
    RC=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
      RC=2
    fi
  fi
else
  if [ -n "$CRITICALS" ] ; then
    RC=1
  else
    if [ -n "$WARNINGS" ] ; then
      RC=2
    fi
  fi
  logVerbose "${MESSAGE}"
fi

