Navigation Menu

Skip to content

Commit

Permalink
Bug fix for single mode, new test scripts from Ton
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Galstad committed Jan 30, 2007
1 parent c9a7a97 commit fb118d8
Show file tree
Hide file tree
Showing 17 changed files with 575 additions and 18 deletions.
6 changes: 6 additions & 0 deletions Changelog
Expand Up @@ -3,6 +3,12 @@ NSCA Changelog
**************


2.7.1 - 01/29/2007
------------------
- Fixed bug that prevented single mode daemon from working properly
- Added sample scripts for testing functionality to nsca_tests/ (Ton Voon/Altinity)


2.7 - 12/13/2006
----------------
- Fixed crash from malformed command line
Expand Down
4 changes: 2 additions & 2 deletions configure
Expand Up @@ -1288,9 +1288,9 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu
PKG_NAME=nsca
PKG_VERSION="2.7"
PKG_VERSION="2.7.1"
PKG_HOME_URL="http://www.nagios.org/"
PKG_REL_DATE="12-13-2006"
PKG_REL_DATE="01-29-2007"
ac_aux_dir=
for ac_dir in $srcdir $srcdir/.. $srcdir/../..; do
Expand Down
4 changes: 2 additions & 2 deletions configure.in
Expand Up @@ -9,9 +9,9 @@ AC_CONFIG_HEADER(include/config.h)
AC_PREFIX_DEFAULT(/usr/local/nagios)

PKG_NAME=nsca
PKG_VERSION="2.7"
PKG_VERSION="2.7.1"
PKG_HOME_URL="http://www.nagios.org/"
PKG_REL_DATE="12-13-2006"
PKG_REL_DATE="01-29-2007"

dnl Figure out how to invoke "install" and what install options to use.
AC_PROG_INSTALL
Expand Down
4 changes: 2 additions & 2 deletions include/common.h
Expand Up @@ -24,8 +24,8 @@
#include "config.h"


#define PROGRAM_VERSION "2.7"
#define MODIFICATION_DATE "12-13-2006"
#define PROGRAM_VERSION "2.7.1"
#define MODIFICATION_DATE "01-29-2007"


#define OK 0
Expand Down
2 changes: 1 addition & 1 deletion nsca.spec
@@ -1,5 +1,5 @@
%define name nsca
%define version 2.7
%define version 2.7.1
%define release 1
%define nsusr nagios
%define nsgrp nagios
Expand Down
92 changes: 92 additions & 0 deletions nsca_tests/NSCATest.pm
@@ -0,0 +1,92 @@
#
# DESCRIPTION:
# Helper object class for NSCA testing
#
# COPYRIGHT:
# Copyright (C) 2007 Altinity Limited
# Copyright is freely given to Ethan Galstad if included in the NSCA distribution
#
# LICENCE:
# GNU GPLv2

package NSCATest;

use strict;
use Class::Struct;
use IO::File;

struct NSCATest => {
config => '$',
pid => '$',
timeout => '$',
};

$| = 1; # Autoflush on

sub start {
my ($self, $mode) = @_;
$mode ||= "--single";

printf "Starting nsca with $mode\n";
system("../src/nsca -c nsca_".$self->config.".cfg $mode");

sleep 1; # Let daemon start
open F, "var/nsca.pid" or die "No pid file found";
chop(my $pid = <F>);
close F;
$self->pid($pid);

open(F, "> var/nagios.cmd") or die "Cannot create var/nagios.cmd";
close F;
return $pid;
}

sub stop {
my $self = shift;
print "Stopping nsca: ".$self->pid.$/;
kill "TERM", $self->pid;
$self->pid(undef);
unlink "var/nagios.cmd", "var/nsca.dump";
sleep 1; # Let daemon die
}

sub send {
my ($self, $data) = @_;
my @output = map { join("\t", @$_)."\n" } @$data;
open SEND, "| ".$self->send_cmd;
print SEND @output;
close SEND;
}

sub send_cmd {
my ($self) = @_;
my $timeout = $self->timeout || 2;
return "../src/send_nsca -to $timeout -p 56677 -H localhost -c send_".$self->config.".cfg";
}

