Brandon,

I took most of your comments and sample data and put it in the following 
program. Taking the data out of the program and parsing it from files and 
directories, might be a good exercise for you. Generally variables are best 
declared close to where they are first used. Since the scope of where $host is 
first used, is a different scope (using my) from where $state is first used. I 
declared them together, in an outer scope for consistency and flexibility (of 
the code). I had better whitespace in the program editor than in the email 
editor, that again is something you can adjust with blank lines and tabs. Let 
us know if any questions.

#!/usr/bin/perl
use strict;
use warnings;
use 5.012; # this script requires Perl 5.12 or greater
# parse_nagios.pl
=begin comment
I am attempting to parse a Nagios status.dat file. The sections I am
interested in are the "hoststatus" sections.  Any other section I want
to simply ignore.  Within the hoststatus sections are a few lines I am
looking for:  host_name=<whatever> and current_state=<whatever>, I'd
simply like to perform a task based on these "whatever"'s.  For example,
output lines such as:
some_host:1
another_host:0
Where some_host and another_host are host_name's from the file, and 1
and 0 are current_state's for the corresponding host.
=end comment
=cut
local $/ = "hoststatus {";   # change input record separator, start where 
interested in.
my ($host,$state);
while( <DATA> ) {
    # stop at end of section at "}"
    my $end = index($_, "}") ;
    my $section = substr($_, 0, $end) ; # section loses "}" ;
    # split section up by lines.
    my @lines=split /\n/, $section ;
    for my $line (@lines) {
        given ($line) {
            when (/host_name=(.*)/) {
                $host = $1 ;
            }
            when (/current_state=(.*)/) {
                $state = $1 ;
                say "$host:$state" ;
            }
        }
    }
}
__DATA__
info {
        created=1328049467
        version=3.2.3
        last_update_check=1328014790
        update_available=1
        last_version=3.2.3
        new_version=3.3.1
        }
programstatus {
        modified_host_attributes=0
        modified_service_attributes=0
        nagios_pid=1370
        daemon_mode=1
         (lots of stuff)
         }
hoststatus {
        host_name=some_host
        modified_attributes=0
        check_command=check-host-alive
        check_period=
        notification_period=24x7
        check_interval=5.000000
        retry_interval=1.000000
        event_handler=
        has_been_checked=1
        should_be_scheduled=1
        check_execution_time=0.012
        check_latency=0.246
        check_type=0
        current_state=1
        last_hard_state=0
        last_event_id=0
        current_event_id=0
        current_problem_id=0
        last_problem_id=0
        plugin_output=OK - 10.1.1.1.1: rta 1.822ms, lost 0%
        long_plugin_output=
        performance_data=rta=1.822ms;3000.000;5000.000;0; pl=0%;80;100;;
rtmax=1.822ms;;;; rtmin=1.822ms;;;;
        last_check=1328049317
        next_check=1328049627
        check_options=0
        current_attempt=1
        max_attempts=10
        state_type=1
        last_state_change=1328047050
        last_hard_state_change=1328047050
        last_time_up=1328049327
        last_time_down=0
        last_time_unreachable=0
        last_notification=0
        next_notification=0
        no_more_notifications=0
        current_notification_number=0
        current_notification_id=0
        notifications_enabled=1
        problem_has_been_acknowledged=0
        acknowledgement_type=0
        active_checks_enabled=1
        passive_checks_enabled=1
        event_handler_enabled=1
        flap_detection_enabled=1
        failure_prediction_enabled=1
        process_performance_data=1
        obsess_over_host=1
        last_update=1328049467
        is_flapping=0
        percent_state_change=0.00
        scheduled_downtime_depth=0
        }
hoststatus {
        host_name=another_host
        modified_attributes=0
        check_command=check-host-alive
        check_period=
        notification_period=24x7
        check_interval=5.000000
        retry_interval=1.000000
        event_handler=
        has_been_checked=1
        should_be_scheduled=1
        check_execution_time=10.011
        check_latency=0.268
        check_type=0
        current_state=0
        last_hard_state=0
        last_event_id=7
        current_event_id=8
        current_problem_id=3
        last_problem_id=1
        plugin_output=CRITICAL - 10.1.1.2: rta nan, lost 100%
        long_plugin_output=
        performance_data=rta=0.000ms;3000.000;5000.000;0; pl=100%;80;100;;
rtmax=0.000ms;;;; rtmin=0.000ms;;;;
        last_check=1328049417
        next_check=1328049497
        check_options=0
        current_attempt=3
        max_attempts=10
        state_type=0
        last_state_change=1328049437
        last_hard_state_change=1328049057
        last_time_up=1328049057
        last_time_down=1328049437
        last_time_unreachable=0
        last_notification=0
        next_notification=0
        no_more_notifications=0
        current_notification_number=0
        current_notification_id=0
        notifications_enabled=1
        problem_has_been_acknowledged=0
        acknowledgement_type=0
        active_checks_enabled=1
        passive_checks_enabled=1
        event_handler_enabled=1
        flap_detection_enabled=1
        failure_prediction_enabled=1
        process_performance_data=1
        obsess_over_host=1
        last_update=1328049467
        is_flapping=0
        percent_state_change=17.30
        scheduled_downtime_depth=0
        }
servicestatus {
        host_name=some_host
        service_description=PING
        modified_attributes=0
        check_command=check_ping!7000.0,40%!9000.0,70%
        check_period=24x7
        notification_period=24x7
        check_interval=5.000000
         (lots more stuff...)
         }


Sincerely,
David Kronheim
Production Support Tier II
Gateway Error Correction, VZ450 EDI, EDI Billing, & Metakey/LIA
484-213-1315

This communication is confidential.  Frontier only sends and receives email on 
the basis of the terms set out at http://www.frontier.com/email_disclaimer.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to