
Pre-requirements

1. Nagios installed out of the box. I have Nagios up and running on Ubuntu (currently 14.04).
    You configure it for your needs.

2. beanstalkd installed on the same host. 
    sudo apt-get install beanstalkd
    I have in /etc/default/beanstalkd the following (just don't remember what was
    in there in distribution)
	## Defaults for the beanstalkd init script, /etc/init.d/beanstalkd on
	## Debian systems.

	BEANSTALKD_LISTEN_ADDR=127.0.0.1
	BEANSTALKD_LISTEN_PORT=11300

	# You can use BEANSTALKD_EXTRA to pass additional options. See beanstalkd(1)
	# for a list of the available options. Uncomment the following line for
	# persistent job storage.
	BEANSTALKD_EXTRA="-b /var/lib/beanstalkd"

3. python-beanstalkc library as NAR written on Python
    sudo apt-get install python-beanstalkc
    No specific configuration here just be sure that beanstalkd is up and running

4. Don't forget to add nsca support for Nagios host
    sudo apt-get install nsca nsca-client
    
    and nsca client software on each self-registering host

Installation

There is no installation and configuration script, so everything has to be done manually.

1. Make a temporary folder on your Nagios host and unpack zip file there

2. Files nagios-ar and nagios-ar-stat go to /usr/local/bin

3. File nagios-ar.conf goes to /etc/init. This is systemd start configuration

4. Files heart_beat and self_registrar go to /usr/lib/nagios/plugins. They are nagios commands

5. Add the following commands to your Nagios configuration
    #
    #
    #
    define command {
	command_name                   	heart-beat
	command_line                   	$USER1$/heart_beat $HOSTSTATE$ $HOSTADDRESS$
	register                       	1
    }

    define command {
	command_name                   	self_register
	command_line                   	$USER1$/self_registrar "$SERVICESTATE$" "$SERVICEOUTPUT$"
	register                       	1
    }

6. Add the following service to your Nagios configuration
    define service {
	host_name                      	@Nagios_host_name@
        service_description            	Hosts self-registration
        display_name                   	Hosts self-registration
	use                            	passive-service
        is_volatile                    	1
	initial_state                  	u
        active_checks_enabled          	0
	passive_checks_enabled         	1
        check_period                   	24x7
	obsess_over_service            	0
        event_handler                  	self_register
	event_handler_enabled          	1
        flap_detection_enabled         	0
	process_perf_data              	0
        retain_status_information      	1
	notification_period            	24x7
        notifications_enabled          	0
	register                       	1
    }

7. Add the following dynamic host template to your Nagios configuration
    define host {
	name                           	dynamic-host
	initial_state                  	o
	check_interval                 	5
	retry_interval                 	1
	event_handler                  	heart-beat
	event_handler_enabled          	1
	contact_groups                 	admins
	notification_options           	d,u,r
	failure_prediction_enabled     	1
	register                       	0
    }

    All dynamic hosts shall inherit this template (as well as other you require)

8. Restart Nagios to pick up new configuration

How to use

1. Copy and modify self_activate (Linux host) or self_activate.bat (Windows host) to each of self-registered hosts

2. Make self_activate script run on system start, for example in rc.local as shown
   /usr/local/bin/self_activate UP hostname-to-register

3. self_activate script sends command to Nagios through nsca client software to a specific (defined above) service
   Service places host description to a queue
   nagios-ar daemon fetches descriptions, writes appropriate Nagios config files and restart Nagios
   If dynamic host stops responding hear-beat command (defined in host template) de-registers host
