Nagios status.dat: Difference between revisions

From Federal Burro of Information
Jump to navigationJump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 65: Line 65:
print "</table>\n";
print "</table>\n";
</pre>
</pre>
[[Category:Script]]


[[Category:Nagios]]
[[Category:Nagios]]

Latest revision as of 05:46, 6 April 2021


#!/usr/bin/perl -w

use strict;
use CGI qw(:standard);
use File::Slurp;

print header;

my $h = param('h'); # host list, comma seperated.
my $v = param('v'); # which variable to grep out.
my $type ;
my $data; # big data strcuture.
my $rawtype = param('type');
my $statfile = "/usr/local/nagios/var/status.dat";

if ( ! defined ( $h )) { $h = "MCC-Cap1_PRD";  }
if ( ! defined ( $v )) { $v = "current_state"; }

if ( $h =~ /(\w+)/ ) { print "query string is $h<br>\n";     }
else                 { die   "query string is not valid.\n"; }

if ( length($h ) < 3 ) { die "Query too short \n"; }
my %hostlist = map { $_ => 1 } split ( ",", $h ) ;
my $file = read_file( $statfile ) ;
$file =~ s/\t//;
my @services = $file =~ /servicestatus {[\\S\\s]*?}/gm;

while ( $file =~ /servicestatus \{\n([\S\s]*?)\}/mg ) {
    my $service = $1;
    $service =~ s/([ \t]+)//g;
    my @pairs = split ( /\n/ , $service );
    my ($name , $value );
    my %hash;
    foreach ( @pairs ) {
        ($name , $value ) = split /=/;
        if ( defined $value ) { $hash{$name} = $value; }
        else                  { $hash{$name} = "NULL";   }
    }

    if ( $hostlist{$hash{'host_name'}} ) { $data->{$hash{'service_description'}}->{$hash{'host_name'}} = $hash{$v}; }
}
print "<table border=\"1\">";

# print the host header
print "<tr><td>";
foreach my $host ( sort keys %hostlist ) { print "<td>".$host."</td>"; }
print "</tr>\n";

#print per test lines
foreach my $var ( sort keys ( %$data )  ) {
    print "\t<tr><td>".$var."</td>";
    my $tmp = $data->{$var};
    foreach my $host (  sort keys %hostlist ) {
        my $color;
        if ( $tmp->{$host} eq "0" ) { $color = "green"  };
        if ( $tmp->{$host} eq "1" ) { $color = "yellow" };
        if ( $tmp->{$host} eq "2" ) { $color = "red"    };
        if ( $tmp->{$host} eq ""  ) { $color = "purple" };
        print "<td bgcolor=\"".$color."\">".$tmp->{$host}."</td>";
    }
    print "</tr>\n";
}
print "</table>\n";