check_nagios.t 2.9 KB

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