Class: Netchk::ICMPPingVerifier

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

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ ICMPPingVerifier

Returns a new instance of ICMPPingVerifier.



13
14
15
16
17
# File 'lib/netchk/icmp_ping_verifier.rb', line 13

def initialize(**options)
  @hosts = options['hosts'] || %w[1.1.1.1 8.8.8.8]
  @count = options['count'] || 20
  @interval = options['interval'] || 0.2
end

Instance Method Details

#verifyObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/netchk/icmp_ping_verifier.rb', line 19

def verify
  stats = @hosts.map do |host|
    host_stats = ping(host, @count)

    average = host_stats[:durations].empty? ? 'N/A' : (host_stats[:durations].avg * 1000).round(2)
    errors = host_stats[:failures].to_f / (host_stats[:failures] + host_stats[:durations].count) * 100
    puts "Stats for #{host} ping - average: #{average} ms, error rate: #{errors.round(2)}%"

    [host, host_stats]
  end.to_h

  all_durations = stats.values.flat_map { |host_stats| host_stats[:durations] }
  overall_average = all_durations.empty? ? 'N/A' : (all_durations.avg * 1000).round(2)
  overall_errors = stats.values.flat_map { |host_stats| host_stats[:failures].to_f / host_stats[:count] * 100 }.avg
  puts "Overall stats for ping - average: #{overall_average} ms, error rate: #{overall_errors.round(2)}%"
end