Using NagiosStatusLog, or obtaining status info

Matthew Jurgens nagiosusers at edcint.co.nz
Sun Jul 24 06:38:23 CEST 2011


I did a similar thing a while back and from memory I think I found that 
there were no pre-coded modules that worked properly.
Parsing the nagios status file yourself is quite easy (its a nice 
format), so I just wrote my own code.
I use it to generate an html page with a high level graphical status 
which is shown on the following page (along with some info about doing it)

http://www.smartmon.com.au/docs/tiki-index.php?page=Programmatically+Read+Nagios+Status&structure=How-to+Guide

The following is a sub that reads the file and puts it into a hash.

Call the sub like this:

# location of nagios status.dat file - this is the default location
our $nagios_status_dat_file='/var/log/nagios/status.dat';
my 
$nagios_status_file_error=load_nagios_status_file(\%status_data,$nagios_status_file);


sub load_nagios_status_file {
# loads the nagios status file into a nice hash structure
# reads the file and returns the hash
# pass in a hash reference where you want the status loaded to
# pass an optional status data file name (if we want to use the 
non-default one) - full path
my ($status_hashref,$custom_status_data_file)=@_;

my $reason=''; # blank unless a fault
my $status_read_ok=0;
my $status_read_attempt=0;
my $max_status_read_attempts=3;

my $status_dat_file=$custom_status_data_file || $nagios_status_dat_file;

my @status_data=();

while (!$status_read_ok && 
$status_read_attempt<=$max_status_read_attempts) {
    $status_read_attempt++;
    if (open(STATUS,"$status_dat_file")) {
       # now see how much data we get out of it
       @status_data=<STATUS>;
       # we expect a certain number of lines
       if ($#status_data>5) {
          # assume file is ok
          $status_read_ok=1;
       } else {
          $reason="File did not contain enough data - Nagios might not 
be running.";
       }

       close(STATUS);
    } else {
       $reason="Could not access Nagios Status.data file - Nagios might 
not be running.";
    }
}

if ($status_read_ok) {
    # now process the data in the array and load to the hash
    my $service=0;
    my $host=0;
    my $info=0;
    my $hostname='';
    my $service_description='';
    my $host_count=0;
    my $service_count=0;

    foreach my $line (@status_data) {
       # as of Nagios v3
       # we are looking for 2 types of lines - one for Services and Hosts
       # servicestatus {
       #  host_name=host12
       #     service_description=sample_PING
       #  ..... other attributes ......
       #  }
       # AND one for hosts
       # hoststatus {
       #  host_name=host12
       #  ..... other attributes ......
       #  }
       #
       if ($line=~/^servicestatus {/) {
          # start of a service entry
          $service=1;
       } elsif ($line=~/^hoststatus {/) {
          # start of a host entry
          $host=1;
       } elsif ($line=~/^info {/) {
          # this is the info section
          $info=1;
       } elsif ($line=~/^\s*}\s*\n/) {
          # end of a section
          # so close of this part of the hash
          if ($service) {
             $debug && print "Service $service_description on Host: 
$hostname\n";
             $service_count++;
          } elsif ($host) {
             $debug && print "Host: $hostname\n";
             $host_count++;
          }

          $host=0;
          $service=0;
          $info=0;
          $service_description='';
          $hostname='';
       } elsif ($info) {
          # we are in the middle of processing an info record
          # format of lines is attribute=value
          # store in the hash
          $line=~/\s*(.+)=(.+)\n/;
          $$status_hashref{'info'}{$1}=$2;
       } elsif ($service) {
          # we are in the middle of processing a service record
          $line=~/\s*(.+)=(.+)\n/;
          # we need the hostname and the service descriptions to store data
          if ($hostname && $service_description) {
             # just store the data
             
$$status_hashref{'service'}{$hostname}{$service_description}{$1}=$2;
          } elsif ($1 eq 'host_name') {
             $hostname=$2
          } elsif ($1 eq 'service_description') {
             $service_description=$2
          }
       } elsif ($host) {
          # we are in the middle of processing a host record
          $line=~/\s*(.+)=(.+)\n/;
          # we need the hostname to store data
          if ($hostname) {
             # just store the data
             $$status_hashref{'host'}{$hostname}{$1}=$2;
          } elsif ($1 eq 'host_name') {
             $hostname=$2
          }
       }

    }

    # store the counts
    $$status_hashref{'count'}{'host'}=$host_count;
    $$status_hashref{'count'}{'service'}=$service_count;

}

return $reason;
}


On 21/07/2011 9:20 AM, Alex wrote:
> Hi all,
>
> I'm relatively new at using perl, but would like to write a script
> that prints the status of all services on all hosts. I thought the
> StatusLog module would be good, but it doesn't seem to work with the
> latest v3 nagios? It's probably more likely I'm doing something wrong,
> but are there any existing examples of how to use it properly with the
> latest versions of nagios?
>

-- 
Smartmon System Monitoring <http://www.smartmon.com.au>
www.smartmon.com.au <http://www.smartmon.com.au>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.monitoring-lists.org/archive/users/attachments/20110724/491ee241/attachment.html>
-------------- next part --------------
------------------------------------------------------------------------------
Magic Quadrant for Content-Aware Data Loss Prevention
Research study explores the data loss prevention market. Includes in-depth
analysis on the changes within the DLP market, and the criteria used to
evaluate the strengths and weaknesses of these DLP solutions.
http://www.accelacomm.com/jaw/sfnl/114/51385063/
-------------- next part --------------
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null


More information about the Users mailing list