Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug fix for single mode, new test scripts from Ton
- Loading branch information
Ethan Galstad
committed
Jan 30, 2007
1 parent
c9a7a97
commit fb118d8
Showing
17 changed files
with
575 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
Oops, something went wrong.