Code

99c434acb960e8b10d535ebd32e82b094f551db4
[nagiosplug.git] / plugins / t / check_disk.t
1 #! /usr/bin/perl -w -I ..
2 #
3 # Disk Space Tests via check_disk
4 #
5 # $Id$
6 #
8 # TODO: Add in tests for perf data. Need to beef up Nagios::Plugin::Performance to cater for max, min, etc
10 use strict;
11 use Test::More;
12 use NPTest;
13 use POSIX qw(ceil floor);
15 my $successOutput = '/^DISK OK/';
16 my $failureOutput = '/^DISK CRITICAL/';
17 my $warningOutput = '/^DISK WARNING/';
19 my $result;
21 my $mountpoint_valid  = getTestParameter( "NP_MOUNTPOINT_VALID", "Path to valid mountpoint",  "/");
22 my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to another valid mountpoint. Must be different from 1st one", "/var");
24 if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") {
25         plan skip_all => "Need 2 mountpoints to test";
26 } else {
27         plan tests => 78;
28 }
30 $result = NPTest->testCmd( 
31         "./check_disk -w 1% -c 1% -p $mountpoint_valid -w 1% -c 1% -p $mountpoint2_valid" 
32         );
33 cmp_ok( $result->return_code, "==", 0, "Checking two mountpoints (must have at least 1% free in space and inodes)");
34 my $c = 0;
35 $_ = $result->output;
36 $c++ while /\(/g;       # counts number of "(" - should be two
37 cmp_ok( $c, '==', 2, "Got two mountpoints in output");
40 # Get perf data
41 # Should use Nagios::Plugin
42 my @perf_data = sort(split(/ /, $result->perf_output));
45 # Calculate avg_free free on mountpoint1 and mountpoint2
46 # because if you check in the middle, you should get different errors
47 $_ = $result->output;
48 my ($free_on_mp1, $free_on_mp2) = (m/\((\d+)%.*\((\d+)%/);
49 die "Cannot parse output: $_" unless ($free_on_mp1 && $free_on_mp2);
50 my $avg_free = ceil(($free_on_mp1+$free_on_mp2)/2);
51 my ($more_free, $less_free);
52 if ($free_on_mp1 > $free_on_mp2) {
53         $more_free = $mountpoint_valid;
54         $less_free = $mountpoint2_valid;
55 } elsif ($free_on_mp1 < $free_on_mp2) {
56         $more_free = $mountpoint2_valid;
57         $less_free = $mountpoint_valid;
58 } else {
59         die "Two mountpoints are the same - cannot do rest of test";
60 }
63 # Do same for inodes
64 $_ = $result->output;
65 my ($free_inode_on_mp1, $free_inode_on_mp2) = (m/inode=(\d+)%.*inode=(\d+)%/);
66 die "Cannot parse free inodes: $_" unless ($free_inode_on_mp1 && $free_inode_on_mp2);
67 my $avg_inode_free = ceil(($free_inode_on_mp1 + $free_inode_on_mp2)/2);
68 my ($more_inode_free, $less_inode_free);
69 if ($free_inode_on_mp1 > $free_inode_on_mp2) {
70         $more_inode_free = $mountpoint_valid;
71         $less_inode_free = $mountpoint2_valid;
72 } elsif ($free_inode_on_mp1 < $free_inode_on_mp2) {
73         $more_inode_free = $mountpoint2_valid;
74         $less_inode_free = $mountpoint_valid;
75 } else {
76         die "Two mountpoints with same inodes free - cannot do rest of test";
77 }
79 # Verify performance data
80 # First check absolute thresholds...
81 $result = NPTest->testCmd(
82         "./check_disk -w 20 -c 10 -p $mountpoint_valid"
83         );
84 $_ = $result->perf_output;
85 my ($warn_absth_data, $crit_absth_data, $total_absth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/);
86 is ($warn_absth_data, $total_absth_data - 20, "Wrong warning in perf data using absolute thresholds");
87 is ($crit_absth_data, $total_absth_data - 10, "Wrong critical in perf data using absolute thresholds");
89 # Then check percent thresholds.
90 $result = NPTest->testCmd(
91         "./check_disk -w 20% -c 10% -p $mountpoint_valid"
92         );
93 $_ = $result->perf_output;
94 my ($warn_percth_data, $crit_percth_data, $total_percth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/);
95 is ($warn_percth_data, int((1-20/100)*$total_percth_data), "Wrong warning in perf data using percent thresholds");
96 is ($crit_percth_data, int((1-10/100)*$total_percth_data), "Wrong critical in perf data using percent thresholds");
99 # Check when order of mount points are reversed, that perf data remains same
100 $result = NPTest->testCmd( 
101         "./check_disk -w 1% -c 1% -p $mountpoint2_valid -w 1% -c 1% -p $mountpoint_valid" 
102         );
103 @_ = sort(split(/ /, $result->perf_output));
104 is_deeply( \@perf_data, \@_, "perf data for both filesystems same when reversed");
107 # Basic filesystem checks for sizes
108 $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free" );
109 cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free");
110 like  ( $result->output, $successOutput, "OK output" );
111 like  ( $result->only_output, qr/free space/, "Have free space text");
112 like  ( $result->only_output, qr/$more_free/, "Have disk name in text");
114 $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free -p $less_free" );
115 cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free and $less_free");
116 $_ = $result->output;
117 my ($free_mb_on_mp1, $free_mb_on_mp2) = (m/(\d+) MB .* (\d+) MB /g);
118 my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2;
122 $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" );
123 is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs");
125 $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
126 cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
128 $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
129 cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
131 $result = NPTest->testCmd( 
132         "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free" 
133         );
134 cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
135 like( $result->output, $failureOutput, "Right output" );
138 $result = NPTest->testCmd(
139         "./check_disk -w $avg_free% -c 0% -p $less_free"
140         );
141 cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
143 $result = NPTest->testCmd(
144         "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
145         );
146 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
148 $result = NPTest->testCmd( 
149         "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
150         );
151 cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
152 my $all_disks = $result->output;
154 $result = NPTest->testCmd(
155         "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
156         );
157 isnt( $result->output, $all_disks, "-e gives different output");
159 # Need spaces around filesystem name in case less_free and more_free are nested
160 like( $result->output, qr/ $less_free /, "Found problem $less_free");
161 unlike( $result->only_output, qr/ $more_free /, "Has ignored $more_free as not a problem");
162 like( $result->perf_output, qr/ $more_free=/, "But $more_free is still in perf data");
164 $result = NPTest->testCmd(
165         "./check_disk -w $avg_free% -c 0% -p $more_free"
166         );
167 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free");
169 $result = NPTest->testCmd(
170         "./check_disk -w $avg_free% -c $avg_free% -p $less_free"
171         );
172 cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free");
173 $result = NPTest->testCmd(
174         "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free"
175         );
176 cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical");
178 $result = NPTest->testCmd(
179         "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free"
180         );
181 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
185 # Basic inode checks for sizes
187 $result = NPTest->testCmd( "./check_disk --icritical 1% --iwarning 1% -p $more_inode_free" );
188 is( $result->return_code, 0, "At least 1% free on inodes for both mountpoints");
190 $result = NPTest->testCmd( "./check_disk -K 100% -W 100% -p $less_inode_free" );
191 is( $result->return_code, 2, "Critical requesting 100% free inodes for both mountpoints");
193 $result = NPTest->testCmd( "./check_disk --iwarning 1% --icritical 1% -p $more_inode_free -K 100% -W 100% -p $less_inode_free" );
194 is( $result->return_code, 2, "Get critical on less_inode_free mountpoint $less_inode_free");
196 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free" );
197 is( $result->return_code, 1, "Get warning on less_inode_free, when checking average");
199 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free ");
200 is( $result->return_code, 0, "Get ok on more_inode_free when checking average");
202 $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" );
203 is ($result->return_code, 1, "Combine above two tests, get warning");
204 $all_disks = $result->output;
206 $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" );
207 isnt( $result->output, $all_disks, "-e gives different output");
208 like( $result->output, qr/$less_inode_free/, "Found problem $less_inode_free");
209 unlike( $result->only_output, qr/$more_inode_free\s/, "Has ignored $more_inode_free as not a problem");
210 like( $result->perf_output, qr/$more_inode_free/, "But $more_inode_free is still in perf data");
212 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free" );
213 is( $result->return_code, 0, "Get ok on more_inode_free mountpoint, checking average");
215 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" );
216 is( $result->return_code, 2, "Get critical on less_inode_free, checking average");
218 $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" );
219 is( $result->return_code, 2, "Combining above two tests, get critical");
221 $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" );
222 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
229 TODO: {
230         local $TODO = "Invalid percent figures";
231         $result = NPTest->testCmd(
232                 "./check_disk -w 10% -c 15% -p $mountpoint_valid"
233                 );
234         cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
237 $result = NPTest->testCmd( 
238         "./check_disk -p $mountpoint_valid -w 10% -c 15%"
239         );
240 cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" );
242 $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} );      # 100% empty
243 cmp_ok( $result->return_code, "==", 2, "100% empty" );
244 like( $result->output, $failureOutput, "Right output" );
246 $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
247 cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
249 $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} );      # 100 GB empty
250 cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
253 # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
254 $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
255 cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
256 like ( $result->only_output, qr(^[^;]*;[^;]*$), "Select only one path with positional arguments");
258 $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
259 cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
261 $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
262 cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
264 TODO: {
265         local $TODO = "Invalid values";
266         $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
267         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
269         $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
270         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
272         $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
273         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
276 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
277 cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
278 like( $result->output, '/^DISK CRITICAL - /bob is not accessible:.*$/', 'Output OK');
280 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
281 my $root_output = $result->output;
283 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
284 cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
285 cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
287 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -E -p /etc " );
288 cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified");
290 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E " );
291 cmp_ok( $result->return_code, '==', 3, "-E/--exact-match must be specified before -p");
293 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -r /etc -E " );
294 cmp_ok( $result->return_code, '==', 3, "-E/--exact-match must be specified before -r");
296 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
297 cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
298 unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
300 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" );
301 unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice");
303 # are partitions added if -C is given without path selection -p ?
304 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -C -w 0% -c 0% -p $mountpoint_valid" );
305 like( $result->output, '/;.*;\|/', "-C selects partitions if -p is not given");
307 # grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit
308 $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" );
309 cmp_ok( $result->return_code, '==', 2, "grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit");
311 # grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c
312 $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" );
313 cmp_ok( $result->return_code, '==', 1, "grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c ");
315 # grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit
316 $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" );
317 cmp_ok( $result->return_code, '==', 0, "grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit");
319 # grouping: exit unknown if group name is given after -p 
320 $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" );
321 cmp_ok( $result->return_code, '==', 3, "Invalid options: -p must come after groupname");
323 # regex: exit unknown if given regex is not compileable
324 $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -r '('" );
325 cmp_ok( $result->return_code, '==', 3, "Exit UNKNOWN if regex is not compileable");
327 # ignore: exit unknown, if all pathes are deselected using -i
328 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '$mountpoint_valid' -i '$mountpoint2_valid'" );
329 cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored (case sensitive)");
331 # ignore: exit unknown, if all pathes are deselected using -I
332 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -I '".uc($mountpoint_valid)."' -I '".uc($mountpoint2_valid)."'" );
333 cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored (case insensitive)");
335 # ignore: exit unknown, if all pathes are deselected using -i
336 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '.*'" );
337 cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored using -i '.*'");
339 # ignore: test if ignored path is actually ignored
340 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^$mountpoint2_valid\$'");
341 like( $result->output, qr/$mountpoint_valid/, "output data does have $mountpoint_valid in it");
342 unlike( $result->output, qr/$mountpoint2_valid/, "output data does not have $mountpoint2_valid in it");
344 # ignore: test if all pathes are listed when ignore regex doesn't match
345 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^barbazJodsf\$'");
346 like( $result->output, qr/$mountpoint_valid/, "ignore: output data does have $mountpoint_valid when regex doesn't match");
347 like( $result->output, qr/$mountpoint2_valid/,"ignore: output data does have $mountpoint2_valid when regex doesn't match");