Class: EzNemo::Ping

Inherits:
Object
  • Object
show all
Defined in:
lib/eznemo/monitor/ping.rb

Overview

ICMP ping plugin

Constant Summary collapse

DEFAULT_MIN_INTERVAL =
10
DEFAULT_TIMEOUT =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePing

Returns a new instance of Ping.



12
13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/eznemo/monitor/ping.rb', line 10

def config
  @config
end

#monitorObject (readonly)

Returns the value of attribute monitor.



9
10
11
# File 'lib/eznemo/monitor/ping.rb', line 9

def monitor
  @monitor
end

Instance Method Details

#add_check(check) ⇒ Object

Add a check using this plugin

Parameters:



42
43
44
45
46
47
48
# 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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

#nameSymbol

Gets called by Monitor at regisration

Returns:

  • (Symbol)


24
25
26
# File 'lib/eznemo/monitor/ping.rb', line 24

def name
  return :ping
end

#registered(mon) ⇒ Object

Gets called by Monitor after regisration

Parameters:

  • mon (Object)

    parent Monitor object



30
31
32
33
34
35
36
37
38
# 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