#!/usr/bin/perl
# Written by Alexander SIlveröhrt 2013-04-03
# If you have any suggestions for improvments or new features
# Or just want to complain about some nasty unforseen bug.
# The please drop me a line. Cocoon.is@gmail.com
#
# default Input file example
#10.9.0.2 host1
#10.9.0.3 host2
#10.9.0.4 host3
#10.9.0.5 host4
# If using flag --columnorder then file columns are reversed and file needed would look like
#host1 10.9.0.2
#host2 10.9.0.3
#host3 10.9.0.4
#host4 10.9.0.5

use strict;
use Getopt::Long;

our($SCRIPT, $VERSION, %OPTS, $infile_flag, $c_order_flag, $outfile_flag, $h_group_flag, $delimiter_flag, $h_flag, $d_flag, $v_flag);


$SCRIPT = "generate_nagios_cfgfile_from_ip_file.pl";
$VERSION = "1.0.1";

GetOptions ("i|I|inf|INF|infile|INFILE:s" => \$infile_flag,
		"c|C|column|COLUMN|columnorder|COLUMNORDER!" => \$c_order_flag,
                "o|O|out|OUT|outfile|OUTFILE:s" => \$outfile_flag,
		"ho|HO|host|HOST|hostgroup|HOSTGROUP:s" => \$h_group_flag,
		"del|DEL|delimiter|DELIMITER:s" => \$delimiter_flag,
                "h|H|help" => \$h_flag,
                "d|debug" => \$d_flag);

my $INFILE = $infile_flag;
my $OUTFILE = $outfile_flag;
my $HOSTGROUP = $h_group_flag;
my $DELIMITER = $delimiter_flag;
my $COLUMNORDER = $c_order_flag;
my $VERSIONFLAG = $v_flag;
my $HELP = $h_flag;
my $DEBUG = $d_flag;


my $COLUMNORDER_IP;
my $COLUMNORDER_HOST;

##############################
### Some Default behaviour ###
##############################
if ($VERSIONFLAG)
{
        printf ("$VERSION\n");
        exit;
} 
if ($HELP)
{
	_usage();
}
if (!$INFILE)
{
        if ($DEBUG)
        {
                printf("\nNo INFILE passed into the program. We can't continue without it..Exiting!\n");
        }
	_usage();
}
if (!$OUTFILE)
{
        if ($DEBUG)
        {
                printf("\nNo OUTFILE passed into the program. We will use default OUTFILE \"myoutfile.cfg\"\n");
        }
        _usage();
}
if (!$HOSTGROUP)
{
	if ($DEBUG)
	{
		printf("\nNo HOSTGROUP passed into the program. We will use default HOSTGROUP: \"my_hostgroup\"\n");
	}
	$HOSTGROUP = "my_hostgroup";
}
if (!$DELIMITER)
{
        if ($DEBUG)
        {
                printf("\nNo DELIMITER passed into the program. We will use default DELIMITER: space \" \"\n");
        }
	$DELIMITER = " ";
}
if (!$COLUMNORDER)
{
        if ($DEBUG)
        {
                printf("\nNo COLUMNORDER passed into the program COLUMNORDER = $COLUMNORDER. We will use default COLUMNORDER: IP And HOST\n");
        }
	$COLUMNORDER_IP = 0;
	$COLUMNORDER_HOST = 1;
}
else
{
        if ($DEBUG)
        {
                printf("\nCOLUMNORDER change passed into the program COLUMNORDER = $COLUMNORDER. We will reverse COLUMNORDER to: HOST and IP\n");
        }
	$COLUMNORDER_IP = 1;
	$COLUMNORDER_HOST = 0;
}

### Main ###
if ($DEBUG)
{
	printf("\nINFILE = $INFILE, OUTFILE = $OUTFILE\nHOSTGROUP = $HOSTGROUP, DELIMITER = $DELIMITER\nCOLUMNORDER = $COLUMNORDER, COLUMNORDER_IP = $COLUMNORDER_IP\nCOLUMNORDER_HOST = $COLUMNORDER_HOST, HELP = $HELP, VERSION = $VERSION");
}
_createNagiosFile($INFILE, $OUTFILE);

