check_nagios.t 3.0 KB

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