4
0

check_disk.t 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #! /usr/bin/perl -w -I ..
  2. #
  3. # Disk Space Tests via check_disk
  4. #
  5. # $Id$
  6. #
  7. # TODO: Add in tests for perf data. Need to beef up Nagios::Plugin::Performance to cater for max, min, etc
  8. use strict;
  9. use Test::More;
  10. use NPTest;
  11. use POSIX qw(ceil floor);
  12. my $successOutput = '/^DISK OK/';
  13. my $failureOutput = '/^DISK CRITICAL/';
  14. my $warningOutput = '/^DISK WARNING/';
  15. my $result;
  16. my $mountpoint_valid = getTestParameter( "NP_MOUNTPOINT_VALID", "Path to valid mountpoint", "/");
  17. my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to another valid mountpoint. Must be different from 1st one", "/var");
  18. if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") {
  19. plan skip_all => "Need 2 mountpoints to test";
  20. } else {
  21. plan tests => 57;
  22. }
  23. $result = NPTest->testCmd(
  24. "./check_disk -w 1% -c 1% -p $mountpoint_valid -w 1% -c 1% -p $mountpoint2_valid"
  25. );
  26. cmp_ok( $result->return_code, "==", 0, "Checking two mountpoints (must have at least 1% free in space and inodes)");
  27. my $c = 0;
  28. $_ = $result->output;
  29. $c++ while /\(/g; # counts number of "(" - should be two
  30. cmp_ok( $c, '==', 2, "Got two mountpoints in output");
  31. # Get perf data
  32. # Should use Nagios::Plugin
  33. my @perf_data = sort(split(/ /, $result->perf_output));
  34. # Calculate avg_free free on mountpoint1 and mountpoint2
  35. # because if you check in the middle, you should get different errors
  36. $_ = $result->output;
  37. my ($free_on_mp1, $free_on_mp2) = (m/\((\d+)%.*\((\d+)%/);
  38. die "Cannot parse output: $_" unless ($free_on_mp1 && $free_on_mp2);
  39. my $avg_free = ceil(($free_on_mp1+$free_on_mp2)/2);
  40. my ($more_free, $less_free);
  41. if ($free_on_mp1 > $free_on_mp2) {
  42. $more_free = $mountpoint_valid;
  43. $less_free = $mountpoint2_valid;
  44. } elsif ($free_on_mp1 < $free_on_mp2) {
  45. $more_free = $mountpoint2_valid;
  46. $less_free = $mountpoint_valid;
  47. } else {
  48. die "Two mountpoints are the same - cannot do rest of test";
  49. }
  50. # Do same for inodes
  51. $_ = $result->output;
  52. my ($free_inode_on_mp1, $free_inode_on_mp2) = (m/inode=(\d+)%.*inode=(\d+)%/);
  53. die "Cannot parse free inodes: $_" unless ($free_inode_on_mp1 && $free_inode_on_mp2);
  54. my $avg_inode_free = ceil(($free_inode_on_mp1 + $free_inode_on_mp2)/2);
  55. my ($more_inode_free, $less_inode_free);
  56. if ($free_inode_on_mp1 > $free_inode_on_mp2) {
  57. $more_inode_free = $mountpoint_valid;
  58. $less_inode_free = $mountpoint2_valid;
  59. } elsif ($free_inode_on_mp1 < $free_inode_on_mp2) {
  60. $more_inode_free = $mountpoint2_valid;
  61. $less_inode_free = $mountpoint_valid;
  62. } else {
  63. die "Two mountpoints with same inodes free - cannot do rest of test";
  64. }
  65. # Check when order of mount points are reversed, that perf data remains same
  66. $result = NPTest->testCmd(
  67. "./check_disk -w 1% -c 1% -p $mountpoint2_valid -w 1% -c 1% -p $mountpoint_valid"
  68. );
  69. @_ = sort(split(/ /, $result->perf_output));
  70. is_deeply( \@perf_data, \@_, "perf data for both filesystems same when reversed");
  71. # Basic filesystem checks for sizes
  72. $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free" );
  73. cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free");
  74. like ( $result->output, $successOutput, "OK output" );
  75. like ( $result->only_output, qr/free space/, "Have free space text");
  76. like ( $result->only_output, qr/$more_free/, "Have disk name in text");
  77. $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" );
  78. is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs");
  79. $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
  80. cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
  81. $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
  82. cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
  83. $result = NPTest->testCmd(
  84. "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free"
  85. );
  86. cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
  87. like( $result->output, $failureOutput, "Right output" );
  88. $result = NPTest->testCmd(
  89. "./check_disk -w $avg_free% -c 0% -p $less_free"
  90. );
  91. cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
  92. $result = NPTest->testCmd(
  93. "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
  94. );
  95. cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
  96. $result = NPTest->testCmd(
  97. "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free"
  98. );
  99. cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
  100. my $all_disks = $result->output;
  101. $result = NPTest->testCmd(
  102. "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free"
  103. );
  104. isnt( $result->output, $all_disks, "-e gives different output");
  105. # Need spaces around filesystem name in case less_free and more_free are nested
  106. like( $result->output, qr/ $less_free /, "Found problem $less_free");
  107. unlike( $result->only_output, qr/ $more_free /, "Has ignored $more_free as not a problem");
  108. like( $result->perf_output, qr/ $more_free=/, "But $more_free is still in perf data");
  109. $result = NPTest->testCmd(
  110. "./check_disk -w $avg_free% -c 0% -p $more_free"
  111. );
  112. 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");
  121. $result = NPTest->testCmd(
  122. "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free"
  123. );
  124. cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
  125. # Basic inode checks for sizes
  126. $result = NPTest->testCmd( "./check_disk --icritical 1% --iwarning 1% -p $more_inode_free" );
  127. is( $result->return_code, 0, "At least 1% free on inodes for both mountpoints");
  128. $result = NPTest->testCmd( "./check_disk -K 100% -W 100% -p $less_inode_free" );
  129. is( $result->return_code, 2, "Critical requesting 100% free inodes for both mountpoints");
  130. $result = NPTest->testCmd( "./check_disk --iwarning 1% --icritical 1% -p $more_inode_free -K 100% -W 100% -p $less_inode_free" );
  131. is( $result->return_code, 2, "Get critical on less_inode_free mountpoint $less_inode_free");
  132. $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free" );
  133. is( $result->return_code, 1, "Get warning on less_inode_free, when checking average");
  134. $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free ");
  135. is( $result->return_code, 0, "Get ok on more_inode_free when checking average");
  136. $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" );
  137. is ($result->return_code, 1, "Combine above two tests, get warning");
  138. $all_disks = $result->output;
  139. $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" );
  140. isnt( $result->output, $all_disks, "-e gives different output");
  141. like( $result->output, qr/$less_inode_free/, "Found problem $less_inode_free");
  142. unlike( $result->only_output, qr/$more_inode_free/, "Has ignored $more_inode_free as not a problem");
  143. like( $result->perf_output, qr/$more_inode_free/, "But $more_inode_free is still in perf data");
  144. $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free" );
  145. is( $result->return_code, 0, "Get ok on more_inode_free mountpoint, checking average");
  146. $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" );
  147. is( $result->return_code, 2, "Get critical on less_inode_free, checking average");
  148. $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" );
  149. is( $result->return_code, 2, "Combining above two tests, get critical");
  150. $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" );
  151. cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
  152. TODO: {
  153. local $TODO = "Invalid percent figures";
  154. $result = NPTest->testCmd(
  155. "./check_disk -w 10% -c 15% -p $mountpoint_valid"
  156. );
  157. cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
  158. }
  159. $result = NPTest->testCmd(
  160. "./check_disk -p $mountpoint_valid -w 10% -c 15%"
  161. );
  162. cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" );
  163. $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} ); # 100% empty
  164. cmp_ok( $result->return_code, "==", 2, "100% empty" );
  165. like( $result->output, $failureOutput, "Right output" );
  166. $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
  167. cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
  168. $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} ); # 100 GB empty
  169. cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
  170. # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
  171. $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
  172. cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
  173. $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
  174. cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
  175. $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
  176. cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
  177. TODO: {
  178. local $TODO = "Invalid values";
  179. $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
  180. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  181. $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
  182. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  183. $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
  184. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  185. }
  186. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
  187. cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
  188. cmp_ok( $result->output, 'eq', 'DISK CRITICAL - /bob does not exist', 'Output OK');
  189. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
  190. my $root_output = $result->output;
  191. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
  192. cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
  193. cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
  194. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E" );
  195. cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified");
  196. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
  197. cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
  198. unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
  199. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" );
  200. unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice");