Class: ISPMonitor::Checks::PingCheck

Inherits:
BaseCheck
  • Object
show all
Defined in:
lib/isp_monitor/checks/ping_check.rb

Instance Attribute Summary collapse

Attributes inherited from BaseCheck

#config, #interval, #name

Instance Method Summary collapse

Methods inherited from BaseCheck

#run

Constructor Details

#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

#countObject (readonly)

Returns the value of attribute count.



3
4
5
# File 'lib/isp_monitor/checks/ping_check.rb', line 3

def count
  @count
end

#targetObject (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

#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