Class: Igp::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/igp/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

expects a configuration hash as options which may contain:

:type => symbol indicating protocol to use (:icmp,:udp,:tcp:http,:https,:ldap,:ldaps)
:limit => number of pings to perform (nil => infinite)
:interval => number of seconds to wait between pings (default = 5)
:url => destination url (required for http/s and ldap/s)
:host => host name or IP address (required for icmp,tcp, and udp)
:port => optionally specify the port for host (else default port is assumed)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/igp/base.rb', line 20

def initialize(options = {})
  @options = options
  @limit = options[:limit]
  @interval = options[:interval] || 5
  case options[:type]
  when :icmp
    @ping_handler = Net::Ping::External.new(@options[:host],@options[:port])
  when :udp
    @ping_handler = Net::Ping::UDP.new(@options[:host],@options[:port])
  when :tcp
    @ping_handler = Net::Ping::TCP.new(@options[:host],@options[:port])
  when :http, :https
    @ping_handler = Net::Ping::HTTP.new(@options[:url])
  when :ldap, :ldaps
    @ping_handler = Net::Ping::LDAP.new(@options[:url])
  end
end

Instance Attribute Details

#intervalObject (readonly)

number of seconds to wait between pings (default = 5)



10
11
12
# File 'lib/igp/base.rb', line 10

def interval
  @interval
end

#limitObject (readonly)

number of pings to perform (nil => infinite)



8
9
10
# File 'lib/igp/base.rb', line 8

def limit
  @limit
end

#optionsObject (readonly)

holds the parsed options



4
5
6
# File 'lib/igp/base.rb', line 4

def options
  @options
end

#ping_handlerObject (readonly)

the Net::Ping handler for the specific protocol required



6
7
8
# File 'lib/igp/base.rb', line 6

def ping_handler
  @ping_handler
end

Instance Method Details

#runObject

main routine to run a complete ping test



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/igp/base.rb', line 39

def run
  return unless ping_handler
  header
  ping_count=0
  while (limit.nil? || ping_count < limit) do
    status = ping_handler.ping?
    log status,ping_handler.duration,ping_handler.exception
    ping_count += 1
    sleep interval if (limit.nil? || ping_count < limit)
  end
end