sub read_cmd {
my ($self, $file) = @_;
$file ||= "var/nagios.cmd";
my $fh = IO::File->new($file) or die "Can't open $file";
$self->process_data($fh);
}

sub process_data {
my ($self, $fh) = @_;
my $data = [];
while(<$fh>) {
chop;
my @bits = /\[\d+\] PROCESS_(?:HOST|SERVICE)_CHECK_RESULT;([^;]+);(?:([^;]+);)?([0123]);(.*)$/o;

# Remove the service name if doesn't exist
splice @bits, 1, 1 unless defined $bits[1];

push @$data, [ @bits ];
}
return $data;
}

# Was thinking of calling $self->stop in DESTROY, but with the forking
# going on, this wouldn't work

1;
38 changes: 38 additions & 0 deletions nsca_tests/README
@@ -0,0 +1,38 @@
NSCA Tests
==========
Originally contributed by Ton Voon/Altinity 01/26/2007.

These tests are designed to make sure that the Nagios daemon can start up
and accept messages by comparing the output in the dummy nagios.cmd file with
the sent data.

There are 3 tests at the moment:

* basic - just sends a few passive checks and makes sure that the nagios.cmd file receives them

* multiple - runs the same as basic, but several times to check the daemon can handle multiple requests

* simultaneous - runs lots of send_nscas at the same time (well, nearly). Uses Parallel::Forker to setup all the sends then executes them all at once. Expect about 200 extra processes to hit your server!


Requirements
------------
You will need to have the following CPAN modules installed:

Test::More
Class::Struct
Clone
Parallel::Forker


Running the Tests
-----------------
1. Run the NSCA configure script and compile the NSCA binaries

2. Run the tests from this directory by running the following command:

./runtests




40 changes: 40 additions & 0 deletions nsca_tests/basic.t
@@ -0,0 +1,40 @@
#!/usr/bin/perl
#
# DESCRIPTION:
# Test sending basic passive results to nsca
#
# COPYRIGHT:
# Copyright (C) 2007 Altinity Limited
# Copyright is freely given to Ethan Galstad if included in the NSCA distribution
#
# LICENCE:
# GNU GPLv2

use strict;
use NSCATest;
use Test::More;

plan tests => 2;

my $data = [
["hostname", "0", "Plugin output"],
["hostname-with-other-bits", "1", "More data to be read"],
["hostname.here", "2", "Check that ; are okay to receive"],
["host", "service", 0, "A good result here"],
["host54", "service with spaces", 1, "Warning! My flies are undone!"],
["host-robin", "service with a :)", 2, "Critical? Alert! Alert!"],
["host-batman", "another service", 3, "Unknown - the only way to travel"],
];

foreach my $type qw(--single --daemon) {
my $nsca = NSCATest->new( config => "basic" );

$nsca->start($type);
$nsca->send($data);
sleep 1; # Need to wait for --daemon to finish processing

my $output = $nsca->read_cmd;
is_deeply($data, $output, "Got all data as expected");

$nsca->stop;
}
50 changes: 50 additions & 0 deletions nsca_tests/multiple.t
@@ -0,0 +1,50 @@
#!/usr/bin/perl
#
# DESCRIPTION:
# Test sending more than one passive result to nsca
#
# COPYRIGHT:
# Copyright (C) 2007 Altinity Limited
# Copyright is freely given to Ethan Galstad if included in the NSCA distribution
#
# LICENCE:
# GNU GPLv2


use strict;
use NSCATest;
use Test::More;
use Clone qw(clone);

my $iterations = 10;

plan tests => 2;

my $data = [
["hostname", "0", "Plugin output"],
["host", "service", 0, "A good result here"],
];

my $copies = [];
for (1 .. $iterations) {
my $c = clone($data);
push @$copies, @$c;
}

foreach my $type qw(--single --daemon) {
my $nsca = NSCATest->new( config => "basic" );

$nsca->start($type);

my $i = 0;
for($i; $i < $iterations; $i++) {
$nsca->send($data);
}
sleep 1; # Need to wait for --daemon to finish processing

my $output = $nsca->read_cmd;

is_deeply( $output, $copies );

$nsca->stop;
}

0 comments on commit fb118d8

Please sign in to comment.