Class: Net::NTP::Check::StatsdClient

Inherits:
Object
  • Object
show all
Defined in:
lib/net/ntp/check/statsd.rb

Overview

Overview

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

Constant Summary collapse

DEFAULT_STATSD_HOST =
'localhost'
DEFAULT_STATSD_PORT =
8125
DEFAULT_STATSD_GAUGE_BASE =
'net.ntp.check'
DEFAULT_TIMEOUT =
5

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ StatsdClient

Returns a new instance of StatsdClient.



17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#send_offset_statsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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