Code

Remove the "- free space" if status is OK
[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 use strict;
9 use Test::More;
10 use NPTest;
11 use POSIX qw(ceil floor);
13 my $successOutput = '/^DISK OK/';
14 my $failureOutput = '/^DISK CRITICAL/';
15 my $warningOutput = '/^DISK WARNING/';
17 my $result;
19 my $mountpoint_valid  = getTestParameter( "NP_MOUNTPOINT_VALID", "Path to valid mountpoint",  "/");
20 my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to another valid mountpoint. Must be different from 1st one", "/var");
22 if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") {
23         plan skip_all => "Need 2 mountpoints to test";
24 } else {
25         plan tests => 42;
26 }
28 $result = NPTest->testCmd( 
29         "./check_disk -w 1% -c 1% -p $mountpoint_valid -w 1% -c 1% -p $mountpoint2_valid" 
30         );
31 cmp_ok( $result->return_code, "==", 0, "Checking two mountpoints (must have at least 1% free)");
32 my $c = 0;
33 $_ = $result->output;
34 $c++ while /\(/g;       # counts number of "(" - should be two
35 cmp_ok( $c, '==', 2, "Got two mountpoints in output");
37 # Calculate avg_free free on mountpoint1 and mountpoint2
38 # because if you check in the middle, you should get different errors
39 $_ = $result->output;
40 my ($free_on_mp1, $free_on_mp2) = (m/\((\d+)%.*\((\d+)%/);
41 die "Cannot parse output: $_" unless ($free_on_mp1 && $free_on_mp2);
42 my $avg_free = ceil(($free_on_mp1+$free_on_mp2)/2);
43 my ($more_free, $less_free);
44 if ($free_on_mp1 > $free_on_mp2) {
45         $more_free = $mountpoint_valid;
46         $less_free = $mountpoint2_valid;
47 } elsif ($free_on_mp1 < $free_on_mp2) {
48         $more_free = $mountpoint2_valid;
49         $less_free = $mountpoint_valid;
50 } else {
51         die "Two mountpoints are the same - cannot do rest of test";
52 }
55 $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free" );
56 cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free");
57 like  ( $result->output, $successOutput, "OK output" );
58 like  ( $result->only_output, qr/free space/, "Have free space text");
59 like  ( $result->only_output, qr/$more_free/, "Have disk name in text");
61 $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" );
62 is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs");
64 $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
65 cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
67 $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
68 cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
70 $result = NPTest->testCmd( 
71         "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free" 
72         );
73 cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
74 like( $result->output, $failureOutput, "Right output" );
79 $result = NPTest->testCmd(
80         "./check_disk -w $avg_free% -c 0% -p $less_free"
81         );
82 cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
84 $result = NPTest->testCmd(
85         "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
86         );
87 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
89 $result = NPTest->testCmd( 
90         "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
91         );
92 cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
93 my $all_disks = $result->output;
95 $result = NPTest->testCmd(
96         "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
97         );
98 isnt( $result->output, $all_disks, "-e gives different output");
99 like( $result->output, qr/$less_free/, "Found problem $less_free");
100 unlike( $result->only_output, qr/$more_free/, "Has ignored $more_free as not a problem");
101 like( $result->perf_output, qr/$more_free/, "But $more_free is still in perf data");
106 $result = NPTest->testCmd(
107         "./check_disk -w $avg_free% -c 0% -p $more_free"
108         );
109 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free");
111 $result = NPTest->testCmd(
112         "./check_disk -w $avg_free% -c $avg_free% -p $less_free"
113         );
114 cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free");
115 $result = NPTest->testCmd(
116         "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free"
117         );
118 cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical");
120 $result = NPTest->testCmd(
121         "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free"
122         );
123 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
127 TODO: {
128         local $TODO = "Invalid percent figures";
129         $result = NPTest->testCmd(
130                 "./check_disk -w 10% -c 15% -p $mountpoint_valid"
131                 );
132         cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
135 $result = NPTest->testCmd( 
136         "./check_disk -p $mountpoint_valid -w 10% -c 15%"
137         );
138 cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" );
140 $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} );      # 100% empty
141 cmp_ok( $result->return_code, "==", 2, "100% empty" );
142 like( $result->output, $failureOutput, "Right output" );
144 $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
145 cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
147 $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} );      # 100 GB empty
148 cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
151 # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
152 $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
153 cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
155 $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
156 cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
158 $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
159 cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
161 TODO: {
162         local $TODO = "Invalid values";
163         $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
164         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
166         $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
167         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
169         $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
170         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
173 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
174 cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
175 cmp_ok( $result->output, 'eq', 'DISK CRITICAL - /bob does not exist', 'Output OK');
177 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
178 my $root_output = $result->output;
180 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
181 cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
182 cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
184 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E" );
185 cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified");
187 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
188 cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
189 unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
191 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" );
192 unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice");