check_ntp.t 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #! /usr/bin/perl -w -I ..
  2. #
  3. # Testing NTP
  4. #
  5. # $Id$
  6. #
  7. use strict;
  8. use Test::More;
  9. use NPTest;
  10. my @PLUGINS1 = ('check_ntp', 'check_ntp_peer', 'check_ntp_time');
  11. my @PLUGINS2 = ('check_ntp_peer');
  12. plan tests => (12 * scalar(@PLUGINS1)) + (6 * scalar(@PLUGINS2));
  13. my $res;
  14. my $ntp_service = getTestParameter( "NP_GOOD_NTP_SERVICE",
  15. "A host providing NTP service",
  16. "pool.ntp.org");
  17. my $no_ntp_service = getTestParameter( "NP_NO_NTP_SERVICE",
  18. "A host NOT providing the NTP service",
  19. "localhost" );
  20. my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE",
  21. "The hostname of system not responsive to network requests",
  22. "10.0.0.1" );
  23. my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID",
  24. "An invalid (not known to DNS) hostname",
  25. "nosuchhost");
  26. my $ntp_okmatch1 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs/';
  27. my $ntp_warnmatch1 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs/';
  28. my $ntp_critmatch1 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs/';
  29. my $ntp_okmatch2 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}/';
  30. my $ntp_warnmatch2 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}/';
  31. my $ntp_critmatch2 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}/';
  32. my $ntp_noresponse = '/^(CRITICAL - Socket timeout after 3 seconds)|(NTP CRITICAL: No response from NTP server)$/';
  33. my $ntp_nosuchhost = '/^check_ntp.*: Invalid hostname/address - ' . $hostname_invalid . '/';
  34. foreach my $plugin (@PLUGINS1) {
  35. SKIP: {
  36. skip "No NTP server defined", 1 unless $ntp_service;
  37. $res = NPTest->testCmd(
  38. "./$plugin -H $ntp_service -w 1000 -c 2000"
  39. );
  40. cmp_ok( $res->return_code, '==', 0, "$plugin: Good NTP result (simple check)" );
  41. like( $res->output, $ntp_okmatch1, "$plugin: Output match OK (simple check)" );
  42. $res = NPTest->testCmd(
  43. "./$plugin -H $ntp_service -w 1000: -c 2000"
  44. );
  45. cmp_ok( $res->return_code, '==', 1, "$plugin: Warning NTP result (simple check)" );
  46. like( $res->output, $ntp_warnmatch1, "$plugin: Output match WARNING (simple check)" );
  47. $res = NPTest->testCmd(
  48. "./$plugin -H $ntp_service -w 1000 -c 2000:"
  49. );
  50. cmp_ok( $res->return_code, '==', 2, "$plugin: Critical NTP result (simple check)" );
  51. like( $res->output, $ntp_critmatch1, "$plugin: Output match CRITICAL (simple check)" );
  52. }
  53. SKIP: {
  54. skip "No bad NTP server defined", 1 unless $no_ntp_service;
  55. $res = NPTest->testCmd(
  56. "./$plugin -H $no_ntp_service -t 3"
  57. );
  58. cmp_ok( $res->return_code, '==', 2, "$plugin: No NTP service" );
  59. like( $res->output, $ntp_noresponse, "$plugin: Output match no NTP service" );
  60. }
  61. $res = NPTest->testCmd(
  62. "./$plugin -H $host_nonresponsive -t 3"
  63. );
  64. cmp_ok( $res->return_code, '==', 2, "$plugin: Server not responding" );
  65. like( $res->output, $ntp_noresponse, "$plugin: Output match non-responsive" );
  66. $res = NPTest->testCmd(
  67. "./$plugin -H $hostname_invalid"
  68. );
  69. cmp_ok( $res->return_code, '==', 3, "$plugin: Invalid hostname/address" );
  70. like( $res->output, $ntp_nosuchhost, "$plugin: Output match invalid hostname/address" );
  71. }
  72. foreach my $plugin (@PLUGINS2) {
  73. SKIP: {
  74. skip "No NTP server defined", 1 unless $ntp_service;
  75. $res = NPTest->testCmd(
  76. "./$plugin -H $ntp_service -w 1000 -c 2000 -W 20 -C 21 -j 100000 -k 200000"
  77. );
  78. cmp_ok( $res->return_code, '==', 0, "$plugin: Good NTP result with jitter and stratum check" );
  79. like( $res->output, $ntp_okmatch2, "$plugin: Output match OK with jitter and stratum" );
  80. $res = NPTest->testCmd(
  81. "./$plugin -H $ntp_service -w 1000 -c 2000 -W \\~:-1 -C 21 -j 100000 -k 200000"
  82. );
  83. cmp_ok( $res->return_code, '==', 1, "$plugin: Warning NTP result with jitter and stratum check" );
  84. like( $res->output, $ntp_warnmatch2, "$plugin: Output match WARNING with jitter and stratum" );
  85. $res = NPTest->testCmd(
  86. "./$plugin -H $ntp_service -w 1000 -c 2000 -W 20 -C 21 -j 100000 -k \\~:-1"
  87. );
  88. cmp_ok( $res->return_code, '==', 2, "$plugin: Critical NTP result with jitter and stratum check" );
  89. like( $res->output, $ntp_critmatch2, "$plugin: Output match CRITICAL with jitter and stratum" );
  90. }
  91. }