check_disk.t 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 => 42;
  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)");
  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. # Calculate avg_free free on mountpoint1 and mountpoint2
  32. # because if you check in the middle, you should get different errors
  33. $_ = $result->output;
  34. my ($free_on_mp1, $free_on_mp2) = (m/\((\d+)%.*\((\d+)%/);
  35. die "Cannot parse output: $_" unless ($free_on_mp1 && $free_on_mp2);
  36. my $avg_free = ceil(($free_on_mp1+$free_on_mp2)/2);
  37. my ($more_free, $less_free);
  38. if ($free_on_mp1 > $free_on_mp2) {
  39. $more_free = $mountpoint_valid;
  40. $less_free = $mountpoint2_valid;
  41. } elsif ($free_on_mp1 < $free_on_mp2) {
  42. $more_free = $mountpoint2_valid;
  43. $less_free = $mountpoint_valid;
  44. } else {
  45. die "Two mountpoints are the same - cannot do rest of test";
  46. }
  47. $result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free" );
  48. cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free");
  49. like ( $result->output, $successOutput, "OK output" );
  50. like ( $result->only_output, qr/free space/, "Have free space text");
  51. like ( $result->only_output, qr/$more_free/, "Have disk name in text");
  52. $result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" );
  53. is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs");
  54. $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
  55. cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
  56. $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
  57. cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
  58. $result = NPTest->testCmd(
  59. "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free"
  60. );
  61. cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
  62. like( $result->output, $failureOutput, "Right output" );
  63. $result = NPTest->testCmd(
  64. "./check_disk -w $avg_free% -c 0% -p $less_free"
  65. );
  66. cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
  67. $result = NPTest->testCmd(
  68. "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
  69. );
  70. cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
  71. $result = NPTest->testCmd(
  72. "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free"
  73. );
  74. cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
  75. my $all_disks = $result->output;
  76. $result = NPTest->testCmd(
  77. "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free"
  78. );
  79. isnt( $result->output, $all_disks, "-e gives different output");
  80. like( $result->output, qr/$less_free/, "Found problem $less_free");
  81. unlike( $result->only_output, qr/$more_free/, "Has ignored $more_free as not a problem");
  82. like( $result->perf_output, qr/$more_free/, "But $more_free is still in perf data");
  83. $result = NPTest->testCmd(
  84. "./check_disk -w $avg_free% -c 0% -p $more_free"
  85. );
  86. cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free");
  87. $result = NPTest->testCmd(
  88. "./check_disk -w $avg_free% -c $avg_free% -p $less_free"
  89. );
  90. cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free");
  91. $result = NPTest->testCmd(
  92. "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free"
  93. );
  94. cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical");
  95. $result = NPTest->testCmd(
  96. "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free"
  97. );
  98. cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference");
  99. TODO: {
  100. local $TODO = "Invalid percent figures";
  101. $result = NPTest->testCmd(
  102. "./check_disk -w 10% -c 15% -p $mountpoint_valid"
  103. );
  104. cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
  105. }
  106. $result = NPTest->testCmd(
  107. "./check_disk -p $mountpoint_valid -w 10% -c 15%"
  108. );
  109. cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" );
  110. $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} ); # 100% empty
  111. cmp_ok( $result->return_code, "==", 2, "100% empty" );
  112. like( $result->output, $failureOutput, "Right output" );
  113. $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
  114. cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
  115. $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} ); # 100 GB empty
  116. cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
  117. # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
  118. $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
  119. cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
  120. $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
  121. cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
  122. $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
  123. cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
  124. TODO: {
  125. local $TODO = "Invalid values";
  126. $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
  127. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  128. $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
  129. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  130. $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
  131. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  132. }
  133. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" );
  134. cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" );
  135. cmp_ok( $result->output, 'eq', 'DISK CRITICAL - /bob does not exist', 'Output OK');
  136. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" );
  137. my $root_output = $result->output;
  138. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" );
  139. cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" );
  140. cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /");
  141. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E" );
  142. cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified");
  143. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" );
  144. cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical");
  145. unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it");
  146. $result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" );
  147. unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice");