diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_nt.c | 20 | ||||
-rwxr-xr-x | plugins/tests/check_nt.t | 77 |
2 files changed, 90 insertions, 7 deletions
diff --git a/plugins/check_nt.c b/plugins/check_nt.c index 4d9157a6..ee5e2a63 100644 --- a/plugins/check_nt.c +++ b/plugins/check_nt.c | |||
@@ -93,6 +93,7 @@ int main(int argc, char **argv){ | |||
93 | char *temp_string_perf=NULL; | 93 | char *temp_string_perf=NULL; |
94 | char *description=NULL,*counter_unit = NULL; | 94 | char *description=NULL,*counter_unit = NULL; |
95 | char *minval = NULL, *maxval = NULL, *errcvt = NULL; | 95 | char *minval = NULL, *maxval = NULL, *errcvt = NULL; |
96 | char *fds=NULL, *tds=NULL; | ||
96 | 97 | ||
97 | double total_disk_space=0; | 98 | double total_disk_space=0; |
98 | double free_disk_space=0; | 99 | double free_disk_space=0; |
@@ -214,13 +215,18 @@ int main(int argc, char **argv){ | |||
214 | else { | 215 | else { |
215 | asprintf(&send_buffer,"%s&4&%s", req_password, value_list); | 216 | asprintf(&send_buffer,"%s&4&%s", req_password, value_list); |
216 | fetch_data (server_address, server_port, send_buffer); | 217 | fetch_data (server_address, server_port, send_buffer); |
217 | free_disk_space=atof(strtok(recv_buffer,"&")); | 218 | fds=strtok(recv_buffer,"&"); |
218 | total_disk_space=atof(strtok(NULL,"&")); | 219 | tds=strtok(NULL,"&"); |
219 | percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100; | 220 | if(fds!=NULL) |
220 | warning_used_space = ((float)warning_value / 100) * total_disk_space; | 221 | free_disk_space=atof(fds); |
221 | critical_used_space = ((float)critical_value / 100) * total_disk_space; | 222 | if(tds!=NULL) |
223 | total_disk_space=atof(tds); | ||
224 | |||
225 | if (total_disk_space>0 && free_disk_space>=0) { | ||
226 | percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100; | ||
227 | warning_used_space = ((float)warning_value / 100) * total_disk_space; | ||
228 | critical_used_space = ((float)critical_value / 100) * total_disk_space; | ||
222 | 229 | ||
223 | if (free_disk_space>=0) { | ||
224 | asprintf(&temp_string,_("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"), | 230 | asprintf(&temp_string,_("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"), |
225 | value_list, total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, | 231 | value_list, total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, |
226 | percent_used_space, free_disk_space / 1073741824, (free_disk_space / total_disk_space)*100); | 232 | percent_used_space, free_disk_space / 1073741824, (free_disk_space / total_disk_space)*100); |
@@ -238,7 +244,7 @@ int main(int argc, char **argv){ | |||
238 | output_message = strdup (temp_string); | 244 | output_message = strdup (temp_string); |
239 | perfdata = temp_string_perf; | 245 | perfdata = temp_string_perf; |
240 | } else { | 246 | } else { |
241 | output_message = strdup (_("Free disk space : Invalid drive ")); | 247 | output_message = strdup (_("Free disk space : Invalid drive")); |
242 | return_code=STATE_UNKNOWN; | 248 | return_code=STATE_UNKNOWN; |
243 | } | 249 | } |
244 | } | 250 | } |
diff --git a/plugins/tests/check_nt.t b/plugins/tests/check_nt.t new file mode 100755 index 00000000..d1600c77 --- /dev/null +++ b/plugins/tests/check_nt.t | |||
@@ -0,0 +1,77 @@ | |||
1 | #! /usr/bin/perl -w -I .. | ||
2 | # | ||
3 | # Test check_nt by having a stub check_nt daemon | ||
4 | # | ||
5 | |||
6 | use strict; | ||
7 | use Test::More; | ||
8 | use NPTest; | ||
9 | use FindBin qw($Bin); | ||
10 | |||
11 | use IO::Socket; | ||
12 | use IO::Select; | ||
13 | use POSIX; | ||
14 | |||
15 | my $port = 50000 + int(rand(1000)); | ||
16 | |||
17 | my $pid = fork(); | ||
18 | if ($pid) { | ||
19 | # Parent | ||
20 | #print "parent\n"; | ||
21 | # give our webserver some time to startup | ||
22 | sleep(1); | ||
23 | } else { | ||
24 | # Child | ||
25 | #print "child\n"; | ||
26 | |||
27 | my $server = IO::Socket::INET->new( | ||
28 | LocalPort => $port, | ||
29 | Type => SOCK_STREAM, | ||
30 | Reuse => 1, | ||
31 | Proto => "tcp", | ||
32 | Listen => 10, | ||
33 | ) or die "Cannot be a tcp server on port $port: $@"; | ||
34 | |||
35 | $server->autoflush(1); | ||
36 | |||
37 | print "Please contact me at port $port\n"; | ||
38 | while (my $client = $server->accept ) { | ||
39 | my $data = ""; | ||
40 | my $rv = $client->recv($data, POSIX::BUFSIZ, 0); | ||
41 | |||
42 | my ($password, $command, $arg) = split('&', $data); | ||
43 | |||
44 | if ($command eq "4") { | ||
45 | if ($arg eq "c") { | ||
46 | print $client "930000000&1000000000"; | ||
47 | } elsif ($arg eq "d") { | ||
48 | print $client "UNKNOWN: Drive is not a fixed drive"; | ||
49 | } | ||
50 | } | ||
51 | } | ||
52 | exit; | ||
53 | } | ||
54 | |||
55 | END { if ($pid) { print "Killing $pid\n"; kill "INT", $pid } }; | ||
56 | |||
57 | if ($ARGV[0] && $ARGV[0] eq "-d") { | ||
58 | sleep 1000; | ||
59 | } | ||
60 | |||
61 | if (-x "./check_nt") { | ||
62 | plan tests => 4; | ||
63 | } else { | ||
64 | plan skip_all => "No check_nt compiled"; | ||
65 | } | ||
66 | |||
67 | my $result; | ||
68 | my $command = "./check_nt -H 127.0.0.1 -p $port"; | ||
69 | |||
70 | $result = NPTest->testCmd( "$command -v USEDDISKSPACE -l c" ); | ||
71 | is( $result->return_code, 0, "USEDDISKSPACE c"); | ||
72 | is( $result->output, q{c:\ - total: 0.93 Gb - used: 0.07 Gb (7%) - free 0.87 Gb (93%) | 'c:\ Used Space'=0.07Gb;0.00;0.00;0.00;0.93}, "Output right" ); | ||
73 | |||
74 | $result = NPTest->testCmd( "$command -v USEDDISKSPACE -l d" ); | ||
75 | is( $result->return_code, 3, "USEDDISKSPACE d - invalid"); | ||
76 | is( $result->output, "Free disk space : Invalid drive", "Output right" ); | ||
77 | |||