#!/bin/sh
#
# $Id: nsca.functions 306 2014-03-20 17:27:42Z phil $
#
# wrapper functions to use with NSCA
# 
# Author: Philippe Kueck <projects at unixadm dot org>
#

plugindir="/usr/lib64/nagios/plugins"
nsca_host=${nsca_host:-localhost}

# type: helper
# purpose: send a host unreachable
# nsca format: <remote host><tab>2<tab>CRITICAL - Host Unreachable
send_unreachable() {
	remote=$1
	echo -e "$1\t2\tCRITICAL - Host Unreachable ($1)\n" | \
	/usr/sbin/send_nsca -H ${nsca_host} -c /etc/nagios/send_nsca.cfg \
		>& /dev/null
}

# type: host check
# purpose: dummy "I am alive"
# arguments: remote host
# nsca format: <remote host><tab><return code><tab><output>
host_alive() {
	remote=$1
	echo -e "${remote}\t0\tup and running\n" | \
	/usr/sbin/send_nsca -H ${nsca_host} -c /etc/nagios/send_nsca.cfg \
		>& /dev/null
	return 0
}

# type: host check
# purpose: host-alive
# arguments: remote host
# nsca format: <remote host><tab><return code><tab><output>
check_ping() {
	remote=$1
	out=$(${plugindir}/check_ping -H ${remote} -w 3000.0,80% -c 5000.0,100% -p 5)
	ret=$?
	echo -e "${remote}\t${ret}\t${out}\n" | \
	/usr/sbin/send_nsca -H ${nsca_host} -c /etc/nagios/send_nsca.cfg \
		>& /dev/null
	return $ret
}

# type: service
# purpose: initiate check for localhost, but identify as `hostname -f`
# arguments: plugin name, plugin arguments
# nsca format: <hostname><tab>pasv_<plugin name><tab><return code><tab><output>
check_local() {
	plugin=$1 ; shift
	remote=$(hostname -f)
	args=$@
	out=$(${plugindir}/${plugin} ${args})
	ret=$?
	echo -e "${remote}\tpasv_${plugin}\t${ret}\t${out}\n" | \
	/usr/sbin/send_nsca -H ${nsca_host} -c /etc/nagios/send_nsca.cfg \
		>& /dev/null
	return $ret
}

# type: service
# purpose: initiate check for remote host
# arguments: remote host, plugin name, plugin arguments
# nsca format: <remote host><tab>pasv_<plugin name><tab><return code><tab><output>
check() {
	plugin=$1 ; shift
	remote=$1 ; shift
	args=$@
	[ "$remote" == "localhost" ] && remote=$(hostname -f)
	out=$(${plugindir}/${plugin} -H ${remote} ${args})
	ret=$?
	echo -e "${remote}\tpasv_${plugin}\t${ret}\t${out}\n" | \
	/usr/sbin/send_nsca -H ${nsca_host} -c /etc/nagios/send_nsca.cfg \
		>& /dev/null
	return $ret
}

# type: service
# purpose: initiate check for localhost, identify as `hostname -f` and send extra
#          text after the plugin name
# arguments: plugin name, plugin arguments
# nsca format: <remote host><tab>pasv_<plugin name> <text><tab><return code><tab><output>
check_extra() {
	plugin=$1 ; shift
	remote=$(hostname -f)
	extra=$1 ; shift
	args=$@
	out=$(${plugindir}/${plugin} ${args})
	ret=$?
	echo -e "${remote}\tpasv_${plugin} ${extra}\t${ret}\t${out}\n" | \
	/usr/sbin/send_nsca -H ${nsca_host} -c /etc/nagios/send_nsca.cfg \
		>& /dev/null
	return $ret
}

# type: service
# purpose: initiate check for remote host via nrpe
# arguments: remote host, plugin name, plugin arguments
# nsca format: <remote host><tab>pasv_<plugin name><tab><return code><tab><output>
check_nrpe() {
	plugin=$1 ; shift
	remote=$1 ; shift
	args=$@
	out=$(${plugindir}/check_nrpe -H ${remote} -c ${plugin})
	ret=$?
	echo -e "${remote}\tpasv_${plugin}\t${ret}\t${out}\n" | \
	/usr/sbin/send_nsca -H ${nsca_host} -c /etc/nagios/send_nsca.cfg \
		> /dev/null
	return $ret
}
