Code

check_disk now errors if a specified directory does not exist (cf df /foo)
[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 => 32;
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 100 -c 100 -p $more_free" );
56 cmp_ok( $result->return_code, '==', 0, "At least 100 bytes available on $more_free");
57 like  ( $result->output, $successOutput, "OK output" );
59 $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
60 cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
62 $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
63 cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
65 $result = NPTest->testCmd( 
66         "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free" 
67         );
68 cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
69 like( $result->output, $failureOutput, "Right output" );
74 $result = NPTest->testCmd(
75         "./check_disk -w $avg_free% -c 0% -p $less_free"
76         );
77 cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
79 $result = NPTest->testCmd(
80         "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
81         );
82 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
84 $result = NPTest->testCmd( 
85         "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" 
86         );
87 cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
91 $result = NPTest->testCmd(
92         "./check_disk -w $avg_free% -c 0% -p $more_free"
93         );
94 cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free");
96 $result = NPTest->testCmd(
97         "./check_disk -w $avg_free% -c $avg_free% -p $less_free"
98         );
99 cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free");
101 $result = NPTest->testCmd(
102         "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free"
103         );
104 cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical");
108 $result = NPTest->testCmd(
109         "./check_disk -w 10% -c 15% -p $mountpoint_valid"
110         );
111 cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
113 TODO: {
114     local $TODO = "-p must come after -w and -c";
115     $result = NPTest->testCmd( 
116         "./check_disk -p $mountpoint_valid -w 10% -c 15%"
117         );
118     cmp_ok( $result->return_code, "==", 3, "Invalid options - order unimportant" );
121 $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} );      # 100% empty
122 cmp_ok( $result->return_code, "==", 2, "100% empty" );
123 like( $result->output, $failureOutput, "Right output" );
125 TODO: {
126         local $TODO = "Requesting 100GB free is should be critical";
127         $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
128         cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
131 TODO: {
132     local $TODO = "-u GB does not work";
133     $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} );      # 100 GB empty
134     cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
138 # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
139 $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
140 cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
142 $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
143 cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
145 $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
146 cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
148 $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
149 cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
151 TODO: {
152         local $TODO = "Need to properly check input";
153         $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
154         cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
157 $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
158 cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
160 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
161 cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
162 cmp_ok( $result->output, 'eq', 'DISK CRITICAL - /bob does not exist', 'Output OK');
164 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
165 my $root_output = $result->output;
167 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
168 cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
169 cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
171 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
172 cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
173 unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");