Skip to content

feat: Add NTP (Network Time Protocol) monitoring support#49

Merged
servak merged 2 commits into
mainfrom
feature/ntp-monitoring
Jul 3, 2025
Merged

feat: Add NTP (Network Time Protocol) monitoring support#49
servak merged 2 commits into
mainfrom
feature/ntp-monitoring

Conversation

@servak
Copy link
Copy Markdown
Owner

@servak servak commented Jul 3, 2025

Summary

  • Implement comprehensive NTP monitoring with time synchronization tracking
  • Add support for both ntp://server[:port] and ntp:server[:port] formats
  • Monitor response time and time offset from NTP servers with configurable thresholds
  • Integrate seamlessly with existing ProbeManager and configuration system

Key Features

NTP Monitoring Capabilities

  • Response Time Measurement: Track NTP query response times (similar to ping RTT)
  • Time Offset Detection: Monitor time drift between local system and NTP server
  • Configurable Thresholds: Set max_offset to alert on excessive time drift
  • Multiple Server Support: Monitor multiple NTP servers simultaneously

Protocol Support

  • New Format: ntp://time.google.com, ntp://pool.ntp.org:123
  • Legacy Format: ntp:time.google.com, ntp:pool.ntp.org:123
  • Default Configuration: Uses pool.ntp.org:123 with no offset limit

Configuration Example

prober:
  ntp-strict:
    probe: ntp
    ntp:
      server: "pool.ntp.org"
      port: 123
      max_offset: 100ms  # Alert if time drift > 100ms

Implementation Details

Files Added

  • internal/prober/ntp.go: Core NTP prober implementation (400+ lines)
  • internal/prober/ntp_test.go: Comprehensive test suite (200+ lines, 75+ test cases)

Files Modified

  • internal/config/config.go: Add NTP default configuration
  • internal/prober/config.go: Add NTP validation support
  • internal/prober/manager.go: Integrate NTP prober creation
  • internal/prober/validate_test.go: Add NTP validation tests

Test Coverage

  • Configuration validation (server, port, max_offset ranges)
  • Target parsing (format variations, error handling)
  • NTP time conversion functions (RFC-compliant timestamp handling)
  • Event registration and error handling
  • Integration with ProbeManager

Test Results

Verified against real NTP servers with accurate results:

Response Time Monitoring

$ ./mping batch --count 2 ntp://time.google.com ntp://pool.ntp.org:123
┌────────────────────────┬──────┬──────┬──────┬────────┬───────┬───────┬───────┬───────┐
│ HOST                   │ SENT │ SUCC │ FAIL │ LOSS   │ LAST  │ AVG   │ BEST  │ WORST │
├────────────────────────┼──────┼──────┼──────┼────────┼───────┼───────┼───────┼───────┤
│ ntp://pool.ntp.org:123 │    2 │    2 │    0 │   0.0% │  14ms │  10ms │   7ms │  14ms │
│ ntp://time.google.com  │    2 │    2 │    0 │   0.0% │  51ms │  47ms │  43ms │  51ms │
└────────────────────────┴──────┴──────┴──────┴────────┴───────┴───────┴───────┴───────┘

Time Offset Detection

With strict 50ms offset limit:

$ ./mping batch --count 3 --config strict-ntp.yml ntp-strict://time.google.com
┌──────────────────────────────┬──────┬──────┬──────┬────────┬──────┬─────┬──────┬───────┬───────────┬───────────┬─────────────────────────────────────────────────┐
│ HOST                         │ SENT │ SUCC │ FAIL │ LOSS   │ LAST │ AVG │ BEST │ WORST │ LAST SUCC │ LAST FAIL │ FAIL REASON                                     │
├──────────────────────────────┼──────┼──────┼──────┼────────┼──────┼─────┼──────┼───────┼───────────┼───────────┼─────────────────────────────────────────────────┤
│ ntp-strict://time.google.com │    4 │    0 │    4 │ 100.0% │ -    │ -   │ -    │ -     │ -         │ 23:52:51  │ time offset too large: 107.444863ms (max: 50ms) │
└──────────────────────────────┴──────┴──────┴──────┴────────┴──────┴─────┴──────┴───────┴───────────┴───────────┴─────────────────────────────────────────────────┘

Accuracy Verification

Compared against system sntp tool:

$ sntp time.google.com
+0.105244 +/- 0.042827 time.google.com 216.239.35.4

# mping detected: 107.444863ms offset
# sntp detected:  105.244ms offset  
# Difference: <3ms (excellent accuracy)

Test Plan

  • Verify NTP packet creation and parsing follows RFC standards
  • Test response time measurement accuracy
  • Test time offset calculation against system tools
  • Validate configuration parsing and validation
  • Test integration with existing prober architecture
  • Verify error handling for unreachable servers
  • Test threshold-based alerting functionality
  • Confirm backward compatibility with existing features

Closes #38

servak added 2 commits July 3, 2025 23:55
- Implement NTPProber with comprehensive time synchronization monitoring
- Support both ntp://server[:port] and ntp:server[:port] formats
- Monitor response time and time offset from NTP servers
- Add configurable max_offset threshold for time drift detection
- Include extensive test suite with 75+ test cases covering:
  - Configuration validation (server, port, max_offset)
  - Target parsing (new/legacy formats, with/without ports)
  - Time conversion functions (NTP timestamp <-> time.Time)
  - Event registration and error handling
- Integrate with existing ProbeManager and configuration system
- Add NTP default configuration (pool.ntp.org:123)

Tested with real NTP servers:
- time.google.com: ~105ms offset detected correctly
- pool.ntp.org: Response times 7-15ms measured accurately
- Threshold testing: 50ms limit triggers errors, 200ms limit allows success
- Validates against sntp tool results for accuracy verification
- Add NTP protocol to supported protocols table with examples
- Include NTP usage examples in Quick Start section
- Add comprehensive NTP configuration documentation
- Add NTP to custom prober examples
- Update feature list to include NTP monitoring
- Set default MaxOffset to 5 seconds for practical time drift detection
- Add time package import for MaxOffset configuration

README updates include:
- Protocol table: ntp://[server[:port]] format examples
- Quick start: NTP monitoring and mixed protocol examples
- Usage section: NTP command examples
- Configuration: Complete NTP settings with max_offset explanation
- Custom probers: ntp-strict example with 100ms threshold
@servak servak force-pushed the feature/ntp-monitoring branch from 921050a to e964ee1 Compare July 3, 2025 15:07
@servak servak merged commit d072cab into main Jul 3, 2025
4 checks passed
@servak servak deleted the feature/ntp-monitoring branch July 3, 2025 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add NTP Probe support for time synchronization monitoring

1 participant