Class: Igp::Base
- Inherits:
-
Object
- Object
- Igp::Base
- Defined in:
- lib/igp/base.rb
Overview
main class that runs the ping task
Defined Under Namespace
Classes: Format
Instance Attribute Summary collapse
-
#formatter ⇒ Object
readonly
the output formatter.
-
#interval ⇒ Object
readonly
number of seconds to wait between pings (default = 5).
-
#limit ⇒ Object
readonly
number of pings to perform (nil => infinite).
-
#options ⇒ Object
readonly
holds the parsed options.
-
#ping_handler ⇒ Object
readonly
the Net::Ping handler for the specific protocol required.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Base
constructor
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). -
#run ⇒ Object
main routine to run a complete ping test.
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)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/igp/base.rb', line 23 def initialize( = {}) @formatter = Format.new @options = @limit = [:limit] @interval = [:interval] || 5 case [: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
#formatter ⇒ Object (readonly)
the output formatter
7 8 9 |
# File 'lib/igp/base.rb', line 7 def formatter @formatter end |
#interval ⇒ Object (readonly)
number of seconds to wait between pings (default = 5)
13 14 15 |
# File 'lib/igp/base.rb', line 13 def interval @interval end |
#limit ⇒ Object (readonly)
number of pings to perform (nil => infinite)
11 12 13 |
# File 'lib/igp/base.rb', line 11 def limit @limit end |
#options ⇒ Object (readonly)
holds the parsed options
5 6 7 |
# File 'lib/igp/base.rb', line 5 def @options end |
#ping_handler ⇒ Object (readonly)
the Net::Ping handler for the specific protocol required
9 10 11 |
# File 'lib/igp/base.rb', line 9 def ping_handler @ping_handler end |
Instance Method Details
#run ⇒ Object
main routine to run a complete ping test
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/igp/base.rb', line 43 def run return unless ping_handler && formatter formatter.header( '# It goes PING! .. testing',[:url], (limit ? "#{limit} times - once" : nil), 'every',interval,'seconds' ) ping_count=0 while (limit.nil? || ping_count < limit) do status = ping_handler.ping? formatter.log(status,formatter.duration(ping_handler.duration),ping_handler.exception) ping_count += 1 sleep interval if (limit.nil? || ping_count < limit) end end |