Code

Test Cases for check_disk's -r, -R, -C and -g
[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 => 68;
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 print $result->output."\n";
118 my ($free_mb_on_mp1, $free_mb_on_mp2) = (m/(\d+) MB .* (\d+) MB /g);
119 my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2;
120 print "$free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2\n";
124 $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" );
125 is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs");
127 $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
128 cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
130 $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
131 cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
133 $result = NPTest->testCmd( 
134         "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free" 
135         );
136 cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
137 like( $result->output, $failureOutput, "Right output" );
140 $result = NPTest->testCmd(
141         "./check_disk -w $avg_free% -c 0% -p $less_free"
142         );
143 cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
145 $result = NPTest->testCmd(
146         "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
147         );
148 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
150 $result = NPTest->testCmd( 
151         "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
152         );
153 cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
154 my $all_disks = $result->output;
156 $result = NPTest->testCmd(
157         "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
158         );
159 isnt( $result->output, $all_disks, "-e gives different output");
161 # Need spaces around filesystem name in case less_free and more_free are nested
162 like( $result->output, qr/ $less_free /, "Found problem $less_free");
163 unlike( $result->only_output, qr/ $more_free /, "Has ignored $more_free as not a problem");
164 like( $result->perf_output, qr/ $more_free=/, "But $more_free is still in perf data");
166 $result = NPTest->testCmd(
167         "./check_disk -w $avg_free% -c 0% -p $more_free"
168         );
169 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free");
171 $result = NPTest->testCmd(
172         "./check_disk -w $avg_free% -c $avg_free% -p $less_free"
173         );
174 cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free");
175 $result = NPTest->testCmd(
176         "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free"
177         );
178 cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical");
180 $result = NPTest->testCmd(
181         "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free"
182         );
183 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
187 # Basic inode checks for sizes
189 $result = NPTest->testCmd( "./check_disk --icritical 1% --iwarning 1% -p $more_inode_free" );
190 is( $result->return_code, 0, "At least 1% free on inodes for both mountpoints");
192 $result = NPTest->testCmd( "./check_disk -K 100% -W 100% -p $less_inode_free" );
193 is( $result->return_code, 2, "Critical requesting 100% free inodes for both mountpoints");
195 $result = NPTest->testCmd( "./check_disk --iwarning 1% --icritical 1% -p $more_inode_free -K 100% -W 100% -p $less_inode_free" );
196 is( $result->return_code, 2, "Get critical on less_inode_free mountpoint $less_inode_free");
198 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free" );
199 is( $result->return_code, 1, "Get warning on less_inode_free, when checking average");
201 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free ");
202 is( $result->return_code, 0, "Get ok on more_inode_free when checking average");
204 $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" );
205 is ($result->return_code, 1, "Combine above two tests, get warning");
206 $all_disks = $result->output;
208 $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" );
209 isnt( $result->output, $all_disks, "-e gives different output");
210 like( $result->output, qr/$less_inode_free/, "Found problem $less_inode_free");
211 unlike( $result->only_output, qr/$more_inode_free\s/, "Has ignored $more_inode_free as not a problem");
212 like( $result->perf_output, qr/$more_inode_free/, "But $more_inode_free is still in perf data");
214 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free" );
215 is( $result->return_code, 0, "Get ok on more_inode_free mountpoint, checking average");
217 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" );
218 is( $result->return_code, 2, "Get critical on less_inode_free, checking average");
220 $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" );
221 is( $result->return_code, 2, "Combining above two tests, get critical");
223 $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" );
224 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
231 TODO: {
232         local $TODO = "Invalid percent figures";
233         $result = NPTest->testCmd(
234                 "./check_disk -w 10% -c 15% -p $mountpoint_valid"
235                 );
236         cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
239 $result = NPTest->testCmd( 
240         "./check_disk -p $mountpoint_valid -w 10% -c 15%"
241         );
242 cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" );
244 $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} );      # 100% empty
245 cmp_ok( $result->return_code, "==", 2, "100% empty" );
246 like( $result->output, $failureOutput, "Right output" );
248 $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
249 cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
251 $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} );      # 100 GB empty
252 cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
255 # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
256 $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
257 cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
259 $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
260 cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
262 $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
263 cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
265 TODO: {
266         local $TODO = "Invalid values";
267         $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
268         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
270         $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
271         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
273         $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
274         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
277 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
278 cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
279 cmp_ok( $result->output, 'eq', 'DISK CRITICAL - /bob does not exist', 'Output OK');
281 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
282 my $root_output = $result->output;
284 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
285 cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
286 cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
288 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E" );
289 cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified");
291 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
292 cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
293 unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
295 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" );
296 unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice");
298 # are partitions added if -C is given without path selection -p ?
299 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -C -w 0% -c 0% -p $mountpoint_valid" );
300 like( $result->output, '/;.*;\|/', "-C selects partitions if -p is not given");
302 # grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit
303 $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" );
304 cmp_ok( $result->return_code, '==', 2, "grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit");
306 # grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c
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, '==', 1, "grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c ");
310 # grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit
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, '==', 0, "grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit");
314 # grouping: exit unknown if group name is given after -p 
315 $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" );
316 cmp_ok( $result->return_code, '==', 3, "Invalid options: -p must come after groupname");
318 # regex: exit unknown if given regex is not compileable
319 $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -r '('" );
320 cmp_ok( $result->return_code, '==', 3, "Exit UNKNOWN if regex is not compileable");