4
0

check_disk.t 17 KB

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