From cdc06cc3e2c4670d3cd46b0a03adcf7e6958eff1 Mon Sep 17 00:00:00 2001 From: Peter Bray Date: Mon, 25 Jul 2005 01:47:15 +0000 Subject: [1185704] New Testing Infrastructure. Complete rewrite of the original testing infrastructure and all test cases (to use the new infrastructure) See NPTest.pm and issue 1185704 for more details. git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1207 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/Helper.pm b/Helper.pm deleted file mode 100644 index 198a648..0000000 --- a/Helper.pm +++ /dev/null @@ -1,44 +0,0 @@ -package Helper; -use strict; - -use Exporter(); -use vars qw($VERSION @ISA @EXPORT); -$VERSION = 0.01; -@ISA=qw(Exporter); -@EXPORT=qw(&get_option); - -sub get_option ($$) { - my $file = 'Cache'; - my $response; - my $var = shift; - - require "$file.pm"; - if(defined($Cache::{$var})){ - $response=$Cache::{$var}; - return $$response; - } - - my $request = shift; - my $filename; - my $path; - foreach $path (@INC) { - $filename="$path/$file.pm"; - last if (-e $filename); - } - print STDERR "Enter $request\n"; - $response=; - chop($response); - open(CACHE,"<$filename") or die "Cannot open cache for reading"; - undef $/; - my $cache = ; - $/="\n"; - close CACHE; - $cache =~ s/^(\@EXPORT\s*=\s*qw\(\s*[^\)]*)\)\s*;/$1 $var\)\;/msg; - $cache =~ s/^1;[\n\s]*\Z/\$$var=\"$response\"\;\n1\;\n/msg; - open(CACHE,">$filename") or die "Cannot open cache for writing"; - print CACHE $cache; - close CACHE; - return $response; -} - -1; diff --git a/Makefile.am b/Makefile.am index ca570e3..a7c5ffc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -5,7 +5,7 @@ SUBDIRS = intl lib plugins plugins-scripts m4 po EXTRA_DIST = config.rpath \ ABOUT-NLS ACKNOWLEDGEMENTS AUTHORS BUGS CHANGES CODING FAQ LEGAL \ REQUIREMENTS SUPPORT THANKS \ - Helper.pm contrib pkg nagios-plugins.spec + NPTest.pm contrib pkg nagios-plugins.spec ACLOCAL_AMFLAGS = -I m4 diff --git a/NPTest.pm b/NPTest.pm new file mode 100644 index 0000000..f3c874b --- /dev/null +++ b/NPTest.pm @@ -0,0 +1,554 @@ +package NPTest; + +# +# Helper Functions for testing Nagios Plugins +# + +require Exporter; +@ISA = qw(Exporter); +@EXPORT = qw(getTestParameter checkCmd skipMissingCmd); +@EXPORT_OK = qw(DetermineTestHarnessDirectory TestsFrom SetCacheFilename); + +use strict; +use warnings; + +use Cwd; +use File::Basename; + +use IO::File; +use Data::Dumper; + +use Test; + +use vars qw($VERSION); +$VERSION = do { my @r = (q$Revision$ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker + +=head1 NAME + +NPTest - Simplify the testing of Nagios Plugins + +=head1 DESCRIPTION + +This modules provides convenience functions to assist in the testing +of Nagios Plugins, making the testing code easier to read and write; +hopefully encouraging the development of more complete test suite for +the Nagios Plugins. It is based on the patterns of testing seen in the +1.4.0 release, and continues to use the L module as the basis of +testing. + +=head1 FUNCTIONS + +This module defines three public functions, C, +C and C. These are exported by +default via the C statement. + +=over + +=item C + +A flexible and user override-able method of collecting, storing and +retrieving test parameters. This function allows the test harness +developer to interactively request test parameter information from the +user, when the no means of obtaining the information automatically has +been successful. The user is provided with the option of accepting +test harness developer's default value for the parameter, if a suggested +default is provided. + +User supplied responses are stored in an external (file-based) +cache. These values are retrieved on subsequent runs alleviating the +user of reconfirming the previous entered responses. The user is able +to override the value of a parameter on any given run by setting the +associated environment variable. These environment variable based +overrides are not stored in the cache, allowing one-time and what-if +based tests on the command line without polluting the cache. + +The option exists to store parameters in a scoped means, allowing a +test harness to a localise a parameter should the need arise. This +allows a parameter of the same name to exist in a test harness +specific scope, while not affecting the globally scoped parameter. The +scoping identifier is the name of the test harness sans the trailing +".t". All cache searches first look to a scoped parameter before +looking for the parameter at global scope. Thus for a test harness +called "check_disk.t" requesting the parameter "mountpoint_valid", the +cache is first searched for "check_disk"/"mountpoint_valid", if this +fails, then a search is conducted for "mountpoint_valid". + +The facilitate quick testing setup, it is possible to accept all the +developer provided defaults by setting the environment variable +"NPTEST_ACCEPTDEFAULT" to "1" (or any other perl truth value). Note +that, such defaults are not stored in the cache, as there is currently +no mechanism to edit existing cache entries, save the use of text +editor or removing the cache file completely. + +=item C + +This function attempts to encompass the majority of test styles used +in testing Nagios Plugins. As each plug-in is a separate command, the +typical tests we wish to perform are against the exit status of the +command and the output (if any) it generated. Simplifying these tests +into a single function call, makes the test harness easier to read and +maintain and allows additional functionality (such as debugging) to be +provided withoutadditional effort on the part of the test harness +developer. + +It is possible to enable debugging via the environment variable +C. If this environment variable exists and its value in PERL's +boolean context evaluates to true, debugging is enabled. + +The function prototype can be expressed as follows: + + Parameter 1 : command => DEFINED SCALAR(string) + Parameter 2 : desiredExitStatus => ONE OF + SCALAR(integer) + ARRAYREF(integer) + HASHREF(integer,string) + UNDEFINED + Parameter 3 : desiredOutput => SCALAR(string) OR UNDEFINED + Parameter 4 : exceptions => HASH(integer,string) OR UNDEFINED + Returns : SCALAR(integer) as defined by Test::ok(...) + +The function treats the first parameter C<$command> as a command line +to execute as part of the test, it is executed only once and its exit +status (C<$?EE8>) and output are captured. + +At this point if debugging is enabled the command, its exit status and +output are displayed to the tester. + +C allows the testing of either the exit status or the +generated output or both, not testing either will result in neither +the C or C functions being called, +something you probably don't want. Note that each defined test +(C<$desiredExitStatus> and C<$desiredOutput>) results in a invocation +of either C or C, so remember this +when counting the number of tests to place in the C +call. + +Many Nagios Plugins test network services, some of which may not be +present on all systems. To cater for this, C allows the +tester to define exceptions based on the command's exit status. These +exceptions are provided to skip tests if the test case developer +believes the service is not being provided. For example, if a site +does not have a POP3 server, the test harness could map the +appropriate exit status to a useful message the person running the +tests, telling the reason the test is being skipped. + +Example: + +my %exceptions = ( 2 =E "No POP Server present?" ); + +$t += checkCmd( "./check_pop I", 0, undef, %exceptions ); + +Thus, in the above example, an exit status of 2 does not result in a +failed test case (as the exit status is not the desired value of 0), +but a skipped test case with the message "No POP Server present?" +given as the reason. + +Sometimes the exit status of a command should be tested against a set +of possible values, rather than a single value, this could especially +be the case in failure testing. C support two methods +of testing against a set of desired exit status values. + +=over + +=item * + +Firstly, if C<$desiredExitStatus> is a reference to an array of exit +stati, if the actual exit status of the command is present in the +array, it is used in the call to C when testing the +exit status. + +=item * + +Alternatively, if C<$desiredExitStatus> is a reference to a hash of +exit stati (mapped to the strings "continue" or "skip"), similar +processing to the above occurs with the side affect of determining if +any generated output testing should proceed. Note: only the string +"skip" will result in generated output testing being skipped. + +=back + +=item C + +If a command is missing and the test harness must C some +or all of the tests in a given test harness this function provides a +simple iterator to issue an appropriate message the requested number +of times. + +=back + +=head1 SEE ALSO + +L + +The rest of the code, as I have only commented on the major public +functions that test harness writers will use, not all the code present +in this helper module. + +=head1 AUTHOR + +Copyright (c) 2005 Peter Bray. All rights reserved. + +This package is free software and is provided "as is" without express +or implied warranty. It may be used, redistributed and/or modified +under the same terms as the Nagios Plugins release. + +=cut + +# +# Package Scope Variables +# + +my( %CACHE ) = (); + +# I'm not really sure wether to house a site-specific cache inside +# or outside of the extracted source / build tree - lets default to outside +my( $CACHEFILENAME ) = ( exists( $ENV{'NPTESTCACHE'} ) && $ENV{'NPTESTCACHE'} ) + ? $ENV{'NPTESTCACHE'} : "/var/tmp/NPTest.cache"; # "../Cache.pdd"; + +# +# Testing Functions +# + +sub checkCmd +{ + my( $command, $desiredExitStatus, $desiredOutput, %exceptions ) = @_; + + my $output = `${command}`; + my $exitStatus = $? >> 8; + + $output = "" unless defined( $output ); + chomp( $output ); + + if ( exists( $ENV{'NPTEST_DEBUG'} ) && $ENV{'NPTEST_DEBUG'} ) + { + my( $pkg, $file, $line ) = caller(0); + + print "checkCmd: Called from line $line in $file\n"; + print "Testing : ${command}\n"; + print "Result : ${exitStatus} AND '${output}'\n"; + } + + my $testStatus; + + my $testOutput = "continue"; + + if ( defined( $desiredExitStatus ) ) + { + if ( ref $desiredExitStatus eq "ARRAY" ) + { + if ( scalar( grep { $_ == $exitStatus } @{$desiredExitStatus} ) ) + { + $desiredExitStatus = $exitStatus; + } + else + { + $desiredExitStatus = -1; + } + } + elsif ( ref $desiredExitStatus eq "HASH" ) + { + if ( exists( ${$desiredExitStatus}{$exitStatus} ) ) + { + if ( defined( ${$desiredExitStatus}{$exitStatus} ) ) + { + $testOutput = ${$desiredExitStatus}{$exitStatus}; + } + $desiredExitStatus = $exitStatus; + } + else + { + $desiredExitStatus = -1; + } + } + + if ( %exceptions && exists( $exceptions{$exitStatus} ) ) + { + $testStatus += skip( $exceptions{$exitStatus}, $exitStatus, $desiredExitStatus ); + } + else + { + $testStatus += ok( $exitStatus, $desiredExitStatus ); + } + } + + if ( defined( $desiredOutput ) ) + { + if ( $testOutput ne "skip" ) + { + $testStatus += ok( $output, $desiredOutput ); + } + else + { + $testStatus += skip( "Skipping output test as requested", $output, $desiredOutput ); + } + } + + return $testStatus; +} + + +sub skipMissingCmd +{ + my( $command, $count ) = @_; + + my $testStatus; + + for ( 1 .. $count ) + { + $testStatus += skip( "Missing ${command} - tests skipped", 1 ); + } + + return $testStatus; +} + +sub getTestParameter +{ + my( $param, $envvar, $default, $brief, $scoped ) = @_; + + # Apply default values for optional arguments + $scoped = ( defined( $scoped ) && $scoped ); + + my $testharness = basename( (caller(0))[1], ".t" ); # used for scoping + + if ( defined( $envvar ) && exists( $ENV{$envvar} ) && $ENV{$envvar} ) + { + return $ENV{$envvar} + } + + my $cachedValue = SearchCache( $param, $testharness ); + if ( defined( $cachedValue ) && $cachedValue ) + { + return $cachedValue; + } + + my $defaultValid = ( defined( $default ) && $default ); + my $autoAcceptDefault = ( exists( $ENV{'NPTEST_ACCEPTDEFAULT'} ) && $ENV{'NPTEST_ACCEPTDEFAULT'} ); + + if ( $autoAcceptDefault && $defaultValid ) + { + return $default; + } + + my $userResponse = ""; + + while ( $userResponse eq "" ) + { + print STDERR "\n"; + print STDERR "Test Harness : $testharness\n"; + print STDERR "Test Parameter : $param\n"; + print STDERR "Environment Variable : $envvar\n"; + print STDERR "Brief Description : $brief\n"; + print STDERR "Enter value ", ($defaultValid ? "[${default}]" : "[]"), " => "; + $userResponse = ; + $userResponse = "" if ! defined( $userResponse ); # Handle EOF + chomp( $userResponse ); + if ( $defaultValid && $userResponse eq "" ) + { + $userResponse = $default; + } + } + + print STDERR "\n"; + + # define all user responses at global scope + SetCacheParameter( $param, ( $scoped ? $testharness : undef ), $userResponse ); + + return $userResponse; +} + +# +# Internal Cache Management Functions +# + +sub SearchCache +{ + my( $param, $scope ) = @_; + + LoadCache(); + + if ( exists( $CACHE{$scope} ) && exists( $CACHE{$scope}{$param} ) ) + { + return $CACHE{$scope}{$param}; + } + + if ( exists( $CACHE{$param} ) ) + { + return $CACHE{$param}; + } +} + +sub SetCacheParameter +{ + my( $param, $scope, $value ) = @_; + + if ( defined( $scope ) ) + { + $CACHE{$scope}{$param} = $value; + } + else + { + $CACHE{$param} = $value; + } + + SaveCache(); +} + +sub LoadCache +{ + return if exists( $CACHE{'_cache_loaded_'} ); + + if ( -f $CACHEFILENAME ) + { + my( $fileHandle ) = new IO::File; + + if ( ! $fileHandle->open( "< ${CACHEFILENAME}" ) ) + { + print STDERR "NPTest::LoadCache() : Problem opening ${CACHEFILENAME} : $!\n"; + return; + } + + my( $fileContents ) = join( "\n", <$fileHandle> ); + + $fileHandle->close(); + + my( $contentsRef ) = eval $fileContents; + %CACHE = %{$contentsRef}; + + } + + $CACHE{'_cache_loaded_'} = 1; +} + + +sub SaveCache +{ + delete $CACHE{'_cache_loaded_'}; + + my( $fileHandle ) = new IO::File; + + if ( ! $fileHandle->open( "> ${CACHEFILENAME}" ) ) + { + print STDERR "NPTest::LoadCache() : Problem saving ${CACHEFILENAME} : $!\n"; + return; + } + + my( $dataDumper ) = new Data::Dumper( [ \%CACHE ] ); + + $dataDumper->Terse(1); + + print $fileHandle $dataDumper->Dump(); + + $fileHandle->close(); + + $CACHE{'_cache_loaded_'} = 1; +} + +# +# (Questionable) Public Cache Management Functions +# + +sub SetCacheFilename +{ + my( $filename ) = @_; + + # Unfortunately we can not validate the filename + # in any meaningful way, as it may not yet exist + $CACHEFILENAME = $filename; +} + + +# +# Test Harness Wrapper Functions +# + +sub DetermineTestHarnessDirectory +{ + my( $userSupplied ) = @_; + + # User Supplied + if ( defined( $userSupplied ) && $userSupplied ) + { + if ( -d $userSupplied ) + { + return $userSupplied; + } + else + { + return undef; # userSupplied is invalid -> FAIL + } + } + + # Simple Case : "t" is a subdirectory of the current directory + if ( -d "./t" ) + { + return "./t"; + } + + # To be honest I don't understand which case satisfies the + # original code in test.pl : when $tstdir == `pwd` w.r.t. + # $tstdir =~ s|^(.*)/([^/]+)/?$|$1/$2|; and if (-d "../../$2/t") + # Assuming pwd is "/a/b/c/d/e" then we are testing for "/a/b/c/e/t" + # if I understand the code correctly (a big assumption) + + # Simple Case : the current directory is "t" + my $pwd = cwd(); + + if ( $pwd =~ m|/t$| ) + { + return $pwd; + + # The alternate that might work better is + # chdir( ".." ); + # return "./t"; + # As the current test harnesses assume the application + # to be tested is in the current directory (ie "./check_disk ....") + } + + return undef; +} + +sub TestsFrom +{ + my( $directory, $excludeIfAppMissing ) = @_; + + $excludeIfAppMissing = 0 unless defined( $excludeIfAppMissing ); + + if ( ! opendir( DIR, $directory ) ) + { + print STDERR "NPTest::TestsFrom() - Failed to open ${directory} : $!\n"; + return (); + } + + my( @tests ) = (); + + my $filename; + my $application; + + while ( $filename = readdir( DIR ) ) + { + if ( $filename =~ m/\.t$/ ) + { + if ( $excludeIfAppMissing ) + { + $application = basename( $filename, ".t" ); + if ( ! -e $application ) + { + print STDERR "No application (${application}) found for test harness (${filename})\n"; + next; + } + } + push @tests, "${directory}/${filename}"; + } + } + + closedir( DIR ); + + return @tests; +} + + + +1; +# +# End of File +# diff --git a/configure.in b/configure.in index a7f7b0e..f80be76 100644 --- a/configure.in +++ b/configure.in @@ -2,7 +2,7 @@ dnl Process this file with autoconf to produce a configure script. AC_REVISION ($Revision$) AC_PREREQ(2.58) AC_INIT(nagios-plugins,1.5) -AC_CONFIG_SRCDIR(Helper.pm) +AC_CONFIG_SRCDIR(NPTest.pm) AM_INIT_AUTOMAKE AM_CONFIG_HEADER(config.h) AC_CANONICAL_HOST diff --git a/plugins-scripts/t/check_rpc.t b/plugins-scripts/t/check_rpc.t index afcb867..9fff0ee 100644 --- a/plugins-scripts/t/check_rpc.t +++ b/plugins-scripts/t/check_rpc.t @@ -1,19 +1,22 @@ +#! /usr/bin/perl -w -I .. +# +# Remote Procedure Call (RPC) Tests via check_rpc +# +# $Id$ +# + use strict; use Test; -use vars qw($tests); +use NPTest; +use vars qw($tests); BEGIN {$tests = 2; plan tests => $tests} -my $null = ''; -my $cmd; -my $str; -my $t=0; +my $successOutput = '/^check_rpc/'; + +my $t; -$cmd = "./check_rpc -V"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^check_rpc/'; +$t += checkCmd( "./check_rpc -V", 0, $successOutput ); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t index f1e436d..f2427fb 100644 --- a/plugins/t/check_disk.t +++ b/plugins/t/check_disk.t @@ -1,31 +1,33 @@ +#! /usr/bin/perl -w -I .. +# +# Disk Space Tests via check_disk +# +# $Id$ +# + use strict; use Test; +use NPTest; + use vars qw($tests); +BEGIN {$tests = 10; plan tests => $tests} + +my $successOutput = '/^DISK OK - /'; +my $failureOutput = '/^DISK CRITICAL - /'; + +my $mountpoint_valid = getTestParameter( "mountpoint_valid", "NP_MOUNTPOINT_VALID", "/", + "The path to a valid mountpoint" ); -BEGIN {$tests = 6; plan tests => $tests} +my $mountpoint_invalid = getTestParameter( "mountpoint_invalid", "NP_MOUNTPOINT_INVALID", "/missing", + "The path to a invalid (non-existent) mountpoint" ); -my $null = ''; -my $cmd; -my $str; my $t; -$cmd = "./check_disk 100 100 /"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^(Disk ok - +[\.0-9]+|DISK OK - )/'; - -$cmd = "./check_disk -w 0 -c 0 /"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^(Disk ok - +[\.0-9]+|DISK OK - )/'; - -$cmd = "./check_disk 0 0 /"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); -$t += ok $str, '/^(Only +[\.0-9]+|DISK CRITICAL - )/'; +$t += checkCmd( "./check_disk 100 100 ${mountpoint_valid}", 0, $successOutput ); +$t += checkCmd( "./check_disk -w 0 -c 0 ${mountpoint_valid}", 0, $successOutput ); +$t += checkCmd( "./check_disk -w 1\% -c 1\% ${mountpoint_valid}", 0, $successOutput ); +$t += checkCmd( "./check_disk 0 0 ${mountpoint_valid}", 2, $failureOutput ); +$t += checkCmd( "./check_disk 100 100 ${mountpoint_invalid}", 2, '/not found/' ); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); diff --git a/plugins/t/check_dns.t b/plugins/t/check_dns.t index bee1d34..fbaca79 100644 --- a/plugins/t/check_dns.t +++ b/plugins/t/check_dns.t @@ -1,28 +1,42 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# Domain Name Server (DNS) Tests via check_dns +# +# $Id$ +# use strict; -use Cache; use Test; +use NPTest; + use vars qw($tests); +BEGIN {$tests = 6; plan tests => $tests} + +my $successOutput = '/DNS OK: [\.0-9]+ seconds response time/'; -BEGIN {$tests = 3; plan tests => $tests} +my $hostname_valid = getTestParameter( "hostname_valid", "NP_HOSTNAME_VALID", "localhost", + "A valid (known to DNS) hostname" ); -#`nslookup localhost > /dev/null 2>&1` || exit(77); +my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", + "An invalid (not known to DNS) hostname" ); + +my $dns_server = getTestParameter( "dns_server", "NP_DNS_SERVER", undef, + "A non default (remote) DNS server" ); -my $null = ''; -my $cmd; -my $str; my $t; -$str = `./check_dns $Cache::dnshost -to 5`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/DNS OK: +[\.0-9]+ seconds response time, /'; +# +# Default DNS Server +# +$t += checkCmd( "./check_dns -H $hostname_valid -t 5", 0, $successOutput ); +$t += checkCmd( "./check_dns -H $hostname_invalid -t 1", 2 ); -$cmd = "./check_dns $Cache::nullhost -to 1"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); +# +# Specified DNS Server +# +$t += checkCmd( "./check_dns -H $hostname_valid -s $dns_server -t 5", 0, $successOutput ); +$t += checkCmd( "./check_dns -H $hostname_invalid -s $dns_server -t 1", 2 ); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); + diff --git a/plugins/t/check_fping.t b/plugins/t/check_fping.t index 629ee35..c59d59e 100644 --- a/plugins/t/check_fping.t +++ b/plugins/t/check_fping.t @@ -1,37 +1,43 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# FPing Tests via check_fping +# # $Id$ +# use strict; -use Cache; use Test; +use NPTest; + use vars qw($tests); -BEGIN {$tests = 3; plan tests => $tests} +BEGIN {$tests = 4; plan tests => $tests} -exit(0) unless (-x "./check_fping"); +my $successOutput = '/^FPING OK - /'; +my $failureOutput = '/^FPING CRITICAL - /'; -#`fping 127.0.0.1 > /dev/null 2>&1` || exit(77); +my $host_responsive = getTestParameter( "host_responsive", "NP_HOST_RESPONSIVE", "localhost", + "The hostname of system responsive to network requests" ); -my $null = ''; -my $cmd; -my $str; -my $t; -my $stat; +my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", + "The hostname of system not responsive to network requests" ); +my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", + "An invalid (not known to DNS) hostname" ); -$cmd = "./check_fping 127.0.0.1"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^FPING OK - 127.0.0.1/'; -$cmd = "./check_fping $Cache::nullhost"; -$str = `$cmd`; -if ($?>>8 == 1 or $?>>8 == 2) { - $stat = 2; +my $t; + +if ( -x "./check_fping" ) +{ + $t += checkCmd( "./check_fping $host_responsive", 0, $successOutput ); + $t += checkCmd( "./check_fping $host_nonresponsive", [ 1, 2 ] ); + $t += checkCmd( "./check_fping $hostname_invalid", [ 1, 2 ] ); +} +else +{ + $t += skipMissingCmd( "./check_fping", $tests ); } -$t += ok $stat,2; -print "Test was: $cmd\n" if (($?>>8) < 1); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); diff --git a/plugins/t/check_ftp.t b/plugins/t/check_ftp.t index d9e9d9c..47a73e1 100644 --- a/plugins/t/check_ftp.t +++ b/plugins/t/check_ftp.t @@ -1,32 +1,34 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# File Transfer Protocol (FTP) Test via check_ftp +# +# $Id$ +# -#use strict; -use Cache; +use strict; use Test; +use NPTest; + use vars qw($tests); +BEGIN {$tests = 4; plan tests => $tests} -BEGIN {$tests = 3; plan tests => $tests} +my $host_tcp_ftp = getTestParameter( "host_tcp_ftp", "NP_HOST_TCP_FTP", "localhost", + "A host providing the FTP Service (an FTP server)"); -my $null = ''; -my $cmd; -my $str; -my $t; +my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", + "The hostname of system not responsive to network requests" ); -$cmd = "./check_ftp $Cache::hostname -wt 300 -ct 600"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/FTP OK -\s+[0-9]?\.?[0-9]+ second response time/'; +my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", + "An invalid (not known to DNS) hostname" ); -#$cmd = "./check_ftp $Cache::noserver -wt 0 -ct 0"; -#$str = `$cmd`; -#$t += ok $?>>8,2; -#print "Test was: $cmd\n" unless ($?); +my $successOutput = '/FTP OK -\s+[0-9]?\.?[0-9]+ second response time/'; -$cmd = "./check_ftp $Cache::nullhost -wt 0 -ct 0 -to 1"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); +my $t; + +$t += checkCmd( "./check_ftp $host_tcp_ftp -wt 300 -ct 600", 0, $successOutput ); +$t += checkCmd( "./check_ftp $host_nonresponsive -wt 0 -ct 0 -to 1", 2 ); +$t += checkCmd( "./check_ftp $hostname_invalid -wt 0 -ct 0", 2 ); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); + diff --git a/plugins/t/check_hpjd.t b/plugins/t/check_hpjd.t index b4e198d..b749c77 100644 --- a/plugins/t/check_hpjd.t +++ b/plugins/t/check_hpjd.t @@ -1,32 +1,42 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# HP JetDirect Test via check_hpjd +# +# $Id$ +# use strict; -use Helper; -use Cache; use Test; +use NPTest; + use vars qw($tests); +BEGIN {$tests = 5; plan tests => $tests} -BEGIN {$tests = 4; plan tests => $tests} +my $successOutput = '/^Printer ok - /'; +my $failureOutput = '/Timeout: No [Rr]esponse from /'; -exit(0) unless (-x "./check_hpjd"); +my $host_tcp_hpjd = getTestParameter( "host_tcp_hpjd", "NP_HOST_TCP_HPJD", undef, + "A host (usually a printer) providing the HP-JetDirect Services" ); -my $null = ''; -my $cmd; -my $str; -my $t; -my $printer = get_option("hpjd_printer","HP Jet-Direct card address"); +my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", + "The hostname of system not responsive to network requests" ); -$cmd = "./check_hpjd $printer"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^Printer ok - /'; +my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", + "An invalid (not known to DNS) hostname" ); -$cmd = "./check_hpjd $Cache::noserver"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); -$t += ok $str, '/Timeout: No response from /'; +my $t; + +if ( -x "./check_hpjd" ) +{ + $t += checkCmd( "./check_hpjd $host_tcp_hpjd", 0, $successOutput ); + $t += checkCmd( "./check_hpjd $host_nonresponsive", 2, $failureOutput ); + $t += checkCmd( "./check_hpjd $hostname_invalid", 3 ); +} +else +{ + $t += skipMissingCmd( "./check_hpjd", $tests ); +} exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); + diff --git a/plugins/t/check_http.t b/plugins/t/check_http.t index 5be549a..56d939b 100644 --- a/plugins/t/check_http.t +++ b/plugins/t/check_http.t @@ -1,22 +1,36 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# HyperText Transfer Protocol (HTTP) Test via check_http +# +# $Id$ +# use strict; -use Cache; use Test; +use NPTest; + use vars qw($tests); +BEGIN {$tests = 4; plan tests => $tests} -BEGIN {$tests = 3; plan tests => $tests} +my $host_tcp_http = getTestParameter( "host_tcp_http", "NP_HOST_TCP_HTTP", "localhost", + "A host providing the HTTP Service (a web server)" ); -my $null = ''; -my $str; -my $t; +my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", + "The hostname of system not responsive to network requests" ); -$str = `./check_http $Cache::httphost -wt 300 -ct 600`; -$t += ok $?>>8,0; -$t += ok $str, '/(HTTP\s[o|O][k|K]\s)?\s?HTTP\/1.[01]\s[0-9]{3}\s(OK|Found)\s-\s+[0-9]+\sbytes\sin\s+([0-9]+|[0-9]+\.[0-9]+)\sseconds/'; +my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", + "An invalid (not known to DNS) hostname" ); -$str = `./check_http $Cache::nullhost -wt 1 -ct 2`; -$t += ok $?>>8,2; +my $successOutput = '/(HTTP\s[o|O][k|K]\s)?\s?HTTP\/1.[01]\s[0-9]{3}\s(OK|Found)\s-\s+[0-9]+\sbytes\sin\s+([0-9]+|[0-9]+\.[0-9]+)\sseconds/'; + +my %exceptions = ( 2 => "No Web Server present?" ); + +my $t; + +$t += checkCmd( "./check_http $host_tcp_http -wt 300 -ct 600", { 0 => 'continue', 2 => 'skip' }, $successOutput, %exceptions ); +$t += checkCmd( "./check_http $host_nonresponsive -wt 1 -ct 2", 2 ); +$t += checkCmd( "./check_http $hostname_invalid -wt 1 -ct 2", 2 ); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); + diff --git a/plugins/t/check_imap.t b/plugins/t/check_imap.t index 47494e5..f86faa4 100644 --- a/plugins/t/check_imap.t +++ b/plugins/t/check_imap.t @@ -1,34 +1,39 @@ -#! /usr/bin/perl -w - -#use strict; -use Cache; +#! /usr/bin/perl -w -I .. +# +# Internet Mail Access Protocol (IMAP) Server Tests via check_imap +# +# $Id$ +# + +use strict; use Test; +use NPTest; + use vars qw($tests); +BEGIN {$tests = 5; plan tests => $tests} -BEGIN {$tests = 3; plan tests => $tests} +my $host_tcp_smtp = getTestParameter( "host_tcp_smtp", "NP_HOST_TCP_SMTP", "mailhost", + "A host providing an STMP Service (a mail server)"); -my $null = ''; -my $cmd; -my $str; -my $t; +my $host_tcp_imap = getTestParameter( "host_tcp_imap", "NP_HOST_TCP_IMAP", $host_tcp_smtp, + "A host providing an IMAP Service (a mail server)"); -$cmd = "./check_imap $Cache::mailhost"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); +my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", + "The hostname of system not responsive to network requests" ); -$cmd = "./check_imap -H $Cache::mailhost -p 143 -w 9 -c 9 -t 10 -e '* OK'"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); +my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", + "An invalid (not known to DNS) hostname" ); +my %exceptions = ( 2 => "No IMAP Server present?" ); + +my $t; + +$t += checkCmd( "./check_imap $host_tcp_imap", 0, undef, %exceptions ); +$t += checkCmd( "./check_imap -H $host_tcp_imap -p 143 -w 9 -c 9 -t 10 -e '* OK'", 0, undef, %exceptions ); +$t += checkCmd( "./check_imap $host_tcp_imap -p 143 -wt 9 -ct 9 -to 10 -e '* OK'", 0, undef, %exceptions ); +$t += checkCmd( "./check_imap $host_nonresponsive", 2 ); +$t += checkCmd( "./check_imap $hostname_invalid", 2 ); -# Reverse compatibility -$cmd = "./check_imap $Cache::mailhost -p 143 -wt 9 -ct 9 -to 10 -e '* OK'"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); - diff --git a/plugins/t/check_load.t b/plugins/t/check_load.t index 414e09d..8f954dc 100644 --- a/plugins/t/check_load.t +++ b/plugins/t/check_load.t @@ -1,27 +1,25 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# Load Average Tests via check_load +# +# $Id$ +# use strict; use Test; -use vars qw($tests); +use NPTest; +use vars qw($tests); BEGIN {$tests = 4; plan tests => $tests} -my $null = ''; -my $cmd; -my $str; -my $t; +my $successOutput = '/^OK - load average: [0-9]\.?[0-9]+, [0-9]\.?[0-9]+, [0-9]\.?[0-9]+/'; +my $failureOutput = '/^CRITICAL - load average: [0-9]\.?[0-9]+, [0-9]\.?[0-9]+, [0-9]\.?[0-9]+/'; -$cmd = "./check_load -w 100,100,100 -c 100,100,100"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^OK - load average: [0-9]\.?[0-9]+, [0-9]\.?[0-9]+, [0-9]\.?[0-9]+/'; +my $t; -$cmd = "./check_load -w 0,0,0 -c 0,0,0"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); -$t += ok $str, '/^CRITICAL - load average: [0-9]\.?[0-9]+, [0-9]\.?[0-9]+, [0-9]\.?[0-9]+/'; +$t += checkCmd( "./check_load -w 100,100,100 -c 100,100,100", 0, $successOutput ); +$t += checkCmd( "./check_load -w 0,0,0 -c 0,0,0", 2, $failureOutput ); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); + diff --git a/plugins/t/check_mysql.t b/plugins/t/check_mysql.t index 0fae65f..ad42359 100644 --- a/plugins/t/check_mysql.t +++ b/plugins/t/check_mysql.t @@ -1,26 +1,33 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# MySQL Database Server Tests via check_mysql +# +# $Id$ +# use strict; -use Helper; -use Cache; use Test; +use NPTest; + use vars qw($tests); BEGIN {$tests = 2; plan tests => $tests} -exit(0) unless (-x "./check_mysql"); - -my $null = ''; -my $cmd; -my $str; my $t; -my $mysqlserver = get_option("mysqlserver","host for MYSQL tests"); +my $failureOutput = '/Access denied for user: /'; + +if ( -x "./check_mysql" ) +{ + my $mysqlserver = getTestParameter( "mysql_server", "NP_MYSQL_SERVER", undef, + "A MySQL Server"); -$cmd = "./check_mysql -H $mysqlserver -P 3306"; -$str = `$cmd`; -$t += ok $?>>8,2; -$t += ok $str, '/Access denied for user: /'; + $t += checkCmd( "./check_mysql -H $mysqlserver -P 3306", 2, $failureOutput ); +} +else +{ + $t += skipMissingCmd( "./check_mysql", $tests ); +} exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); diff --git a/plugins/t/check_ping.t b/plugins/t/check_ping.t index 97bc660..49c568a 100644 --- a/plugins/t/check_ping.t +++ b/plugins/t/check_ping.t @@ -1,33 +1,36 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# Ping Response Tests via check_ping +# +# $Id$ +# use strict; -use Cache; use Test; +use NPTest; + use vars qw($tests); -BEGIN {$tests = 5; plan tests => $tests} +BEGIN {$tests = 6; plan tests => $tests} + +my $successOutput = '/PING (ok|OK) - Packet loss = +[0-9]{1,2}\%, +RTA = [\.0-9]+ ms/'; +my $failureOutput = '/Packet loss = +[0-9]{1,2}\%, +RTA = [\.0-9]+ ms/'; + +my $host_responsive = getTestParameter( "host_responsive", "NP_HOST_RESPONSIVE", "localhost", + "The hostname of system responsive to network requests" ); + +my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", + "The hostname of system not responsive to network requests" ); + +my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", + "An invalid (not known to DNS) hostname" ); -my $null = ''; -my $cmd; -my $str; my $t; -$cmd = "./check_ping 127.0.0.1 100 100 1000 1000 -p 1"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/PING (ok|OK) - Packet loss = +[0-9]{1,2}\%, +RTA = [\.0-9]+ ms/'; - -$cmd = "./check_ping 127.0.0.1 0 0 0 0 -p 1"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); -$t += ok $str, '/Packet loss = +[0-9]{1,2}\%, +RTA = [\.0-9]+ ms/'; - -$cmd = "./check_ping $Cache::nullhost 0 0 0 0 -p 1 -to 1"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); +$t += checkCmd( "./check_ping $host_responsive 100 100 1000 1000 -p 1", 0, $successOutput ); +$t += checkCmd( "./check_ping $host_responsive 0 0 0 0 -p 1", 2, $failureOutput ); +$t += checkCmd( "./check_ping $host_nonresponsive 0 0 0 0 -p 1 -to 1", 2 ); +$t += checkCmd( "./check_ping $hostname_invalid 0 0 0 0 -p 1 -to 1", 3 ); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); diff --git a/plugins/t/check_pop.t b/plugins/t/check_pop.t index 60b5a4e..e78f963 100644 --- a/plugins/t/check_pop.t +++ b/plugins/t/check_pop.t @@ -1,31 +1,38 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# Post Office Protocol (POP) Server Tests via check_pop +# +# $Id$ +# -#use strict; -use Cache; +use strict; use Test; +use NPTest; + use vars qw($tests); +BEGIN {$tests = 5; plan tests => $tests} -BEGIN {$tests = 3; plan tests => $tests} +my $host_tcp_smtp = getTestParameter( "host_tcp_smtp", "NP_HOST_TCP_SMTP", "mailhost", + "A host providing an STMP Service (a mail server)"); -my $null = ''; -my $cmd; -my $str; -my $t; +my $host_tcp_pop = getTestParameter( "host_tcp_pop", "NP_HOST_TCP_POP", $host_tcp_smtp, + "A host providing an POP Service (a mail server)"); + +my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", + "The hostname of system not responsive to network requests" ); -$cmd = "./check_pop $Cache::mailhost"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); +my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", + "An invalid (not known to DNS) hostname" ); -$cmd = "./check_pop -H $Cache::mailhost -p 110 -w 9 -c 9 -t 10 -e '+OK'"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); +my %exceptions = ( 2 => "No POP Server present?" ); + +my $t; -$cmd = "./check_pop $Cache::mailhost -p 110 -wt 9 -ct 9 -to 10 -e '+OK'"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); +$t += checkCmd( "./check_pop $host_tcp_pop", 0, undef, %exceptions ); +$t += checkCmd( "./check_pop -H $host_tcp_pop -p 110 -w 9 -c 9 -t 10 -e '+OK'", 0, undef, %exceptions ); +$t += checkCmd( "./check_pop $host_tcp_pop -p 110 -wt 9 -ct 9 -to 10 -e '+OK'", 0, undef, %exceptions ); +$t += checkCmd( "./check_pop $host_nonresponsive", 2 ); +$t += checkCmd( "./check_pop $hostname_invalid", 2 ); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); diff --git a/plugins/t/check_procs.t b/plugins/t/check_procs.t index da49ac6..cb5f122 100644 --- a/plugins/t/check_procs.t +++ b/plugins/t/check_procs.t @@ -1,51 +1,24 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# Process Tests via check_procs +# +# $Id$ +# use strict; -use Cache; use Test; -use vars qw($tests); +use NPTest; +use vars qw($tests); BEGIN {$tests = 10; plan tests => $tests} -my $null = ''; -my $cmd; -my $str; my $t; -# Reverse Compatibility -$cmd = "./check_procs -w 100000 -c 100000"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^PROCS OK: [0-9]+ process(es)?$/'; - -# Reverse Compatibility -$cmd = "./check_procs -w 100000 -c 100000 -s Z"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^PROCS OK: [0-9]+ process(es)? with /'; - -# Reverse Compatibility -$cmd = "./check_procs -w 0 -c 10000000"; -$str = `$cmd`; -$t += ok $?>>8,1; -print "Test was: $cmd\n" unless ($?); -$t += ok $str, '/^PROCS WARNING: [0-9]+ process(es)?$/'; - -# Reverse Compatibility -$cmd = "./check_procs -w 0 -c 0"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); -$t += ok $str, '/^PROCS CRITICAL: [0-9]+ process(es)?$/'; - -# Reverse Compatibility -$cmd = "./check_procs -w 0 -c 0 -s S"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); -$t += ok $str, '/^PROCS CRITICAL: [0-9]+ process(es)? with /'; +$t += checkCmd( "./check_procs -w 100000 -c 100000", 0, '/^PROCS OK: [0-9]+ process(es)?$/' ); +$t += checkCmd( "./check_procs -w 100000 -c 100000 -s Z", 0, '/^PROCS OK: [0-9]+ process(es)? with /' ); +$t += checkCmd( "./check_procs -w 0 -c 10000000", 1, '/^PROCS WARNING: [0-9]+ process(es)?$/' ); +$t += checkCmd( "./check_procs -w 0 -c 0", 2, '/^PROCS CRITICAL: [0-9]+ process(es)?$/' ); +$t += checkCmd( "./check_procs -w 0 -c 0 -s S", 2, '/^PROCS CRITICAL: [0-9]+ process(es)? with /' ); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); diff --git a/plugins/t/check_smtp.t b/plugins/t/check_smtp.t index 2a82b87..3bf32ec 100644 --- a/plugins/t/check_smtp.t +++ b/plugins/t/check_smtp.t @@ -1,31 +1,34 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# Simple Mail Transfer Protocol (SMTP) Test via check_smtp +# +# $Id$ +# -#use strict; -use Cache; +use strict; use Test; +use NPTest; + use vars qw($tests); +BEGIN {$tests = 5; plan tests => $tests} -BEGIN {$tests = 3; plan tests => $tests} +my $host_tcp_smtp = getTestParameter( "host_tcp_smtp", "NP_HOST_TCP_SMTP", "mailhost", + "A host providing an STMP Service (a mail server)"); -my $null = ''; -my $cmd; -my $str; -my $t; +my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", + "The hostname of system not responsive to network requests" ); -$cmd = "./check_smtp $Cache::mailhost"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); +my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", + "An invalid (not known to DNS) hostname" ); +my %exceptions = ( 2 => "No SMTP Server present?" ); -$cmd = "./check_smtp -H $Cache::mailhost -p 25 -t 1 -w 9 -c 9 -t 10 -e 220"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); +my $t; -$cmd = "./check_smtp -H $Cache::mailhost -p 25 -wt 9 -ct 9 -to 10 -e 220"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); +$t += checkCmd( "./check_smtp $host_tcp_smtp", 0, undef, %exceptions ); +$t += checkCmd( "./check_smtp -H $host_tcp_smtp -p 25 -t 1 -w 9 -c 9 -t 10 -e 220", 0, undef, %exceptions ); +$t += checkCmd( "./check_smtp -H $host_tcp_smtp -p 25 -wt 9 -ct 9 -to 10 -e 220", 0, undef, %exceptions ); +$t += checkCmd( "./check_smtp $host_nonresponsive", 2 ); +$t += checkCmd( "./check_smtp $hostname_invalid", 3 ); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); diff --git a/plugins/t/check_snmp.t b/plugins/t/check_snmp.t index 162b0b9..b45b6c0 100644 --- a/plugins/t/check_snmp.t +++ b/plugins/t/check_snmp.t @@ -1,52 +1,57 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# Simple Network Management Protocol (SNMP) Test via check_snmp +# +# $Id$ +# use strict; -use Helper; -use Cache; use Test; -use vars qw($tests); +use NPTest; -BEGIN {$tests = 8; plan tests => $tests} +use vars qw($tests); +BEGIN {$tests = 12; plan tests => $tests} -my $null = ''; -my $cmd; -my $str; my $t; -my $community=get_option("snmp_community","SNMP community name"); - -exit(0) unless (-x "./check_snmp"); - -$cmd = "./check_snmp -H 127.0.0.1 -C $community -o system.sysUpTime.0 -w 1: -c 1:"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -chomp $str; -$t += ok $str, '/^SNMP OK - \d+/'; - -$cmd = "./check_snmp -H 127.0.0.1 -C $community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 1:1 -c 1:1"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -chomp $str; -$t += ok $str, '/^SNMP OK - 1\s*$/'; - -$cmd = "./check_snmp -H 127.0.0.1 -C $community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 0 -c 1:"; -$str = `$cmd`; -$t += ok $?>>8,1; -print "Test was: $cmd\n" unless ($?); -chomp $str; -$t += ok $str, '/^SNMP WARNING - \*1\*\s*$/'; - -$cmd = "./check_snmp -H 127.0.0.1 -C $community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w :0 -c 0"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); -chomp $str; -$t += ok $str, '/^SNMP CRITICAL - \*1\*\s*$/'; - -#host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 = 1 -#enterprises.ucdavis.memory.memAvailSwap.0 -#./check_snmp 127.0.0.1 -C staff -o enterprises.ucdavis.diskTable.dskEntry.dskAvail.1,enterprises.ucdavis.diskTable.dskEntry.dskPercent.1 -w 100000: -c 50000: -l Space on root -u 'bytes free (','% used)' + +if ( -x "./check_snmp" ) +{ + my $host_snmp = getTestParameter( "host_snmp", "NP_HOST_SNMP", "localhost", + "A host providing an SNMP Service"); + + my $snmp_community = getTestParameter( "snmp_community", "NP_SNMP_COMMUNITY", "public", + "The SNMP Community string for SNMP Testing" ); + + my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", + "The hostname of system not responsive to network requests" ); + + my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", + "An invalid (not known to DNS) hostname" ); + + my %exceptions = ( 3 => "No SNMP Server present?" ); + + + $t += checkCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:", + { 0 => 'continue', 3 => 'skip' }, '/^SNMP OK - \d+/', %exceptions ); + + $t += checkCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 1:1 -c 1:1", + { 0 => 'continue', 3 => 'skip' }, '/^SNMP OK - 1\s*$/', %exceptions ); + + $t += checkCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 0 -c 1:", + { 1 => 'continue', 3 => 'skip' }, '/^SNMP WARNING - \*1\*\s*$/', %exceptions ); + + $t += checkCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w :0 -c 0", + { 2 => 'continue', 3 => 'skip' }, '/^SNMP CRITICAL - \*1\*\s*$/', %exceptions ); + + $t += checkCmd( "./check_snmp -H $host_nonresponsive -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:", 3, '/SNMP problem - /' ); + + $t += checkCmd( "./check_snmp -H $hostname_invalid -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:", 3, '/SNMP problem - /' ); + +} +else +{ + $t += skipMissingCmd( "./check_snmp", $tests ); +} exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t index 5b702f0..348010d 100644 --- a/plugins/t/check_swap.t +++ b/plugins/t/check_swap.t @@ -1,34 +1,25 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# Swap Space Tests via check_swap +# +# $Id$ +# use strict; -use Cache; use Test; -use vars qw($tests); +use NPTest; +use vars qw($tests); BEGIN {$tests = 6; plan tests => $tests} -my $null = ''; -my $cmd; -my $str; my $t; -$cmd = "./check_swap 100 100"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^Swap ok - Swap used\: +[0-9]{1,2}\% \([0-9]+ bytes out of [0-9]+\)$/'; - -$cmd = "./check_swap 0 0"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); -$t += ok $str, '/^CRITICAL - Swap used\: +[0-9]{1,2}\% \([0-9]+ bytes out of [0-9]+\)$/'; +my $successOutput = '/^SWAP OK - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; +my $failureOutput = '/^SWAP CRITICAL - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; -$cmd = "./check_swap 100 100 1000000000 1000000000"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); -$t += ok $str, '/^CRITICAL - Swap used\: +[0-9]{1,2}\% \([0-9]+ bytes out of [0-9]+\)$/'; +$t += checkCmd( "./check_swap -w 1048576 -c 1048576", 0, $successOutput ); # 1MB free +$t += checkCmd( "./check_swap -w 1\% -c 1\%", 0, $successOutput ); # 1% free +$t += checkCmd( "./check_swap -w 100\% -c 100\%", 2, $failureOutput ); # 100% free (always fails) exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); diff --git a/plugins/t/check_tcp.t b/plugins/t/check_tcp.t index 21c3b77..ffe559d 100644 --- a/plugins/t/check_tcp.t +++ b/plugins/t/check_tcp.t @@ -1,27 +1,34 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# TCP Connection Based Tests via check_tcp +# +# $Id$ +# -#use strict; -use Cache; +use strict; use Test; +use NPTest; + use vars qw($tests); +BEGIN {$tests = 5; plan tests => $tests} -BEGIN {$tests = 3; plan tests => $tests} +my $host_tcp_http = getTestParameter( "host_tcp_http", "NP_HOST_TCP_HTTP", "localhost", + "A host providing the HTTP Service (a web server)" ); -my $null = ''; -my $cmd; -my $str; -my $t; +my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", + "The hostname of system not responsive to network requests" ); + +my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", + "An invalid (not known to DNS) hostname" ); -$cmd = "./check_tcp $Cache::hostname -p 80 -wt 300 -ct 600"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "$cmd\n" if ($?); -$t += ok $str, '/^TCP OK\s-\s+[0-9]?\.?[0-9]+ second response time on port 80/'; +my $successOutput = '/^TCP OK\s-\s+[0-9]?\.?[0-9]+ second response time on port [0-9]+/'; + +my $t; -$cmd = "./check_tcp $Cache::nullhost -p 81 -wt 0 -ct 0 -to 1"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "$cmd\n" unless ($?); +$t += checkCmd( "./check_tcp $host_tcp_http -p 80 -wt 300 -ct 600", 0, $successOutput ); +$t += checkCmd( "./check_tcp $host_tcp_http -p 81 -wt 0 -ct 0 -to 1", 2 ); # use invalid port for this test +$t += checkCmd( "./check_tcp $host_nonresponsive -p 80 -wt 0 -ct 0 -to 1", 2 ); +$t += checkCmd( "./check_tcp $hostname_invalid -p 80 -wt 0 -ct 0 -to 1", 2 ); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); diff --git a/plugins/t/check_time.t b/plugins/t/check_time.t index 4d8c5c2..05878dc 100644 --- a/plugins/t/check_time.t +++ b/plugins/t/check_time.t @@ -1,52 +1,40 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# System Time Tests via check_time +# +# $Id$ +# use strict; -use Cache; -use Helper; use Test; +use NPTest; + use vars qw($tests); +BEGIN {$tests = 8; plan tests => $tests} -BEGIN {$tests = 6; plan tests => $tests} +my $host_udp_time = getTestParameter( "host_udp_time", "NP_HOST_UDP_TIME", "localhost", + "A host providing the UDP Time Service" ); -my $null = ''; -my $cmd; -my $str; -my $t; -my $udp_hostname=get_option("udp_hostname","UDP host name"); +my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", + "The hostname of system not responsive to network requests" ); -# standard mode +my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", + "An invalid (not known to DNS) hostname" ); -$cmd = "./check_time -H $udp_hostname -w 999999,59 -c 999999,59 -t 60"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^TIME OK - [0-9]+ second time difference$/'; +my $successOutput = '/^TIME OK - [0-9]+ second time difference/'; -$cmd = "./check_time -H $udp_hostname -w 999999 -W 59 -c 999999 -C 59 -t 60"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^TIME OK - [0-9]+ second time difference$/'; +my $t; -# reverse compatibility mode +# standard mode +$t += checkCmd( "./check_time -H $host_udp_time -w 999999,59 -c 999999,59 -t 60", 0, $successOutput ); +$t += checkCmd( "./check_time -H $host_udp_time -w 999999 -W 59 -c 999999 -C 59 -t 60", 0, $successOutput ); -$cmd = "./check_time $udp_hostname -wt 59 -ct 59 -cd 999999 -wd 999999 -to 60"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^TIME OK - [0-9]+ second time difference$/'; +# reverse compatibility mode +$t += checkCmd( "./check_time $host_udp_time -wt 59 -ct 59 -cd 999999 -wd 999999 -to 60", 0, $successOutput ); # failure mode - -#$cmd = "./check_time -H $Cache::nullhost -t 1"; -#$str = `$cmd`; -#$t += ok $?>>8,255; -#print "Test was: $cmd\n" unless ($?); - -#$cmd = "./check_time -H $Cache::noserver -t 1"; -#$str = `$cmd`; -#$t += ok $?>>8,255; -#print "$cmd\n" unless ($?); +$t += checkCmd( "./check_time -H $host_nonresponsive -t 1", 2 ); +$t += checkCmd( "./check_time -H $hostname_invalid -t 1", 3 ); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); diff --git a/plugins/t/check_udp.t b/plugins/t/check_udp.t index abbf5e4..c80e08a 100644 --- a/plugins/t/check_udp.t +++ b/plugins/t/check_udp.t @@ -1,24 +1,33 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# UDP Connection Based Tests via check_udp +# +# $Id$ +# -#use strict; -use Cache; -use Helper; +use strict; use Test; +use NPTest; + use vars qw($tests); +BEGIN {$tests = 3; plan tests => $tests} #TODO# Update to 4 when the commented out test is fixed -BEGIN {$tests = 3; plan tests => $tests} +my $host_udp_time = getTestParameter( "host_udp_time", "NP_HOST_UDP_TIME", "localhost", + "A host providing the UDP Time Service" ); -my $null = ''; -my $str; -my $t; -my $hostname=get_option("udp_hostname","UDP host name"); +my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", + "The hostname of system not responsive to network requests" ); + +my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", + "An invalid (not known to DNS) hostname" ); -$str = `./check_udp $hostname -p 37 -wt 300 -ct 600`; -$t += ok $?>>8,0; -$t += ok $str, '/^Connection accepted on port 37 - [0-9]+ second response time$/'; +my $successOutput = '/^Connection accepted on port [0-9]+ - [0-9]+ second response time$/'; + +my $t; -$str = `./check_udp $Cache::nullhost -p 80 -wt 0 -ct 0 -to 1`; -$t += ok $?>>8,2; +$t += checkCmd( "./check_udp -H $host_udp_time -p 37 -wt 300 -ct 600", 0, $successOutput ); +$t += checkCmd( "./check_udp $host_nonresponsive -p 37 -wt 0 -ct 0 -to 1", 2 ); +#TODO# $t += checkCmd( "./check_udp $hostname_invalid -p 37 -wt 0 -ct 0 -to 1", 2 ); # Currently returns 0 (ie success) exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); diff --git a/plugins/t/check_users.t b/plugins/t/check_users.t index 593f173..4b313d3 100644 --- a/plugins/t/check_users.t +++ b/plugins/t/check_users.t @@ -1,28 +1,25 @@ -#! /usr/bin/perl -w +#! /usr/bin/perl -w -I .. +# +# Logged in Users Tests via check_users +# +# $Id$ +# use strict; -use Cache; use Test; -use vars qw($tests); +use NPTest; +use vars qw($tests); BEGIN {$tests = 4; plan tests => $tests} -my $null = ''; -my $cmd; -my $str; -my $t; +my $successOutput = '/^USERS OK - [0-9]+ users currently logged in/'; +my $failureOutput = '/^USERS CRITICAL - [0-9]+ users currently logged in/'; -$cmd = "./check_users 1000 1000"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^USERS OK - +[0-9]+ users currently logged in$/'; +my $t; -$cmd = "./check_users 0 0"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); -$t += ok $str, '/^USERS CRITICAL - [0-9]+ +users currently logged in$/'; +$t += checkCmd( "./check_users 1000 1000", 0, $successOutput ); +$t += checkCmd( "./check_users 0 0", 2, $failureOutput ); exit(0) if defined($Test::Harness::VERSION); exit($tests - $t); + diff --git a/plugins/t/check_vsz.t b/plugins/t/check_vsz.t deleted file mode 100644 index 9597261..0000000 --- a/plugins/t/check_vsz.t +++ /dev/null @@ -1,28 +0,0 @@ -#! /usr/bin/perl -w - -use strict; -use Cache; -use Test; -use vars qw($tests); - -BEGIN {$tests = 4; plan tests => $tests} - -my $null = ''; -my $cmd; -my $str; -my $t; - -$cmd = "./check_vsz 100000 1000000 init"; -$str = `$cmd`; -$t += ok $?>>8,0; -print "Test was: $cmd\n" if ($?); -$t += ok $str, '/^ok \(all VSZ\<[0-9]+\)/'; - -$cmd = "./check_vsz 0 0"; -$str = `$cmd`; -$t += ok $?>>8,2; -print "Test was: $cmd\n" unless ($?); -$t += ok $str, '/^CRITICAL \(VSZ\>[0-9]+\)/'; - -exit(0) if defined($Test::Harness::VERSION); -exit($tests - $t); diff --git a/test.pl.in b/test.pl.in index e88c473..22d0576 100755 --- a/test.pl.in +++ b/test.pl.in @@ -1,91 +1,51 @@ -#!/usr/bin/perl -w +#!/usr/bin/perl -w -I .. -I ../.. +# +# Wrapper for running the test harnesses +# +# $Id$ +# + use strict; -my $file = '../Cache'; -unless (-f "$file.pm") { - open(CACHE,">$file.pm") or die "Cannot open cache"; - print CACHE "package Cache; -require Exporter; -\@ISA=qw(Exporter); -\@EXPORT=qw(); -1; -"; - close CACHE; -} +use Getopt::Long; -use Helper; -my ($tstdir,$spath,$hostname,$httphost,$mailhost,$dnshost,$noserver,$nullhost,$quickcheck); +use NPTest qw(DetermineTestHarnessDirectory TestsFrom); -use Getopt::Long; -GetOptions - ("tstdir:s"=>\$tstdir, - "spath:s"=>\$spath, - "hostname:s"=>\$hostname, - "httpname:s"=>\$httphost, - "mailhost:s"=>\$mailhost, - "dnshost:s"=>\$dnshost, - "noserver:s"=>\$noserver, - "nullhost:s"=>\$nullhost, - "quickcheck"=>\$quickcheck); +my $tstdir; + +if ( ! GetOptions( "testdir:s" => \$tstdir ) ) +{ + print "Usage: ${0} [--testdir=] [ ...]\n"; + exit 1; +} -$spath = "." unless ($spath); +my @tests; -unless ($quickcheck) { - - $hostname = get_option("hostname","host for FTP/UDP tests") unless ($hostname); - $httphost = get_option("httphost","host for HTTP tests") unless ($httphost); - $mailhost = get_option("mailhost","host for SMTP/IMAP/POP tests") unless ($mailhost); - $dnshost = get_option("dnshost","hostname to lookup for DNS tests") unless ($dnshost); - $noserver = get_option("noserver","host that rejects above services") unless ($noserver); - # This machine should not be locatable from your network. Use IP - # private addresses like 10.x.x.x and pick one that does not exist - # on your LAN/WAN - $nullhost = get_option("nullhost","nonexistent IP address (e.g., 10.0.0.0)") unless ($nullhost); +if ( scalar( @ARGV ) ) +{ + @tests = @ARGV; } +else +{ + my $directory = DetermineTestHarnessDirectory( $tstdir ); + + if ( !defined( $directory ) ) + { + print STDERR "$0: Unable to determine the test harness directory - ABORTING\n"; + exit 2; + } -my @dots; -if (@ARGV) { - @dots = @ARGV; -} else { - unless ($tstdir) { - if (-d './t') { - $tstdir = './t'; - } else { - $tstdir = $ENV{PWD}; - $tstdir = `/bin/pwd` unless defined($tstdir); - chomp $tstdir; - if (defined($tstdir)) { - $tstdir =~ s|^(.*)/([^/]+)/?$|$1/$2|; - if (-d "../../$2/t") { - $tstdir = "../../$2/t"; - } elsif (-d "$tstdir/t") { - $tstdir = "$tstdir/t"; - } - } else { - die "Could not get PWD from environment\n"; - } - } - } - $tstdir = './t' unless ($tstdir); - opendir(DIR, $tstdir) || die "can't opendir $tstdir: $!"; - while ($file = readdir(DIR)) { - push @dots, "$tstdir/$file" if ($file =~ m/^[^\.]+\.t$/); - } - closedir DIR; + @tests = TestsFrom( $directory, 1 ); } -my $prog; -my $test; -my @progs; -foreach $test (@dots) { - $prog=`basename $test .t`; - chomp $prog; - if ( -e "$prog" ){ - push @progs, "$test"; - }else{ - print "No binary found for $prog\n"; - } + +if ( ! scalar( @tests ) ) +{ + print STDERR "$0: Unable to determine the test harnesses to run - ABORTING\n"; + exit 3; } use Test::Harness; + #$Test::Harness::verbose=1; -runtests(@progs); + +runtests( @tests ); -- cgit v0.10-9-g596f