Code

Re-added perf data to check_disk
[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 => 42;
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)");
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 $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free" );
58 cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free");
59 like  ( $result->output, $successOutput, "OK output" );
60 like  ( $result->only_output, qr/free space/, "Have free space text");
61 like  ( $result->only_output, qr/$more_free/, "Have disk name in text");
63 $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" );
64 is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs");
66 $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
67 cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
69 $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
70 cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
72 $result = NPTest->testCmd( 
73         "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free" 
74         );
75 cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
76 like( $result->output, $failureOutput, "Right output" );
81 $result = NPTest->testCmd(
82         "./check_disk -w $avg_free% -c 0% -p $less_free"
83         );
84 cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
86 $result = NPTest->testCmd(
87         "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
88         );
89 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
91 $result = NPTest->testCmd( 
92         "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
93         );
94 cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
95 my $all_disks = $result->output;
97 $result = NPTest->testCmd(
98         "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
99         );
100 isnt( $result->output, $all_disks, "-e gives different output");
101 like( $result->output, qr/$less_free/, "Found problem $less_free");
102 unlike( $result->only_output, qr/$more_free/, "Has ignored $more_free as not a problem");
103 like( $result->perf_output, qr/$more_free/, "But $more_free is still in perf data");
108 $result = NPTest->testCmd(
109         "./check_disk -w $avg_free% -c 0% -p $more_free"
110         );
111 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free");
113 $result = NPTest->testCmd(
114         "./check_disk -w $avg_free% -c $avg_free% -p $less_free"
115         );
116 cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free");
117 $result = NPTest->testCmd(
118         "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free"
119         );
120 cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical");
122 $result = NPTest->testCmd(
123         "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free"
124         );
125 cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
129 TODO: {
130         local $TODO = "Invalid percent figures";
131         $result = NPTest->testCmd(
132                 "./check_disk -w 10% -c 15% -p $mountpoint_valid"
133                 );
134         cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
137 $result = NPTest->testCmd( 
138         "./check_disk -p $mountpoint_valid -w 10% -c 15%"
139         );
140 cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" );
142 $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} );      # 100% empty
143 cmp_ok( $result->return_code, "==", 2, "100% empty" );
144 like( $result->output, $failureOutput, "Right output" );
146 $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
147 cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
149 $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} );      # 100 GB empty
150 cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
153 # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
154 $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
155 cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
157 $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
158 cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
160 $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
161 cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
163 TODO: {
164         local $TODO = "Invalid values";
165         $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
166         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
168         $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
169         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
171         $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
172         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
175 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
176 cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
177 cmp_ok( $result->output, 'eq', 'DISK CRITICAL - /bob does not exist', 'Output OK');
179 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
180 my $root_output = $result->output;
182 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
183 cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
184 cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
186 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E" );
187 cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified");
189 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
190 cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
191 unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
193 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" );
194 unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice");