check_disk.t 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #! /usr/bin/perl -w -I ..
  2. #
  3. # Disk Space Tests via check_disk
  4. #
  5. #
  6. # TODO: Add in tests for perf data. Need to beef up Nagios::Plugin::Performance to cater for max, min, etc
  7. use strict;
  8. use Test::More;
  9. use NPTest;
  10. use POSIX qw(ceil floor);
  11. my $successOutput = '/^DISK OK/';
  12. my $failureOutput = '/^DISK CRITICAL/';
  13. my $warningOutput = '/^DISK WARNING/';
  14. my $result;
  15. my $mountpoint_valid = getTestParameter( "NP_MOUNTPOINT_VALID", "Path to valid mountpoint", "/");
  16. my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to another valid mountpoint. Must be different from 1st one", "/var");
  17. if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") {
  18. plan skip_all => "Need 2 mountpoints to test";
  19. } else {
  20. plan tests => 78;
  21. }
  22. $result = NPTest->testCmd(
  23. "./check_disk -w 1% -c 1% -p $mountpoint_valid -w 1% -c 1% -p $mountpoint2_valid"
  24. );
  25. cmp_ok( $result->return_code, "==", 0, "Checking two mountpoints (must have at least 1% free in space and inodes)");
  26. my $c = 0;
  27. $_ = $result->output;
  28. $c++ while /\(/g; # counts number of "(" - should be two
  29. cmp_ok( $c, '==', 2, "Got two mountpoints in output");
  30. # Get perf data
  31. # Should use Nagios::Plugin
  32. my @perf_data = sort(split(/ /, $result->perf_output));
  33. # Calculate avg_free free on mountpoint1 and mountpoint2
  34. # because if you check in the middle, you should get different errors
  35. $_ = $result->output;
  36. my ($free_on_mp1, $free_on_mp2) = (m/\((\d+)%.*\((\d+)%/);
  37. die "Cannot parse output: $_" unless ($free_on_mp1 && $free_on_mp2);
  38. my $avg_free = ceil(($free_on_mp1+$free_on_mp2)/2);
  39. my ($more_free, $less_free);
  40. if ($free_on_mp1 > $free_on_mp2) {
  41. $more_free = $mountpoint_valid;
  42. $less_free = $mountpoint2_valid;
  43. } elsif ($free_on_mp1 < $free_on_mp2) {
  44. $more_free = $mountpoint2_valid;
  45. $less_free = $mountpoint_valid;
  46. } else {
  47. die "Two mountpoints are the same - cannot do rest of test";
  48. }
  49. # Do same for inodes
  50. $_ = $result->output;
  51. my ($free_inode_on_mp1, $free_inode_on_mp2) = (m/inode=(\d+)%.*inode=(\d+)%/);
  52. die "Cannot parse free inodes: $_" unless ($free_inode_on_mp1 && $free_inode_on_mp2);
  53. my $avg_inode_free = ceil(($free_inode_on_mp1 + $free_inode_on_mp2)/2);
  54. my ($more_inode_free, $less_inode_free);
  55. if ($free_inode_on_mp1 > $free_inode_on_mp2) {
  56. $more_inode_free = $mountpoint_valid;
  57. $less_inode_free = $mountpoint2_valid;
  58. } elsif ($free_inode_on_mp1 < $free_inode_on_mp2) {
  59. $more_inode_free = $mountpoint2_valid;
  60. $less_inode_free = $mountpoint_valid;
  61. } else {
  62. die "Two mountpoints with same inodes free - cannot do rest of test";
  63. }
  64. # Verify performance data
  65. # First check absolute thresholds...
  66. $result = NPTest->testCmd(
  67. "./check_disk -w 20 -c 10 -p $mountpoint_valid"
  68. );
  69. $_ = $result->perf_output;
  70. my ($warn_absth_data, $crit_absth_data, $total_absth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/);
  71. is ($warn_absth_data, $total_absth_data - 20, "Wrong warning in perf data using absolute thresholds");
  72. is ($crit_absth_data, $total_absth_data - 10, "Wrong critical in perf data using absolute thresholds");
  73. # Then check percent thresholds.
  74. $result = NPTest->testCmd(
  75. "./check_disk -w 20% -c 10% -p $mountpoint_valid"
  76. );
  77. $_ = $result->perf_output;
  78. my ($warn_percth_data, $crit_percth_data, $total_percth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/);
  79. is ($warn_percth_data, int((1-20/100)*$total_percth_data), "Wrong warning in perf data using percent thresholds");
  80. is ($crit_percth_data, int((1-10/100)*$total_percth_data), "Wrong critical in perf data using percent thresholds");
  81. # Check when order of mount points are reversed, that perf data remains same
  82. $result = NPTest->testCmd(
  83. "./check_disk -w 1% -c 1% -p $mountpoint2_valid -w 1% -c 1% -p $mountpoint_valid"
  84. );
  85. @_ = sort(split(/ /, $result->perf_output));
  86. is_deeply( \@perf_data, \@_, "perf data for both filesystems same when reversed");
  87. # Basic filesystem checks for sizes
  88. $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free" );
  89. cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free");
  90. like ( $result->output, $successOutput, "OK output" );
  91. like ( $result->only_output, qr/free space/, "Have free space text");
  92. like ( $result->only_output, qr/$more_free/, "Have disk name in text");
  93. $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free -p $less_free" );
  94. cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free and $less_free");
  95. $_ = $result->output;
  96. my ($free_mb_on_mp1, $free_mb_on_mp2) = (m/(\d+) MB .* (\d+) MB /g);
  97. my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2;
  98. $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" );
  99. is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs");
  100. $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
  101. cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
  102. $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
  103. cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
  104. $result = NPTest->testCmd(
  105. "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free"
  106. );
  107. cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
  108. like( $result->output, $failureOutput, "Right output" );
  109. $result = NPTest->testCmd(
  110. "./check_disk -w $avg_free% -c 0% -p $less_free"
  111. );
  112. cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
  113. $result = NPTest->testCmd(
  114. "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
  115. );
  116. cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
  117. $result = NPTest->testCmd(
  118. "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free"
  119. );
  120. cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
  121. my $all_disks = $result->output;
  122. $result = NPTest->testCmd(
  123. "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free"
  124. );
  125. isnt( $result->output, $all_disks, "-e gives different output");
  126. # Need spaces around filesystem name in case less_free and more_free are nested
  127. like( $result->output, qr/ $less_free /, "Found problem $less_free");
  128. unlike( $result->only_output, qr/ $more_free /, "Has ignored $more_free as not a problem");
  129. like( $result->perf_output, qr/ $more_free=/, "But $more_free is still in perf data");
  130. $result = NPTest->testCmd(
  131. "./check_disk -w $avg_free% -c 0% -p $more_free"
  132. );
  133. cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free");
  134. $result = NPTest->testCmd(
  135. "./check_disk -w $avg_free% -c $avg_free% -p $less_free"
  136. );
  137. cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free");
  138. $result = NPTest->testCmd(
  139. "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free"
  140. );
  141. cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical");
  142. $result = NPTest->testCmd(
  143. "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free"
  144. );
  145. cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
  146. # Basic inode checks for sizes
  147. $result = NPTest->testCmd( "./check_disk --icritical 1% --iwarning 1% -p $more_inode_free" );
  148. is( $result->return_code, 0, "At least 1% free on inodes for both mountpoints");
  149. $result = NPTest->testCmd( "./check_disk -K 100% -W 100% -p $less_inode_free" );
  150. is( $result->return_code, 2, "Critical requesting 100% free inodes for both mountpoints");
  151. $result = NPTest->testCmd( "./check_disk --iwarning 1% --icritical 1% -p $more_inode_free -K 100% -W 100% -p $less_inode_free" );
  152. is( $result->return_code, 2, "Get critical on less_inode_free mountpoint $less_inode_free");
  153. $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free" );
  154. is( $result->return_code, 1, "Get warning on less_inode_free, when checking average");
  155. $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free ");
  156. is( $result->return_code, 0, "Get ok on more_inode_free when checking average");
  157. $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" );
  158. is ($result->return_code, 1, "Combine above two tests, get warning");
  159. $all_disks = $result->output;
  160. $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" );
  161. isnt( $result->output, $all_disks, "-e gives different output");
  162. like( $result->output, qr/$less_inode_free/, "Found problem $less_inode_free");
  163. unlike( $result->only_output, qr/$more_inode_free\s/, "Has ignored $more_inode_free as not a problem");
  164. like( $result->perf_output, qr/$more_inode_free/, "But $more_inode_free is still in perf data");
  165. $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free" );
  166. is( $result->return_code, 0, "Get ok on more_inode_free mountpoint, checking average");
  167. $result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" );
  168. is( $result->return_code, 2, "Get critical on less_inode_free, checking average");
  169. $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" );
  170. is( $result->return_code, 2, "Combining above two tests, get critical");
  171. $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" );
  172. cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
  173. TODO: {
  174. local $TODO = "Invalid percent figures";
  175. $result = NPTest->testCmd(
  176. "./check_disk -w 10% -c 15% -p $mountpoint_valid"
  177. );
  178. cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
  179. }
  180. $result = NPTest->testCmd(
  181. "./check_disk -p $mountpoint_valid -w 10% -c 15%"
  182. );
  183. cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" );
  184. $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} ); # 100% empty
  185. cmp_ok( $result->return_code, "==", 2, "100% empty" );
  186. like( $result->output, $failureOutput, "Right output" );
  187. $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
  188. cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
  189. $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} ); # 100 GB empty
  190. cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
  191. # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
  192. $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
  193. cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
  194. like ( $result->only_output, qr(^[^;]*;[^;]*$), "Select only one path with positional arguments");
  195. $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
  196. cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
  197. $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
  198. cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
  199. TODO: {
  200. local $TODO = "Invalid values";
  201. $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
  202. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  203. $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
  204. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  205. $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
  206. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  207. }
  208. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
  209. cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
  210. like( $result->output, '/^DISK CRITICAL - /bob is not accessible:.*$/', 'Output OK');
  211. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
  212. my $root_output = $result->output;
  213. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
  214. cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
  215. cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
  216. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -E -p /etc " );
  217. cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified");
  218. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E " );
  219. cmp_ok( $result->return_code, '==', 3, "-E/--exact-match must be specified before -p");
  220. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -r /etc -E " );
  221. cmp_ok( $result->return_code, '==', 3, "-E/--exact-match must be specified before -r");
  222. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
  223. cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
  224. unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
  225. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" );
  226. unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice");
  227. # are partitions added if -C is given without path selection -p ?
  228. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -C -w 0% -c 0% -p $mountpoint_valid" );
  229. like( $result->output, '/;.*;\|/', "-C selects partitions if -p is not given");
  230. # grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit
  231. $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" );
  232. cmp_ok( $result->return_code, '==', 2, "grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit");
  233. # grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c
  234. $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" );
  235. cmp_ok( $result->return_code, '==', 1, "grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c ");
  236. # grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit
  237. $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" );
  238. cmp_ok( $result->return_code, '==', 0, "grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit");
  239. # grouping: exit unknown if group name is given after -p
  240. $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" );
  241. cmp_ok( $result->return_code, '==', 3, "Invalid options: -p must come after groupname");
  242. # regex: exit unknown if given regex is not compileable
  243. $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -r '('" );
  244. cmp_ok( $result->return_code, '==', 3, "Exit UNKNOWN if regex is not compileable");
  245. # ignore: exit unknown, if all pathes are deselected using -i
  246. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '$mountpoint_valid' -i '$mountpoint2_valid'" );
  247. cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored (case sensitive)");
  248. # ignore: exit unknown, if all pathes are deselected using -I
  249. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -I '".uc($mountpoint_valid)."' -I '".uc($mountpoint2_valid)."'" );
  250. cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored (case insensitive)");
  251. # ignore: exit unknown, if all pathes are deselected using -i
  252. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '.*'" );
  253. cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored using -i '.*'");
  254. # ignore: test if ignored path is actually ignored
  255. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^$mountpoint2_valid\$'");
  256. like( $result->output, qr/$mountpoint_valid/, "output data does have $mountpoint_valid in it");
  257. unlike( $result->output, qr/$mountpoint2_valid/, "output data does not have $mountpoint2_valid in it");
  258. # ignore: test if all pathes are listed when ignore regex doesn't match
  259. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^barbazJodsf\$'");
  260. like( $result->output, qr/$mountpoint_valid/, "ignore: output data does have $mountpoint_valid when regex doesn't match");
  261. like( $result->output, qr/$mountpoint2_valid/,"ignore: output data does have $mountpoint2_valid when regex doesn't match");