diff options
author | Ton Voon <tonvoon@users.sourceforge.net> | 2006-01-30 16:10:50 +0000 |
---|---|---|
committer | Ton Voon <tonvoon@users.sourceforge.net> | 2006-01-30 16:10:50 +0000 |
commit | 795100ae5124915bb647a304d5dfe2ada2f44ab0 (patch) | |
tree | b3fd0dc1a88ebb533f074c476ee020e32877632e | |
parent | c8a9bf228fdd34b021796f577a404cb8b0cba1f9 (diff) | |
download | monitoring-plugins-795100ae5124915bb647a304d5dfe2ada2f44ab0.tar.gz |
Added libtap tests for utils.c library functions. Removed redundant
test files
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1303 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r-- | configure.in | 10 | ||||
-rw-r--r-- | doc/developer-guidelines.sgml | 24 | ||||
-rw-r--r-- | plugins/Makefile.am | 3 | ||||
-rw-r--r-- | plugins/tests/.cvsignore | 3 | ||||
-rw-r--r-- | plugins/tests/Makefile.am | 21 | ||||
-rw-r--r-- | plugins/tests/README | 5 | ||||
-rw-r--r-- | plugins/tests/check_disk | 9 | ||||
-rw-r--r-- | plugins/tests/check_dns | 9 | ||||
-rw-r--r-- | plugins/tests/check_ftp | 9 | ||||
-rw-r--r-- | plugins/tests/check_hpjd | 9 | ||||
-rw-r--r-- | plugins/tests/check_http | 9 | ||||
-rw-r--r-- | plugins/tests/check_load | 9 | ||||
-rw-r--r-- | plugins/tests/check_ping | 13 | ||||
-rw-r--r-- | plugins/tests/check_procs | 23 | ||||
-rw-r--r-- | plugins/tests/check_swap | 13 | ||||
-rw-r--r-- | plugins/tests/check_users | 9 | ||||
-rw-r--r-- | plugins/tests/check_vsz | 9 | ||||
-rw-r--r-- | plugins/tests/test_utils.c | 109 | ||||
-rwxr-xr-x | plugins/tests/test_utils.t | 6 | ||||
-rw-r--r-- | plugins/utils.c | 55 | ||||
-rw-r--r-- | plugins/utils.h | 13 |
21 files changed, 244 insertions, 126 deletions
diff --git a/configure.in b/configure.in index 9303850a..9afbf60e 100644 --- a/configure.in +++ b/configure.in | |||
@@ -74,6 +74,15 @@ AC_SUBST(with_nagios_group) | |||
74 | INSTALL_OPTS="-o $with_nagios_user -g $with_nagios_group" | 74 | INSTALL_OPTS="-o $with_nagios_user -g $with_nagios_group" |
75 | AC_SUBST(INSTALL_OPTS) | 75 | AC_SUBST(INSTALL_OPTS) |
76 | 76 | ||
77 | AC_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 | |||
77 | AC_ARG_WITH(trusted_path, | 86 | AC_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 3c37e5c1..ad6f59e9 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 > end</para> | 212 | <listitem><para>start ≤ 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> |
601 | If you want a summary test, run: "cd plugins && perl -MTest::Harness -e 'runtests(@ARGV)' t/check_disk.t". | 601 | If you want a summary test, run: "cd plugins && prove t/check_disk.t". |
602 | This runs the test in a summary format. | 602 | This 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> |
615 | Will be looking at using libtap, which is utilised by the FreeBSD team. The output is | 615 | Uses <ulink url="http://jc.ngo.org.uk/trac-bin/trac.cgi/wiki/LibTap">the libtap library</ulink>, which gives |
616 | based on perl's TAP (Test Anything Protocol) format, so that Test::Harness will understand | 616 | perl's TAP |
617 | results. 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> | ||
621 | To run tests using the libtap library, download from http://people.freebsd.org/~nik/public_distfiles/ | ||
622 | and compile. There is a problem with tap-1.01 | ||
623 | where <ulink url="http://jc.ngo.org.uk/trac-bin/trac.cgi/ticket/25">pthread support doesn't appear to work</ulink> | ||
624 | properly on non-FreeBSD systems. Compile with 'CPPFLAGS="-UHAVE_LIBPTHREAD" ./configure'. You do not need | ||
625 | to install. | ||
626 | </para> | ||
627 | |||
628 | <para> | ||
629 | You then have to run the Nagios Plugins' configure with the --with-libtap-object=full_path to the compiled | ||
630 | tap.o file. | ||
631 | Then 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 26f9c8a8..a67911ce 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 | ||
3 | SUBDIRS = tests | ||
4 | |||
3 | VPATH = $(top_srcdir) $(top_srcdir)/lib $(top_srcdir)/plugins $(top_srcdir)/plugins/t | 5 | VPATH = $(top_srcdir) $(top_srcdir)/lib $(top_srcdir)/plugins $(top_srcdir)/plugins/t |
4 | 6 | ||
5 | INCLUDES = -I.. -I$(top_srcdir)/lib -I$(top_srcdir)/intl @LDAPINCLUDE@ @PGINCLUDE@ @SSLINCLUDE@ | 7 | INCLUDES = -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) | |||
41 | TESTS = @PLUGIN_TEST@ | 43 | TESTS = @PLUGIN_TEST@ |
42 | 44 | ||
43 | test: | 45 | test: |
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 | ||
46 | AM_INSTALL_PROGRAM_FLAGS = @INSTALL_OPTS@ | 49 | AM_INSTALL_PROGRAM_FLAGS = @INSTALL_OPTS@ |
diff --git a/plugins/tests/.cvsignore b/plugins/tests/.cvsignore new file mode 100644 index 00000000..95112e15 --- /dev/null +++ b/plugins/tests/.cvsignore | |||
@@ -0,0 +1,3 @@ | |||
1 | Makefile | ||
2 | Makefile.in | ||
3 | test_utils | ||
diff --git a/plugins/tests/Makefile.am b/plugins/tests/Makefile.am new file mode 100644 index 00000000..0d82be12 --- /dev/null +++ b/plugins/tests/Makefile.am | |||
@@ -0,0 +1,21 @@ | |||
1 | |||
2 | noinst_PROGRAMS = @EXTRA_TEST@ | ||
3 | |||
4 | # These two lines support "make check", but we use "make test" | ||
5 | TESTS = @EXTRA_TEST@ | ||
6 | check_PROGRAMS = @EXTRA_TEST@ | ||
7 | |||
8 | INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/intl | ||
9 | |||
10 | EXTRA_PROGRAMS = test_utils | ||
11 | |||
12 | LIBS = @LIBINTL@ | ||
13 | |||
14 | test_utils_SOURCES = test_utils.c | ||
15 | test_utils_CFLAGS = -g -I.. | ||
16 | test_utils_LDFLAGS = -L.. | ||
17 | test_utils_LDADD = ../utils.o @EXTRA_TAPOBJ@ | ||
18 | |||
19 | test: ${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 00000000..66935e4d --- /dev/null +++ b/plugins/tests/README | |||
@@ -0,0 +1,5 @@ | |||
1 | The tests in here use the libtap library functions | ||
2 | (http://jc.ngo.org.uk/trac-bin/trac.cgi/wiki/LibTap), so are | ||
3 | more for unit testing the utils.c library functions. | ||
4 | |||
5 | However, 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 c4323e0b..00000000 --- a/plugins/tests/check_disk +++ /dev/null | |||
@@ -1,9 +0,0 @@ | |||
1 | check_disk | ||
2 | |||
3 | [normal] | ||
4 | 100 100 / | ||
5 | ^Disk ok - +[\.0-9]+ | ||
6 | |||
7 | [critical] | ||
8 | 0 0 / | ||
9 | ^Only +[\.0-9]+ | ||
diff --git a/plugins/tests/check_dns b/plugins/tests/check_dns deleted file mode 100644 index 2e9b9a09..00000000 --- a/plugins/tests/check_dns +++ /dev/null | |||
@@ -1,9 +0,0 @@ | |||
1 | check_dns | ||
2 | |||
3 | [normal] | ||
4 | 127.0.0.1 | ||
5 | DNS ok - +[\.0-9]+ seconds response time, Address\(es\) is\/are 127\.0\.0\.1 | ||
6 | |||
7 | [critical] | ||
8 | $nullhost | ||
9 | DNS (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 4266ebdc..00000000 --- a/plugins/tests/check_ftp +++ /dev/null | |||
@@ -1,9 +0,0 @@ | |||
1 | check_ftp | ||
2 | |||
3 | [normal] | ||
4 | $hostname -wt 300 -ct 600 | ||
5 | FTP 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 df35996f..00000000 --- a/plugins/tests/check_hpjd +++ /dev/null | |||
@@ -1,9 +0,0 @@ | |||
1 | check_hpjd | ||
2 | |||
3 | [normal] | ||
4 | $printer | ||
5 | ^Printer ok - | ||
6 | |||
7 | [critical] | ||
8 | $noserver | ||
9 | Timeout: No response from ns | ||
diff --git a/plugins/tests/check_http b/plugins/tests/check_http deleted file mode 100644 index e09f6ff7..00000000 --- a/plugins/tests/check_http +++ /dev/null | |||
@@ -1,9 +0,0 @@ | |||
1 | check_http | ||
2 | |||
3 | [normal] | ||
4 | www.infoplease.com -wt 300 -ct 600 | ||
5 | HTTP/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 5e4be3a4..00000000 --- a/plugins/tests/check_load +++ /dev/null | |||
@@ -1,9 +0,0 @@ | |||
1 | check_load | ||
2 | |||
3 | [normal] | ||
4 | 100 100 100 100 100 100 | ||
5 | ^load average: +[\.0-9]+, +[\.0-9]+, +[\.0-9]+$ | ||
6 | |||
7 | [critical] | ||
8 | 0 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 115cbaa3..00000000 --- a/plugins/tests/check_ping +++ /dev/null | |||
@@ -1,13 +0,0 @@ | |||
1 | check_ping | ||
2 | |||
3 | [normal] | ||
4 | 127.0.0.1 100 100 1000 1000 -p 1 | ||
5 | PING ok - Packet loss = +[0-9]{1,2}\%, +RTA = [\.0-9]+ ms | ||
6 | |||
7 | [critical] | ||
8 | 127.0.0.1 0 0 0 0 -p 1 | ||
9 | Packet loss = +[0-9]{1,2}\%, +RTA = [\.0-9]+ ms | ||
10 | |||
11 | [critical] | ||
12 | $nullhost 0 0 0 0 -p 1 -to 1 | ||
13 | CRITICAL - Plugin timed out after 1 seconds | ||
diff --git a/plugins/tests/check_procs b/plugins/tests/check_procs deleted file mode 100644 index f7c65222..00000000 --- a/plugins/tests/check_procs +++ /dev/null | |||
@@ -1,23 +0,0 @@ | |||
1 | check_procs | ||
2 | |||
3 | # this is a comment | ||
4 | |||
5 | [normal] | ||
6 | 100000 100000 | ||
7 | ^OK - [0-9]+ processes running$ | ||
8 | |||
9 | [normal] | ||
10 | 100000 100000 Z | ||
11 | ^OK - [0-9]+ processes with Z status$ | ||
12 | |||
13 | [warning] | ||
14 | 0 10000000 | ||
15 | ^WARNING - [0-9]+ processes running$ | ||
16 | |||
17 | [critical] | ||
18 | 0 0 | ||
19 | ^CRITICAL - [0-9]+ processes running$ | ||
20 | |||
21 | [critical] | ||
22 | 0 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 b6cbb42f..00000000 --- a/plugins/tests/check_swap +++ /dev/null | |||
@@ -1,13 +0,0 @@ | |||
1 | check_swap | ||
2 | |||
3 | [normal] | ||
4 | 100 100 | ||
5 | ^Swap ok - Swap used\: +[0-9]{1,2}\% \([0-9]+ bytes out of [0-9]+\)$ | ||
6 | |||
7 | [critical] | ||
8 | 0 0 | ||
9 | ^CRITICAL - Swap used\: +[0-9]{1,2}\% \([0-9]+ bytes out of [0-9]+\)$ | ||
10 | |||
11 | [critical] | ||
12 | 100 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 5a7959a5..00000000 --- a/plugins/tests/check_users +++ /dev/null | |||
@@ -1,9 +0,0 @@ | |||
1 | check_users | ||
2 | |||
3 | [normal] | ||
4 | 1000 1000 | ||
5 | ^Users ok - +[0-9]+ users logged in$ | ||
6 | |||
7 | [critical] | ||
8 | 0 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 4fdec936..00000000 --- a/plugins/tests/check_vsz +++ /dev/null | |||
@@ -1,9 +0,0 @@ | |||
1 | check_vsz | ||
2 | |||
3 | [normal] | ||
4 | 100000 1000000 init | ||
5 | ^ok \(all VSZ\<[0-9]+\) | ||
6 | |||
7 | [critical] | ||
8 | 0 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 00000000..1fda3675 --- /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 | |||
21 | const char *progname = "utils"; | ||
22 | |||
23 | #include "common.h" | ||
24 | #include "utils.h" | ||
25 | #include "popen.h" | ||
26 | |||
27 | #include "tap.h" | ||
28 | |||
29 | int | ||
30 | main (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 | |||
107 | void 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 00000000..8686ad2a --- /dev/null +++ b/plugins/tests/test_utils.t | |||
@@ -0,0 +1,6 @@ | |||
1 | #!/usr/bin/perl | ||
2 | use Test::More; | ||
3 | if (! -e "./test_utils") { | ||
4 | plan skip_all => "./test_utils not compiled - check ./configure --with-libtap-object is defined"; | ||
5 | } | ||
6 | exec "./test_utils"; | ||
diff --git a/plugins/utils.c b/plugins/utils.c index 8b31c5a2..dbb25202 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 | ||
268 | void set_threshold_start (threshold *this, double value) { | ||
269 | this->start = value; | ||
270 | this->start_infinity = FALSE; | ||
271 | } | ||
272 | |||
273 | void set_threshold_end (threshold *this, double value) { | ||
274 | this->end = value; | ||
275 | this->end_infinity = FALSE; | ||
276 | } | ||
277 | |||
278 | threshold | ||
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 |
271 | int | 326 | int |
diff --git a/plugins/utils.h b/plugins/utils.h index 2d976345..f47d0533 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 | |||
64 | typedef 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 | |||
72 | threshold *parse_threshold (char *); | ||
73 | |||
61 | #ifndef HAVE_GETTIMEOFDAY | 74 | #ifndef HAVE_GETTIMEOFDAY |
62 | int gettimeofday(struct timeval *, struct timezone *); | 75 | int gettimeofday(struct timeval *, struct timezone *); |
63 | #endif | 76 | #endif |