On 31/01/2012 22:43, Brandon Phelps wrote:
Hello all,

I am attempting to parse a Nagios status.dat file and am curious about
what the most efficient way to do this would be. I'm sure I could come
up with something but it would be very convoluted and I'm sure there is
a better way. Below is a sample of the 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.

Any pointers or examples would be appreciated!

Here is a sample of the file:

[snip data]

Thanks,
Brandon


Hi Brandon

The program below seems to do what you require. Hope it helps.

Rob

use strict;
use warnings;

open my $fh, '<', 'status.dat' or die $!;

my $hoststatus;
my %data;

while (<$fh>) {

  $hoststatus++ if /\bhoststatus\s*\{/;
  next unless $hoststatus;

  if (/\}/) {
    print "$data{host_name}:$data{current_state}\n";
    %data = ();
    $hoststatus = 0;
  }
  elsif (/(\w+)=(.*\S)/) {
    $data{$1} = $2;
  }
}

--
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