Code

Fixed inode thresholds, regressed from previous release
[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 => 56;
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");
39 # Calculate avg_free free on mountpoint1 and mountpoint2
40 # because if you check in the middle, you should get different errors
41 $_ = $result->output;
42 my ($free_on_mp1, $free_on_mp2) = (m/\((\d+)%.*\((\d+)%/);
43 die "Cannot parse output: $_" unless ($free_on_mp1 && $free_on_mp2);
44 my $avg_free = ceil(($free_on_mp1+$free_on_mp2)/2);
45 my ($more_free, $less_free);
46 if ($free_on_mp1 > $free_on_mp2) {
47         $more_free = $mountpoint_valid;
48         $less_free = $mountpoint2_valid;
49 } elsif ($free_on_mp1 < $free_on_mp2) {
50         $more_free = $mountpoint2_valid;
51         $less_free = $mountpoint_valid;
52 } else {
53         die "Two mountpoints are the same - cannot do rest of test";
54 }
57 # Do same for inodes
58 $_ = $result->output;
59 my ($free_inode_on_mp1, $free_inode_on_mp2) = (m/inode=(\d+)%.*inode=(\d+)%/);
60 die "Cannot parse free inodes: $_" unless ($free_inode_on_mp1 && $free_inode_on_mp2);
61 my $avg_inode_free = ceil(($free_inode_on_mp1 + $free_inode_on_mp2)/2);
62 my ($more_inode_free, $less_inode_free);
63 if ($free_inode_on_mp1 > $free_inode_on_mp2) {
64         $more_inode_free = $mountpoint_valid;
65         $less_inode_free = $mountpoint2_valid;
66 } elsif ($free_on_mp1 < $free_on_mp2) {
67         $more_inode_free = $mountpoint2_valid;
68         $less_inode_free = $mountpoint_valid;
69 } else {
70         die "Two mountpoints with same inodes free - cannot do rest of test";
71 }
75 # Basic filesystem checks for sizes
77 $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free" );
78 cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free");
79 like  ( $result->output, $successOutput, "OK output" );
80 like  ( $result->only_output, qr/free space/, "Have free space text");
81 like  ( $result->only_output, qr/$more_free/, "Have disk name in text");
83 $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" );
84 is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs");
86 $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
87 cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
89 $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
90 cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
92 $result = NPTest->testCmd( 
93         "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free" 
94         );
95 cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
96 like( $result->output, $failureOutput, "Right output" );
99 $result = NPTest->testCmd(
100         "./check_disk -w $avg_free% -c 0% -p $less_free"
101         );
102 cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
104 $result = NPTest->testCmd(
105         "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
106         );
107 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
109 $result = NPTest->testCmd( 
110         "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
111         );
112 cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
113 my $all_disks = $result->output;
115 $result = NPTest->testCmd(
116         "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
117         );
118 isnt( $result->output, $all_disks, "-e gives different output");
119 like( $result->output, qr/$less_free/, "Found problem $less_free");
120 unlike( $result->only_output, qr/$more_free/, "Has ignored $more_free as not a problem");
121 like( $result->perf_output, qr/$more_free/, "But $more_free is still in perf data");
123 $result = NPTest->testCmd(
124         "./check_disk -w $avg_free% -c 0% -p $more_free"
125         );
126 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free");
128 $result = NPTest->testCmd(
129         "./check_disk -w $avg_free% -c $avg_free% -p $less_free"
130         );
131 cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free");
132 $result = NPTest->testCmd(
133         "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free"
134         );
135 cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical");
137 $result = NPTest->testCmd(
138         "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free"
139         );
140 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
144 # Basic inode checks for sizes
146 $result = NPTest->testCmd( "./check_disk --icritical 1% --iwarning 1% -p $more_inode_free" );
147 is( $result->return_code, 0, "At least 1% free on inodes for both mountpoints");
149 $result = NPTest->testCmd( "./check_disk -K 100% -W 100% -p $less_inode_free" );
150 is( $result->return_code, 2, "Critical requesting 100% free inodes for both mountpoints");
152 $result = NPTest->testCmd( "./check_disk --iwarning 1% --icritical 1% -p $more_inode_free -K 100% -W 100% -p $less_inode_free" );
153 is( $result->return_code, 2, "Get critical on less_inode_free mountpoint $less_inode_free");
155 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free" );
156 is( $result->return_code, 1, "Get warning on less_inode_free, when checking average");
158 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free ");
159 is( $result->return_code, 0, "Get ok on more_inode_free when checking average");
161 $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" );
162 is ($result->return_code, 1, "Combine above two tests, get warning");
163 $all_disks = $result->output;
165 $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" );
166 isnt( $result->output, $all_disks, "-e gives different output");
167 like( $result->output, qr/$less_inode_free/, "Found problem $less_inode_free");
168 unlike( $result->only_output, qr/$more_inode_free/, "Has ignored $more_inode_free as not a problem");
169 like( $result->perf_output, qr/$more_inode_free/, "But $more_inode_free is still in perf data");
171 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free" );
172 is( $result->return_code, 0, "Get ok on more_inode_free mountpoint, checking average");
174 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" );
175 is( $result->return_code, 2, "Get critical on less_inode_free, checking average");
177 $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" );
178 is( $result->return_code, 2, "Combining above two tests, get critical");
180 $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" );
181 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
188 TODO: {
189         local $TODO = "Invalid percent figures";
190         $result = NPTest->testCmd(
191                 "./check_disk -w 10% -c 15% -p $mountpoint_valid"
192                 );
193         cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
196 $result = NPTest->testCmd( 
197         "./check_disk -p $mountpoint_valid -w 10% -c 15%"
198         );
199 cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" );
201 $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} );      # 100% empty
202 cmp_ok( $result->return_code, "==", 2, "100% empty" );
203 like( $result->output, $failureOutput, "Right output" );
205 $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
206 cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
208 $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} );      # 100 GB empty
209 cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
212 # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
213 $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
214 cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
216 $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
217 cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
219 $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
220 cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
222 TODO: {
223         local $TODO = "Invalid values";
224         $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
225         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
227         $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
228         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
230         $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
231         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
234 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
235 cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
236 cmp_ok( $result->output, 'eq', 'DISK CRITICAL - /bob does not exist', 'Output OK');
238 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
239 my $root_output = $result->output;
241 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
242 cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
243 cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
245 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E" );
246 cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified");
248 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
249 cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
250 unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
252 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" );
253 unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice");