Created
July 1, 2013 21:10
-
-
Save zaius/5904665 to your computer and use it in GitHub Desktop.
Website monitoring with SMS notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'curb' | |
require 'yaml' | |
hosts = %w( | |
example.com | |
google.com | |
) | |
@sid = 'YOUR TWILIO SID' | |
@key = 'YOUR TWILIO KEY' | |
def send_sms text | |
params = { | |
From: 'YOUR TWILIO NUMBER', | |
To: 'YOUR CELL PHONE NUMBER', | |
Body: text | |
} | |
url = "https://#{@sid}:#{@key}@api.twilio.com/2010-04-01/Accounts/#{@sid}/SMS/Messages" | |
Curl.post url, params | |
end | |
file = '/tmp/last_responses' | |
last_responses = YAML.load_file(file) if File.exist?(file) | |
last_responses ||= {} | |
hosts.each do |host| | |
last_responses[host] ||= 200 | |
result = Curl.get(host) { |c| c.follow_location = true } | |
code = result.response_code | |
send_sms "#{Time.now.utc}: #{host} response code #{code}" if code != last_responses[host] | |
last_responses[host] = code | |
end | |
File.open(file, 'w') { |f| f.write last_responses.to_yaml } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assuming debian/ubuntu. | |
# Change this path to wherever you put the hostcheck.rb file | |
chmod u+x /usr/local/bin/hostcheck.rb | |
gem install curb | |
# This will run every 5m. Change the 5 to whatever frequency you want the checks run. | |
cat << EOF | sudo tee /etc/cron.d/hostcheck | |
*/5 * * * * `whoami` /usr/local/bin/hostcheck.rb | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment