#!/bin/bash
####################################################################################
#
# self_activate
#
# Self_activate 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 register a host. Script
#   can be run any to on host which should be registered or ony any host on behalf
#   of such host. I personally run it from rc.local startup script. You can choose
#   whatever suits better your purposes.
#
# 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
#   HOSTGROUP - hostgroups separated by comma to include new host to
#   USE - host templates to use for new host registered
#
# 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"
HOSTGROUP="computer-hardware,linux-servers,ntp-servers,ping-servers,proxmox-servers,ssh-servers"
USE="generic-host,debian-servers"

case "$1" in
    "UP"|"OK")
    echo -e $NSCA_HOST'\t'$SERVICE_NAME'\t1\t"add '$2' '$HOSTGROUP' '$USE'"'|send_nsca $NSCA_HOST -c /etc/send_nsca.cfg
    echo -e $NSCA_HOST'\t'$SERVICE_NAME'\t0\t"'$2' added"'|send_nsca $NSCA_HOST -c /etc/send_nsca.cfg
    ;;
    *);;
esac
exit 0