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