diff options
author | Ton Voon <tonvoon@users.sourceforge.net> | 2006-03-24 16:12:05 (GMT) |
---|---|---|
committer | Ton Voon <tonvoon@users.sourceforge.net> | 2006-03-24 16:12:05 (GMT) |
commit | 8c3e7428ae21999e813b288c3d55d3870c9ef258 (patch) | |
tree | f5000d9a6cbbc6bf57a03ed8575ea4a39442997d | |
parent | 19d76b3fb60aea7470b7bd03485669a531456d7a (diff) | |
download | monitoring-plugins-8c3e7428ae21999e813b288c3d55d3870c9ef258.tar.gz |
udp checks require and send and receive option. Tests updated so if
nc is available, will check send and receive working correctly
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1353 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r-- | plugins/check_tcp.c | 6 | ||||
-rw-r--r-- | plugins/t/check_udp.t | 67 |
2 files changed, 50 insertions, 23 deletions
diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c index 5c287b7..d8fc26e 100644 --- a/plugins/check_tcp.c +++ b/plugins/check_tcp.c | |||
@@ -198,13 +198,17 @@ main (int argc, char **argv) | |||
198 | 198 | ||
199 | if(flags & FLAG_VERBOSE) { | 199 | if(flags & FLAG_VERBOSE) { |
200 | printf("Using service %s\n", SERVICE); | 200 | printf("Using service %s\n", SERVICE); |
201 | printf("Port: %d\n", PORT); | 201 | printf("Port: %d\n", server_port); |
202 | printf("flags: 0x%x\n", (int)flags); | 202 | printf("flags: 0x%x\n", (int)flags); |
203 | } | 203 | } |
204 | 204 | ||
205 | if(EXPECT && !server_expect_count) | 205 | if(EXPECT && !server_expect_count) |
206 | server_expect_count++; | 206 | server_expect_count++; |
207 | 207 | ||
208 | if(PROTOCOL==IPPROTO_UDP && !(server_expect_count && server_send)){ | ||
209 | usage(_("With UDP checks, a send/expect string must be specified.")); | ||
210 | } | ||
211 | |||
208 | /* set up the timer */ | 212 | /* set up the timer */ |
209 | signal (SIGALRM, socket_timeout_alarm_handler); | 213 | signal (SIGALRM, socket_timeout_alarm_handler); |
210 | alarm (socket_timeout); | 214 | alarm (socket_timeout); |
diff --git a/plugins/t/check_udp.t b/plugins/t/check_udp.t index c80e08a..c9228ad 100644 --- a/plugins/t/check_udp.t +++ b/plugins/t/check_udp.t | |||
@@ -6,28 +6,51 @@ | |||
6 | # | 6 | # |
7 | 7 | ||
8 | use strict; | 8 | use strict; |
9 | use Test; | 9 | use Test::More; |
10 | use NPTest; | 10 | use NPTest; |
11 | 11 | ||
12 | use vars qw($tests); | 12 | my $res; |
13 | BEGIN {$tests = 3; plan tests => $tests} #TODO# Update to 4 when the commented out test is fixed | 13 | |
14 | plan tests => 14; | ||
15 | |||
16 | $res = NPTest->testCmd( "./check_udp2 -H localhost -p 3333" ); | ||
17 | cmp_ok( $res->return_code, '==', 3, "Need send/expect string"); | ||
18 | like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK"); | ||
19 | |||
20 | $res = NPTest->testCmd( "./check_udp2 -H localhost -p 3333 -s send" ); | ||
21 | cmp_ok( $res->return_code, '==', 3, "Need expect string"); | ||
22 | like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK"); | ||
23 | |||
24 | $res = NPTest->testCmd( "./check_udp2 -H localhost -p 3333 -e expect" ); | ||
25 | cmp_ok( $res->return_code, '==', 3, "Need send string"); | ||
26 | like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK"); | ||
27 | |||
28 | $res = NPTest->testCmd( "./check_udp2 -H localhost -p 3333 -s foo -e bar" ); | ||
29 | cmp_ok( $res->return_code, '==', 2, "Errors correctly because no udp service running" ); | ||
30 | like ( $res->output, '/No data received from host/', "Output OK"); | ||
31 | |||
32 | SKIP: { | ||
33 | skip "No netcat available", 6 unless (system("which nc > /dev/null") == 0); | ||
34 | open (NC, "echo 'barbar' | nc -l -p 3333 -u |"); | ||
35 | sleep 1; | ||
36 | $res = NPTest->testCmd( "./check_udp2 -H localhost -p 3333 -s '' -e barbar -4" ); | ||
37 | cmp_ok( $res->return_code, '==', 0, "Got barbar response back" ); | ||
38 | like ( $res->output, '/\[barbar\]/', "Output OK"); | ||
39 | close NC; | ||
40 | |||
41 | my $pid = open(NC, "nc -l -p 3333 -u |"); # Start up a udp server listening on port 3333 | ||
42 | alarm(7); | ||
43 | sleep 1; | ||
44 | $SIG{ALRM} = sub { kill 'INT', $pid }; | ||
45 | my $start = time; | ||
46 | $res = NPTest->testCmd( "./check_udp2 -H localhost -p 3333 -s foofoo -e barbar -t 5 -4" ); | ||
47 | my $duration = time - $start; | ||
48 | cmp_ok( $res->return_code, '==', '2', "Hung waiting for response"); | ||
49 | like ( $res->output, '/Socket timeout after 5 seconds/', "Timeout message"); | ||
50 | cmp_ok( $duration, '==', 5, "Timeout exactly right"); | ||
51 | my $read_nc = <NC>; | ||
52 | # nc gets killed here - I think expects a linefeed from stdin, so doesn't exit itself | ||
53 | close NC; | ||
54 | cmp_ok( $read_nc, 'eq', "foofoo", "Data received correctly" ); | ||
55 | } | ||
14 | 56 | ||
15 | my $host_udp_time = getTestParameter( "host_udp_time", "NP_HOST_UDP_TIME", "localhost", | ||
16 | "A host providing the UDP Time Service" ); | ||
17 | |||
18 | my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", | ||
19 | "The hostname of system not responsive to network requests" ); | ||
20 | |||
21 | my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", | ||
22 | "An invalid (not known to DNS) hostname" ); | ||
23 | |||
24 | my $successOutput = '/^Connection accepted on port [0-9]+ - [0-9]+ second response time$/'; | ||
25 | |||
26 | my $t; | ||
27 | |||
28 | $t += checkCmd( "./check_udp -H $host_udp_time -p 37 -wt 300 -ct 600", 0, $successOutput ); | ||
29 | $t += checkCmd( "./check_udp $host_nonresponsive -p 37 -wt 0 -ct 0 -to 1", 2 ); | ||
30 | #TODO# $t += checkCmd( "./check_udp $hostname_invalid -p 37 -wt 0 -ct 0 -to 1", 2 ); # Currently returns 0 (ie success) | ||
31 | |||
32 | exit(0) if defined($Test::Harness::VERSION); | ||
33 | exit($tests - $t); | ||