class EzNemo::Ping

ICMP ping plugin

Constants

DEFAULT_MIN_INTERVAL
DEFAULT_TIMEOUT

Attributes

config[R]
monitor[R]

Public Class Methods

new() click to toggle source
# File lib/eznemo/monitor/ping.rb, line 12
def initialize
  os = RbConfig::CONFIG['host_os']
  case
  when os.include?('darwin')
    @os = 'bsd'
  when os.include?('linux')
    @os = 'linux'
  end
end

Public Instance Methods

add_check(check) click to toggle source

Add a check using this plugin @param check [EzNemo::Check]

# File lib/eznemo/monitor/ping.rb, line 42
def add_check(check)
  min = config[:min_interval]
  check[:interval] = min if check[:interval] < min
  EM.add_periodic_timer(check[:interval]) do
    self.send("#{@os}_ping", check)
  end
end
bsd_ping(check) click to toggle source
# File lib/eznemo/monitor/ping.rb, line 67
def bsd_ping(check)
  result = create_result_for_check(check)
  args = {timeout: config[:timeout] * 1000}
  EM.system(build_cmd(check, args)) do |output, status|
    case status.exitstatus
    when 0
      expr = /=\s*([0-9\.]+)/
      expr =~ output
      set_ok_result(result, $1.to_f)
    when 2
      set_ng_result(result)
    else
      set_error_result(result, output)
    end
    monitor.report(result)
  end
end
linux_ping(check) click to toggle source
# File lib/eznemo/monitor/ping.rb, line 50
def linux_ping(check)
  result = create_result_for_check(check)
  EM.system(build_cmd(check)) do |output, status|
    case status.exitstatus
    when 0
      expr = /=\s*([0-9\.]+)/
      expr =~ output
      set_ok_result(result, $1.to_f)
    when 1
      set_ng_result(result)
    else
      set_error_result(result, output)
    end
    monitor.report(result)
  end
end
name() click to toggle source

Gets called by Monitor at regisration @return [Symbol]

# File lib/eznemo/monitor/ping.rb, line 24
def name
  return :ping
end
registered(mon) click to toggle source

Gets called by Monitor after regisration @param mon [Object] parent Monitor object

# File lib/eznemo/monitor/ping.rb, line 30
def registered(mon)
  @monitor = mon
  @config = EzNemo.config[:monitor][:ping] if EzNemo.config[:monitor]
  @config ||= {}
  @config[:path] ||= 'ping'
  @config[:min_interval] ||= DEFAULT_MIN_INTERVAL
  @config[:timeout] ||= DEFAULT_TIMEOUT
  EzNemo.logger.info 'Ping plugin registered.'
end

Private Instance Methods

build_cmd(check, args = {}) click to toggle source

@param check [EzNemo::Check] @param args [Hash] overriding arguments

# File lib/eznemo/monitor/ping.rb, line 99
def build_cmd(check, args = {})
  h = {
    path: config[:path],
    timeout: config[:timeout],
    options: "#{config[:cmd_opts]} #{check[:options]}",
    hostname: check[:hostname]
  }
  h.merge!(args)
  "#{h[:path]} -c 1 -nqW #{h[:timeout]} #{h[:options]} #{h[:hostname]}"
end
create_result_for_check(check) click to toggle source

@param check [EzNemo::Check] @return [EzNemo::Result]

# File lib/eznemo/monitor/ping.rb, line 89
def create_result_for_check(check)
  Result::new do |r|
    r.timestamp = Time.now
    r.check = check
    r.probe = EzNemo.config[:probe][:name]
  end
end
set_err_result(result, msg) click to toggle source
# File lib/eznemo/monitor/ping.rb, line 122
def set_err_result(result, msg)
  msg = 'see log' if msg.nil? || msg.size == 0
  result.status = false
  result.response_ms = 0
  result.status_desc = "ERROR: #{msg}".chomp
end
set_ng_result(result) click to toggle source
# File lib/eznemo/monitor/ping.rb, line 116
def set_ng_result(result)
  result.status = false
  result.response_ms = 0
  result.status_desc = 'NG'
end
set_ok_result(result, ms) click to toggle source
# File lib/eznemo/monitor/ping.rb, line 110
def set_ok_result(result, ms)
  result.status = true
  result.response_ms = ms
  result.status_desc = 'OK'
end