#!/bin/sh
#
# Copyright (c) 2013 Holger Weiss <holger@weiss.in-berlin.de>
#
# This file is free software; Holger Weiss gives unlimited permission to copy
# and/or distribute it, with or without modifications, as long as this notice is
# preserved.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY, to the extent permitted by law; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

set -e
set -u

send_nsca_args=${SEND_NSCA_ARGS:+$SEND_NSCA_ARGS}

die()
{
	echo >&2 "$@"
	exit 1
}

usage()
{
	die "Usage: $0 [-C <comment>] [-c <config-file>] [-H <host>] [-S <service>]"
}

while getopts C:c:H:hS: option
do
	case $option in
	C)
		comment=$OPTARG
		;;
	c)
		send_nsca_args="${send_nsca_args:+$send_nsca_args }-c $OPTARG"
		;;
	H)
		host=$OPTARG
		;;
	S)
		service=$OPTARG
		;;
	h|\?)
		usage
		;;
	esac
done

shift `expr $OPTIND - 1`
test $# -eq 0 || usage

host=${host:-`hostname -s`}
service=${service:+$service}
author=`getent passwd "$USER" | cut -d: -f5`
comment=${comment:-"Submitted by send_nsca@`hostname -s`"}

if [ -z "$service" ]
then	# Host acknowledgement.
	printf '%b' "ACKNOWLEDGE_HOST_PROBLEM;$host;1;0;1;$author;$comment\n" \
	    | send_nsca -C $send_nsca_args
else	# Service acknowledgement.
	printf '%b' "ACKNOWLEDGE_SVC_PROBLEM;$host;$service;1;0;1;$author;$comment\n" \
	    | send_nsca -C $send_nsca_args
fi

# vim:set joinspaces noexpandtab textwidth=80:
