check_nagios.t 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #! /usr/bin/perl -w -I ..
  2. #
  3. # check_nagios tests
  4. #
  5. #
  6. use strict;
  7. use Test::More;
  8. use NPTest;
  9. if (`uname -s` eq "SunOS\n") {
  10. plan skip_all => "Ignoring tests on solaris because of pst3";
  11. } else {
  12. plan tests => 13;
  13. }
  14. my $successOutput = '/^NAGIOS OK: /';
  15. my $warningOutput = '/^NAGIOS WARNING: /';
  16. my $failureOutput = '/^NAGIOS CRITICAL: /';
  17. my $nagios1 = "t/check_nagios.nagios1.status.log";
  18. my $nagios2 = "t/check_nagios.nagios2.status.dat";
  19. my $result;
  20. # Did use init, but MacOSX 10.4 replaces init with launchd
  21. # Alternative is to insist that nagios is running to run this test
  22. # Reasonable to expect cron because build servers will
  23. # invoke cron to run a build
  24. my $procname = "cron";
  25. $result = NPTest->testCmd(
  26. "./check_nagios -F $nagios1 -e 5 -C $procname"
  27. );
  28. cmp_ok( $result->return_code, '==', 1, "Log over 5 minutes old" );
  29. like ( $result->output, $warningOutput, "Output for warning correct" );
  30. my $now = time;
  31. # This substitution is dependent on the testcase
  32. system( "perl -pe 's/1133537544/$now/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1";
  33. $result = NPTest->testCmd(
  34. "./check_nagios -F $nagios1.tmp -e 1 -C $procname"
  35. );
  36. cmp_ok( $result->return_code, "==", 0, "Log up to date" );
  37. like ( $result->output, $successOutput, "Output for success correct" );
  38. my $later = $now - 61;
  39. system( "perl -pe 's/1133537544/$later/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1";
  40. $result = NPTest->testCmd(
  41. "./check_nagios -F $nagios1.tmp -e 1 -C $procname"
  42. );
  43. cmp_ok( $result->return_code, "==", 1, "Log correctly seen as over 1 minute old" );
  44. my ($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/;
  45. like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" );
  46. $result = NPTest->testCmd(
  47. "./check_nagios -F $nagios1.tmp -e 5 -C unlikely_command_string"
  48. );
  49. cmp_ok( $result->return_code, "==", 2, "Nagios command not found" );
  50. like ( $result->output, $failureOutput, "Output for failure correct" );
  51. $result = NPTest->testCmd(
  52. "./check_nagios -F $nagios2 -e 5 -C $procname"
  53. );
  54. cmp_ok( $result->return_code, "==", 1, "Nagios2 for logfile over 5 mins old" );
  55. $now = time;
  56. system( "perl -pe 's/1133537302/$now/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2";
  57. $result = NPTest->testCmd(
  58. "./check_nagios -F $nagios2.tmp -e 1 -C $procname"
  59. );
  60. cmp_ok( $result->return_code, "==", 0, "Nagios2 log up to date" );
  61. $later = $now - 61;
  62. system( "perl -pe 's/1133537302/$later/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2";
  63. $result = NPTest->testCmd(
  64. "./check_nagios -F $nagios2.tmp -e 1 -C $procname"
  65. );
  66. cmp_ok( $result->return_code, "==", 1, "Nagios2 log correctly seen as over 1 minute old" );
  67. ($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/;
  68. like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" );
  69. $result = NPTest->testCmd(
  70. "./check_nagios -F t/check_nagios.t -e 1 -C $procname"
  71. );
  72. cmp_ok( $result->return_code, "==", 2, "Invalid log file" );