Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Parsing nagios status.dat

by atcroft (Abbot)
on Apr 01, 2016 at 15:06 UTC ( [id://1159285]=note: print w/replies, xml ) Need Help??


in reply to Parsing nagios status.dat

Here is a quick bit of "one-linery" that appears to work much as requested (at least with both the sample data you provided and a status.dat file from a Nagios system I have access to). It creates a hash, loads the values (with the type of entry in a TYPE field), and displays the entry using Data::Dumper. (Code also includes a "one-liner" that generates the sample data you provided.)

Code:

perl -Mstrict -Mwarnings -le ' sub tab { return q{ } x 4; } foreach my $s ( q(hoststatus {), tab . q{host_name=AAA1}, tab . q{modified_attributes=0}, tab . q{check_command=check-host-alive}, tab . q{check_period=24x7}, tab . q{notification_period=24x7}, tab . q{check_interval=5.000000}, tab . q{retry_interval=1.000000}, tab . q(}), q{}, q(servicestatus {), tab . q{host_name=AAA1}, tab . q{plugin_output=eth0:UP, eth2:UP, eth3:UP, eth1:UP:4 UP: OK}, tab . q(}) ) { print $s; } ' \ | \ perl -Mstrict -Mwarnings -MData::Dumper -lne ' use vars qw(%s); BEGIN{ $Data::Dumper::Deepcopy = $Data::Dumper::Sortkeys = 1; }; chomp; if ( m/\}/ ) { print Data::Dumper->Dump( [ \%s, ], [ qw( *s ) ] ); %s = (); } next if ( m/^\s*$/imsx ); $s{TYPE} = $1 if ( m/^\s*(\S+)\s*\{/imsx ); $s{$1} = $2 if ( m/^\s+([^=]+)=(.+)$/imsx ); '

Output:

%s = ( 'TYPE' => 'hoststatus', 'check_command' => 'check-host-alive', 'check_interval' => '5.000000', 'check_period' => '24x7', 'host_name' => 'AAA1', 'modified_attributes' => '0', 'notification_period' => '24x7', 'retry_interval' => '1.000000' ); %s = ( 'TYPE' => 'servicestatus', 'host_name' => 'AAA1', 'plugin_output' => 'eth0:UP, eth2:UP, eth3:UP, eth1:UP:4 UP: OK +' );

Hope that helps.

Update: 2016-04-01
Quote mentions of "one-liner", as the code was written over multiple lines for readability.

Update: 2016-04-01
To answer the question in Re^2: Parsing nagios status.dat, instead of the first perl code (before the pipe ('|' character)), you would instead put something like cat /path/to/status.dat | perl .... (If you have questions regarding execution of scripts or "one-liners", you may wish to speak to your local sysadmin or help desk.)

Replies are listed 'Best First'.
Re^2: Parsing nagios status.dat
by leostereo (Beadle) on Apr 01, 2016 at 15:51 UTC

    Wow !! thanks for the response. Two questions: 1; How should I run this script, are there 2 separated file? 2; where are you declaring the path to the status.dat file ? Thanks!

      Leo, what atcroft was pointing out is that you need to put your code like this:

      cat status.dat | \ perl -Mstrict -Mwarnings -MData::Dumper -lne ' use vars qw(%s); BEGIN{ $Data::Dumper::Deepcopy = $Data::Dumper::Sortkeys = 1; }; chomp; if ( m/\}/ ) { print Data::Dumper->Dump( [ \%s, ], [ qw( *s ) ] ); %s = (); } next if ( m/^\s*$/imsx ); $s{TYPE} = $1 if ( m/^\s*(\S+)\s*\{/imsx ); $s{$1} = $2 if ( m/^\s+([^=]+)=(.+)$/imsx ); '
Re^2: Parsing nagios status.dat
by leostereo (Beadle) on Apr 04, 2016 at 16:03 UTC

    Ok ... I can not get it to work ... So far I have:

    #!/usr/bin/perl sub tab { return q{ } x 4; } my $host=shift(@ARGV); foreach my $s ( q(servicestatus {), tab . 'host_name='.$host, tab . 'plugin_output='./*./, tab . q(}) ) { print $s; }

    I can not get the string following the "plugin_output=" Sorry, I did not mentioned that , there are several lines beetween the "host_name" and the "plugin.." Any ideas ?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1159285]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-19 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found