#!/bin/bash
# E-mail notification script for Nagios
# Ben Winslow <rain@bluecherry.net>, Dec 2002

# Configuration:
# From: email address
FROM_ADDRESS="Nagios Notification <devnull@bluecherry.net>"

# msg_text() - Generates the plaintext part of the message
function msg_text()
{
	cat << _E_O_M_
--$MIME_SEP
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

******************* `printf "%-15s" $NOTIFICATIONTYPE` Notification *******************
       Service: $SERVICEDESC
          Host: $HOSTALIAS ($HOSTADDRESS)
         State: $SERVICESTATE

*********************** Detailed Information ***********************
$OUTPUT

********************* Notification Information *********************
   Alerts Sent: $NOTIFICATIONNUMBER
     Timestamp: $DATETIME

*********************** Monitor Information ************************
  Last checked: `duration $((DATE_TIMET - LASTCHECK))` ago
   Last change: `duration $((DATE_TIMET - LASTSTATECHANGE))` ago
Execution time: `duration $EXECUTIONTIME`

_E_O_M_
}

# msg_html() - Generates the HTML part of the message
function msg_html()
{
	cat << _E_O_M_
--$MIME_SEP
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <title>Problem Report</title>
 <style type="text/css">
table.problem {
	border: 1px #a4a4a4 solid;
	border-collapse: collapse;
}
th {
	background-color: #517eb4;
	color: #fff;
	text-align: center;
}
th.main {
	font-size: 1.75em;
}
th.sub {
	font-size: 1.25em;
}
td {
	background-color: #eaeaea;
	color: #000;
	text-align: left;
}
td.description {
	font-weight: bold;
}
 </style>
</head>
<body>
<center>
<table border="1" bgcolor="#eaeaea" cellspacing="0" class="problem">
  <tr><th colspan="2" bgcolor="#517eb4" class="main">$NOTIFICATIONTYPE Notification</th></tr>
  <tr><td class="description">Service:</td><td class="data">$SERVICEDESC</td></tr>
  <tr><td class="description">Host:</td><td class="data">$HOSTALIAS ($HOSTADDRESS)</td></tr>
  <tr><td class="description">State:</td><td class="data">$SERVICESTATE</td></tr>
  <tr><th colspan="2" bgcolor="#517eb4" class="sub">Detailed Information</th></tr>
  <tr><td colspan="2" class="data">$OUTPUT</td></tr>
  <tr><th colspan="2" bgcolor="#517eb4" class="sub">Notification Information</th></tr>
  <tr><td class="description">Alerts Sent:</td><td class="data">$NOTIFICATIONNUMBER</td></tr>
  <tr><td class="description">Timestamp:</td><td class="data">$DATETIME</td></tr>
  <tr><th colspan="2" bgcolor="#517eb4" class="sub">Monitor Information</th></tr>
  <tr><td class="description">Last checked:</td><td class="data">`duration $((DATE_TIMET - LASTCHECK))` ago</td></tr>
  <tr><td class="description">Last change:</td><td class="data">`duration $((DATE_TIMET - LASTSTATECHANGE))` ago</td></tr>
  <tr><td class="description">Execution time:</td><td class="data">`duration $EXECUTIONTIME`</td></tr>
</table>
</center>
</body>
</html>

_E_O_M_
}

# msg_header() - Generates the message headers
function msg_header()
{
if [ "$SERVICEDESC" = "N/A" ]; then
	SUBJECT="$NOTIFICATIONTYPE: $HOSTALIAS is $SERVICESTATE"
else
	SUBJECT="$NOTIFICATIONTYPE: $HOSTALIAS/$SERVICEDESC is $SERVICESTATE"
fi

cat << _E_O_M_
From: $FROM_ADDRESS
To: $CONTACTALIAS <$CONTACTEMAIL>
Subject: $SUBJECT
Date: $DATE_RFC822
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="$MIME_SEP"
User-Agent: html-email (sh) nagios

This is a multipart message as defined by RFC2049 (MIME).

_E_O_M_
}

# msg_footer - finalize the message
function msg_footer()
{
	echo "--$MIME_SEP--"
}

# collect_args - put the parameters into variables
function collect_args()
{
	CONTACTNAME="$1"
	CONTACTALIAS="$2"
	CONTACTEMAIL="$3"
	CONTACTPAGER="$4"
	shift 4

	HOSTNAME="$1"
	HOSTALIAS="$2"
	HOSTADDRESS="$3"
	HOSTSTATE="$4"
	shift 4

	SERVICEDESC="$1"
	SERVICESTATE="$2"
	OUTPUT="$3"
	PERFDATA="$4"
	EXECUTIONTIME="$5"
	LATENCY="$6"
	shift 6

	NOTIFICATIONTYPE="$1"
	NOTIFICATIONNUMBER="$2"
	shift 2

	DATETIME="$1"
	SHORTTIME="$2"
	DATE="$3"
	TIME="$4"
	TIMET="$5"
	LASTCHECK="$6"
	LASTSTATECHANGE="$7"
	shift 7

	ADMINEMAIL="$1"
	ADMINPAGER="$2"
}

# duration - convert seconds into a readable time
function duration()
{
	TEXT=""

	DURATION="$1"
	# weeks
	NUM_WEEKS=$((DURATION / 604800))
	# days
	NUM_DAYS=$(((DURATION / 86400) % 7))
	# hours
	NUM_HOURS=$(((DURATION / 3600) % 60))
	# minutes
	NUM_MINUTES=$(((DURATION / 60) % 60))
	# seconds
	NUM_SECONDS=$((DURATION % 60))

	if [ "$2" != "short" ]; then
		TEXT_WEEKS=" weeks, "
		TEXT_DAYS=" days, "
		TEXT_HOURS=" hours, "
		TEXT_MINUTES=" minutes, "
		TEXT_SECONDS=" seconds"
	else
		TEXT_WEEKS="w"
		TEXT_DAYS="d"
		TEXT_HOURS="h"
		TEXT_MINUTES="m"
		TEXT_SECONDS="s"
	fi
	if [ $NUM_WEEKS -ne 0 ]; then
		TEXT="$TEXT$NUM_WEEKS$TEXT_WEEKS"
	fi
	if [ $NUM_DAYS -ne 0 ]; then
		TEXT="$TEXT$NUM_DAYS$TEXT_DAYS"
	fi
	if [ $NUM_HOURS -ne 0 ]; then
		TEXT="$TEXT$NUM_HOURS$TEXT_HOURS"
	fi
	if [ $NUM_MINUTES -ne 0 ]; then
		TEXT="$TEXT$NUM_MINUTES$TEXT_MINUTES"
	fi
	TEXT="$TEXT$NUM_SECONDS$TEXT_SECONDS"
	echo "$TEXT"
}

# script_init - initialize some internal variables
function script_init()
{
	MIME_SEP="----=MIME.`mcookie`"
	DATE_RFC822="`date -R`"
	# FIXME: not portable
	DATE_TIMET="`date +%s`"
	if [ -z "$SERVICEDESC" ]; then
		SERVICEDESC="N/A"
		SERVICESTATE="$HOSTSTATE"
	fi
}

# run it
collect_args "$@"
script_init

msg_header
msg_text
msg_html
msg_footer