### END MAIN ###
sub _createNagiosFile
{
	my $IN_FILE_PATH = "$_[0]";
	my $OUT_FILE_PATH = "$_[1]";	

	if ($DEBUG)
	{
		printf ("\nIN function: _createNagiosFile.  INFILE = $INFILE, OUTFILE = $OUTFILE");
	}

        open IP_HOST_FILE, "<$_[0]" or die $!;
	open NAGIOSFILE, ">>$_[1]" or die $!;

        my @ip_host_FileLineToColumnSplit;
        my @readLine;
        my @IPs;
	my @HOSTNAMEs;
	my $LINE_OF_HOSTNAMEs;
        my $i = 0;

print NAGIOSFILE << "LABEL";
###################################
###	Host definitions here	###
###################################

LABEL

        while ($readLine[$i] = <IP_HOST_FILE>)
        {
	        if ($DEBUG)
        	{
			printf ("\nreadLine[$i] = $readLine[$i]");
		}

                ### First split it up into 2 columns based on passed in delimiter or default "space" ###
                @ip_host_FileLineToColumnSplit = split(/$DELIMITER/, $readLine[$i]);

	        if ($DEBUG)
        	{
			printf("\nip_host_FileLineToColumnSplit[0] = $ip_host_FileLineToColumnSplit[0],\nip_host_FileLineToColumnSplit[1] = $ip_host_FileLineToColumnSplit[1]");
		}

		$IPs[$i] = $ip_host_FileLineToColumnSplit[$COLUMNORDER_IP];
		$HOSTNAMEs[$i] = $ip_host_FileLineToColumnSplit[$COLUMNORDER_HOST];
		chomp $HOSTNAMEs[$i];
	        if ($DEBUG)
       	 	{
			printf("\nIPs[$i] = $IPs[$i]\nHOSTNAMEs[$i] = $HOSTNAMEs[$i]");
		}
### Print out the hosts to the file ###
print NAGIOSFILE << "LABEL";

define host{
        use             generic-switch
        host_name       $HOSTNAMEs[$i]
        alias           $HOSTNAMEs[$i]
        address         $IPs[$i]
	}
LABEL
        

                $i++;
        } ### <-- End while loop

	### Create a host "member" line with all hosts in $HOSTNAMEs ###
	$i = 0;
	while ($i < @HOSTNAMEs)
	{
		$LINE_OF_HOSTNAMEs .= ("$HOSTNAMEs[$i]" . ", ");
		$i++;
		if ($DEBUG)
		{
			printf("\ni = $i, LINE_OF_HOSTNAMEs = $LINE_OF_HOSTNAMEs");
		}
	}
	### Remove the last space " " and last comma "," from the line of hosts ###
	chop $LINE_OF_HOSTNAMEs;
	chop $LINE_OF_HOSTNAMEs;

	if ($DEBUG)
	{
		printf("\nLINE_OF_HOSTNAMEs = $LINE_OF_HOSTNAMEs");
	}

### Print out the hostgroup and respective hostgroup host member to the file ###
print NAGIOSFILE << "LABEL";
###################################
### Hostgroup definitions here  ###
###################################
define hostgroup{
	hostgroup_name	$HOSTGROUP
	alias		$HOSTGROUP
	members		$LINE_OF_HOSTNAMEs
	}
LABEL

### Print out the default ping service and respective service host members to the file.		###
### Remember that this service and generic_service may need to be adjusted to fit your system	###
print NAGIOSFILE << "LABEL";
###################################
###  Service definitions here   ###
###################################
define service{
	use			generic-service
	host_name		$LINE_OF_HOSTNAMEs
	service_description	PING
	check_command		check_ping!100.0,20%!500.0,60%
	}
LABEL

	### Lets finish up and close the files ###
        close  (IP_HOST_FILE);
	close (NAGIOSFILE);
}

