Class: ISPMonitor::Checks::DownloadCheck

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

Constant Summary collapse

DEFAULT_URL =
"https://gist.githubusercontent.com/khaykov/a6105154becce4c0530da38e723c2330/raw/41ab415ac41c93a198f7da5b47d604956157c5c3/gistfile1.txt"

Instance Attribute Summary collapse

Attributes inherited from BaseCheck

#config, #interval, #name

Instance Method Summary collapse

Methods inherited from BaseCheck

#run

Constructor Details

#initialize(config) ⇒ DownloadCheck

Returns a new instance of DownloadCheck.



6
7
8
9
# File 'lib/isp_monitor/checks/download_check.rb', line 6

def initialize(config)
  super(config)
  @url = config.fetch(:url, DEFAULT_URL)
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



2
3
4
# File 'lib/isp_monitor/checks/download_check.rb', line 2

def url
  @url
end

Instance Method Details

#checkObject



11
12
13
# File 'lib/isp_monitor/checks/download_check.rb', line 11

def check
  log_event 'download-speed', download(url)
end

#download(url) ⇒ Object



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/download_check.rb', line 15

def download(url)
  uri = URI(url)

  start_at = nil
  end_at = nil
  size_bytes = 0

  res = Net::HTTP.start(uri.hostname, use_ssl: uri.scheme == "https") do |http|
    req = Net::HTTP::Get.new(uri)
    start_at = Time.current

    http.request(req) do |res|
      res.read_body do |chunk|
        size_bytes += chunk.bytesize
      end
      end_at = Time.current
    end
  end

  duration_s = end_at - start_at
  bits = size_bytes * 8
  throughput_bps = bits / duration_s
  size_mb = size_bytes / 1024.0 / 1024.0
  throughput_mbps = throughput_bps / 1000.0 / 1000.0

  {
    url:,
    hostname: uri.hostname,
    duration_s:,
    size_bytes:,
    size_mb: size_mb.round(2),
    throughput_bps: throughput_bps.round(2),
    throughput_mbps: throughput_mbps.round(2),
    response_code: res.code,
    response_headers: res.to_hash,
  }
end