Code

2c0802cc36dd7bfd9ba38094403ec79c7323dfd7
[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 => 61;
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 -e -w 1 -c 1 -p $more_free" );
115 is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs");
117 $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
118 cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
120 $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
121 cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
123 $result = NPTest->testCmd( 
124         "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free" 
125         );
126 cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
127 like( $result->output, $failureOutput, "Right output" );
130 $result = NPTest->testCmd(
131         "./check_disk -w $avg_free% -c 0% -p $less_free"
132         );
133 cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
135 $result = NPTest->testCmd(
136         "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
137         );
138 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
140 $result = NPTest->testCmd( 
141         "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
142         );
143 cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
144 my $all_disks = $result->output;
146 $result = NPTest->testCmd(
147         "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
148         );
149 isnt( $result->output, $all_disks, "-e gives different output");
151 # Need spaces around filesystem name in case less_free and more_free are nested
152 like( $result->output, qr/ $less_free /, "Found problem $less_free");
153 unlike( $result->only_output, qr/ $more_free /, "Has ignored $more_free as not a problem");
154 like( $result->perf_output, qr/ $more_free=/, "But $more_free is still in perf data");
156 $result = NPTest->testCmd(
157         "./check_disk -w $avg_free% -c 0% -p $more_free"
158         );
159 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free");
161 $result = NPTest->testCmd(
162         "./check_disk -w $avg_free% -c $avg_free% -p $less_free"
163         );
164 cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free");
165 $result = NPTest->testCmd(
166         "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free"
167         );
168 cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical");
170 $result = NPTest->testCmd(
171         "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free"
172         );
173 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
177 # Basic inode checks for sizes
179 $result = NPTest->testCmd( "./check_disk --icritical 1% --iwarning 1% -p $more_inode_free" );
180 is( $result->return_code, 0, "At least 1% free on inodes for both mountpoints");
182 $result = NPTest->testCmd( "./check_disk -K 100% -W 100% -p $less_inode_free" );
183 is( $result->return_code, 2, "Critical requesting 100% free inodes for both mountpoints");
185 $result = NPTest->testCmd( "./check_disk --iwarning 1% --icritical 1% -p $more_inode_free -K 100% -W 100% -p $less_inode_free" );
186 is( $result->return_code, 2, "Get critical on less_inode_free mountpoint $less_inode_free");
188 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free" );
189 is( $result->return_code, 1, "Get warning on less_inode_free, when checking average");
191 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free ");
192 is( $result->return_code, 0, "Get ok on more_inode_free when checking average");
194 $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" );
195 is ($result->return_code, 1, "Combine above two tests, get warning");
196 $all_disks = $result->output;
198 $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" );
199 isnt( $result->output, $all_disks, "-e gives different output");
200 like( $result->output, qr/$less_inode_free/, "Found problem $less_inode_free");
201 unlike( $result->only_output, qr/$more_inode_free\s/, "Has ignored $more_inode_free as not a problem");
202 like( $result->perf_output, qr/$more_inode_free/, "But $more_inode_free is still in perf data");
204 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free" );
205 is( $result->return_code, 0, "Get ok on more_inode_free mountpoint, checking average");
207 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" );
208 is( $result->return_code, 2, "Get critical on less_inode_free, checking average");
210 $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" );
211 is( $result->return_code, 2, "Combining above two tests, get critical");
213 $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" );
214 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
221 TODO: {
222         local $TODO = "Invalid percent figures";
223         $result = NPTest->testCmd(
224                 "./check_disk -w 10% -c 15% -p $mountpoint_valid"
225                 );
226         cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
229 $result = NPTest->testCmd( 
230         "./check_disk -p $mountpoint_valid -w 10% -c 15%"
231         );
232 cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" );
234 $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} );      # 100% empty
235 cmp_ok( $result->return_code, "==", 2, "100% empty" );
236 like( $result->output, $failureOutput, "Right output" );
238 $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
239 cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
241 $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} );      # 100 GB empty
242 cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
245 # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
246 $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
247 cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
249 $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
250 cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
252 $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
253 cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
255 TODO: {
256         local $TODO = "Invalid values";
257         $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
258         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
260         $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
261         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
263         $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
264         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
267 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
268 cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
269 cmp_ok( $result->output, 'eq', 'DISK CRITICAL - /bob does not exist', 'Output OK');
271 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
272 my $root_output = $result->output;
274 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
275 cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
276 cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
278 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E" );
279 cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified");
281 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
282 cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
283 unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
285 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" );
286 unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice");