Re: [Nagios-users] Using NagiosStatusLog, or obtaining status info

2011-07-23 Thread Matthew Jurgens
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+Statusstructure=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_data5) {
 # 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

Re: [Nagios-users] Using NagiosStatusLog, or obtaining status info

2011-07-20 Thread Alex
Hi,

I wanted to add to my previous mail that I just found the list_tags()
function (from StatusLog.pm) which returns the list of all the status
variables from the status.dat file. However, it returns some others
that don't appear in the status.dat file, such as time_up and
time_down.

Where do these variables come from? I'm trying to upgrade a legacy
application that used nagios v1, and I'd like to recreate the
status.log file. This log file contained those variables time_up and
time_down, so I'd like to report the value of those variables in my
new version.

I hope someone can help me to clarify the discrepancies between the v1
log and the v3 log, and how to obtain all the variables and their
values from the v3 log using perl.

 The documentation says Nagios::Service::Status has the following
 accessor methods (For V1):, then lists those methods. However, many
 return null, such as host_up. Are these methods only available in
 nagios v1? If so, how do I get a status of each service for all hosts
 and services?

Thanks,
Alex

--
10 Tips for Better Web Security
Learn 10 ways to better secure your business today. Topics covered include:
Web security, SSL, hacker attacks  Denial of Service (DoS), private keys,
security Microsoft Exchange, secure Instant Messaging, and much more.
http://www.accelacomm.com/jaw/sfnl/114/51426210/
___
Nagios-users mailing list
Nagios-users@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