check_ntp.t 4.4 KB

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