Class: Nines::PingCheck

Inherits:
Check
  • Object
show all
Defined in:
lib/nines/ping_check.rb

Instance Attribute Summary collapse

Attributes inherited from Check

#address, #cycles, #group, #hostname, #interval, #logger, #name, #notifier, #port, #since, #timeout, #up

Instance Method Summary collapse

Methods inherited from Check

#log_status

Constructor Details

#initialize(group, options) ⇒ PingCheck

Returns a new instance of PingCheck.



8
9
10
11
12
# File 'lib/nines/ping_check.rb', line 8

def initialize(group, options)
  super(group, options)
  
  @protocol = (options['protocol'] || 'icmp').downcase
end

Instance Attribute Details

#protocolObject

Returns the value of attribute protocol.



6
7
8
# File 'lib/nines/ping_check.rb', line 6

def protocol
  @protocol
end

Instance Method Details

#debugObject

shortcuts



15
# File 'lib/nines/ping_check.rb', line 15

def debug     ; Nines::App.debug    ; end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nines/ping_check.rb', line 17

def run
  while Nines::App.continue do
    check_started = Time.now
    @address = Dnsruby::Resolv.getaddress(hostname)
    
    @pinger = case protocol
      when 'tcp'  then Net::Ping::TCP.new(hostname, nil, timeout)
      when 'udp'  then Net::Ping::UDP.new(hostname, nil, timeout)
      when 'icmp'
        if Process::UID == 0
          Net::Ping::ICMP.new(hostname, nil, timeout)
        else
          Net::Ping::External.new(hostname, nil, timeout)
        end
      else "invalid ping protocol #{protocol}"
    end
    
    # the check
    log_status(@pinger.ping?, "#{protocol == 'icmp' ? 'icmp' : "#{protocol}/#{port}"} ping on #{hostname} (#{address})")
    
    break if debug
    
    wait = interval.to_f - (Time.now - check_started)
    while wait > 0 do
      break unless Nines::App.continue
      sleep [1, wait].min
      wait -= 1
    end
  end
end