Guest User

brandon-nagios-parse

a guest
Feb 1st, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.76 KB | None | 0 0
  1. #!/usr/local/bin/perl
  2.  
  3. # always a good idea, forces scope to if/for/foreach/while blocks
  4. use strict;
  5.  
  6. # open the file, into a file handle called $RR - die with sad face if unable
  7. # to open file
  8. open my $RR, '<', 'nagios.dat' || die 'unable to open nagios.dat :(';
  9.  
  10. # we need to know if were inside a 'hoststatus' block or not
  11. # this variable keeps track of that
  12. my $we_are_inside_hoststatus_block = 0;
  13.  
  14. # the logic is to set  $we_are_inside_hoststatus_block to 1 ( true )
  15. # when we see the hoststatus block begins and then set it to 0 ( false )
  16. # when the block is done
  17.  
  18. # once were inside a hoststatus block were going to save the actual hostname
  19. # in this variable
  20. my $current_block_hostname;
  21.  
  22. # read file line by line
  23. while (<$RR>) {
  24.  
  25.         # each line coming into this block of code is automatically
  26.         # called $_ by perl, were going to rename it to $line
  27.         # to make the example clearer
  28.         my $line = $_;
  29.  
  30.         # each line thats coming in has a \n ( carriage return - new line)
  31.         # at the end - we dont need that. chomp() removes it
  32.         chomp $line;
  33.  
  34.         # theres two possible scenarios
  35.         # 1 . were outside a hoststatus block
  36.         # 2 . were inside  a hoststatus block
  37.  
  38.         if ($we_are_inside_hoststatus_block) {
  39.  
  40.                 # theres two possible scenarios inside of here
  41.                 # 1 . we already know the hostname of this hoststatus block
  42.                 # 2 . we do not
  43.  
  44.                 if ($current_block_hostname) {
  45.  
  46.                         # case 1
  47.  
  48.                         # were inside a hoststatus block
  49.                         # and we know what host_name it is for
  50.                         # all we need is the current_state
  51.  
  52.                         # read the big comment in the else{}
  53.                         # block below to know what this is doing
  54.                         # \d+ means one or more numbers
  55.                         next unless $line =~ /\s+current_state=(\d+)/;
  56.  
  57.                         my $current_state = $1;
  58.  
  59.                         # we have everything we need
  60.                         # host_name and current_state and were in a
  61.                         # host status block - all we need to do is
  62.                         # print this information to the screen
  63.                         print "$current_block_hostname:$current_state\n";
  64.  
  65.                         # since were finished with this hoststatus
  66.                         # block we can reset our $we_are_inside_hoststatus_block
  67.                         # and $current_block_hostname variables
  68.                         # so the script starts looking for a new
  69.                         # hoststatus block to process
  70.  
  71.                         $we_are_inside_hoststatus_block = undef;
  72.                         $current_block_hostname         = undef;
  73.                         next;
  74.  
  75.                 }
  76.                 else {
  77.  
  78.                         # case 2
  79.  
  80.                         # were inside a hostsatus block but we still
  81.                         # dont know what hostname its for
  82.                         #
  83.                         # we dont care about any line unless
  84.                         # it matches the regular expression :
  85.                         # one or more spaces
  86.                         # 'host_name='
  87.                         # \S+ means : something thats not whitespace
  88.                         # the parentheses mean we want to capture
  89.                         # the value between them
  90.                         # perl will automatically call the first
  91.                         # captured value $1, the second one $2
  92.                         # so on and so forth
  93.                         next unless $line =~ /\s+host_name=(\S+)/;
  94.  
  95.                         # cool we have the hostname of this hoststatus
  96.                         # block
  97.                         $current_block_hostname = $1;
  98.  
  99.                         # go to the next line of the file
  100.                         next;
  101.  
  102.                 }
  103.  
  104.         }
  105.         else {
  106.  
  107.                 # we are not inside a hoststatus block
  108.                 # in this case we dont care about any of these lines
  109.                 # unless its a line that STARTS a hoststatus block
  110.  
  111.                 # does the line match the regular expression that
  112.                 # informs us we are starting a hoststatus block ?
  113.                 # ^ means beginning of line
  114.                 # \s+ means one or more spaces
  115.                 # so were looking for a line that begins with
  116.                 # 'hoststatus' has one or more spaces and then has a '{'
  117.                 if ( $line =~ /^hoststatus\s+{/ ) {
  118.  
  119.                         # were inside a new hoststatus block
  120.                         $we_are_inside_hoststatus_block = 1;
  121.  
  122.                 }
  123.  
  124.         }
  125.  
  126. }
  127.  
  128. close $RR;
Advertisement
Add Comment
Please, Sign In to add comment