Class: Netchk::ICMPPingVerifier
- Inherits:
-
Object
- Object
- Netchk::ICMPPingVerifier
- Defined in:
- lib/netchk/icmp_ping_verifier.rb
Instance Method Summary collapse
-
#initialize(**options) ⇒ ICMPPingVerifier
constructor
A new instance of ICMPPingVerifier.
- #verify ⇒ Object
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(**) @hosts = ['hosts'] || %w[1.1.1.1 8.8.8.8] @count = ['count'] || 20 @interval = ['interval'] || 0.2 end |
Instance Method Details
#verify ⇒ Object
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 |