Class: Watcher::HttpWatcher

Inherits:
Base show all
Defined in:
lib/watcher/http_watcher.rb

Overview

Checks an http connection if it is active and returns the expected results.

Options

url

The URL to query (required)

response

The response code that is expected from the operation

content_match

A regular expression that is matched against the result. The watcher fails if the expression doesn’t match

timeout

The timeout for the connection attempt. Defaults to 10 sec

falloff

If a successful connection is made, this is subtracted from the internal severity. (Default: 100, completely reset previous failures)

If neither response nor content_match are given, the watcher will expect a 200 OK response from the server.

This watcher resets the current severity on each successful connect, so that only continuous failures count against the trigger condition - see the falloff option.

Instance Attribute Summary

Attributes inherited from Base

#name, #severity

Instance Method Summary collapse

Methods inherited from Base

#setup_actions

Constructor Details

#initialize(config) ⇒ HttpWatcher

Returns a new instance of HttpWatcher.



26
27
28
29
30
31
32
33
34
35
# File 'lib/watcher/http_watcher.rb', line 26

def initialize(config)
  @url = config.get_value(:url, false)
  match = config.get_value(:content_match)
  @content_match = Regexp.new(match) if(match)
  response = config.get_value(:response)
  @response = ((!response && !match) ? "200" : response)
  @timeout = config.get_value(:timeout, 10).to_i
  @falloff = config.get_value(:falloff, 100).to_i
  @current_severity = 0
end

Instance Method Details

#watch_it!Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/watcher/http_watcher.rb', line 37

def watch_it!
  url = URI.parse(@url)
  res = Net::HTTP.start(url.host, url.port) do |http| 
    http.read_timeout = @timeout
    http.get(url.path) 
  end
  test_failed = false
  if(@response && (@response.to_s != res.code))
    test_failed = "Unexpected HTTP response: #{res.code} for #{@url} - expected #{@response}"
  elsif(@content_match && !@content_match.match(res.body))
    test_failed = "Did not find #{@content_match.to_s} at #{@url}"
  end
  @current_severity = [0, @current_severity - @falloff].min unless(test_failed)
  dog_log.debug('HttpWatcher') { "Watch of #{@url} resulted in #{test_failed}" }
  test_failed
rescue Exception => e
  dog_log.debug('HttpWatcher') { "Watch of #{@url} had exception #{e.message}"}
  "Exception on connect to #{@url} - #{e.message}"
end