[Nagiosplug-help] Get status from external application

Thomas Guyot-Sionnest dermoth at aei.ca
Tue Apr 24 06:37:57 CEST 2007


On 23/04/07 02:53 AM, Olivier Raginel wrote:
> Hello,
> 
> I'm just wondering if there is any easy way to get the status of a host 
> / service from an external application. For example, if I want to create 
> a webpage with just some green / orange / red lights with a specific 
> status, or some pseudo-interfaces so have status on my desktop / warf?
> 
> I've seen some things such as NXE and GNG, but they seem really 
> specific.
> Something like a webservice interface for Nagios would make my day. Will 
> I have to write it? :)

If you know Perl you can use Nagios::StatusLog.

There are some good examples coming along with Nagios::Statuslog. Here's
one more:


#!/usr/bin/perl

use Nagios::StatusLog;
use Getopt::Std;
use vars qw($opt_c $opt_o $opt_v $opt_h);
use strict;
use warnings;

getopts('c:v:h');

if (!$opt_c || defined($opt_h)) {
  require File::Basename;
  print "Must specify location of Nagios configuration with -c
option.\n" if (!$opt_c);
  die "USAGE: " . File::Basename::basename($0) . " -c <cfg_file> [ -v
<cfg_version ]\n";
}

# Only tested with Nagios v2.7 (2.0)
my $version = (defined($opt_v) ? $opt_v : '2.0');

open (CONFIG, $opt_c) or die "Can't open Nagios config file: $!";

my ($status_file, $command_file) = ();
while (<CONFIG>) {
  next unless m/^(status_file|command_file)=(.+)$/;
  $status_file = $2 if ($1 eq 'status_file');
  $command_file = $2 if ($1 eq 'command_file');
}
close (CONFIG);

if (!$status_file || !$command_file) {
  die "Could not find status_file and command_file in the main Nagios
config file.";
}

my $log = Nagios::StatusLog->new(
  Filename => $status_file,
  Version  => $version
);

# Get time, initialize variables
my $ts = time;
my ($hcount, $scount);

foreach my $host ($log->list_hosts()) {
  my $hchk = 0;
  foreach my $service ($log->list_services_on_host($host)) {
    # With Nagios::Object I could check for volatile services, but it is
    # way too buggy.
    # This is a HACK
    if ($$log{'SERVICE'}{$host}{$service}{'should_be_scheduled'} == 0 &&
        $$log{'SERVICE'}{$host}{$service}{'active_checks_enabled'} == 0) {
      open (COMMAND, ">$command_file") or die "Can't open Nagios command
file: $!";
      printf COMMAND "[%lu]
ACKNOWLEDGE_SVC_PROBLEM;%s;%s;%i;%i;%i;%s;%s\n", $ts, $host, $service,
0, 0, 0, 'Admin', 'This service have been acknowledged automatically';
      close(COMMAND);
      $scount++;
      $hchk = 1;
    }
  }
  $hcount += $hchk;
}

print "Acknowledged $scount passive services on $hcount hosts\n";

__END__

Thomas




More information about the Help mailing list