### OUr nice help function ###
sub _usage
{
print << "USAGE"; 
\n$SCRIPT $VERSION
Usage: $SCRIPT -|--i|I|inf|INF|infile|INFILE <PATH to IP and HOst File> -|--o|O|out|OUT|outfile|OUTFILE <name of the file to be saved> -|--ho|HO|host|HOST|hostgroup|HOSTGROUP <name of hostgroup hosts will belong to> -|-- del|DEL|delimiter|DELIMITER <delimiter that separates the IP column from host column in the infile> -|--h|H|help -|--d|debug

	i.e
	./$SCRIPT -INFILE cisco_routers.txt -OUTFILE myrouters.cfg -HOSTGROUP Cisco_routers
	./$SCRIPT -In ip_hosts_tests.txt -O myrouters.cfg -HO Cisco_routers --delimiter ";" --debug
	./$SCRIPT -In --help
	./$SCRIPT -In --version

-|--i|I|inf|INF|infile|INFILE		This is the IP and HOST file to read from.
					DFEAULT It needs to have a format with 2 columns and where IP is the first and HOSTNAME is the second. Like:
					10.9.0.2 host1
					10.9.0.3 host2
					10.9.0.4 host3
					10.9.0.5 host4
					.
					.

					THIS column order can be changed by passing in the flag -|--COLUMNORDER. (see below --COLUMNORDER section for detailed help).
					If using flag --columnorder then INFILE columns are reversed and INFILE needed would look like:

					host1 10.9.0.2
					host2 10.9.0.3
					host3 10.9.0.4
					host4 10.9.0.5
					.
					.

					The delimiter or column separator between the 2 columns can be specified with the flag 	
					-|--delimiter (see below --delimiter section for detailed help).
					(NO DEFAULT FILE but default DELIMITER is space " " and DEFAULT COLUMN ORDER is IP and then HOST)

-|--o|O|out|OUT|outfile|OUTFILE		This needs the be the name of the resulting NAGIOS config file. This is the file that will contain all the hosts 
					and the hostgroup and default ping service for the hosts in above INFILE.
					(DEFAULT is myoutfile.cfg)

-|--ho|HO|host|HOST|hostgroup|HOSTGROUP	This needs the be the name of the resulting hostgroup_name in above OUTFILE.
					Default without a passed in hostgroup_name it will look like below:
					define hostgroup{
			        	hostgroup_name  my_hostgroup
				        alias           my_hostgroup
				        members         host1, host2, host3 etc.
        				}
					(DEFAULT HOSTGROUP is my_hostgroup) 

-|--del|DEL|delimiter|DELIMITER		If your IP and HOST INFILE is not separated with a space " " between the columns (IP and HOST column). 
					Then this is where you tell the program which separator should be used instead.

-|--c|C|column|COLUMN|columnorder|COLUMNORDER If your IP and HOST INFILE column format doesn't have IP column before HOST COLUMN. 
					Meaning that the column order is reversed with HOST column before IP column.
					Then this is the flag you need to pass on into the program. With this flag the needed file format will go from  
                                        DEFAULT format with 2 columns and where IP is the first and HOSTNAME is the second. Like:
                                        10.9.0.2 host1
                                        10.9.0.3 host2
                                        10.9.0.4 host3
                                        10.9.0.5 host4
                                        .
                                        .

					To 2 columns where HOSNAME is the first and IP is the second. Like:
                                        host1 10.9.0.2
                                        host2 10.9.0.3
                                        host3 10.9.0.4
                                        host4 10.9.0.5
					.
					.
			
					(DEFAULT is --NOCOLUMNORDER meaning column IP first and column HOST second)
					
-|--he|HE|help|HELP		Prints this help screen

-|--v|V|VERSION|version		Prints program version

-d|D|DEBUG|debug			Enable debugging (DEFAULT disabled)

					INFO: If you have any suggestions for improvements or new features
					Or just want to complain about some nasty unforseen bug.
					Then please drop me a line. Cocoon.is\@gmail.com
USAGE
   exit 1;
}
