Class: MultiPing::Processor

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Processor

attr_accessor :results



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/multi_ping.rb', line 17

def initialize(options={})
  @count = options.fetch :count, MultiPing::DEFAULT[:count]
  @count = @count.to_i if @count.kind_of? String
  @parallel = options.fetch :parallel, MultiPing::DEFAULT[:parallel]
  @verbose = options.fetch :verbose, false
  @threshold = options.fetch :threshold, @count
  @threshold = @threshold.to_i if @threshold.kind_of? String
  @hosts = options.fetch :hosts, []
  @results = {}
  @queue = Queue.new
  @hosts.each { |host| @queue << host }
end

Instance Method Details

#processObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/multi_ping.rb', line 31

def process
  workers = (0..@parallel.to_i).map do
    Thread.new do
      begin
        while host = @queue.pop(true)
          host = host.gsub("\n", '')
          icmp = Net::Ping::ICMP.new(host)
          rtary = []
          pingfails = 0
          # puts 'starting to ping'
          (1..@count).each do
            if icmp.ping
              rtary << icmp.duration
            else
              pingfails += 1
              if pingfails >= @threshold
                pingfails = @count # make it all failed
                break
              end
            end
          end
          if pingfails < @count
            avg = rtary.inject(0) {|sum, i| sum + i} * 1000/(@count - pingfails)
            quality = pingfails == 0 ? "Perfect!":"#{pingfails} within #{@count} packets were droped"
            puts "#{host} | Response Time : #{avg.round(2)} ms | #{quality}"  if @verbose
            @results[host] = [avg, (@count - pingfails) * 100.0 / @count]
          else
            @results[host] = [9999, 0.0]
          end
        end
      rescue ThreadError
      end
    end
  end
  workers.map(&:join)
  self
end

#resultsObject



69
70
71
# File 'lib/multi_ping.rb', line 69

def results
  @results.sort_by{|k,v| v[1] * - 100 - 100 / v[0]}
end