check_disk.t 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 => 78;
  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. # Verify performance data
  66. # First check absolute thresholds...
  67. $result = NPTest->testCmd(
  68. "./check_disk -w 20 -c 10 -p $mountpoint_valid"
  69. );
  70. $_ = $result->perf_output;
  71. my ($warn_absth_data, $crit_absth_data, $total_absth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/);
  72. is ($warn_absth_data, $total_absth_data - 20, "Wrong warning in perf data using absolute thresholds");
  73. is ($crit_absth_data, $total_absth_data - 10, "Wrong critical in perf data using absolute thresholds");
  74. # Then check percent thresholds.
  75. $result = NPTest->testCmd(
  76. "./check_disk -w 20% -c 10% -p $mountpoint_valid"
  77. );
  78. $_ = $result->perf_output;
  79. my ($warn_percth_data, $crit_percth_data, $total_percth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/);
  80. is ($warn_percth_data, int((1-20/100)*$total_percth_data), "Wrong warning in perf data using percent thresholds");
  81. is ($crit_percth_data, int((1-10/100)*$total_percth_data), "Wrong critical in perf data using percent thresholds");
  82. # Check when order of mount points are reversed, that perf data remains same
  83. $result = NPTest->testCmd(
  84. "./check_disk -w 1% -c 1% -p $mountpoint2_valid -w 1% -c 1% -p $mountpoint_valid"
  85. );
  86. @_ = sort(split(/ /, $result->perf_output));
  87. is_deeply( \@perf_data, \@_, "perf data for both filesystems same when reversed");
  88. # Basic filesystem checks for sizes
  89. $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free" );
  90. cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free");
  91. like ( $result->output, $successOutput, "OK output" );
  92. like ( $result->only_output, qr/free space/, "Have free space text");
  93. like ( $result->only_output, qr/$more_free/, "Have disk name in text");
  94. $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free -p $less_free" );
  95. cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free and $less_free");
  96. $_ = $result->output;
  97. my ($free_mb_on_mp1, $free_mb_on_mp2) = (m/(\d+) MB .* (\d+) MB /g);
  98. my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2;
  99. $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" );
  100. is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs");
  101. $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
  102. cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
  103. $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
  104. cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
  105. $result = NPTest->testCmd(
  106. "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free"
  107. );
  108. cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
  109. like( $result->output, $failureOutput, "Right output" );
  110. $result = NPTest->testCmd(
  111. "./check_disk -w $avg_free% -c 0% -p $less_free"
  112. );
  113. cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
  114. $result = NPTest->testCmd(
  115. "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
  116. );
  117. cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
  118. $result = NPTest->testCmd(
  119. "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free"
  120. );
  121. cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
  122. my $all_disks = $result->output;
  123. $result = NPTest->testCmd(
  124. "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free"
  125. );
  126. isnt( $result->output, $all_disks, "-e gives different output");
  127. # Need spaces around filesystem name in case less_free and more_free are nested
  128. like( $result->output, qr/ $less_free /, "Found problem $less_free");
  129. unlike( $result->only_output, qr/ $more_free /, "Has ignored $more_free as not a problem");
  130. like( $result->perf_output, qr/ $more_free=/, "But $more_free is still in perf data");
  131. $result = NPTest->testCmd(
  132. "./check_disk -w $avg_free% -c 0% -p $more_free"
  133. );
  134. cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free");
  135. $result = NPTest->testCmd(
  136. "./check_disk -w $avg_free% -c $avg_free% -p $less_free"
  137. );
  138. cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free");
  139. $result = NPTest->testCmd(
  140. "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free"
  141. );
  142. cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical");
  143. $result = NPTest->testCmd(
  144. "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free"
  145. );
  146. cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
  147. # Basic inode checks for sizes
  148. $result = NPTest->testCmd( "./check_disk --icritical 1% --iwarning 1% -p $more_inode_free" );
  149. is( $result->return_code, 0, "At least 1% free on inodes for both mountpoints");
  150. $result = NPTest->testCmd( "./check_disk -K 100% -W 100% -p $less_inode_free" );
  151. is( $result->return_code, 2, "Critical requesting 100% free inodes for both mountpoints");
  152. $result = NPTest->testCmd( "./check_disk --iwarning 1% --icritical 1% -p $more_inode_free -K 100% -W 100% -p $less_inode_free" );
  153. is( $result->return_code, 2, "Get critical on less_inode_free mountpoint $less_inode_free");
  154. $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free" );
  155. is( $result->return_code, 1, "Get warning on less_inode_free, when checking average");
  156. $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free ");
  157. is( $result->return_code, 0, "Get ok on more_inode_free when checking average");
  158. $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" );
  159. is ($result->return_code, 1, "Combine above two tests, get warning");
  160. $all_disks = $result->output;
  161. $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" );
  162. isnt( $result->output, $all_disks, "-e gives different output");
  163. like( $result->output, qr/$less_inode_free/, "Found problem $less_inode_free");
  164. unlike( $result->only_output, qr/$more_inode_free\s/, "Has ignored $more_inode_free as not a problem");
  165. like( $result->perf_output, qr/$more_inode_free/, "But $more_inode_free is still in perf data");
  166. $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free" );
  167. is( $result->return_code, 0, "Get ok on more_inode_free mountpoint, checking average");
  168. $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" );
  169. is( $result->return_code, 2, "Get critical on less_inode_free, checking average");
  170. $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" );
  171. is( $result->return_code, 2, "Combining above two tests, get critical");
  172. $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" );
  173. cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
  174. TODO: {
  175. local $TODO = "Invalid percent figures";
  176. $result = NPTest->testCmd(
  177. "./check_disk -w 10% -c 15% -p $mountpoint_valid"
  178. );
  179. cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
  180. }
  181. $result = NPTest->testCmd(
  182. "./check_disk -p $mountpoint_valid -w 10% -c 15%"
  183. );
  184. cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" );
  185. $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} ); # 100% empty
  186. cmp_ok( $result->return_code, "==", 2, "100% empty" );
  187. like( $result->output, $failureOutput, "Right output" );
  188. $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
  189. cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
  190. $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} ); # 100 GB empty
  191. cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
  192. # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
  193. $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
  194. cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
  195. like ( $result->only_output, qr(^[^;]*;[^;]*$), "Select only one path with positional arguments");
  196. $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
  197. cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
  198. $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
  199. cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
  200. TODO: {
  201. local $TODO = "Invalid values";
  202. $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
  203. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  204. $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
  205. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  206. $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
  207. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  208. }
  209. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
  210. cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
  211. like( $result->output, '/^DISK CRITICAL - /bob is not accessible:.*$/', 'Output OK');
  212. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
  213. my $root_output = $result->output;
  214. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
  215. cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
  216. cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
  217. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -E -p /etc " );
  218. cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified");
  219. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E " );
  220. cmp_ok( $result->return_code, '==', 3, "-E/--exact-match must be specified before -p");
  221. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -r /etc -E " );
  222. cmp_ok( $result->return_code, '==', 3, "-E/--exact-match must be specified before -r");
  223. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
  224. cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
  225. unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
  226. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" );
  227. unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice");
  228. # are partitions added if -C is given without path selection -p ?
  229. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -C -w 0% -c 0% -p $mountpoint_valid" );
  230. like( $result->output, '/;.*;\|/', "-C selects partitions if -p is not given");
  231. # grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit
  232. $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" );
  233. cmp_ok( $result->return_code, '==', 2, "grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit");
  234. # grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c
  235. $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" );
  236. cmp_ok( $result->return_code, '==', 1, "grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c ");
  237. # grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit
  238. $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" );
  239. cmp_ok( $result->return_code, '==', 0, "grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit");
  240. # grouping: exit unknown if group name is given after -p
  241. $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" );
  242. cmp_ok( $result->return_code, '==', 3, "Invalid options: -p must come after groupname");
  243. # regex: exit unknown if given regex is not compileable
  244. $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -r '('" );
  245. cmp_ok( $result->return_code, '==', 3, "Exit UNKNOWN if regex is not compileable");
  246. # ignore: exit unknown, if all pathes are deselected using -i
  247. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '$mountpoint_valid' -i '$mountpoint2_valid'" );
  248. cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored (case sensitive)");
  249. # ignore: exit unknown, if all pathes are deselected using -I
  250. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -I '".uc($mountpoint_valid)."' -I '".uc($mountpoint2_valid)."'" );
  251. cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored (case insensitive)");
  252. # ignore: exit unknown, if all pathes are deselected using -i
  253. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '.*'" );
  254. cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored using -i '.*'");
  255. # ignore: test if ignored path is actually ignored
  256. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^$mountpoint2_valid\$'");
  257. like( $result->output, qr/$mountpoint_valid/, "output data does have $mountpoint_valid in it");
  258. unlike( $result->output, qr/$mountpoint2_valid/, "output data does not have $mountpoint2_valid in it");
  259. # ignore: test if all pathes are listed when ignore regex doesn't match
  260. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^barbazJodsf\$'");
  261. like( $result->output, qr/$mountpoint_valid/, "ignore: output data does have $mountpoint_valid when regex doesn't match");
  262. like( $result->output, qr/$mountpoint2_valid/,"ignore: output data does have $mountpoint2_valid when regex doesn't match");