Class: WatchmonkeyCli::Checkers::UdpPort

Inherits:
WatchmonkeyCli::Checker show all
Defined in:
lib/watchmonkey_cli/checkers/udp_port.rb

Constant Summary

Constants included from Helper

Helper::BYTE_UNITS

Instance Attribute Summary

Attributes inherited from WatchmonkeyCli::Checker

#app

Instance Method Summary collapse

Methods inherited from WatchmonkeyCli::Checker

#_tolog, #blank_config, checker_name, checker_name=, #debug, descendants, #error, #info, inherited, #init, #initialize, #local, maxrt, maxrt=, #rsafe, #safe, #spawn_sub, #start, #stop

Methods included from Helper

#human_filesize, #human_number, #human_seconds

Constructor Details

This class inherits a constructor from WatchmonkeyCli::Checker

Instance Method Details

#check!(result, host, port, opts = {}) ⇒ Object



13
14
15
16
# File 'lib/watchmonkey_cli/checkers/udp_port.rb', line 13

def check! result, host, port, opts = {}
  result.result = port_open?(host.is_a?(String) ? host : host.is_a?(WatchmonkeyCli::LoopbackConnection) ? "127.0.0.1" : host.opts[:host_name] || host.opts[:host] || host.opts[:ip], port, opts)
  result.error! "#{opts[:message]}" unless result.result
end

#enqueue(host, port, opts = {}) ⇒ Object



6
7
8
9
10
11
# File 'lib/watchmonkey_cli/checkers/udp_port.rb', line 6

def enqueue host, port, opts = {}
  opts = { message: "Port #{port} (UDP) is not reachable!", timeout: 2 }.merge(opts)
  host = app.fetch_connection(:loopback, :local) if !host || host == :local
  host = app.fetch_connection(:ssh, host) if host.is_a?(Symbol)
  app.enqueue(self, host, port, opts)
end

#port_open?(ip, port, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/watchmonkey_cli/checkers/udp_port.rb', line 18

def port_open?(ip, port, opts = {})
  Timeout::timeout(opts[:timeout] ? opts[:timeout] : 3600) do
    s = UDPSocket.new
    s.connect(ip, port)
    s.send "aaa", 0
    s.recv(1)
    s.close
  end
  true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
  return false
rescue Timeout::Error
  return true
end