#!/bin/bash
####################################################################################
#
# heart_beat
#
# Heart_beat is a part of Nagios automatic hosts self-registration software.
# NAR for simplicity.
# For detailer NAR description read README file.
#
# Usage:
#   Script sends commands to Nagios-AR daemon (via NSCA) to deregister a host.
#   Create Nagios host template 'dynamic-host'. See sample file included in NAR
#   package. Create Nagios command 'heart-beat' for this script. NAR automatically
#   assigns 'dynamic-host' template to any new registered host. When host gets
#   'DOWN' or 'UNREACHANBLE' event handler is called and deregister host.
#
# Arguments:
#   $1 - $HOSTSTATE$
#   $2 - $HOSTNAME$
#
# Reguirements:
#   Nagios with NSCA server - up and running in reach.
#   Nagios-AR - up and running on Nagios host.
#
# Configuration:
#   NSCA_HOST - Nagios host to send messages to
#   SERVICE_NAME - Nagios passive service to send messages to
#
# Note: Script sends two messages because Nagios calls event_handler only on status
#   change. Because we don't know current state of the service we must change status
#   explicitly.
#
# (C) 2016 Andrey A. Porodko <andrey.porodko@gmail.com>
# (C) 2016 Axian Ltd, http://axian.ru
#
# v0.1 - initial release
#
####################################################################################
NSCA_HOST="smb-monitor.office.axian.ru"
SERVICE_NAME="Hosts self-registration"

case "$1" in
    "DOWN"|"CRITICAL"|"UNREACHANBLE")
    echo -e $NSCA_HOST'\t'$SERVICE_NAME'\t1\t"remove '$2'"'|send_nsca $NSCA_HOST -c /etc/send_nsca.cfg
    echo -e $NSCA_HOST'\t'$SERVICE_NAME'\t0\t"'$2' removed"'|send_nsca $NSCA_HOST -c /etc/send_nsca.cfg
    ;;
    *);;
esac
exit 0