#!/bin/sh
# $Id: nagios-notify.sh,v 1.3 2006/08/03 12:39:22 glen Exp $
#
# Template based Nagios notify script.
# Requires: sed, awk
#
# Author: Elan Ruusame <glen@pld-linux.org>
# Date: 2006-08-03
# License: Same as Nagios (GPL v2)
#

templatedir='/etc/nagios/templates'
prog="${0##*/}"

# Substutute Nagios $VAR$-s (which are exported to environment by Nagios) from template.
template_subst() {
	local tmpl="$1"
	awk 'BEGIN {
		for (var in ENVIRON) {
			if (substr(var, 1, length("NAGIOS_")) == "NAGIOS_") {
				val = ENVIRON[var];
				var = substr(var, 1 + length("NAGIOS_"));
				gsub(/#/,"\\#",val);
				printf("s#\$%s\$#%s#g\n", var, val);
			}
		}
		exit;
	}' | sed -f - $tmpl
}

if [ -z "$NAGIOS_STATUSDATAFILE" ]; then
	echo >&2 "$prog: This program must be ran from Nagios."
	exit 1
fi

# extract nagios version from status file
export NAGIOS_VERSION=$(awk -F= '/version=/{print $2}' $NAGIOS_STATUSDATAFILE)

tmpl="$templatedir/$1.tmpl"
if [ ! -f "$tmpl" ]; then
	echo >&2 "$prog: template '$tmpl' can not be found!"
	exit 1
fi

template_subst "$tmpl"
