Monday, March 25, 2013

Nagios Audit

Nagios Web Interface shows a lot of info about the hosts and services, but it 's not easy to find all the hosts and services problems that were already acknowledged or were scheduled for downtime. Also, when you have a lot of hosts and services to monitor, it is sometime hard to tell if the scheduled downtime should be over or if the check should be re-enabled.

The following script will show all the hosts and services that were disabled, acknowledged, or scheduled for downtime. It print out a html page, so it could potential be integrated with the nagios web interface.

(You can download the script here: nagios_audit.pl)


#!/usr/bin/perl
 use LWP;

my $browser = LWP::UserAgent->new;
$browser->agent('Nagios Audit');

my $servername='localhost';
my $port = 80;
my $realm = 'Nagios Access';
my $username = 'nagiosaudit';
my $password = 'password';

$browser->credentials("$servername:$port", $realm, $username, $password);





my $url='http://'.$servername.'/nagios/cgi-bin/status.cgi?host=all';
my $response = $browser->get(
  $url
);

my $nagios_status=$response->as_string;

Find_Disabled_Check_Host();

Find_Disabled_Notification_Host();

Find_Scheduled_Downtime_Host();

Find_Acknowledged_Host();

Find_Disabled_Check_Service();

Find_Disabled_Notification_Service();

Find_Scheduled_Downtime_Service();

Find_Acknowledged_Service();

#####################################################################
sub Find_Host
{


my $search_string=$_[0];


#go through all the hosts, (the one on the left column)
my @lines=split(/<TD align=left valign=center CLASS=/,$nagios_status);


foreach my $line (@lines)
  {
        #If it's type one and the host check is disabled, then find the host name
  if ( $line=~m/A HREF='extinfo.cgi\?type=1&host=/ && $line=~m/$search_string/ )

        {

        my @temp=split(/<TD ALIGN=center valign=center><A HREF='extinfo.cgi\?type=1&host=/,$line);

        my $str=@temp[0];

        my $start_pos=index($str,"host=");

        my $offset=5;

        $start_pos+=$offset;

        my $end_pos=index($str,"' title");

        my $length=$end_pos-$start_pos;

        my $host_name=substr($str,$start_pos,$length);


        print $host_name . "\n<BR><BR>";



        }
  }

}


#####################################################################
sub Find_Disabled_Check_Host
{
 print "\n<BR><BR>---------------------------------------------------\n<BR><BR>";
 print "Active Check for the following hosts were disabled:\n<BR>";
 print "---------------------------------------------------\n<BR>\n<BR>";

 Find_Host('Checks of this host have been disabled');


}

#####################################################################

sub Find_Disabled_Notification_Host
{
 print "\n<BR>---------------------------------------------------\n<BR>";
 print "Notification for the following hosts were disabled:\n<BR>";
 print "---------------------------------------------------\n<BR>\n<BR>";

Find_Host('Notifications for this host have been disabled');


}
#####################################################################

sub Find_Scheduled_Downtime_Host
{
 print "\n<BR>---------------------------------------------------\n<BR>";
 print "The following hosts are under maintenance:\n<BR>";
 print "---------------------------------------------------\n<BR>\n<BR>";

 Find_Host('This host is currently in a period of scheduled downtime');



}

#####################################################################

sub Find_Acknowledged_Host
{
 print "\n<BR>---------------------------------------------------\n<BR>";
 print "The following hosts' problems have been acknowledged\n<BR>";
 print "---------------------------------------------------\n<BR>\n<BR>";

 Find_Host('This host problem has been acknowledged');



}
#####################################################################
sub Find_Service
{
 #print "\n<BR>---------------------------------------------------\n<BR>";
 #print "Active Check for the following services were disabled:\n<BR>";
 #print "---------------------------------------------------\n<BR>\n<BR>";
#go through all the services
my @lines=split(/<TD ALIGN=LEFT valign=center CLASS=/,$nagios_status);

my $search_string=$_[0];

foreach my $line (@lines)
  {
        #If it's type one and the service check is disabled, then find the host name and service name
  if ( $line=~m/A HREF='extinfo.cgi\?type=2&host=/ && $line=~m/$search_string/ )

        {

        my @temp=split(/<TD ALIGN=center valign=center><A HREF='extinfo.cgi\?type=2&host=/,$line);

        my $str=@temp[1];





        my $start_pos=0;

        my $offset=0;

        $start_pos+=$offset;

        my $end_pos=index($str,'&');

        my $length=$end_pos-$start_pos;

        my $host_name=substr($str,$start_pos,$length);


        my @temp2=split(/#comments/,$str);

        $str=@temp2[0];


        $start_pos=index($str,"service=");

         $offset=8;

        $start_pos+=$offset;

         $end_pos=index($str,"'>");

         my $service_name="";

        if($end_pos!=-1)
        {
        $length=$end_pos-$start_pos;

        $service_name=substr($str,$start_pos,$length);
        }
        else
        {
         $service_name=substr($str,$start_pos);
        }

        print $host_name . " : " . $service_name . "\n<BR>";



        }
  }

}
#####################################################################
sub Find_Disabled_Check_Service
{
 print "\n<BR>---------------------------------------------------\n<BR>";
 print "Active Check for the following services were disabled:\n<BR>";
 print "---------------------------------------------------\n<BR>\n<BR>";

 Find_Service('Active checks of the service have been disabled - only passive checks are being accepted');

}


#####################################################################
sub Find_Disabled_Notification_Service
{
 print "\n<BR>---------------------------------------------------\n<BR>";
 print "Notification for the following services were disabled:\n<BR>";
 print "---------------------------------------------------\n<BR>\n<BR>";

 Find_Service('Notifications for this service have been disabled');

}


#####################################################################
sub Find_Scheduled_Downtime_Service
{
 print "\n<BR>---------------------------------------------------\n<BR>";
 print "The following services are under maintenance:\n<BR>";
 print "---------------------------------------------------\n<BR>\n<BR>";

 Find_Service('This service is currently in a period of scheduled downtime');

}

#####################################################################
sub Find_Acknowledged_Service
{
 print "\n<BR>---------------------------------------------------\n<BR>";
 print "The following services' problems have been acknowledged\n<BR>";
 print "---------------------------------------------------\n<BR>\n<BR>";

 Find_Service('This service problem has been acknowledged');

}