Code

Check_disk now calls stat() for all filesystems to check.
[nagiosplug.git] / plugins / t / check_disk.t
index 50d2e6d5016520da54619dd077a65ebbf5c2f735..4f5c4bc4a9ce2100ceded22555a7add9f982d304 100644 (file)
@@ -24,7 +24,7 @@ my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to anoth
 if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") {
        plan skip_all => "Need 2 mountpoints to test";
 } else {
-       plan tests => 57;
+       plan tests => 69;
 }
 
 $result = NPTest->testCmd( 
@@ -76,6 +76,24 @@ if ($free_inode_on_mp1 > $free_inode_on_mp2) {
        die "Two mountpoints with same inodes free - cannot do rest of test";
 }
 
+# Verify performance data
+# First check absolute thresholds...
+$result = NPTest->testCmd(
+        "./check_disk -w 20 -c 10 -p $mountpoint_valid"
+        );
+$_ = $result->perf_output;
+my ($warn_absth_data, $crit_absth_data, $total_absth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/);
+is ($warn_absth_data, $total_absth_data - 20, "Wrong warning in perf data using absolute thresholds");
+is ($crit_absth_data, $total_absth_data - 10, "Wrong critical in perf data using absolute thresholds");
+
+# Then check percent thresholds.
+$result = NPTest->testCmd(
+        "./check_disk -w 20% -c 10% -p $mountpoint_valid"
+        );
+$_ = $result->perf_output;
+my ($warn_percth_data, $crit_percth_data, $total_percth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/);
+is ($warn_percth_data, int((1-20/100)*$total_percth_data), "Wrong warning in perf data using percent thresholds");
+is ($crit_percth_data, int((1-10/100)*$total_percth_data), "Wrong critical in perf data using percent thresholds");
 
 
 # Check when order of mount points are reversed, that perf data remains same
@@ -93,6 +111,14 @@ like  ( $result->output, $successOutput, "OK output" );
 like  ( $result->only_output, qr/free space/, "Have free space text");
 like  ( $result->only_output, qr/$more_free/, "Have disk name in text");
 
+$result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free -p $less_free" );
+cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free and $less_free");
+$_ = $result->output;
+my ($free_mb_on_mp1, $free_mb_on_mp2) = (m/(\d+) MB .* (\d+) MB /g);
+my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2;
+
+
+
 $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" );
 is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs");
 
@@ -180,7 +206,7 @@ $all_disks = $result->output;
 $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" );
 isnt( $result->output, $all_disks, "-e gives different output");
 like( $result->output, qr/$less_inode_free/, "Found problem $less_inode_free");
-unlike( $result->only_output, qr/$more_inode_free/, "Has ignored $more_inode_free as not a problem");
+unlike( $result->only_output, qr/$more_inode_free\s/, "Has ignored $more_inode_free as not a problem");
 like( $result->perf_output, qr/$more_inode_free/, "But $more_inode_free is still in perf data");
 
 $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free" );
@@ -227,6 +253,7 @@ cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
 # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
 $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
 cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
+like ( $result->only_output, qr(^[^;]*;[^;]*$), "Select only one path with positional arguments");
 
 $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
 cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
@@ -248,7 +275,7 @@ TODO: {
 
 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
 cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
-cmp_ok( $result->output, 'eq', 'DISK CRITICAL - /bob does not exist', 'Output OK');
+like( $result->output, '/^DISK CRITICAL - /bob is not accessible:.*$/', 'Output OK');
 
 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
 my $root_output = $result->output;
@@ -266,3 +293,28 @@ unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
 
 $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" );
 unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice");
+
+# are partitions added if -C is given without path selection -p ?
+$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -C -w 0% -c 0% -p $mountpoint_valid" );
+like( $result->output, '/;.*;\|/', "-C selects partitions if -p is not given");
+
+# grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit
+$result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all + 1) ." -c ". ($free_mb_on_all + 1) ."-g group -p $mountpoint_valid -p $mountpoint2_valid" );
+cmp_ok( $result->return_code, '==', 2, "grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit");
+
+# grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c
+$result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all + 1) ." -c ". ($free_mb_on_all - 1) ." -g group -p $mountpoint_valid -p $mountpoint2_valid" );
+cmp_ok( $result->return_code, '==', 1, "grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c ");
+
+# grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit
+$result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all - 1) ." -c ". ($free_mb_on_all - 1) ." -g group -p $mountpoint_valid -p $mountpoint2_valid" );
+cmp_ok( $result->return_code, '==', 0, "grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit");
+
+# grouping: exit unknown if group name is given after -p 
+$result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all - 1) ." -c ". ($free_mb_on_all - 1) ." -p $mountpoint_valid -g group -p $mountpoint2_valid" );
+cmp_ok( $result->return_code, '==', 3, "Invalid options: -p must come after groupname");
+
+# regex: exit unknown if given regex is not compileable
+$result = NPTest->testCmd( "./check_disk -w 1 -c 1 -r '('" );
+cmp_ok( $result->return_code, '==', 3, "Exit UNKNOWN if regex is not compileable");
+