class Net::NTP::Check::StatsdClient

Overview

Sends Net::NTP::Check offset data to statsd

Constants

DEFAULT_STATSD_GAUGE_BASE
DEFAULT_STATSD_HOST
DEFAULT_STATSD_PORT
DEFAULT_TIMEOUT

Public Class Methods

new(opts = {}) click to toggle source
# File lib/net/ntp/check/statsd.rb, line 17
def initialize(opts = {})
  @stats = {}
  @statsd_host = opts.fetch(:statsd_host, DEFAULT_STATSD_HOST)
  @statsd_port = opts.fetch(:statsd_port, DEFAULT_STATSD_PORT)
  @statsd_gauge_base = opts.fetch(:statsd_gauge_base,
                                  DEFAULT_STATSD_GAUGE_BASE)
  @ntp_timeout = opts.fetch(:ntp_timeout, DEFAULT_TIMEOUT)
  @ntp_hosts = opts.fetch(:ntp_hosts,
                          Net::NTP::Check::DEFAULT_SERVERS)
  @statsd = Statsd.new(@statsd_host, @statsd_port)
end

Public Instance Methods

send_offset_stats() click to toggle source
# File lib/net/ntp/check/statsd.rb, line 29
def send_offset_stats
  err_code = 0
  offset_stats!
  @statsd.gauge("#{@statsd_gauge_base}.offset_s",
                @stats[:offset])
  @statsd.gauge("#{@statsd_gauge_base}.offset_ms",
                @stats[:offset_in_ms])

  rescue SocketError => e
    err_code = 3

  rescue Timeout::Error => e
    err_code = 2

  rescue StandardError => e
    err_code = 1
  ensure
    @statsd.gauge("#{@statsd_gauge_base}.err", err_code)
    warn 'Problem running Net::NTP::Check::StatsdClient stats '\
      "gathering: #{e.message} (error code #{err_code})" unless
      err_code == 0
    warn e.backtrace.join("\n") if err_code == 1
    exit err_code unless err_code == 0
end

Private Instance Methods

offset_stats!() click to toggle source
# File lib/net/ntp/check/statsd.rb, line 56
def offset_stats!
  stats_data_filtered = Net::NTP::Check.get_offsets_filtered(
    @ntp_hosts, @ntp_timeout)
  msg = "stats_data_filtered: #{stats_data_filtered}"
  Net::NTP::Check.logger.debug(msg)
  @stats[:offset] = stats_data_filtered
  @stats[:offset_in_ms] = @stats[:offset] * 1000
  Net::NTP::Check.logger.debug("stats: #{@stats}")
end