Skip to content

Instantly share code, notes, and snippets.

@boy-jer
Forked from zaius/hostcheck.rb
Created July 1, 2013 21:53
Show Gist options
  • Save boy-jer/5904968 to your computer and use it in GitHub Desktop.
Save boy-jer/5904968 to your computer and use it in GitHub Desktop.
#!/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 }
# 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