Method: ISPMonitor::Checks::PingCheck#check

Defined in:
lib/isp_monitor/checks/ping_check.rb

#checkObject



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