2.2 on fedora 3

Don Alfredo don.alfredo at pandora.be
Tue Dec 21 01:42:29 CET 2004


On Friday 17 December 2004 17:23, Lance Haig wrote:
.....
> What would be the right order to start populating the files to create a
> working version?
>
> I have created the system entry so my "Nagios" server is defined but I
> would like to add a new device so that I can start nagios.
.........
 try this perl script : 
i found it somewhere at skolelinux.no as part of a student project. Modified 
it a little - (i've checked, its GPL'd , but was not signed by the authors, 
so whoever wrote it, thanks !) - to remove some minor errors, and changed 
paths to the places where SuSE rpm's install nagios
The script  will create a basic config, or scan your local network (change the 
ip ranges in the script to match yours, you may also uncomment the "services" 
part, which i didn't have a need for)
So, don't forget : this is based on SusE , your install most likely will have 
other paths...
I will be using this perl thingie as a base to roll-out installs. 
The write-ups i intend to do include split configurations, and defaults for 
(mostly windoze) servers, when its finished i will post it here or on 
nagiosexchange.org

-----------
#!/usr/bin/perl -w
#======================================
use IO::Socket::INET;
use Net::Ping;
use strict;
#======================================
my(@hosts, @ports);
my($hostname, $servicedescription, $checkcommand, $maxcheckattempts, 
$normalcheckinterval, $retrycheckinterval);
my($checkperiod, $notificationinterval, $notificationperiod, 
$notificationoptions, $contactgroups);
my($alias, $address, $contactname, $hostnotificationperiod, 
$servicenotificationperiod);
my($hostnotificationoptions, $servicenotificationoptions, $contactgroupname, 
$members, $commandname, $commandline);
my($i, $nav, $choice, $host, $port, $sock, $email, $pager, @contacts, 
$contact, $user, $prefix);
#======================================
@hosts = qw(192.168.25.0);
@ports = qw(21 22 25 53 80 110 143);
$nav = Net::Ping->new("icmp", 0.6);
#======================================
format SERVICEDEFINITION =
define service {
        use                             generic-service
        
        host_name                       
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $hostname
        service_description             
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $servicedescription
        check_command                   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $checkcommand
        max_check_attempts              @<<<<<<<<<<<<<
        $maxcheckattempts
        normal_check_interval           @<<<<<<<<<<<<<
        $normalcheckinterval
        retry_check_interval            @<<<<<<<<<<<<<
        $retrycheckinterval
        check_period                    @<<<<<<<<<<<<<
        $checkperiod
        notification_interval           @<<<<<<<<<<<<<
        $notificationinterval
        notification_period             @<<<<<<<<<<<<<
        $notificationperiod
        notification_options            @<<<<<<<<<<<<<
        $notificationoptions
        }

.
format HOSTDEFINITION =
define host {
        use                             generic-host
        
        host_name                       
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $hostname
        alias                           
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $alias
        address                         @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $address
        max_check_attempts              @<<<<<<<<<<<<<
        $maxcheckattempts  
        notification_interval           @<<<<<<<<<<<<<
        $notificationinterval
        notification_period             @<<<<<<<<<<<<<
        $notificationperiod  
        notification_options            @<<<<<<<<<<<<<
        $notificationoptions
        }

.
format CONTACTDEFINITION =
define contact {
        contact_name                    
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $contactname
        alias                           
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $alias
        host_notification_period        @<<<<<<<<<<<<<
        $hostnotificationperiod
        service_notification_period     @<<<<<<<<<<<<<
        $servicenotificationperiod
        host_notification_options       @<<<<<<<<<<<<<
        $hostnotificationoptions
        notification_options    @<<<<<<<<<<<<<
        $servicenotificationoptions
        email                           
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $email
        pager                           
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $pager
        }

.
format CONTACTGROUPDEFINITION =
define contactgroup {
        contactgroup_name               
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $contactgroupname
        alias                           
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $alias
        members                         
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $members
        }

.
format COMMANDDEFINITION =
define command {
        command_name                    @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $commandname
        command_line                    
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $commandline
        }

.
#======================================
print "==================================================\n";
$prefix = inpstr("Enter a path to where the configuration files will be stored 
[default=/etc/nagios]: ", "/etc/nagios");
if(! -d $prefix) {
        $choice = choose("Directory $prefix does not exist, should I create 
it? [default=yes]: ", "yes");
        if($choice eq 'yes') { 
                system("mkdir -p $prefix") == 0 || die "Unable to create 
directory $prefix\n";
        }
        else { die "Exiting configuration of nagios plugins\n"; }
}

open(SERVICES, "> $prefix/services.cfg") || die("Unable to open 
$prefix/services.cfg\n");
print SERVICES "
# Generic service definition template
define service{
name                            generic-service ; The 'name' of this service 
template, referenced in other service definitions
active_checks_enabled           0       ; Active service checks are enabled
passive_checks_enabled          0       ; Passive service checks are 
enabled/accepted
parallelize_check               1       ; Active service checks should be 
parallelized (disabling this can lead to major performance problems)
obsess_over_service             1       ; We should obsess over this service 
(if necessary)
check_freshness                 0       ; Default is to NOT check service 
'freshness'
notifications_enabled           0       ; Service notifications are enabled
event_handler_enabled           0       ; Service event handler is enabled
flap_detection_enabled          0       ; Flap detection is enabled
process_perf_data               1       ; Process performance data
retain_status_information       1       ; Retain status information across 
program restarts
retain_nonstatus_information    1       ; Retain non-status information across 
program restarts\n
register                        0       ; DONT REGISTER THIS DEFINITION - ITS 
NOT A REAL SERVICE, JUST A TEMPLATE!
}\n\n";
close(SERVICES);

open(HOSTS, "> $prefix/hosts.cfg") || die("Unable to open 
$prefix/hosts.cfg\n");
print HOSTS "
# Generic host definition template
define host{
name                    generic-host    ; The name of this host template - 
referenced in other host definitions, used for template recursion/resolution
notifications_enabled           0       ; Host notifications are enabled
event_handler_enabled           0       ; Host event handler is enabled
flap_detection_enabled          0       ; Flap detection is enabled
process_perf_data               1       ; Process performance data
retain_status_information       1       ; Retain status information across 
program restarts
retain_nonstatus_information    1       ; Retain non-status information across 
program restarts\n
register                        0       ; DONT REGISTER THIS DEFINITION - ITS 
NOT A REAL HOST, JUST A TEMPLATE!
}\n\n";
close(HOSTS);

# We dont use this, checkcommands should already be defined elsewhere
# # set up checkcommands.cfg
# init_defaults();
# open(COMMANDDEFINITION, ">> $prefix/checkcommands.cfg") || die("Unable to 
open $prefix/checkcommands.cfg\n");
# $commandname = "check_ftp";
# $commandline = "/usr/lib/nagios/plugins/check_ftp -H \$HOSTADDRESS\$";
# write COMMANDDEFINITION;
# $commandname = "check_ssh";
# $commandline = "/usr/lib/nagios/plugins/check_ssh -H \$HOSTADDRESS\$";
# write COMMANDDEFINITION;
# $commandname = "check_smtp";
# $commandline = "/usr/lib/nagios/plugins/check_smtp -H \$HOSTADDRESS\$";
# write COMMANDDEFINITION;
# $commandname = "check_http";
# $commandline = "/usr/lib/nagios/plugins/check_http -H \$HOSTADDRESS\$";
# write COMMANDDEFINITION;
# $commandname = "check_pop";
# $commandline = "/usr/lib/nagios/plugins/check_pop -H \$HOSTADDRESS\$";
# write COMMANDDEFINITION;
# $commandname = "check_imap";
# $commandline = "/usr/lib/nagios/plugins/check_imap -H \$HOSTADDRESS\$";
# write COMMANDDEFINITION;
# $commandname = "check_dns";
# $commandline = "/usr/lib/nagios/plugins/check_dns -H \$HOSTADDRESS\$";
# write COMMANDDEFINITION;
# $commandname = "check_disk";
# $commandline = "/usr/lib/nagios/plugins/check_disk -w 20% -c 5%";
# write COMMANDDEFINITION;
# close(COMMANDDEFINITION);

print "==================================================\n";
init_defaults();
$choice = choose("Do you want to monitor any servers/workstations? 
[default=yes]: ", "yes");
if($choice eq 'yes') {
        init_defaults();
        print "Scanning for server hosts, please wait...\n";
        for($i=1; $i<254; $i++) {
                $host = "192.168.25." . "$i";
                if($nav->ping($host)) {
                        print "Found server $host\n"; 
                        $choice = choose("Do you want me to auto configure 
this server? [default=yes]: ", "yes");
                        if($choice eq 'yes') {
                                config_host($host);
                                config_alive($host);
                                foreach $port (@ports) {
                                        if($sock = 
IO::Socket::INET->new(PeerAddr=>$host, PeerPort=>$port, Proto=>"tcp", 
Timeout=>0.6)) {
                                                config_service($host, $port);
                                                close($sock);
                                        }
                                }
                        }
                        else { print "Skipping $host\n"; }
                }
        }
}

init_defaults();
$choice = choose("Do you want to check if passive hosts is alive? (hubs, 
switches, firewalls, etc) [default=yes]: ", "yes");
if($choice eq 'yes') {
        print "Scanning for passive hosts, please wait...\n";
        foreach $host (@hosts) {
                if($nav->ping($host)) {
                        config_host($host);
                        config_alive($host);
                }
        }
}
else { print "Skipping passive hosts\n"; }

$nav->close();

print "==================================================\n";
init_defaults();
open(CONTACTDEFINITION, "> $prefix/contacts.cfg") || die("Unable to open 
$prefix/contacts.cfg");
while(1) {
        $contactname = inpstr("Enter a contact username [default=$user][q to 
quit]: ", "$user");
        if($contactname eq 'q') { last; }
        push(@contacts, $contactname);
        $user = $contactname;
        $alias = inpstr("Enter an alias for this contact [default=none]: ", 
"none");
        $email = inpstr("Enter an email address for this contact 
[default=$user\@localhost]: ", "$user\@localhost");
        $pager = inpstr("Enter a pager address for this contact 
[default=$user\@localhost]: ", "$user\@localhost");
        write CONTACTDEFINITION;
}
close(CONTACTDEFINITION);

print "==================================================\n";
init_defaults();
$contactgroupname = "nagios-admins";
$alias = "Admin group";
$members = join(",", @contacts);
open(CONTACTGROUPDEFINITION, "> $prefix/contactgroups.cfg") || die("Unable to 
open $prefix/contactgroups.cfg");
write CONTACTGROUPDEFINITION;
close(CONTACTGROUPDEFINITION);

print "Nagios configuration files has been generated succsessfully\n";
$choice = choose("Do you want me to run nagios error report? [default=yes]: ", 
"yes");
if($choice eq 'yes') {
        if(-x "/usr/sbin/nagios" && -f "/etc/nagios/nagios.cfg") {
                system("/usr/sbin/nagios -v /etc/nagios/nagios.cfg") == 0 || 
die("Unable to run nagios report\n");
        }
        else { print "nagios does not appear to be installed!\nSkipping nagios 
error report\n"; }
}
else { print "Skipping nagios error report\n"; }

$choice = choose("Do you want me to restart nagios? [default=yes]: ", "yes");
if($choice eq 'yes') {
        if(-x "/etc/init.d/nagios") {
                system("/etc/init.d/nagios restart") == 0 || die("Unable to 
restart nagios\n");
        }
        else { print "nagios does not appear to be installed!\nSkipping 
restart of nagios\n"; }
}
else { print "Skipping restart of nagios\n"; }

# running apache2 ? change it here,d dude...
$choice = choose("Do you want me to restart apache? [default=yes]: ", "yes");
if($choice eq 'yes') {
        if(-x "/etc/init.d/apache") {
                system("/etc/init.d/apache restart") == 0 || die("Unable to 
restart apache\n");
        }
        else { print "apache does not appear to be installed!\nSkipping 
restart of apache\n"; }
}
else { print "Skipping restart of apache\n"; }

print "==================================================\n";
#======================================
sub init_defaults
{       
        $maxcheckattempts = 3;
        $normalcheckinterval = $retrycheckinterval = 5;
        $checkperiod = $notificationperiod = $hostnotificationperiod = 
$servicenotificationperiod = "24x7";
        $notificationinterval = 30;
        $notificationoptions = "w,c,r";
        $contactgroups = "nagios-admins";
        $alias = "none";
        $address = "127.0.0.1";
        $contactname = $members = $user = getlogin();
        $hostnotificationoptions = $servicenotificationoptions = "w,c,r";
        $contactgroupname = "nagios-group";
        $commandname = "check_ping";
        $commandline = "/usr/lib/nagios/plugins/check_ping -H \$HOSTADDRESS\$ 
-w 100,20% -c 500,60%";
        $email = "root\@localhost";
        $pager = "555-5555\@pagergateway.localhost.localdomain";
}
#======================================
sub choose
{
        my($choice);
        print $_[0];
	$choice=<STDIN>;
        chomp($choice);
	if(!length($choice)) { $choice = $_[1]; }
        if($choice eq 'no' || $choice eq 'NO') { return 'no'; }
        else { return 'yes'; }
}
#======================================
sub inpstr
{
        my($str);
        print $_[0];
	$str=<STDIN>;
        chomp($str);
	if(!length($str)) { $str = $_[1]; }
        return $str;
}
#======================================
sub config_service
{
        my($new_host, $new_port) = @_;

       # $hostname = $new_host;
        open(SERVICEDEFINITION, ">> $prefix/services.cfg") || die("Unable to 
open $prefix/services.cfg\n");
        
        if($new_port eq '21') { # ftp
                print "Setting up ftp plugin for host $new_host\n";
                $servicedescription = "FTP service";
                $checkcommand = "check_ftp";
                write(SERVICEDEFINITION);
        }
        elsif($new_port eq '22') { # ssh
                print "Setting up ssh plugin for host $new_host\n";
                $servicedescription = "SSH service";
                $checkcommand = "check_ssh";
                write(SERVICEDEFINITION);
        }
        elsif($new_port eq '25') { # smtp
                print "Setting up smtp plugin for host $new_host\n";
                $servicedescription = "SMTP service";
                $checkcommand = "check_smtp";
                write(SERVICEDEFINITION);
        }
        elsif($new_port eq '53') { # dns
                print "Setting up dns plugin for host $new_host\n";
                $servicedescription = "DNS service";
                $checkcommand = "check_dns";
                write(SERVICEDEFINITION);
        }
        elsif($new_port eq '80') { # http
                print "Setting up http plugin for host $new_host\n";
                $servicedescription = "HTTP service";
                $checkcommand = "check_http";
                write(SERVICEDEFINITION);
        }
        elsif($new_port eq '110') { # pop
                print "Setting up pop plugin for host $new_host\n";
                $servicedescription = "POP service";
                $checkcommand = "check_pop";
                write(SERVICEDEFINITION);
        }
        elsif($new_port eq '143') { # imap
                print "Setting up imap plugin for host $new_host\n";
                $servicedescription = "IMAP service";
                $checkcommand = "check_imap";
                write(SERVICEDEFINITION);
        }
        else { print "Sorry, no support for service on port $new_port\n"; }
        close(SERVICEDEFINITION);
}
#======================================
sub config_alive
{
        my($new_host) = $_[0];
        print "Setting up ping plugin for $new_host\n";
        #$hostname = $new_host;
        $servicedescription = "PING service";
        $checkcommand = "check_ping";
        open(SERVICEDEFINITION, ">> $prefix/services.cfg") || die("Unable to 
open $prefix/services.cfg\n");
        write(SERVICEDEFINITION);
        close(SERVICEDEFINITION);
}
#==================================$email
sub config_host
{
        my($new_host) = $_[0];
        print "Setting up host information for $new_host\n";
        $address = $new_host;
        $hostname = gethostbyaddr($new_host, 2);
	$hostname = inpstr("Enter a hostname : ",$hostname);
        $hostname = $new_host unless defined($hostname);
        $alias = inpstr("Enter an alias for this host [default=none]: ", 
"none");
        open(HOSTDEFINITION, ">> $prefix/hosts.cfg") || die("Unable to open 
$prefix/hosts.cfg\n");
        write(HOSTDEFINITION);
        close(HOSTDEFINITION);
}
#======================================



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null





More information about the Users mailing list