lib/igp/base.rb in igp-0.0.3 vs lib/igp/base.rb in igp-1.0.0

- old
+ new

@@ -1,8 +1,7 @@ # main class that runs the ping task class Igp::Base - # holds the parsed options attr_reader :options # the output formatter attr_reader :formatter # the Net::Ping handler for the specific protocol required @@ -25,60 +24,58 @@ @options = options @limit = options[:limit] @interval = options[:interval] || 5 case options[:type] when :icmp - @ping_handler = Net::Ping::External.new(@options[:host],@options[:port]) + @ping_handler = Net::Ping::External.new(@options[:host], @options[:port]) when :udp - @ping_handler = Net::Ping::UDP.new(@options[:host],@options[:port]) + @ping_handler = Net::Ping::UDP.new(@options[:host], @options[:port]) when :tcp - @ping_handler = Net::Ping::TCP.new(@options[:host],@options[:port]) + @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]) + # TODO: LDAP was retired from net-ping. to add back in one way or another + # when :ldap, :ldaps + # @ping_handler = Net::Ping::LDAP.new(@options[:url]) end end # main routine to run a complete ping test def run return unless ping_handler && formatter + formatter.header( - '# It goes PING! .. testing',options[:url], + '# It goes PING! .. testing', options[:url], (limit ? "#{limit} times - once" : nil), - 'every',interval,'seconds' + 'every', interval, 'seconds' ) - ping_count=0 - while (limit.nil? || ping_count < limit) do + ping_count = 0 + while limit.nil? || ping_count < limit status = ping_handler.ping? - formatter.log(status,formatter.duration(ping_handler.duration),ping_handler.exception) + formatter.log(status, formatter.duration(ping_handler.duration), ping_handler.exception) ping_count += 1 - sleep interval if (limit.nil? || ping_count < limit) + sleep interval if limit.nil? || ping_count < limit end end # handle output formating tasks class Format + TIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%LZ'.freeze - # the time format - in 1.8 strftime doesn't understand milliseconds - TIME_FORMAT = (RUBY_VERSION =~ /^1\.9/) ? "%Y-%m-%dT%H:%M:%S.%LZ" : "%Y-%m-%dT%H:%M:%SZ" - # prints the header structure to STDERR def header(*args) $stderr.puts(args.compact.join(' ')) end - + # logs ping result to STDOUT # +args+ is an array of values to log def log(*args) - $stdout.puts(([Time.now.utc.strftime( TIME_FORMAT )] + args).join(',')) + $stdout.puts(([Time.now.utc.strftime(TIME_FORMAT)] + args).join(',')) $stdout.flush end - + # formats the duration for output. nil duration remains nil def duration(duration) - "%.6f" % duration if duration + format('%.6f', duration) if duration end - end - -end \ No newline at end of file +end