Class: ISPMonitor::Checks::PingCheck
- Defined in:
- lib/isp_monitor/checks/ping_check.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Attributes inherited from BaseCheck
Instance Method Summary collapse
- #check ⇒ Object
-
#initialize(config) ⇒ PingCheck
constructor
A new instance of PingCheck.
Methods inherited from BaseCheck
Constructor Details
permalink #initialize(config) ⇒ PingCheck
Returns a new instance of PingCheck.
5 6 7 8 9 |
# File 'lib/isp_monitor/checks/ping_check.rb', line 5 def initialize(config) super(config) @target = config.fetch(:target) @count = config.fetch(:count, 10) end |
Instance Attribute Details
permalink #count ⇒ Object (readonly)
Returns the value of attribute count.
3 4 5 |
# File 'lib/isp_monitor/checks/ping_check.rb', line 3 def count @count end |
permalink #target ⇒ Object (readonly)
Returns the value of attribute target.
2 3 4 |
# File 'lib/isp_monitor/checks/ping_check.rb', line 2 def target @target end |
Instance Method Details
permalink #check ⇒ Object
[View source]
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/isp_monitor/checks/ping_check.rb', line 11 def check icmp = Net::Ping::ICMP.new(target, nil, 1) response_times = [] ping_fails = 0 count.times.each do begin if icmp.ping response_times << icmp.duration else ping_fails += 1 end rescue StandardError => e ISPMonitor::Logging.notify_error(e) ping_fails += 1 end end avg_rtt = if response_times.present? (response_times.sum / response_times.count).round(4) end jitter = if response_times.count >= 2 jitter_diffs = response_times.each_cons(2).map { |a,b| (a-b).abs } (jitter_diffs.sum / jitter_diffs.count).round(4) else 0 end packet_loss = (ping_fails.to_f / count.to_f * 100.0).round(2) log_event 'ping', { ping_target: target, ping_avg_rtt: avg_rtt, ping_avg_jitter: jitter, ping_packet_loss: packet_loss, ping_attempts: count, ping_fails: ping_fails } end |