check_nagios.t 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. $result = NPTest->testCmd(
  17. "./check_nagios -F $nagios1 -e 5 -C init"
  18. );
  19. cmp_ok( $result->return_code, '==', 1, "Log over 5 minutes old" );
  20. like ( $result->output, $warningOutput, "Output for warning correct" );
  21. my $now = time;
  22. # This substitution is dependant on the testcase
  23. system( "perl -pe 's/1133537544/$now/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1";
  24. $result = NPTest->testCmd(
  25. "./check_nagios -F $nagios1.tmp -e 1 -C init"
  26. );
  27. cmp_ok( $result->return_code, "==", 0, "Log up to date" );
  28. like ( $result->output, $successOutput, "Output for success correct" );
  29. my $later = $now - 61;
  30. system( "perl -pe 's/1133537544/$later/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1";
  31. $result = NPTest->testCmd(
  32. "./check_nagios -F $nagios1.tmp -e 1 -C init"
  33. );
  34. cmp_ok( $result->return_code, "==", 1, "Log correctly seen as over 1 minute old" );
  35. my ($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/;
  36. like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" );
  37. $result = NPTest->testCmd(
  38. "./check_nagios -F $nagios1.tmp -e 5 -C unlikely_command_string"
  39. );
  40. cmp_ok( $result->return_code, "==", 2, "Nagios command not found" );
  41. like ( $result->output, $failureOutput, "Output for failure correct" );
  42. $result = NPTest->testCmd(
  43. "./check_nagios -F $nagios2 -e 5 -C init"
  44. );
  45. cmp_ok( $result->return_code, "==", 1, "Nagios2 for logfile over 5 mins old" );
  46. $now = time;
  47. system( "perl -pe 's/1133537302/$now/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2";
  48. $result = NPTest->testCmd(
  49. "./check_nagios -F $nagios2.tmp -e 1 -C init"
  50. );
  51. cmp_ok( $result->return_code, "==", 0, "Nagios2 log up to date" );
  52. $later = $now - 61;
  53. system( "perl -pe 's/1133537302/$later/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2";
  54. $result = NPTest->testCmd(
  55. "./check_nagios -F $nagios2.tmp -e 1 -C init"
  56. );
  57. cmp_ok( $result->return_code, "==", 1, "Nagios2 log correctly seen as over 1 minute old" );
  58. ($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/;
  59. like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" );
  60. $result = NPTest->testCmd(
  61. "./check_nagios -F t/check_nagios.t -e 1 -C init"
  62. );
  63. cmp_ok( $result->return_code, "==", 2, "Invalid log file" );