#! /usr/bin/perl -w 

# Script to add new hosts to Nagios.
#
# By default creates host, hostgroup contact, contactgroup, extservice, apan def, httpd user and rrd for an apan-ping with link graphs.
#
# Usage: addhost.pl HOSTFILE
# where HOSTFILE is the path to a file with following format
#
# HOSTNAME,IP
#
# where HOSTNAME is the hostname of the SIT (from sitlist) and is also used for the hostgroup and 
# contact groups
# IP is the IP address
# 

# backup config files
`cp --reply=yes /etc/nagios/*.cfg /etc/nagios/bak`;
`cp --reply=yes /usr/share/nagios/apan/apan.cfg /etc/nagios/bak`;


open(INPUTFILE, $ARGV[0]);
open(HOSTFILE,  '>>/etc/nagios/hosts.cfg');
open(HOSTGROUPFILE, '>>/etc/nagios/hostgroups.cfg');
open(SERVICEFILE, '>>/etc/nagios/services.cfg');
open(EXTINFOFILE, '>>/etc/nagios/serviceextinfo.cfg');
open(CONTACTFILE,  '>>/etc/nagios/contacts.cfg');
open(CONTACTGROUPFILE,  '>>/etc/nagios/contactgroups.cfg');
open(APAN,  '>>/usr/share/nagios/apan/apan.cfg');

@INPUTFILE = <INPUTFILE>;
close(INPUTFILE);

foreach $line (@INPUTFILE) {	
	chomp($line);
	($HOSTNAME,$IP,$TEMPLATE) = split(/,/,$line);
	# Define configs to add to files

$contactname = lc($HOSTNAME);

$HOSTFILECONFIG = 
qq!define host{ 
	use			$TEMPLATE 
	host_name		$HOSTNAME   
	alias			$HOSTNAME terminal
	address			$IP
	} \n \n!;

$HOSTGROUPFILECONFIG =
qq!define hostgroup{
	hostgroup_name	$HOSTNAME
	alias		$HOSTNAME client access group
	contact_groups	$HOSTNAME
	members		$HOSTNAME
}\n \n!;

$SERVICEFILECONFIG =
qq!define service{
	use				ping-service		; Name of service template to use
	host_name			$HOSTNAME
	contact_groups			$HOSTNAME
	} \n \n!;

$CONTACTFILECONFIG =
qq!define contact{
	use		client-contact
	contact_name	$contactname
	alias		client access
	} \n \n!;

$CONTACTGROUPFILECONFIG =
qq!define contactgroup{
	contactgroup_name	$HOSTNAME
	alias			$HOSTNAME client access
	members			$contactname
	} \n \n!;

$EXTINFOFILECONFIG =
qq!define serviceextinfo{
use			apan-ping-templ	
host_name               $HOSTNAME
notes_url               /nagios/cgi-bin/apan.cgi?host=$HOSTNAME&service=PING
} \n \n!;

$APANCONFIG = 
qq!$HOSTNAME;PING;/usr/share/nagios/rrd/$HOSTNAME-ping.rrd;ping|loss;ping:AREA loss:AREA;Ping round-trip time;Seconds \n \n!;

	print(HOSTFILE $HOSTFILECONFIG);
	print(HOSTGROUPFILE $HOSTGROUPFILECONFIG);
	print(SERVICEFILE $SERVICEFILECONFIG);
	print(EXTINFOFILE $EXTINFOFILECONFIG);
	print(CONTACTFILE $CONTACTFILECONFIG);
	print(CONTACTGROUPFILE $CONTACTGROUPFILECONFIG);
	print(APAN $APANCONFIG);
	
	`htpasswd -b /etc/nagios/htpasswd.users $contactname dcsat`;
	`/usr/local/rrdtool-1.0.45/bin/rrdtool create /usr/share/nagios/rrd/$HOSTNAME-ping.rrd -s 120  DS:ping:GAUGE:600:U:U DS:loss:GAUGE:600:0:100 RRA:AVERAGE:0.5:1:50400 RRA:MIN:0.5:30:1440  RRA:MAX:0.5:30:1440 RRA:AVERAGE:0.5:30:1440`;
	`chown nagios /usr/share/nagios/rrd/$HOSTNAME-ping.rrd`;
	`chgrp nagios /usr/share/nagios/rrd/$HOSTNAME-ping.rrd`;
	`chmod g+r /usr/share/nagios/rrd/$HOSTNAME-ping.rrd`;

}

close(HOSTFILE);
close(SERVICEFILE);
close(EXTINFOFILE);
close(CONTACTFILE);
close(CONTACTGROUPFILE);
close(APAN);

exit;
