-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheartbeat.rb
82 lines (59 loc) · 1.77 KB
/
heartbeat.rb
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env ruby -wKU
#include
require 'net/http'
require 'net/smtp'
#options
debug = true
send_email = false
email_from = 'from@yourdomain.com'
email_to = 'to@yourdomain.com'
smtp_server = 'smtp.rdslink.ro'
read_timeout = 120 # 2 minutes
#open websites file
File.open('websites.txt', 'r') do |line|
#for every website
while website = line.gets
puts "\n\nVerifying #{website}" unless not debug
#check for comment || temporary disabled
if (website[0..0] == '#')
puts "skipping.." unless not debug
else
#add http://
website.insert(0, "http://")
#fetch HTTP_RESPONSE from the current website
begin
#response = Net::HTTP.get_response(URI.parse(website.to_s))
url = URI.parse(website.to_s)
url.path = "/" if url.path.length < 1
http = Net::HTTP.new(url.host, url.port)
#http.open_timeout = 120
http.read_timeout = read_timeout
response = http.start do |http|
http.request_head(url.path)
end
code = response.code
message = response.message
rescue Exception => e
puts "Exception occured: #{e.message}" unless not debug
code = 600
message = e.message
end
puts "Response: #{code} - #{message}" unless not debug
#check for 2xx or 3xx status
unless(code =~ /2|3\d{2}/ ) then
#email body
message = "From: #{email_from}\nTo: #{email_to}\nSubject: #{website} Unavailable\n\n#{website} : #{code} - #{message}\\n\n"
#send email
unless not send_email then
begin
Net::SMTP.start(smtp_server, 25) do |smtp|
smtp.send_message(message, email_from, email_to)
end
rescue Exception => e
puts "Fatal exception occured: " + e
end
end
end #end check status code
end #end skipped line
end #end each website
end #end file open