summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--configure.in10
-rw-r--r--doc/developer-guidelines.sgml24
-rw-r--r--plugins/Makefile.am3
-rw-r--r--plugins/tests/.cvsignore3
-rw-r--r--plugins/tests/Makefile.am21
-rw-r--r--plugins/tests/README5
-rw-r--r--plugins/tests/check_disk9
-rw-r--r--plugins/tests/check_dns9
-rw-r--r--plugins/tests/check_ftp9
-rw-r--r--plugins/tests/check_hpjd9
-rw-r--r--plugins/tests/check_http9
-rw-r--r--plugins/tests/check_load9
-rw-r--r--plugins/tests/check_ping13
-rw-r--r--plugins/tests/check_procs23
-rw-r--r--plugins/tests/check_swap13
-rw-r--r--plugins/tests/check_users9
-rw-r--r--plugins/tests/check_vsz9
-rw-r--r--plugins/tests/test_utils.c109
-rwxr-xr-xplugins/tests/test_utils.t6
-rw-r--r--plugins/utils.c55
-rw-r--r--plugins/utils.h13
21 files changed, 244 insertions, 126 deletions
diff --git a/configure.in b/configure.in
index 9303850..9afbf60 100644
--- a/configure.in
+++ b/configure.in
@@ -74,6 +74,15 @@ AC_SUBST(with_nagios_group)
74INSTALL_OPTS="-o $with_nagios_user -g $with_nagios_group" 74INSTALL_OPTS="-o $with_nagios_user -g $with_nagios_group"
75AC_SUBST(INSTALL_OPTS) 75AC_SUBST(INSTALL_OPTS)
76 76
77AC_ARG_WITH(libtap-object,
78 ACX_HELP_STRING([--with-libtap-object=path],
79 [full path to tap.o]),
80 EXTRA_TEST=test_utils
81 EXTRA_TAPOBJ=$withval
82 AC_SUBST(EXTRA_TEST)
83 AC_SUBST(EXTRA_TAPOBJ)
84 )
85
77AC_ARG_WITH(trusted_path, 86AC_ARG_WITH(trusted_path,
78 ACX_HELP_STRING([--with-trusted-path=PATH], 87 ACX_HELP_STRING([--with-trusted-path=PATH],
79 [sets trusted path for executables called by scripts (default=/bin:/sbin:/usr/bin:/usr/sbin)]), 88 [sets trusted path for executables called by scripts (default=/bin:/sbin:/usr/bin:/usr/sbin)]),
@@ -1711,6 +1720,7 @@ AC_OUTPUT(
1711 lib/Makefile 1720 lib/Makefile
1712 m4/Makefile 1721 m4/Makefile
1713 plugins/Makefile 1722 plugins/Makefile
1723 plugins/tests/Makefile
1714 plugins-root/Makefile 1724 plugins-root/Makefile
1715 plugins-scripts/Makefile 1725 plugins-scripts/Makefile
1716 plugins-scripts/subst 1726 plugins-scripts/subst
diff --git a/doc/developer-guidelines.sgml b/doc/developer-guidelines.sgml
index 3c37e5c..ad6f59e 100644
--- a/doc/developer-guidelines.sgml
+++ b/doc/developer-guidelines.sgml
@@ -209,7 +209,7 @@
209 209
210 <para>Notes:</para> 210 <para>Notes:</para>
211 <orderedlist> 211 <orderedlist>
212 <listitem><para>start &gt; end</para> 212 <listitem><para>start &le; end</para>
213 </listitem> 213 </listitem>
214 <listitem><para>start and ":" is not required if start=0</para> 214 <listitem><para>start and ":" is not required if start=0</para>
215 </listitem> 215 </listitem>
@@ -598,7 +598,7 @@ need to be fixed. As long as the return code is 0, it will be reported as "test
598</para> 598</para>
599 599
600<para> 600<para>
601If you want a summary test, run: "cd plugins && perl -MTest::Harness -e 'runtests(@ARGV)' t/check_disk.t". 601If you want a summary test, run: "cd plugins && prove t/check_disk.t".
602This runs the test in a summary format. 602This runs the test in a summary format.
603</para> 603</para>
604 604
@@ -612,9 +612,23 @@ link</ulink>
612 612
613<section><title>Testing the C library functions</title> 613<section><title>Testing the C library functions</title>
614<para> 614<para>
615Will be looking at using libtap, which is utilised by the FreeBSD team. The output is 615Uses <ulink url="http://jc.ngo.org.uk/trac-bin/trac.cgi/wiki/LibTap">the libtap library</ulink>, which gives
616based on perl's TAP (Test Anything Protocol) format, so that Test::Harness will understand 616perl's TAP
617results. This is still in planning stages. 617(Test Anything Protocol) output. This is used by the FreeBSD team for their regression testing.
618</para>
619
620<para>
621To run tests using the libtap library, download from http://people.freebsd.org/~nik/public_distfiles/
622and compile. There is a problem with tap-1.01
623where <ulink url="http://jc.ngo.org.uk/trac-bin/trac.cgi/ticket/25">pthread support doesn't appear to work</ulink>
624properly on non-FreeBSD systems. Compile with 'CPPFLAGS="-UHAVE_LIBPTHREAD" ./configure'. You do not need
625to install.
626</para>
627
628<para>
629You then have to run the Nagios Plugins' configure with the --with-libtap-object=full_path to the compiled
630tap.o file.
631Then run "make" and "make test" to run all tests.
618</para> 632</para>
619</section> 633</section>
620 634
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 26f9c8a..a67911c 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -1,5 +1,7 @@
1## Process this file with automake to produce Makefile.in 1## Process this file with automake to produce Makefile.in
2 2
3SUBDIRS = tests
4
3VPATH = $(top_srcdir) $(top_srcdir)/lib $(top_srcdir)/plugins $(top_srcdir)/plugins/t 5VPATH = $(top_srcdir) $(top_srcdir)/lib $(top_srcdir)/plugins $(top_srcdir)/plugins/t
4 6
5INCLUDES = -I.. -I$(top_srcdir)/lib -I$(top_srcdir)/intl @LDAPINCLUDE@ @PGINCLUDE@ @SSLINCLUDE@ 7INCLUDES = -I.. -I$(top_srcdir)/lib -I$(top_srcdir)/intl @LDAPINCLUDE@ @PGINCLUDE@ @SSLINCLUDE@
@@ -41,6 +43,7 @@ TESTS_ENVIRONMENT = perl -I $(top_builddir) -I $(top_srcdir)
41TESTS = @PLUGIN_TEST@ 43TESTS = @PLUGIN_TEST@
42 44
43test: 45test:
46 cd tests && make test
44 perl -I $(top_builddir) -I $(top_srcdir) ../test.pl 47 perl -I $(top_builddir) -I $(top_srcdir) ../test.pl
45 48
46AM_INSTALL_PROGRAM_FLAGS = @INSTALL_OPTS@ 49AM_INSTALL_PROGRAM_FLAGS = @INSTALL_OPTS@
diff --git a/plugins/tests/.cvsignore b/plugins/tests/.cvsignore
new file mode 100644
index 0000000..95112e1
--- /dev/null
+++ b/plugins/tests/.cvsignore
@@ -0,0 +1,3 @@
1Makefile
2Makefile.in
3test_utils
diff --git a/plugins/tests/Makefile.am b/plugins/tests/Makefile.am
new file mode 100644
index 0000000..0d82be1
--- /dev/null
+++ b/plugins/tests/Makefile.am
@@ -0,0 +1,21 @@
1
2noinst_PROGRAMS = @EXTRA_TEST@
3
4# These two lines support "make check", but we use "make test"
5TESTS = @EXTRA_TEST@
6check_PROGRAMS = @EXTRA_TEST@
7
8INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/intl
9
10EXTRA_PROGRAMS = test_utils
11
12LIBS = @LIBINTL@
13
14test_utils_SOURCES = test_utils.c
15test_utils_CFLAGS = -g -I..
16test_utils_LDFLAGS = -L..
17test_utils_LDADD = ../utils.o @EXTRA_TAPOBJ@
18
19test: ${noinst_PROGRAMS}
20 perl -MTest::Harness -e '$$Test::Harness::switches=""; runtests(map {$$_ .= ".t"} @ARGV)' $(EXTRA_PROGRAMS)
21
diff --git a/plugins/tests/README b/plugins/tests/README
new file mode 100644
index 0000000..66935e4
--- /dev/null
+++ b/plugins/tests/README
@@ -0,0 +1,5 @@
1The tests in here use the libtap library functions
2(http://jc.ngo.org.uk/trac-bin/trac.cgi/wiki/LibTap), so are
3more for unit testing the utils.c library functions.
4
5However, it probably should be merged into the plugins/t subdirectory.
diff --git a/plugins/tests/check_disk b/plugins/tests/check_disk
deleted file mode 100644
index c4323e0..0000000
--- a/plugins/tests/check_disk
+++ /dev/null
@@ -1,9 +0,0 @@
1check_disk
2
3[normal]
4100 100 /
5^Disk ok - +[\.0-9]+
6
7[critical]
80 0 /
9^Only +[\.0-9]+
diff --git a/plugins/tests/check_dns b/plugins/tests/check_dns
deleted file mode 100644
index 2e9b9a0..0000000
--- a/plugins/tests/check_dns
+++ /dev/null
@@ -1,9 +0,0 @@
1check_dns
2
3[normal]
4127.0.0.1
5DNS ok - +[\.0-9]+ seconds response time, Address\(es\) is\/are 127\.0\.0\.1
6
7[critical]
8$nullhost
9DNS (problem - Probably a non-existent host/domain|CRITICAL - Non-existent host/domain|CRITICAL - Connection timed out after [0-9]+ seconds)
diff --git a/plugins/tests/check_ftp b/plugins/tests/check_ftp
deleted file mode 100644
index 4266ebd..0000000
--- a/plugins/tests/check_ftp
+++ /dev/null
@@ -1,9 +0,0 @@
1check_ftp
2
3[normal]
4$hostname -wt 300 -ct 600
5FTP ok - [0-9]+ second response time
6
7[critical]
8$noserver -wt 0 -ct 0
9(Invalid FTP response received from host|Connection refused by host)
diff --git a/plugins/tests/check_hpjd b/plugins/tests/check_hpjd
deleted file mode 100644
index df35996..0000000
--- a/plugins/tests/check_hpjd
+++ /dev/null
@@ -1,9 +0,0 @@
1check_hpjd
2
3[normal]
4$printer
5^Printer ok -
6
7[critical]
8$noserver
9Timeout: No response from ns
diff --git a/plugins/tests/check_http b/plugins/tests/check_http
deleted file mode 100644
index e09f6ff..0000000
--- a/plugins/tests/check_http
+++ /dev/null
@@ -1,9 +0,0 @@
1check_http
2
3[normal]
4www.infoplease.com -wt 300 -ct 600
5HTTP/1.1 200 OK - [0-9]+ second response time
6
7[critical]
8$nullhost -wt 1 -ct 2
9(Connection refused by host|Network is unreachable|Connection refused or timed out|Socket timeout after [0-9]+ seconds)$
diff --git a/plugins/tests/check_load b/plugins/tests/check_load
deleted file mode 100644
index 5e4be3a..0000000
--- a/plugins/tests/check_load
+++ /dev/null
@@ -1,9 +0,0 @@
1check_load
2
3[normal]
4100 100 100 100 100 100
5^load average: +[\.0-9]+, +[\.0-9]+, +[\.0-9]+$
6
7[critical]
80 0 0 0 0 0
9^load average: +[\.0-9]+, +[\.0-9]+, +[\.0-9]+ CRITICAL$
diff --git a/plugins/tests/check_ping b/plugins/tests/check_ping
deleted file mode 100644
index 115cbaa..0000000
--- a/plugins/tests/check_ping
+++ /dev/null
@@ -1,13 +0,0 @@
1check_ping
2
3[normal]
4127.0.0.1 100 100 1000 1000 -p 1
5PING ok - Packet loss = +[0-9]{1,2}\%, +RTA = [\.0-9]+ ms
6
7[critical]
8127.0.0.1 0 0 0 0 -p 1
9Packet loss = +[0-9]{1,2}\%, +RTA = [\.0-9]+ ms
10
11[critical]
12$nullhost 0 0 0 0 -p 1 -to 1
13CRITICAL - Plugin timed out after 1 seconds
diff --git a/plugins/tests/check_procs b/plugins/tests/check_procs
deleted file mode 100644
index f7c6522..0000000
--- a/plugins/tests/check_procs
+++ /dev/null
@@ -1,23 +0,0 @@
1check_procs
2
3# this is a comment
4
5[normal]
6100000 100000
7^OK - [0-9]+ processes running$
8
9[normal]
10100000 100000 Z
11^OK - [0-9]+ processes with Z status$
12
13[warning]
140 10000000
15^WARNING - [0-9]+ processes running$
16
17[critical]
180 0
19^CRITICAL - [0-9]+ processes running$
20
21[critical]
220 0 S
23^CRITICAL - [0-9]+ processes with S status$
diff --git a/plugins/tests/check_swap b/plugins/tests/check_swap
deleted file mode 100644
index b6cbb42..0000000
--- a/plugins/tests/check_swap
+++ /dev/null
@@ -1,13 +0,0 @@
1check_swap
2
3[normal]
4100 100
5^Swap ok - Swap used\: +[0-9]{1,2}\% \([0-9]+ bytes out of [0-9]+\)$
6
7[critical]
80 0
9^CRITICAL - Swap used\: +[0-9]{1,2}\% \([0-9]+ bytes out of [0-9]+\)$
10
11[critical]
12100 100 1000000000 10000000000
13^CRITICAL - Swap used\: +[0-9]{1,2}\% \([0-9]+ bytes out of [0-9]+\)$
diff --git a/plugins/tests/check_users b/plugins/tests/check_users
deleted file mode 100644
index 5a7959a..0000000
--- a/plugins/tests/check_users
+++ /dev/null
@@ -1,9 +0,0 @@
1check_users
2
3[normal]
41000 1000
5^Users ok - +[0-9]+ users logged in$
6
7[critical]
80 0
9^[0-9]+ +users currently logged in$
diff --git a/plugins/tests/check_vsz b/plugins/tests/check_vsz
deleted file mode 100644
index 4fdec93..0000000
--- a/plugins/tests/check_vsz
+++ /dev/null
@@ -1,9 +0,0 @@
1check_vsz
2
3[normal]
4100000 1000000 init
5^ok \(all VSZ\<[0-9]+\)
6
7[critical]
80 0
9^CRITICAL \(VSZ\>[0-9]+\)
diff --git a/plugins/tests/test_utils.c b/plugins/tests/test_utils.c
new file mode 100644
index 0000000..1fda367
--- /dev/null
+++ b/plugins/tests/test_utils.c
@@ -0,0 +1,109 @@
1/******************************************************************************
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
17 $Id$
18
19******************************************************************************/
20
21const char *progname = "utils";
22
23#include "common.h"
24#include "utils.h"
25#include "popen.h"
26
27#include "tap.h"
28
29int
30main (int argc, char **argv)
31{
32 threshold *range;
33 double temp;
34
35 plan_tests(40);
36
37 range = parse_threshold("6");
38 ok( range != NULL, "'6' is valid threshold");
39 ok( range->start == 0, "Start correct");
40 ok( range->start_infinity == FALSE, "Not using negative infinity");
41 ok( range->end == 6, "End correct");
42 ok( range->end_infinity == FALSE, "Not using infinity");
43 free(range);
44
45 range = parse_threshold("-7:23");
46 ok( range != NULL, "'-7:23' is valid threshold");
47 ok( range->start == -7, "Start correct");
48 ok( range->start_infinity == FALSE, "Not using negative infinity");
49 ok( range->end == 23, "End correct");
50 ok( range->end_infinity == FALSE, "Not using infinity");
51 free(range);
52
53 range = parse_threshold(":5.75");
54 ok( range != NULL, "':5.75' is valid threshold");
55 ok( range->start == 0, "Start correct");
56 ok( range->start_infinity == FALSE, "Not using negative infinity");
57 ok( range->end == 5.75, "End correct");
58 ok( range->end_infinity == FALSE, "Not using infinity");
59 free(range);
60
61 range = parse_threshold("~:-95.99");
62 ok( range != NULL, "~:-95.99' is valid threshold");
63 ok( range->start_infinity == TRUE, "Using negative infinity");
64 ok( range->end == -95.99, "End correct (with rounding errors)");
65 ok( range->end_infinity == FALSE, "Not using infinity");
66 free(range);
67
68 range = parse_threshold("12345678901234567890:");
69 temp = atof("12345678901234567890"); /* Can't just use this because number too large */
70 ok( range != NULL, "'12345678901234567890:' is valid threshold");
71 ok( range->start == temp, "Start correct");
72 ok( range->start_infinity == FALSE, "Not using negative infinity");
73 ok( range->end_infinity == TRUE, "Using infinity");
74 free(range);
75
76 range = parse_threshold("~:0");
77 ok( range != NULL, "'~:0' is valid threshold");
78 ok( range->start_infinity == TRUE, "Using negative infinity");
79 ok( range->end == 0, "End correct");
80 ok( range->end_infinity == FALSE, "Not using infinity");
81 ok( range->alert_on == OUTSIDE, "Will alert on outside of this range");
82 free(range);
83
84 range = parse_threshold("@0:657.8210567");
85 ok( range != 0, "@0:657.8210567' is a valid threshold");
86 ok( range->start == 0, "Start correct");
87 ok( range->start_infinity == FALSE, "Not using negative infinity");
88 ok( range->end == 657.8210567, "End correct");
89 ok( range->end_infinity == FALSE, "Not using infinity");
90 ok( range->alert_on == INSIDE, "Will alert on inside of this range" );
91 free(range);
92
93 range = parse_threshold("1:1");
94 ok( range != NULL, "'1:1' is a valid threshold");
95 ok( range->start == 1, "Start correct");
96 ok( range->start_infinity == FALSE, "Not using negative infinity");
97 ok( range->end == 1, "End correct");
98 ok( range->end_infinity == FALSE, "Not using infinity");
99 free(range);
100
101 range = parse_threshold("2:1");
102 ok( range == NULL, "''2:1' rejected");
103
104 return exit_status();
105}
106
107void print_usage() {
108 printf("Dummy");
109}
diff --git a/plugins/tests/test_utils.t b/plugins/tests/test_utils.t
new file mode 100755
index 0000000..8686ad2
--- /dev/null
+++ b/plugins/tests/test_utils.t
@@ -0,0 +1,6 @@
1#!/usr/bin/perl
2use Test::More;
3if (! -e "./test_utils") {
4 plan skip_all => "./test_utils not compiled - check ./configure --with-libtap-object is defined";
5}
6exec "./test_utils";
diff --git a/plugins/utils.c b/plugins/utils.c
index 8b31c5a..dbb2520 100644
--- a/plugins/utils.c
+++ b/plugins/utils.c
@@ -265,7 +265,62 @@ is_option (char *str)
265 return FALSE; 265 return FALSE;
266} 266}
267 267
268void set_threshold_start (threshold *this, double value) {
269 this->start = value;
270 this->start_infinity = FALSE;
271}
272
273void set_threshold_end (threshold *this, double value) {
274 this->end = value;
275 this->end_infinity = FALSE;
276}
277
278threshold
279*parse_threshold (char *str) {
280 threshold *temp_threshold;
281 double start;
282 double end;
283 char *end_str;
268 284
285 temp_threshold = (threshold *) malloc(sizeof(threshold));
286
287 /* Set defaults */
288 temp_threshold->start = 0;
289 temp_threshold->start_infinity = FALSE;
290 temp_threshold->end = 0;
291 temp_threshold->end_infinity = TRUE;
292 temp_threshold->alert_on = OUTSIDE;
293
294 if (str[0] == '@') {
295 temp_threshold->alert_on = INSIDE;
296 str++;
297 }
298
299 end_str = index(str, ':');
300 if (end_str != NULL) {
301 if (str[0] == '~') {
302 temp_threshold->start_infinity = TRUE;
303 } else {
304 start = strtod(str, NULL); /* Will stop at the ':' */
305 set_threshold_start(temp_threshold, start);
306 }
307 end_str++; /* Move past the ':' */
308 } else {
309 end_str = str;
310 }
311 end = strtod(end_str, NULL);
312 if (strcmp(end_str, "") != 0) {
313 set_threshold_end(temp_threshold, end);
314 }
315
316 if (temp_threshold->start_infinity == TRUE ||
317 temp_threshold->end_infinity == TRUE ||
318 temp_threshold->start <= temp_threshold->end) {
319 return temp_threshold;
320 }
321 free(temp_threshold);
322 return NULL;
323}
269 324
270#ifdef NEED_GETTIMEOFDAY 325#ifdef NEED_GETTIMEOFDAY
271int 326int
diff --git a/plugins/utils.h b/plugins/utils.h
index 2d97634..f47d053 100644
--- a/plugins/utils.h
+++ b/plugins/utils.h
@@ -58,6 +58,19 @@ struct timeval {
58}; 58};
59#endif 59#endif
60 60
61#define OUTSIDE 0
62#define INSIDE 1
63
64typedef struct threshold_struct {
65 double start;
66 int start_infinity; /* FALSE (default) or TRUE */
67 double end;
68 int end_infinity;
69 int alert_on; /* OUTSIDE (default) or INSIDE */
70 } threshold;
71
72threshold *parse_threshold (char *);
73
61#ifndef HAVE_GETTIMEOFDAY 74#ifndef HAVE_GETTIMEOFDAY
62int gettimeofday(struct timeval *, struct timezone *); 75int gettimeofday(struct timeval *, struct timezone *);
63#endif 76#endif