Code

Added notes about check_disk perf data fix. Added test to check perf data is
[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 => 57;
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 }
81 # Check when order of mount points are reversed, that perf data remains same
82 $result = NPTest->testCmd( 
83         "./check_disk -w 1% -c 1% -p $mountpoint2_valid -w 1% -c 1% -p $mountpoint_valid" 
84         );
85 @_ = sort(split(/ /, $result->perf_output));
86 is_deeply( \@perf_data, \@_, "perf data for both filesystems same when reversed");
89 # Basic filesystem checks for sizes
90 $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free" );
91 cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free");
92 like  ( $result->output, $successOutput, "OK output" );
93 like  ( $result->only_output, qr/free space/, "Have free space text");
94 like  ( $result->only_output, qr/$more_free/, "Have disk name in text");
96 $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" );
97 is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs");
99 $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
100 cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
102 $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
103 cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
105 $result = NPTest->testCmd( 
106         "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free" 
107         );
108 cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
109 like( $result->output, $failureOutput, "Right output" );
112 $result = NPTest->testCmd(
113         "./check_disk -w $avg_free% -c 0% -p $less_free"
114         );
115 cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
117 $result = NPTest->testCmd(
118         "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
119         );
120 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
122 $result = NPTest->testCmd( 
123         "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
124         );
125 cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
126 my $all_disks = $result->output;
128 $result = NPTest->testCmd(
129         "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
130         );
131 isnt( $result->output, $all_disks, "-e gives different output");
133 # Need spaces around filesystem name in case less_free and more_free are nested
134 like( $result->output, qr/ $less_free /, "Found problem $less_free");
135 unlike( $result->only_output, qr/ $more_free /, "Has ignored $more_free as not a problem");
136 like( $result->perf_output, qr/ $more_free=/, "But $more_free is still in perf data");
138 $result = NPTest->testCmd(
139         "./check_disk -w $avg_free% -c 0% -p $more_free"
140         );
141 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free");
143 $result = NPTest->testCmd(
144         "./check_disk -w $avg_free% -c $avg_free% -p $less_free"
145         );
146 cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free");
147 $result = NPTest->testCmd(
148         "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free"
149         );
150 cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical");
152 $result = NPTest->testCmd(
153         "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free"
154         );
155 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
159 # Basic inode checks for sizes
161 $result = NPTest->testCmd( "./check_disk --icritical 1% --iwarning 1% -p $more_inode_free" );
162 is( $result->return_code, 0, "At least 1% free on inodes for both mountpoints");
164 $result = NPTest->testCmd( "./check_disk -K 100% -W 100% -p $less_inode_free" );
165 is( $result->return_code, 2, "Critical requesting 100% free inodes for both mountpoints");
167 $result = NPTest->testCmd( "./check_disk --iwarning 1% --icritical 1% -p $more_inode_free -K 100% -W 100% -p $less_inode_free" );
168 is( $result->return_code, 2, "Get critical on less_inode_free mountpoint $less_inode_free");
170 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free" );
171 is( $result->return_code, 1, "Get warning on less_inode_free, when checking average");
173 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free ");
174 is( $result->return_code, 0, "Get ok on more_inode_free when checking average");
176 $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" );
177 is ($result->return_code, 1, "Combine above two tests, get warning");
178 $all_disks = $result->output;
180 $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" );
181 isnt( $result->output, $all_disks, "-e gives different output");
182 like( $result->output, qr/$less_inode_free/, "Found problem $less_inode_free");
183 unlike( $result->only_output, qr/$more_inode_free/, "Has ignored $more_inode_free as not a problem");
184 like( $result->perf_output, qr/$more_inode_free/, "But $more_inode_free is still in perf data");
186 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free" );
187 is( $result->return_code, 0, "Get ok on more_inode_free mountpoint, checking average");
189 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" );
190 is( $result->return_code, 2, "Get critical on less_inode_free, checking average");
192 $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" );
193 is( $result->return_code, 2, "Combining above two tests, get critical");
195 $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" );
196 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
203 TODO: {
204         local $TODO = "Invalid percent figures";
205         $result = NPTest->testCmd(
206                 "./check_disk -w 10% -c 15% -p $mountpoint_valid"
207                 );
208         cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
211 $result = NPTest->testCmd( 
212         "./check_disk -p $mountpoint_valid -w 10% -c 15%"
213         );
214 cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" );
216 $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} );      # 100% empty
217 cmp_ok( $result->return_code, "==", 2, "100% empty" );
218 like( $result->output, $failureOutput, "Right output" );
220 $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
221 cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
223 $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} );      # 100 GB empty
224 cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
227 # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
228 $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
229 cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
231 $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
232 cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
234 $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
235 cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
237 TODO: {
238         local $TODO = "Invalid values";
239         $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
240         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
242         $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
243         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
245         $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
246         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
249 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
250 cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
251 cmp_ok( $result->output, 'eq', 'DISK CRITICAL - /bob does not exist', 'Output OK');
253 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
254 my $root_output = $result->output;
256 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
257 cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
258 cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
260 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E" );
261 cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified");
263 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
264 cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
265 unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
267 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" );
268 unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice");