#!/apps/cci/perl/bin/perl -w use strict; use Data::Dumper; $| = 1; my $refresh = 15; my $usage = "$0 "; my $path = shift; unless ( -r $path ) { die "Error: $usage\n"; } print "Checking file: $path\n"; # store states my $data; while ( 1 ) { open(my $fh, "<", $path) or die "Couldn't open $path for reading: $!\n"; while ( my $line = <$fh> ) { chomp $line; if ( $line =~ m|^host| ) { process_host( $fh ); } elsif ( $line =~ m|^service| ) { process_service( $fh ); } else { #print "Unparsed: $line\n"; } } close $fh or die "Error closing file: $!\n"; sleep $refresh; } # #_* Subroutines # sub process_host { my ( $fh ) = @_; my %host_data; while ( my ( $param, $value ) = process_line( $fh ) ) { $host_data{$param} = $value; } my %host_status = ( 0 => 'UP', 1 => 'DOWN', ); my $hostname = $host_data{host_name}; my $state = $host_status{ $host_data{current_state} }; my $output = $host_data{plugin_output}; # no host data currently being monitored by sec #print "HOST, $hostname, $state, $output\n"; } sub process_service { my ( $fh ) = @_; my %service_data; while ( my ( $param, $value ) = process_line( $fh ) ) { $service_data{$param} = $value; } my %service_status = ( 0 => 'OK', 1 => 'WARNING', 2 => 'CRITICAL', ); my $hostname = $service_data{host_name}; my $service = $service_data{service_description}; my $state = $service_status{ $service_data{current_state} }; my $output = $service_data{plugin_output}; my $last_check = $service_data{last_check}; my $previous_state = $data->{'SERVICE'}->{$hostname}->{$service}->{'state'}; my $previous_output = $data->{'SERVICE'}->{$hostname}->{$service}->{'output'}; my $previous_check = $data->{'SERVICE'}->{$hostname}->{$service}->{'last_check'}; # if we're seeing this service for the first time, or if the # last_check time has changed, then display the status unless ( defined $previous_check && $last_check == $previous_check ) { print join ", ", ( "SERVICE", $hostname, $service, $state, "$output\n" ); $data->{'SERVICE'}->{$hostname}->{$service}->{'state'} = $state; $data->{'SERVICE'}->{$hostname}->{$service}->{'output'} = $output; $data->{'SERVICE'}->{$hostname}->{$service}->{'last_check'} = $last_check; } } sub process_line { my ( $fh ) = @_; my $line = <$fh>; #print "PROCESS_LINE: $line\n"; chomp $line; return unless $line; return if ( $line =~ m|\}| ); $line =~ m|^([^\=]+)\=(.*)$|; my ( $param, $value ) = ( $1, $2 ); #print "FOO: $param => $value\n"; return unless ( defined $param && defined $value ); $param =~ s|^\s+||; $value =~ s|\s+$||; #print "LINE:$line PARAM:$param VALUE:$value\n"; return ( $param, $value ); }