1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#!/usr/local/bin/perl
# File ID: $Id$
# Last Change: $LastChangedDate$
# Revision: $Rev$
use Nagios::Config;
use Nagios::StatusLog;
use Benchmark ':hireswallclock';
use Getopt::Std;
=head1 NAME
config_status_demo.pl - demonstrate using Nagios::Config and Nagios::StatusLog together.
=head1 USAGE
perl config_status_demo.pl -c /etc/opt/nagios/nagios.cfg -l /var/opt/nagios/status.log
=head1 NOTES
Please send the benchmark outputs to duncs@cpan.org so I can see how the
performance is on boxes and configs other than my own.
This setup is very sensitive to mistmatches between the configuration and the status log.
=cut
our ( $opt_c, $opt_l ) = ();
getopt('c:l:');
die "Must specify location of Nagios configuration with -c option."
if ( !$opt_c );
die "Must specify location of Nagios status log with -l option."
if ( !$opt_l );
my $bench1 = Benchmark->new;
my $cf = Nagios::Config->new( Filename => $opt_c );
my $bench2 = Benchmark->new;
my $log = Nagios::StatusLog->new( Filename => $opt_l );
my $bench10 = Benchmark->new;
foreach my $h ( $cf->list_hosts ) {
next if ( !length $h->host_name ); # avoid a bug in Nagios::Object
foreach my $s ( $h->list_services ) {
my $svcs = $log->service( $h, $s );
if ( $svcs->status ne 'OK' ) { # only print for service not in OK
# comment out the if () { } to print everything
printf "Service %s on %s has status of %s\n",
$s->service_description,
$h->host_name,
$svcs->status;
}
}
}
my $bench11 = Benchmark->new;
printf
"\nTime to parse: %s\nTime to parse Logfile: %s\nTime to print: %s\n\n",
timestr( timediff( $bench2, $bench1 ) ),
timestr( timediff( $bench10, $bench2 ) ),
timestr( timediff( $bench11, $bench10 ) );
|