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