check_disk.t 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #! /usr/bin/perl -w -I ..
  2. #
  3. # Disk Space Tests via check_disk
  4. #
  5. # $Id$
  6. #
  7. use strict;
  8. use Test::More tests => 26;
  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( "mountpoint_valid", "NP_MOUNTPOINT_VALID", "/",
  16. "The path to a valid mountpoint" );
  17. my $mountpoint2_valid = getTestParameter( "mountpoint2_valid", "NP_MOUNTPOINT2_VALID", "/var",
  18. "The path to another valid mountpoint. Must be different from 1st one." );
  19. $result = NPTest->testCmd(
  20. "./check_disk -w 1% -c 1% -p $mountpoint_valid -w 1% -c 1% -p $mountpoint2_valid"
  21. );
  22. cmp_ok( $result->return_code, "==", 0, "Checking two mountpoints (must have at least 1% free)");
  23. my $c = 0;
  24. $_ = $result->output;
  25. $c++ while /\(/g; # counts number of "(" - should be two
  26. cmp_ok( $c, '==', 2, "Got two mountpoints in output");
  27. # Calculate avg_free free on mountpoint1 and mountpoint2
  28. # because if you check in the middle, you should get different errors
  29. $_ = $result->output;
  30. my ($free_on_mp1, $free_on_mp2) = (m/\((\d+)%.*\((\d+)%/);
  31. die "Cannot parse output: $_" unless ($free_on_mp1 && $free_on_mp2);
  32. my $avg_free = ceil(($free_on_mp1+$free_on_mp2)/2);
  33. my ($more_free, $less_free);
  34. if ($free_on_mp1 > $free_on_mp2) {
  35. $more_free = $mountpoint_valid;
  36. $less_free = $mountpoint2_valid;
  37. } elsif ($free_on_mp1 < $free_on_mp2) {
  38. $more_free = $mountpoint2_valid;
  39. $less_free = $mountpoint_valid;
  40. } else {
  41. die "Two mountpoints are the same - cannot do rest of test";
  42. }
  43. $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -p $more_free" );
  44. cmp_ok( $result->return_code, '==', 0, "At least 100 bytes available on $more_free");
  45. like ( $result->output, $successOutput, "OK output" );
  46. $result = NPTest->testCmd( "./check_disk 100 100 $more_free" );
  47. cmp_ok( $result->return_code, '==', 0, "Old syntax okay" );
  48. $result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" );
  49. cmp_ok( $result->return_code, "==", 0, "At least 1% free" );
  50. $result = NPTest->testCmd(
  51. "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free"
  52. );
  53. cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" );
  54. like( $result->output, $failureOutput, "Right output" );
  55. $result = NPTest->testCmd(
  56. "./check_disk -w $avg_free% -c 0% -p $less_free"
  57. );
  58. cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free");
  59. $result = NPTest->testCmd(
  60. "./check_disk -w $avg_free% -c $avg_free% -p $more_free"
  61. );
  62. cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free");
  63. $result = NPTest->testCmd(
  64. "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free"
  65. );
  66. cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning");
  67. $result = NPTest->testCmd(
  68. "./check_disk -w $avg_free% -c 0% -p $more_free"
  69. );
  70. cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free");
  71. $result = NPTest->testCmd(
  72. "./check_disk -w $avg_free% -c $avg_free% -p $less_free"
  73. );
  74. cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free");
  75. $result = NPTest->testCmd(
  76. "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free"
  77. );
  78. cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical");
  79. $result = NPTest->testCmd(
  80. "./check_disk -w 10% -c 15% -p $mountpoint_valid"
  81. );
  82. cmp_ok( $result->return_code, '==', 3, "Invalid command line options" );
  83. TODO: {
  84. local $TODO = "-p must come after -w and -c";
  85. $result = NPTest->testCmd(
  86. "./check_disk -p $mountpoint_valid -w 10% -c 15%"
  87. );
  88. cmp_ok( $result->return_code, "==", 3, "Invalid options - order unimportant" );
  89. }
  90. $result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} ); # 100% empty
  91. cmp_ok( $result->return_code, "==", 2, "100% empty" );
  92. like( $result->output, $failureOutput, "Right output" );
  93. TODO: {
  94. local $TODO = "Requesting 100GB free is should be critical";
  95. $result = NPTest->testCmd( "./check_disk -w 100000 -c 100000 $mountpoint_valid" );
  96. cmp_ok( $result->return_code, '==', 2, "Check for 100GB free" );
  97. }
  98. TODO: {
  99. local $TODO = "-u GB does not work";
  100. $result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u GB ".${mountpoint_valid} ); # 100 GB empty
  101. cmp_ok( $result->return_code, "==", 2, "100 GB empty" );
  102. }
  103. # Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds
  104. $result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} );
  105. cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used");
  106. $result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" );
  107. cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" );
  108. $result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" );
  109. cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" );
  110. $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" );
  111. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  112. TODO: {
  113. local $TODO = "Need to properly check input";
  114. $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" );
  115. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );
  116. }
  117. $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" );
  118. cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" );