4
0

001-normal.t 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. BEGIN {
  5. use File::Basename;
  6. use lib (dirname(__FILE__));
  7. }
  8. use Test::More tests => 44;
  9. use Digest::SHA qw(sha1_hex);
  10. use nrpe;
  11. my @output;
  12. ensure_daemon_running();
  13. switch_config_file("configs/normal.cfg");
  14. restart_daemon();
  15. @output = `$checknrpe -H 127.0.0.1 -p 40321`;
  16. is($?, STATE_OK, 'version check') || diag @output;
  17. like($output[0], qr/NRPE v.*/, 'version check response') || diag @output;
  18. @output = `$checknrpe -H 127.0.0.1 -p 40321 -2`;
  19. is($?, STATE_OK, 'version check - v2 packet') || diag @output;
  20. like($output[0], qr/NRPE v.*/, 'version check response - v2 packet') || diag @output;
  21. # Note: Server may fail v3 packet and we'll retry with a v2 packet. Could check log for difference if we care.
  22. @output = `$checknrpe -H 127.0.0.1 -p 40321 -3`;
  23. is($?, STATE_OK, 'version check - v3 packet') || diag @output;
  24. like($output[0], qr/NRPE v.*/, 'version check response - v3 packet') || diag @output;
  25. # Source address not allowed
  26. SKIP: {
  27. skip 'not linux', 2 if $^O ne 'linux';
  28. @output = `$checknrpe -H 127.0.0.1 -p 40321 -b 127.0.0.2`;
  29. is($?, STATE_UNKNOWN, 'invalid source ip') || diag @output;
  30. like($output[0], qr/CHECK_NRPE: Error - Could not connect to .*: /, 'invalid source ip response') || diag @output;
  31. }
  32. SKIP: {
  33. @output = `$checknrpe -H 127.0.0.1 -p 40321 -c state_ok`;
  34. is($?, STATE_OK, "state_ok exec") || diag explain @output && skip 'failed exec', 2;
  35. is(@output, 1, 'state_ok result lines');
  36. is($output[0], "OK: Everything is normal\n", 'state_ok result');
  37. }
  38. SKIP: {
  39. @output = `$checknrpe -H 127.0.0.1 -p 40321 -c state_warning`;
  40. is($?, STATE_WARNING, "state_warning exec") || diag @output && skip 'failed exec', 2;
  41. is(@output, 1, 'state_warning result lines');
  42. is($output[0], "WARNING: That\'s strange.\n", 'state_warning result');
  43. }
  44. SKIP: {
  45. @output = `$checknrpe -H 127.0.0.1 -p 40321 -c state_critical`;
  46. is($?, STATE_CRITICAL, "state_critical exec") || diag @output && skip 'failed exec', 2;
  47. is(@output, 1, 'state_critical result lines');
  48. is($output[0], "CRITICAL: Danger!\n", 'state_critical result');
  49. }
  50. SKIP: {
  51. @output = `$checknrpe -H 127.0.0.1 -p 40321 -c state_unknown`;
  52. is($?, STATE_UNKNOWN, "state_unknown exec") || diag @output && skip 'failed exec', 2;
  53. is(@output, 1, 'state_unknown result lines');
  54. is($output[0], "UNKNOWN: Huh?\n", 'state_unknown result');
  55. }
  56. SKIP: {
  57. @output = `$checknrpe -H 127.0.0.1 -p 40321 -c lorem_10000`;
  58. is($?, STATE_OK, "10,000 byte test exec") || diag @output && skip 'failed exec', 1;
  59. is(sha1_hex(@output), '2ed4d81e96262f2b08441c1f819166209a2f5337', '10,000 byte test result');
  60. }
  61. SKIP: {
  62. @output = `$checknrpe -H 127.0.0.1 -p 40321 -c lorem_60000`;
  63. is($?, STATE_OK, "60,000 byte test exec") || diag @output && skip 'failed exec', 1;
  64. is(sha1_hex(@output), '2ad3e654c043773c309321d8fb62232833510b8f', '60,000 byte test result');
  65. }
  66. SKIP: {
  67. @output = `$checknrpe -H 127.0.0.1 -p 40321 -c lorem_70000`;
  68. is($?, STATE_OK, "70,000 byte test exec") || diag @output && skip 'failed exec', 1;
  69. # Note: We're expecting the output to be trimmed to 64k.
  70. is(sha1_hex(@output), 'af05e7c515f21dd9b6944e9b092bf1691ae47050', '70,000 byte test result');
  71. if ($? != 0) {
  72. diag(scalar @output);
  73. diag($output[0]);
  74. diag($output[-1]);
  75. }
  76. }
  77. @output = `$checknrpe -H 127.0.0.1 -p 40321 -c invalid_command`;
  78. is($?, STATE_UNKNOWN, 'invalid command');
  79. like($output[0], qr/NRPE: Command 'invalid_command' not defined/, 'invalid command response');
  80. @output = `$checknrpe -H 127.0.0.1 -p 40321 -c nonexistent`;
  81. is($?, STATE_UNKNOWN, 'nonexistent command');
  82. like($output[0], qr/NRPE: Unable to read output/, 'nonexistent command response');
  83. @output = `$checknrpe -H 127.0.0.1 -p 40321 -c timeout`;
  84. is($?, STATE_UNKNOWN, 'timeout command');
  85. like($output[0], qr/NRPE: Command timed out after 5 seconds/, 'timeout command response');
  86. SKIP: {
  87. @output = `$checknrpe -H 127.0.0.1 -p 40321 -2 -c state_ok`;
  88. is($?, STATE_OK, "v2 state_ok exec") || diag @output && skip 'failed exec', 2;
  89. is(@output, 1, 'v2 state_ok result lines');
  90. is($output[0], "OK: Everything is normal\n", 'v2 state_ok result');
  91. }
  92. SKIP: {
  93. @output = `$checknrpe -H 127.0.0.1 -p 40321 -2 -c ""`;
  94. is($?, STATE_UNKNOWN, "v2 null exec") || diag @output && skip 'failed exec', 2;
  95. is(@output, 1, 'v2 null result lines');
  96. like($output[0], qr/CHECK_NRPE: Error - Could not connect to .*: /, 'v2 null result');
  97. }
  98. SKIP: {
  99. @output = `$checknrpe -H 127.0.0.1 -p 40321 -c "state_ok" -a 1 2 3`;
  100. is($?, STATE_UNKNOWN, "args exec") || diag @output && skip 'failed exec', 2;
  101. is(@output, 1, 'args - result lines');
  102. like($output[0], qr/CHECK_NRPE: Error - Could not connect to .*: /, 'args - result');
  103. }
  104. SKIP: {
  105. @output = `$checknrpe -H 127.0.0.1 -p 40321 -c "< >"`;
  106. is($?, STATE_UNKNOWN, "metachars - exec") || diag @output && skip 'failed exec', 2;
  107. is(@output, 1, 'metachars - result lines');
  108. like($output[0], qr/CHECK_NRPE: Error - Could not connect to .*: /, 'metachars - result');
  109. }
  110. done_testing();