Code

check_smtp: Abort on missing/unexpected greeting
[nagiosplug.git] / plugins / t / check_disk.t
1 #! /usr/bin/perl -w -I ..
2 #
3 # Disk Space Tests via check_disk
4 #
5 #
7 # TODO: Add in tests for perf data. Need to beef up Nagios::Plugin::Performance to cater for max, min, etc
9 use strict;
10 use Test::More;
11 use NPTest;
12 use POSIX qw(ceil floor);
14 my $successOutput = '/^DISK OK/';
15 my $failureOutput = '/^DISK CRITICAL/';
16 my $warningOutput = '/^DISK WARNING/';
18 my $result;
20 my $mountpoint_valid  = getTestParameter( "NP_MOUNTPOINT_VALID", "Path to valid mountpoint",  "/");
21 my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to another valid mountpoint. Must be different from 1st one", "/var");
23 if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") {
24         plan skip_all => "Need 2 mountpoints to test";
25 } else {
26         plan tests => 78;
27 }
29 $result = NPTest->testCmd( 
30         "./check_disk -w 1% -c 1% -p $mountpoint_valid -w 1% -c 1% -p $mountpoint2_valid" 
31         );
32 cmp_ok( $result->return_code, "==", 0, "Checking two mountpoints (must have at least 1% free in space and inodes)");
33 my $c = 0;
34 $_ = $result->output;
35 $c++ while /\(/g;       # counts number of "(" - should be two
36 cmp_ok( $c, '==', 2, "Got two mountpoints in output");
39 # Get perf data
40 # Should use Nagios::Plugin
41 my @perf_data = sort(split(/ /, $result->perf_output));
44 # Calculate avg_free free on mountpoint1 and mountpoint2
45 # because if you check in the middle, you should get different errors
46 $_ = $result->output;
47 my ($free_on_mp1, $free_on_mp2) = (m/\((\d+)%.*\((\d+)%/);
48 die "Cannot parse output: $_" unless ($free_on_mp1 && $free_on_mp2);
49 my $avg_free = ceil(($free_on_mp1+$free_on_mp2)/2);
50 my ($more_free, $less_free);
51 if ($free_on_mp1 > $free_on_mp2) {
52         $more_free = $mountpoint_valid;
53         $less_free = $mountpoint2_valid;
54 } elsif ($free_on_mp1 < $free_on_mp2) {
55         $more_free = $mountpoint2_valid;
56         $less_free = $mountpoint_valid;
57 } else {
58         die "Two mountpoints are the same - cannot do rest of test";
59 }
62 # Do same for inodes
63 $_ = $result->output;
64 my ($free_inode_on_mp1, $free_inode_on_mp2) = (m/inode=(\d+)%.*inode=(\d+)%/);
65 die "Cannot parse free inodes: $_" unless ($free_inode_on_mp1 && $free_inode_on_mp2);
66 my $avg_inode_free = ceil(($free_inode_on_mp1 + $free_inode_on_mp2)/2);
67 my ($more_inode_free, $less_inode_free);
68 if ($free_inode_on_mp1 > $free_inode_on_mp2) {
69         $more_inode_free = $mountpoint_valid;
70         $less_inode_free = $mountpoint2_valid;
71 } elsif ($free_inode_on_mp1 < $free_inode_on_mp2) {
72         $more_inode_free = $mountpoint2_valid;
73         $less_inode_free = $mountpoint_valid;
74 } else {
75         die "Two mountpoints with same inodes free - cannot do rest of test";
76 }
78 # Verify performance data
79 # First check absolute thresholds...
80 $result = NPTest->testCmd(
81         "./check_disk -w 20 -c 10 -p $mountpoint_valid"
82         );
83 $_ = $result->perf_output;
84 my ($warn_absth_data, $crit_absth_data, $total_absth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/);
85 is ($warn_absth_data, $total_absth_data - 20, "Wrong warning in perf data using absolute thresholds");
86 is ($crit_absth_data, $total_absth_data - 10, "Wrong critical in perf data using absolute thresholds");
88 # Then check percent thresholds.
89 $result = NPTest->testCmd(
90         "./check_disk -w 20% -c 10% -p $mountpoint_valid"
91         );
92 $_ = $result->perf_output;
93 my ($warn_percth_data, $crit_percth_data, $total_percth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/);
94 is ($warn_percth_data, int((1-20/100)*$total_percth_data), "Wrong warning in perf data using percent thresholds");
95 is ($crit_percth_data, int((1-10/100)*$total_percth_data), "Wrong critical in perf data using percent thresholds");
98 # Check when order of mount points are reversed, that perf data remains same
99 $result = NPTest->testCmd( 
100         "./check_disk -w 1% -c 1% -p $mountpoint2_valid -w 1% -c 1% -p $mountpoint_valid" 
101         );
102 @_ = sort(split(/ /, $result->perf_output));
103 is_deeply( \@perf_data, \@_, "perf data for both filesystems same when reversed");
106 # Basic filesystem checks for sizes
107 $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free" );
108 cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free");
109 like  ( $result->output, $successOutput, "OK output" );
110 like  ( $result->only_output, qr/free space/, "Have free space text");
111 like  ( $result->only_output, qr/$more_free/, "Have disk name in text");
113 $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free -p $less_free" );
114 cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free and $less_free");
115 $_ = $result->output;
116 my ($free_mb_on_mp1, $free_mb_on_mp2) = (m/(\d+) MB .* (\d+) MB /g);
117 my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2;
121 $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" );
122 is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs");
124 $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
125 cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
127 $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
128 cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
130 $result = NPTest->testCmd( 
131         "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free" 
132         );
133 cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
134 like( $result->output, $failureOutput, "Right output" );
137 $result = NPTest->testCmd(
138         "./check_disk -w $avg_free% -c 0% -p $less_free"
139         );
140 cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
142 $result = NPTest->testCmd(
143         "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
144         );
145 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
147 $result = NPTest->testCmd( 
148         "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
149         );
150 cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
151 my $all_disks = $result->output;
153 $result = NPTest->testCmd(
154         "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
155         );
156 isnt( $result->output, $all_disks, "-e gives different output");
158 # Need spaces around filesystem name in case less_free and more_free are nested
159 like( $result->output, qr/ $less_free /, "Found problem $less_free");
160 unlike( $result->only_output, qr/ $more_free /, "Has ignored $more_free as not a problem");
161 like( $result->perf_output, qr/ $more_free=/, "But $more_free is still in perf data");
163 $result = NPTest->testCmd(
164         "./check_disk -w $avg_free% -c 0% -p $more_free"
165         );
166 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free");
168 $result = NPTest->testCmd(
169         "./check_disk -w $avg_free% -c $avg_free% -p $less_free"
170         );
171 cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free");
172 $result = NPTest->testCmd(
173         "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free"
174         );
175 cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical");
177 $result = NPTest->testCmd(
178         "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free"
179         );
180 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
184 # Basic inode checks for sizes
186 $result = NPTest->testCmd( "./check_disk --icritical 1% --iwarning 1% -p $more_inode_free" );
187 is( $result->return_code, 0, "At least 1% free on inodes for both mountpoints");
189 $result = NPTest->testCmd( "./check_disk -K 100% -W 100% -p $less_inode_free" );
190 is( $result->return_code, 2, "Critical requesting 100% free inodes for both mountpoints");
192 $result = NPTest->testCmd( "./check_disk --iwarning 1% --icritical 1% -p $more_inode_free -K 100% -W 100% -p $less_inode_free" );
193 is( $result->return_code, 2, "Get critical on less_inode_free mountpoint $less_inode_free");
195 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free" );
196 is( $result->return_code, 1, "Get warning on less_inode_free, when checking average");
198 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free ");
199 is( $result->return_code, 0, "Get ok on more_inode_free when checking average");
201 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free" );
202 is ($result->return_code, 1, "Combine above two tests, get warning");
203 $all_disks = $result->output;
205 $result = NPTest->testCmd( "./check_disk -e -W $avg_inode_free% -K 0% -p $less_inode_free -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free" );
206 isnt( $result->output, $all_disks, "-e gives different output");
207 like( $result->output, qr/$less_inode_free/, "Found problem $less_inode_free");
208 unlike( $result->only_output, qr/$more_inode_free\s/, "Has ignored $more_inode_free as not a problem");
209 like( $result->perf_output, qr/$more_inode_free/, "But $more_inode_free is still in perf data");
211 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free" );
212 is( $result->return_code, 0, "Get ok on more_inode_free mountpoint, checking average");
214 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" );
215 is( $result->return_code, 2, "Get critical on less_inode_free, checking average");
217 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" );
218 is( $result->return_code, 2, "Combining above two tests, get critical");
220 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free -W $avg_inode_free% -K 0% -p $more_inode_free" );
221 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
228 TODO: {
229         local $TODO = "Invalid percent figures";
230         $result = NPTest->testCmd(
231                 "./check_disk -w 10% -c 15% -p $mountpoint_valid"
232                 );
233         cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
236 $result = NPTest->testCmd( 
237         "./check_disk -p $mountpoint_valid -w 10% -c 15%"
238         );
239 cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" );
241 $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} );      # 100% empty
242 cmp_ok( $result->return_code, "==", 2, "100% empty" );
243 like( $result->output, $failureOutput, "Right output" );
245 $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
246 cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
248 $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} );      # 100 GB empty
249 cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
252 # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
253 $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
254 cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
255 like ( $result->only_output, qr(^[^;]*;[^;]*$), "Select only one path with positional arguments");
257 $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
258 cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
260 $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
261 cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
263 TODO: {
264         local $TODO = "Invalid values";
265         $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
266         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
268         $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
269         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
271         $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
272         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
275 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
276 cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
277 like( $result->output, '/^DISK CRITICAL - /bob is not accessible:.*$/', 'Output OK');
279 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
280 my $root_output = $result->output;
282 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
283 cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
284 cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
286 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -E -p /etc " );
287 cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified");
289 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E " );
290 cmp_ok( $result->return_code, '==', 3, "-E/--exact-match must be specified before -p");
292 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -r /etc -E " );
293 cmp_ok( $result->return_code, '==', 3, "-E/--exact-match must be specified before -r");
295 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
296 cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
297 unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
299 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" );
300 unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice");
302 # are partitions added if -C is given without path selection -p ?
303 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -C -w 0% -c 0% -p $mountpoint_valid" );
304 like( $result->output, '/;.*;\|/', "-C selects partitions if -p is not given");
306 # grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit
307 $result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all + 1) ." -c ". ($free_mb_on_all + 1) ."-g group -p $mountpoint_valid -p $mountpoint2_valid" );
308 cmp_ok( $result->return_code, '==', 2, "grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit");
310 # grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c
311 $result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all + 1) ." -c ". ($free_mb_on_all - 1) ." -g group -p $mountpoint_valid -p $mountpoint2_valid" );
312 cmp_ok( $result->return_code, '==', 1, "grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c ");
314 # grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit
315 $result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all - 1) ." -c ". ($free_mb_on_all - 1) ." -g group -p $mountpoint_valid -p $mountpoint2_valid" );
316 cmp_ok( $result->return_code, '==', 0, "grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit");
318 # grouping: exit unknown if group name is given after -p 
319 $result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all - 1) ." -c ". ($free_mb_on_all - 1) ." -p $mountpoint_valid -g group -p $mountpoint2_valid" );
320 cmp_ok( $result->return_code, '==', 3, "Invalid options: -p must come after groupname");
322 # regex: exit unknown if given regex is not compileable
323 $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -r '('" );
324 cmp_ok( $result->return_code, '==', 3, "Exit UNKNOWN if regex is not compileable");
326 # ignore: exit unknown, if all pathes are deselected using -i
327 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '$mountpoint_valid' -i '$mountpoint2_valid'" );
328 cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored (case sensitive)");
330 # ignore: exit unknown, if all pathes are deselected using -I
331 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -I '".uc($mountpoint_valid)."' -I '".uc($mountpoint2_valid)."'" );
332 cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored (case insensitive)");
334 # ignore: exit unknown, if all pathes are deselected using -i
335 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '.*'" );
336 cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored using -i '.*'");
338 # ignore: test if ignored path is actually ignored
339 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^$mountpoint2_valid\$'");
340 like( $result->output, qr/$mountpoint_valid/, "output data does have $mountpoint_valid in it");
341 unlike( $result->output, qr/$mountpoint2_valid/, "output data does not have $mountpoint2_valid in it");
343 # ignore: test if all pathes are listed when ignore regex doesn't match
344 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^barbazJodsf\$'");
345 like( $result->output, qr/$mountpoint_valid/, "ignore: output data does have $mountpoint_valid when regex doesn't match");
346 like( $result->output, qr/$mountpoint2_valid/,"ignore: output data does have $mountpoint2_valid when regex doesn't match");