<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
    <title></title>
  </head>
  <body bgcolor="#ffffff" text="#000000">
    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.<br>
    Parsing the nagios status file yourself is quite easy (its a nice
    format), so I just wrote my own code.<br>
    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)<br>
    <br>
<a class="moz-txt-link-freetext" href="http://www.smartmon.com.au/docs/tiki-index.php?page=Programmatically+Read+Nagios+Status&structure=How-to+Guide">http://www.smartmon.com.au/docs/tiki-index.php?page=Programmatically+Read+Nagios+Status&structure=How-to+Guide</a><br>
    <br>
    The following is a sub that reads the file and puts it into a hash.<br>
    <br>
    Call the sub like this:<br>
    <br>
    # location of nagios status.dat file - this is the default location<br>
    our $nagios_status_dat_file='/var/log/nagios/status.dat';<br>
    my
$nagios_status_file_error=load_nagios_status_file(\%status_data,$nagios_status_file);<br>
    <br>
    <br>
    sub load_nagios_status_file {<br>
    # loads the nagios status file into a nice hash structure<br>
    # reads the file and returns the hash<br>
    # pass in a hash reference where you want the status loaded to<br>
    # pass an optional status data file name (if we want to use the
    non-default one) - full path<br>
    my ($status_hashref,$custom_status_data_file)=@_;<br>
    <br>
    my $reason=''; # blank unless a fault<br>
    my $status_read_ok=0;<br>
    my $status_read_attempt=0;<br>
    my $max_status_read_attempts=3;<br>
    <br>
    my $status_dat_file=$custom_status_data_file ||
    $nagios_status_dat_file;<br>
    <br>
    my @status_data=();<br>
    <br>
    while (!$status_read_ok &&
    $status_read_attempt<=$max_status_read_attempts) {<br>
       $status_read_attempt++;<br>
       if (open(STATUS,"$status_dat_file")) {<br>
          # now see how much data we get out of it<br>
          @status_data=<STATUS>;<br>
          # we expect a certain number of lines<br>
          if ($#status_data>5) {<br>
             # assume file is ok<br>
             $status_read_ok=1;<br>
          } else {<br>
             $reason="File did not contain enough data - Nagios might
    not be running.";<br>
          }<br>
    <br>
          close(STATUS);<br>
       } else {<br>
          $reason="Could not access Nagios Status.data file - Nagios
    might not be running.";<br>
       }<br>
    }<br>
    <br>
    if ($status_read_ok) {<br>
       # now process the data in the array and load to the hash<br>
       my $service=0;<br>
       my $host=0;<br>
       my $info=0;<br>
       my $hostname='';<br>
       my $service_description='';<br>
       my $host_count=0;<br>
       my $service_count=0;<br>
    <br>
       foreach my $line (@status_data) {<br>
          # as of Nagios v3<br>
          # we are looking for 2 types of lines - one for Services and
    Hosts<br>
          # servicestatus {<br>
          #  host_name=host12<br>
          #     service_description=sample_PING<br>
          #  ..... other attributes ......<br>
          #  }<br>
          # AND one for hosts<br>
          # hoststatus {<br>
          #  host_name=host12<br>
          #  ..... other attributes ......<br>
          #  }<br>
          #<br>
          if ($line=~/^servicestatus {/) {<br>
             # start of a service entry<br>
             $service=1;<br>
          } elsif ($line=~/^hoststatus {/) {<br>
             # start of a host entry<br>
             $host=1;<br>
          } elsif ($line=~/^info {/) {<br>
             # this is the info section<br>
             $info=1;<br>
          } elsif ($line=~/^\s*}\s*\n/) {<br>
             # end of a section<br>
             # so close of this part of the hash<br>
             if ($service) {<br>
                $debug && print "Service $service_description on
    Host: $hostname\n";<br>
                $service_count++;<br>
             } elsif ($host) {<br>
                $debug && print "Host: $hostname\n";<br>
                $host_count++;<br>
             }<br>
    <br>
             $host=0;<br>
             $service=0;<br>
             $info=0;<br>
             $service_description='';<br>
             $hostname='';<br>
          } elsif ($info) {<br>
             # we are in the middle of processing an info record<br>
             # format of lines is attribute=value<br>
             # store in the hash<br>
             $line=~/\s*(.+)=(.+)\n/;<br>
             $$status_hashref{'info'}{$1}=$2;<br>
          } elsif ($service) {<br>
             # we are in the middle of processing a service record<br>
             $line=~/\s*(.+)=(.+)\n/;<br>
             # we need the hostname and the service descriptions to
    store data<br>
             if ($hostname && $service_description) {<br>
                # just store the data<br>
               
    $$status_hashref{'service'}{$hostname}{$service_description}{$1}=$2;<br>
             } elsif ($1 eq 'host_name') {<br>
                $hostname=$2<br>
             } elsif ($1 eq 'service_description') {<br>
                $service_description=$2<br>
             }<br>
          } elsif ($host) {<br>
             # we are in the middle of processing a host record<br>
             $line=~/\s*(.+)=(.+)\n/;<br>
             # we need the hostname to store data<br>
             if ($hostname) {<br>
                # just store the data<br>
                $$status_hashref{'host'}{$hostname}{$1}=$2;<br>
             } elsif ($1 eq 'host_name') {<br>
                $hostname=$2<br>
             }<br>
          }<br>
         <br>
       }<br>
       <br>
       # store the counts<br>
       $$status_hashref{'count'}{'host'}=$host_count;<br>
       $$status_hashref{'count'}{'service'}=$service_count;<br>
    <br>
    }<br>
    <br>
    return $reason;<br>
    }<br>
     <br>
    <br>
    On 21/07/2011 9:20 AM, Alex wrote:
    <blockquote
cite="mid:CAB1R3shMmyA7AP+1g=2VwcT0P0QRVPWXNJaaQHnDJB8ZpC2Faw@mail.gmail.com"
      type="cite">
      <pre wrap="">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?

</pre>
    </blockquote>
    <br>
    <div class="moz-signature">-- <br>
      <font size="-1">
        <a href="http://www.smartmon.com.au">Smartmon System Monitoring</a>
        <br>
        <a href="http://www.smartmon.com.au">www.smartmon.com.au</a> </font></div>
  </body>
</html>