Last active
December 9, 2015 22:59
-
-
Save progrium/4341468 to your computer and use it in GitHub Desktop.
Simple, extendable alerting system to run on Heroku
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 bash | |
curl \ | |
-X 'POST' \ | |
-F "[email protected]" \ | |
-F "[email protected]" \ | |
-F "subject=Website is down!" \ | |
-F "text=Check failed with: $(cat)" \ | |
-F "api_user=$SENDGRID_USERNAME" \ | |
-F "api_key=$SENDGRID_PASSWORD" \ | |
--silent --fail "https://sendgrid.com/api/mail.send.json" |
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 bash | |
curl --silent --fail http://example.com |
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
source 'https://rubygems.org' | |
gem 'clockwork' |
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
watcher: bundle exec clockwork watcher.rb |
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
require 'clockwork' | |
require 'fileutils' | |
include Clockwork | |
include FileUtils | |
Clockwork.configure do |config| | |
config[:logger] = Logger.new(STDOUT) | |
config[:logger].level = Logger::ERROR | |
end | |
def mark_pass(check); chmod File.stat(check).mode & ~0003, check; end | |
def mark_fail(check); chmod File.stat(check).mode | 0007, check; end | |
def mark_alerted(check); chmod File.stat(check).mode | 0070, check; end | |
def marked_alerted?(check) | |
`stat -c %A #{check} | sed 's/......\\(.\\).\\+/\\1/'` == "x\n" | |
end | |
def executables(glob) | |
Dir[glob].select {|path| File.executable? path } | |
end | |
mkdir_p 'output' | |
mkdir_p 'checks' | |
mkdir_p 'alerts' | |
executables('checks/*').each do |check| | |
interval, name = File.basename(check).split('.', 2) | |
puts "loading check #{name} for every #{interval} seconds" | |
every interval.to_i.seconds, name do | |
puts "checking #{name} (#{File.stat(check).mode.to_s(8)})" | |
`#{check} > output/#{name} 2>&1` | |
status = $?.exitstatus | |
if status.zero? | |
mark_pass check | |
else | |
puts " #{name} check failed with status #{status}" | |
mark_fail check | |
if not marked_alerted? check | |
executables('alerts/*').each do |alert| | |
puts " sending #{File.basename(alert)} alert for #{name}" | |
`cat output/#{name} | #{alert} #{name} #{status} > /dev/null 2>&1` | |
if $?.exitstatus.zero? | |
mark_alerted check | |
end | |
end | |
end | |
end | |
end # every | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment