class Remon::Check

Constants

CRITICAL
OK
WARNING

Attributes

mutex[R]
o[R]
opts[R]

Public Class Methods

get_opts() click to toggle source
# File lib/remon/check.rb, line 17
def self.get_opts
  @opts ||= {}
end
name() click to toggle source
# File lib/remon/check.rb, line 5
def self.name
  @name || "undefined"
end
name=(name) click to toggle source
# File lib/remon/check.rb, line 9
def self.name=(name)
  @name = name
end
new(*args, **kwargs, &block) click to toggle source
# File lib/remon/check.rb, line 29
def initialize(*args, **kwargs, &block)
  @tags = kwargs[:tags] || []
  @ttl = kwargs[:ttl] || 10
  @host = kwargs[:host] || Remon.host

  opts = kwargs[:opts] || {}
  verfiy_opts(opts)
  default_opts = self.class.get_opts
  @opts = default_opts.merge opts

  @mutex = Mutex.new
  return if not respond_to? :init

  # propagate only those kwargs which are defined in "init" method definition
  filtered_kwargs = filtered_kwargs(kwargs)

  #workaround a bug in ruby for methods that take 0 args
  if filtered_kwargs.empty?
    init(*args, &block) if respond_to? :init
  else
    init(*args, **filtered_kwargs, &block) if respond_to? :init
  end
end
opts(h) click to toggle source
# File lib/remon/check.rb, line 13
def self.opts(h)
  @opts = h
end

Public Instance Methods

check_name() click to toggle source
# File lib/remon/check.rb, line 61
def check_name
  self.class.name
end
run() click to toggle source
# File lib/remon/check.rb, line 53
def run
  raise NotImplementedError.new "run method not implemented"
end
run_mutex() click to toggle source
# File lib/remon/check.rb, line 57
def run_mutex
  synchronize { run }
end
to_s() click to toggle source
# File lib/remon/check.rb, line 65
def to_s
  "<check:#{check_name}>"
end

Private Instance Methods

critical_event(service) click to toggle source
# File lib/remon/check.rb, line 88
def critical_event(service)
  event({
    service: service,
    description: "failing to execute check",
    state: "critical",
    metric: 1
  })
end
event(**kwargs) click to toggle source
# File lib/remon/check.rb, line 71
def event(**kwargs)
  kwargs[:time] = Time.now.to_i
  kwargs[:tags] = @tags
  kwargs[:ttl] = @ttl
  kwargs[:host] = @host if @host
  kwargs
end
filtered_kwargs(kwargs) click to toggle source
# File lib/remon/check.rb, line 117
def filtered_kwargs(kwargs)
  params = method(:init).parameters
  init_kwargs = params.select { |i| i[0] == :key }.map { |i| i[1] }
  kwargs.select { |k,v| init_kwargs.include? k }
end
service_state(service = nil, metric) click to toggle source
# File lib/remon/check.rb, line 107
def service_state(service = nil, metric)
  wkey = service ? "#{service}_warning".to_sym : :warning
  ckey = service ? "#{service}_critical".to_sym : :critical
  warning = opts.fetch wkey
  critical = opts.fetch ckey
  state(metric, warning: warning, critical: critical)
end
state(metric, warning:, critical:) click to toggle source
# File lib/remon/check.rb, line 97
def state(metric, warning:, critical:)
  if metric < warning
    OK
  elsif metric >= warning && metric < critical
    WARNING
  else
    CRITICAL
  end
end
synchronize() { || ... } click to toggle source
# File lib/remon/check.rb, line 131
def synchronize
  locked = false
  locked = @mutex.try_lock
  if locked
    yield
  else
    logger.error "#{self} already running in another thread"
    return false
  end
ensure
  @mutex.unlock if locked
end
verfiy_opts(opts) click to toggle source
# File lib/remon/check.rb, line 123
def verfiy_opts(opts)
  default_opts = self.class.get_opts
  valid_opts = default_opts.keys
  opts.keys.each do |k|
    raise Error, "invalid opt #{k}" if not valid_opts.include? k
  end
end
warning_event(service) click to toggle source
# File lib/remon/check.rb, line 79
def warning_event(service)
  event({
    service: service,
    description: "failing to execute check",
    state: "warning",
    metric: 0.9
  })
end