module Net::NTP::Check

Overview

Net::NTP::Check checks NTP offset against several NTP servers

Overview

Contains Net::NTP::Check offset checks

Overview

Contains Net::NTP::Check version number

Constants

DEFAULT_SERVERS

Default servers to check against

TIMEOUT

Default timeout for the NTP requests

VERSION

Public Class Methods

get_offset(host = DEFAULT_SERVERS[0], timeout = TIMEOUT) click to toggle source

Get the time offset against the given host

# File lib/net/ntp/check/offset.rb, line 48
def self.get_offset(host = DEFAULT_SERVERS[0], timeout = TIMEOUT)
  get_host_data(host, timeout)[0].offset
end
get_offsets(hosts = DEFAULT_SERVERS, timeout = TIMEOUT) click to toggle source

Get the time offsets against the given host list

# File lib/net/ntp/check/offset.rb, line 36
def self.get_offsets(hosts = DEFAULT_SERVERS, timeout = TIMEOUT)
  offsets = []

  Net::NTP::Check.logger.debug("Host list: #{hosts}")

  hosts.each do |host|
    offsets.push(get_offset(host, timeout))
  end
  offsets
end
get_offsets_filtered(hosts = DEFAULT_SERVERS, timeout = TIMEOUT) click to toggle source

Get the time offsets against the given host list with a bandpass filter applied

# File lib/net/ntp/check/offset.rb, line 23
def self.get_offsets_filtered(hosts = DEFAULT_SERVERS, timeout = TIMEOUT)
  offsets = []
  hosts_multiplier = (9.0 / hosts.length).ceil
  Net::NTP::Check.logger.debug("Hosts given: #{hosts}")
  Net::NTP::Check.logger.debug("Hosts multiplier: #{hosts_multiplier}")
  hosts_multiplier.times do
    offsets.push(get_offsets(hosts, timeout))
    sleep(0.01) # sleep to keep from getting rate limted
  end
  AutoBandPass.filter(offsets.flatten.shuffle[0...9])
end
logger() click to toggle source
# File lib/net/ntp/check.rb, line 14
def self.logger
  @logger ||= Logger.new(STDOUT).tap do |l|
    l.level = Logger::INFO
  end
end

Private Class Methods

get_host_data(host, timeout = TIMEOUT) click to toggle source

Do the NTP request and catch exceptions

# File lib/net/ntp/check/offset.rb, line 55
def self.get_host_data(host, timeout = TIMEOUT)
  t = Net::NTP.get(host, 'ntp', timeout)
  delay = (t.client_time_receive - t.originate_timestamp) -
          (t.transmit_timestamp - t.receive_timestamp)
  msg = "Received NTP data for #{host} with ntp delay of #{delay}"
  Net::NTP::Check.logger.debug(msg)
  return t, delay
rescue SocketError
  err_msg = "Unable to resolve #{host}"
  Net::NTP::Check.logger.debug(err_msg)
  raise SocketError, err_msg
rescue NoMethodError, Timeout::Error
  # need to catch NoMethodError pending bug fix
  # https://github.com/zencoder/net-ntp/pull/9
  err_msg = "Connection timed out to #{host}"
  Net::NTP::Check.logger.debug(err_msg)
  raise Timeout::Error, err_msg
end