Class: Recmon::WebSensor

Inherits:
Sensor
  • Object
show all
Defined in:
lib/recmon/web-sensor.rb

Overview

WebSensor periodically checks that a website is accessible

Instance Method Summary collapse

Methods inherited from Sensor

#check

Constructor Details

#initialize(name, url, freq = 60) ⇒ WebSensor

Create a new WebSensor with the given name, to check the url periodically to check if the website is available



12
13
14
15
# File 'lib/recmon/web-sensor.rb', line 12

def initialize(name, url, freq=60)
  super(name, freq)
  @url = URI.parse(url)
end

Instance Method Details

#senseObject

Called by the Monitor, #sense checks for an HTTP 200 OK response from the URL.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/recmon/web-sensor.rb', line 18

def sense()
  begin
    q = @url.query || ""
    q += (q.length > 0 ? "&" : "?")
    q += "ts=" + Time.now().to_i().to_s()    # make request unique to prevent caching
      req = Net::HTTP::Get.new(@url.path + q)
      res = Net::HTTP.start(@url.host, @url.port) {|http|
        http.request(req)
      }
    up = (res.code == '200')
  rescue
    up = false
  end
  status = up ? "up" : "down"
  return("site=#{@name} status=#{status}")
end