Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/perl
- # always a good idea, forces scope to if/for/foreach/while blocks
- use strict;
- # open the file, into a file handle called $RR - die with sad face if unable
- # to open file
- open my $RR, '<', 'nagios.dat' || die 'unable to open nagios.dat :(';
- # we need to know if were inside a 'hoststatus' block or not
- # this variable keeps track of that
- my $we_are_inside_hoststatus_block = 0;
- # the logic is to set $we_are_inside_hoststatus_block to 1 ( true )
- # when we see the hoststatus block begins and then set it to 0 ( false )
- # when the block is done
- # once were inside a hoststatus block were going to save the actual hostname
- # in this variable
- my $current_block_hostname;
- # read file line by line
- while (<$RR>) {
- # each line coming into this block of code is automatically
- # called $_ by perl, were going to rename it to $line
- # to make the example clearer
- my $line = $_;
- # each line thats coming in has a \n ( carriage return - new line)
- # at the end - we dont need that. chomp() removes it
- chomp $line;
- # theres two possible scenarios
- # 1 . were outside a hoststatus block
- # 2 . were inside a hoststatus block
- if ($we_are_inside_hoststatus_block) {
- # theres two possible scenarios inside of here
- # 1 . we already know the hostname of this hoststatus block
- # 2 . we do not
- if ($current_block_hostname) {
- # case 1
- # were inside a hoststatus block
- # and we know what host_name it is for
- # all we need is the current_state
- # read the big comment in the else{}
- # block below to know what this is doing
- # \d+ means one or more numbers
- next unless $line =~ /\s+current_state=(\d+)/;
- my $current_state = $1;
- # we have everything we need
- # host_name and current_state and were in a
- # host status block - all we need to do is
- # print this information to the screen
- print "$current_block_hostname:$current_state\n";
- # since were finished with this hoststatus
- # block we can reset our $we_are_inside_hoststatus_block
- # and $current_block_hostname variables
- # so the script starts looking for a new
- # hoststatus block to process
- $we_are_inside_hoststatus_block = undef;
- $current_block_hostname = undef;
- next;
- }
- else {
- # case 2
- # were inside a hostsatus block but we still
- # dont know what hostname its for
- #
- # we dont care about any line unless
- # it matches the regular expression :
- # one or more spaces
- # 'host_name='
- # \S+ means : something thats not whitespace
- # the parentheses mean we want to capture
- # the value between them
- # perl will automatically call the first
- # captured value $1, the second one $2
- # so on and so forth
- next unless $line =~ /\s+host_name=(\S+)/;
- # cool we have the hostname of this hoststatus
- # block
- $current_block_hostname = $1;
- # go to the next line of the file
- next;
- }
- }
- else {
- # we are not inside a hoststatus block
- # in this case we dont care about any of these lines
- # unless its a line that STARTS a hoststatus block
- # does the line match the regular expression that
- # informs us we are starting a hoststatus block ?
- # ^ means beginning of line
- # \s+ means one or more spaces
- # so were looking for a line that begins with
- # 'hoststatus' has one or more spaces and then has a '{'
- if ( $line =~ /^hoststatus\s+{/ ) {
- # were inside a new hoststatus block
- $we_are_inside_hoststatus_block = 1;
- }
- }
- }
- close $RR;
Advertisement
Add Comment
Please, Sign In to add comment