-
Notifications
You must be signed in to change notification settings - Fork 9
/
check-web-apps
executable file
·65 lines (60 loc) · 1.45 KB
/
check-web-apps
1
2
3
4
5
6
7
8
9
10
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env ruby
require File.expand_path(File.dirname(__FILE__) + '/shared')
require 'uri'
require 'net/http'
require 'net/https'
def check(web_app)
attempts = 0
begin
response = http_get(web_app['url'], web_app['username'], web_app['password'])
rescue SocketError => e
if attempts < 3
attempts += 1
sleep 1
retry
else
notify_down(web_app, e)
response = nil
end
rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, Timeout::Error => e
notify_down(web_app, e)
response = nil
end
if response && !response.body.include?(web_app['substring'])
notify_down(web_app)
end
rescue => e
STDERR.puts "An exception occurred while checking for #{web_app['url']}"
raise e
end
def notify_down(web_app, exception = nil)
domain = URI.parse(web_app['url']).host
message = "A web application is down!\n"
if exception
message << "Exception: #{exception}\n"
end
message << "URL: #{web_app['url']}\n"
email(config(:from), config(:to),
sprintf("#{config(:subject)}", domain),
message)
end
def http_get(url, username = nil, password = nil)
headers = {}
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == 'https'
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
http.start do
req = Net::HTTP::Get.new(uri.path, headers)
req.basic_auth(username, password) if username
http.request(req)
end
end
def start
config(:web_apps).each do |web_app|
check(web_app)
end
end
start