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


In reply to Re: Parsing nagios status.dat by atcroft
in thread Parsing nagios status.dat by leostereo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.