#!/usr/bin/perl -w
#


### Nagios Bulk Import (nbi.pl)
#
#This perl script will take a nmap generated "grepable" file and create a host file for nagios
# nmap <IP Range> -s[S|T|A|W] -O -oG <FILENAME>
# ./nbi.pl <FILENAME>
# You need to add the service.cfg, command.cfg & the hostgroups.cfg below to your existing files
# 
# Created on Jan 16, 2012 by ldecker (ldecker@gmail.com)
# License: GPL
# No warranties in any way given.
# 
# CHANGELOG
# Revision 1.0  2012/01/16 14:46:05 ldecker
# initial version
# 
# 
# 
# 


use strict;
use warnings;

my(@dataseg, @tempseg, $recfile, $recline, $out1, $elem, $i, $portflg, $osflg);
my($hostname, $hostaddr, $hostos, $hostgrps, $openptrport, $tempfile, $outhost);

sub init
{
 if( $#ARGV eq -1)
 {
  print "nbi.pl <(nmap <IP Range> -s[S|T|A] -O -oG) INPUT FILE>\n";
  exit;
 }
 else
 {
  $recfile=$ARGV[0];
  $outhost="hosts.cfg";
  $tempfile="tempfile$$";
  system("rm $outhost tempfile*");
# This grep statement removes all the single "Up" lines and replaces the () with ^ so the hostname will equal the hostIP - there was no DNS entry
  system("grep -v Up $recfile | sed  \"s/\(\)/^/g\"  | sed  \"s/\(//g\" | sed \"s/\)//g\" | sed \"s/\\//\|/g\" | sed \"s/\|\|\|/\|/g\" | sed \"s/\|\|/\|/g\"  | sed \"s/\t/ /g\" | grep -i -v nmap > $tempfile");
 }
}

sub read_nmap
{
 open(OUTHOST,"> $outhost");
 open(RECFILE,"< $tempfile");
 while($recline=<RECFILE>)
{
  if(length($recline)>0)
  {
   chomp($recline);
   @dataseg = split(/ /,$recline);
   $elem = @dataseg;
   $openptrport=0;
   $portflg=0;
   $osflg=0;
   $hostgrps="";
   $hostos="";
   $hostaddr=$dataseg[1];
# Set the hostname equal to the hostIP - there was no DNS entry
   $hostname=($dataseg[2] eq '^')?$dataseg[1]:$dataseg[2];
   for($i=2;$i<$elem;$i++)
   {
    if($dataseg[$i] eq "Ports:") { $osflg=0;$portflg=1; }
    elsif($dataseg[$i] eq "OS:") { $portflg=0;$osflg=1; }
    elsif($portflg)
    {
     if($dataseg[$i] =~ /open/)
     {
      @tempseg = split(/\|/,$dataseg[$i]);
# This switch block is used to setup the hostgroup membership based on the ports that were
# found open during the nmap run
      SWITCH: {
      if ($tempseg[0] eq 21)   { $hostgrps = $hostgrps."ftp, "; last SWITCH; }
      if ($tempseg[0] eq 22)   { $hostgrps = $hostgrps."ssh, "; last SWITCH; }
      if ($tempseg[0] eq 23)   { $hostgrps = $hostgrps."telnet, "; last SWITCH; }
      if ($tempseg[0] eq 25)   { $hostgrps = $hostgrps."smtp, "; last SWITCH; }
      if ($tempseg[0] eq 53)   { $hostgrps = $hostgrps."dns, "; last SWITCH; }
      if ($tempseg[0] eq 80)   { $hostgrps = $hostgrps."http, "; last SWITCH; }
      if ($tempseg[0] eq 443)  { $hostgrps = $hostgrps."https, "; last SWITCH; }
      if ($tempseg[0] eq 515)  { $hostgrps = $hostgrps."lpd, "; last SWITCH; }
      if ($tempseg[0] eq 631)  { $hostgrps = $hostgrps."ipp, "; last SWITCH; }
      if ($tempseg[0] eq 1433) { $hostgrps = $hostgrps."mssql, "; last SWITCH; }
      if ($tempseg[0] eq 3306) { $hostgrps = $hostgrps."mysql, "; last SWITCH; }
      if ($tempseg[0] eq 3389) { $hostgrps = $hostgrps."termsrv, "; last SWITCH; }
# Remd this port out until I can test correctly VNC
#      if ($tempseg[0] =~/5900/) { $hostgrps = $hostgrps."vnc, "; last SWITCH; }
# Put a switch here because windows shows 9100 port open but I couldn't get an answer
      if ($tempseg[0] =~/9100/) { $openptrport=1; last SWITCH; }
# Template
#      if ($tempseg[0] =~/<port>/) { $hostgrps = $hostgrps."<hostgroupname "; last SWITCH; }
      }
     }   
    }
    elsif($osflg)
    {
     if($dataseg[$i] =~ /Linux|HP-UX|NetBSD|Solaris/i) { $hostos="linux-server"; $hostgrps = $hostgrps."linux "; $osflg=0; }
     elsif($dataseg[$i] =~ /Microsoft/i)               { $hostos="windows-server"; $hostgrps = $hostgrps."windows "; $osflg=0; }
     elsif($dataseg[$i] =~ /Cisco/i)                   { $hostos="generic-switch"; $hostgrps = $hostgrps."switches "; }
     elsif($dataseg[$i] =~ /Aironet/i)                 { $hostos="generic-switch"; $hostgrps = "aironet, switches "; $osflg=0; }
     elsif($dataseg[$i] =~ /printer/i)                 { $hostos="generic-printer";$hostgrps = $openptrport ? $hostgrps."jdir, printers ": $hostgrps."printers "; $osflg=0; }
     elsif($dataseg[$i] =~ /APC/i)                     { $hostos="generic-device"; $hostgrps = $hostgrps."ups "; $osflg=0; }
     elsif($dataseg[$i] =~ /:/)                        { $osflg=0; }
    }
   }
# Default host OS if no host OS was found
   if($hostos eq "") { $hostos="windows-server"; $hostgrps = $hostgrps."windows "; }
   $out1=sprintf("
define host{
            use %s
            host_name %s
            address %s
            hostgroups %s
            }\n", $hostos, $hostname, $hostaddr, $hostgrps);
   print OUTHOST $out1;
  }
 }
 close(OUTHOST);
 close(RECFILE);
}

&init;
&read_nmap;

### hostgroups.cfg ###
#define hostgroup{
# hostgroup_name linux         
# alias Linux Servers
#}
# 
#define hostgroup{
# hostgroup_name windows
# alias Window Servers
#}
# 
#define hostgroup{
# hostgroup_name switches
# alias Network Switches
#}
#
#define hostgroup{
# hostgroup_name printers
# alias Network Printers
#}
#
#define hostgroup{
# hostgroup_name ups
# alias UPS
#}
#
#define hostgroup {
# hostgroup_name ftp
# alias FTP Service
#}
# 
#define hostgroup {
# hostgroup_name ssh
# alias SSH Service
#}
# 
#define hostgroup {
# hostgroup_name telnet
# alias Telnet Service
#}
# 
#define hostgroup {
# hostgroup_name smtp
# alias Email Service
#}
#
#define hostgroup {
# hostgroup_name dns
# alias DNS Service
#}
# 
#define hostgroup {
# hostgroup_name http 
# alias Web Service
#}
#
#define hostgroup {
# hostgroup_name https
# alias Secure Web Service
#}
# 
#define hostgroup {
# hostgroup_name lpd
# alias Unix Print Service
#}
# 
#define hostgroup {
# hostgroup_name ipp
# alias Internet Printing
#}
# 
#define hostgroup {
# hostgroup_name mssql
# alias MS-SQL Service
#}
# 
#define hostgroup {
# hostgroup_name mysql
# alias mysql Service
#}
# 
#define hostgroup {
# hostgroup_name termsrv
# alias Term Service
#}
# 
#define hostgroup {
# hostgroup_name jdir
# alias JetDirect Service
#}
# 
#define hostgroup {
# hostgroup_name vnc
# alias VNC Service
#}
# 
#define hostgroup {
# hostgroup_name aironet
# alias Cisco Aironet
#}
### hostgroups.cfg ###



### service.cfg ###
#define service{
#        use local-service
#        host_name *
#        service_description PING
#        check_command check_ping!100.0,20%!500.0,60%
#        }
#
## Define a service to check SSH
#
#define service{
#        use local-service
#        hostgroup_name                  ssh
#        service_description             SSH
#        servicegroups                   ssh
#        check_command			check_ssh
#        }
#
## Define a service to check HTTP
#
#define service{
#        use local-service
#        hostgroup_name                  http
#        service_description             HTTP
#        servicegroups                   http
#        check_command			check_http
#        }
#
## Define a service to check HTTPS
#
#define service{
#        use local-service
#        hostgroup_name                  https
#        service_description             HTTPS
#        servicegroups                   https
#        check_command			check_https
#        }
#
## Define a service to check FTP
#
#define service{
#        use local-service
#        hostgroup_name                  ftp
#        service_description             FTP
#        servicegroups                   ftp
#        check_command			check_ftp
#        }
#
## Define a service to check Terminal Services
#
#define service{
#        use local-service
#        hostgroup_name                  termsrv
#        service_description             TermSrv
#        servicegroups                   termsrv
#        check_command			check_x224
#        }
#
## Define a service to check DNS
#
#define service{
#        use local-service
#        hostgroup_name                  dns
#        service_description             DNS
#        servicegroups                   dns
#        check_command			check_dns
#        }
#
## Define a service to check VNC
#
##define service{
##        use local-service
##        hostgroup_name                  vnc
##        service_description             VNC
##        servicegroups                   vnc
##        check_command			check_http
##        }
#
## Define a service for Jetdirect
#
#define service{
#        use local-service
#        hostgroup_name                  jdir
#        service_description             JetDirect
#        servicegroups                   jdir
#        check_command                   check_hpjd
#        }
#
#define service{
#        use local-service
#        hostgroup_name                  aironet
#        service_description             Aironet
#        servicegroups                   aironet
#        check_command                   check_airo
#        }
#
### service.cfg ###



### command.cfg ###
## 'check_ftp' command definition
#define command{
#        command_name    check_ftp
#        command_line    $USER1$/check_ftp -H $HOSTADDRESS$ -r crit
#        }
#
#
## 'check_hpjd' command definition
#define command{
#        command_name    check_hpjd
#        command_line    $USER1$/check_tcp -H $HOSTADDRESS$ -p 9100
#        }
#
#
## 'check_http' command definition
#define command{
#        command_name    check_http
#        command_line    $USER1$/check_http -I $HOSTADDRESS$ -p80 -e HTTP/,DOCTYPE
#        }
#
# 'check_https' command definition
#define command{
#        command_name    check_https
#        command_line    $USER1$/check_tcp -H $HOSTADDRESS$ -p 443
#        }
#
## 'check_airo' command definition
#define command{
#        command_name    check_airo
#        command_line    $USER1$/check_tcp -H $HOSTADDRESS$ -p 22
#        }
#
## 'check_ssh' command definition
#define command{
#	command_name	check_ssh
#	command_line	$USER1$/check_ssh $ARG1$ $HOSTADDRESS$
#	}
#
#
## 'check_dns' command definition
#define command{
#        command_name    check_dns
#        command_line    $USER1$/check_dns -s $HOSTADDRESS$ www.yahoo.com
#        }
#
#
## 'check_x224' command definition
#define command{
#        command_name    check_x224
#        command_line    $USER1$/check_x224 -H $HOSTADDRESS$ $ARG1$
#        }
### command.cfg



### servicegroups.cfg
#define servicegroup {
# servicegroup_name ftp
# alias FTP Service
#}
# 
#define servicegroup {
# servicegroup_name ssh
# alias SSH Service
#}
# 
#define servicegroup {
# servicegroup_name telnet
# alias Telnet Service
#}
# 
#define servicegroup {
# servicegroup_name smtp
# alias Email Service
#}
#
#define servicegroup {
# servicegroup_name dns
# alias DNS Service
#}
# 
#define servicegroup {
# servicegroup_name http 
# alias Web Service
#}
#
#define servicegroup {
# servicegroup_name https
# alias Secure Web Service
#}
# 
#define servicegroup {
# servicegroup_name lpd
# alias Unix Print Service
#}
# 
#define servicegroup {
# servicegroup_name ipp
# alias Internet Printing
#}
# 
#define servicegroup {
# servicegroup_name mssql
# alias MS-SQL Service
#}
# 
#define servicegroup {
# servicegroup_name mysql
# alias mysql Service
#}
# 
#define servicegroup {
# servicegroup_name termsrv
# alias Term Service
#}
# 
#define servicegroup {
# servicegroup_name jdir
# alias JetDirect Service
#}
# 
#define servicegroup {
# servicegroup_name vnc
# alias VNC Service
#}
# 
#define servicegroup {
# servicegroup_name aironet
# alias Cisco Aironet
#}
### servicegroups.cfg
