Last active
November 12, 2015 16:19
-
-
Save rkachowski/daba40aed9c31b4b5f38 to your computer and use it in GitHub Desktop.
upd - micropingdom
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
gem "pony" |
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
GEM | |
specs: | |
mail (2.6.3) | |
mime-types (>= 1.16, < 3) | |
mime-types (2.6.1) | |
pony (1.11) | |
mail (>= 2.0) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
pony | |
BUNDLED WITH | |
1.10.6 |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>EnvironmentVariables</key> | |
<dict> | |
<key>GEM_HOME</key> | |
<string>/Users/donaldhutchison/.rvm/gems/ruby-2.2.2</string> | |
<key>GEM_PATH</key> | |
<string>/Users/donaldhutchison/.rvm/gems/ruby-2.2.2:/Users/donaldhutchison/.rvm/gems/ruby-2.2.2@global</string> | |
<key>HOME</key> | |
<string>/Users/donaldhutchison</string> | |
<key>PATH</key> | |
<string>/usr/local/bin:/usr/local/sbin:/Users/donaldhutchison/.rvm/gems/ruby-2.2.2/bin:/Users/donaldhutchison/.rvm/gems/ruby-2.2.2@global/bin:/Users/donaldhutchison/.rvm/rubies/ruby-2.2.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/donaldhutchison/.rvm/bin</string> | |
</dict> | |
<key>Label</key> | |
<string>local.job</string> | |
<key>ProgramArguments</key> | |
<array> | |
<string>/usr/bin/env</string> | |
<string>bundle</string> | |
<string>exec</string> | |
<string>ruby</string> | |
<string>upd.rb</string> | |
<string>run</string> | |
</array> | |
<key>RunAtLoad</key> | |
<true/> | |
<key>StandardErrorPath</key> | |
<string>/tmp/local.job.err</string> | |
<key>StandardOutPath</key> | |
<string>/tmp/local.job.out</string> | |
<key>StartInterval</key> | |
<integer>300</integer> | |
<key>WorkingDirectory</key> | |
<string>/Users/donaldhutchison/workspace/upd</string> | |
</dict> | |
</plist> | |
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 "pony" | |
require 'net/http' | |
require 'json' | |
CONFIG_PATH = File.expand_path "~/.upd" | |
LOG_PATH = File.expand_path "./upd.log" | |
DEFAULT_CONFIG = <<END | |
{ | |
"email":"add email address here", | |
"failing":[], | |
"urls":[ | |
"http://google.de" | |
] | |
} | |
END | |
module Upd | |
@@command = "" | |
@@args = [] | |
module Util | |
def self.call url | |
res = Net::HTTP.get_response URI(url) | |
res.code | |
end | |
def self.pass url | |
pass_message = "#{url} returned 200" | |
$logger.info pass_message | |
if $config["failing"].include? url | |
$config["failing"].delete url | |
write_out | |
send_email pass_message | |
end | |
end | |
def self.fail url, code | |
fail_message = "Received status code #{code} from #{url}" | |
$logger.error fail_message | |
unless $config["failing"].include? url | |
$config["failing"] << url | |
write_out | |
send_email fail_message | |
end | |
end | |
def self.write_out | |
f = File.open(CONFIG_PATH, "w") | |
f << $config.to_json | |
f.close | |
end | |
def self.send_email message | |
email = $config["email"] | |
$logger.info "Sending email to #{email}" | |
Pony.mail(:to => email, :from => "upd-mailer@coolmail.com",:subject => message, :body => message) | |
end | |
end | |
def self.parse_args | |
@@command = ARGV[0] | |
@@args = ARGV[1..ARGV.length] | |
end | |
def self.help | |
puts "Help text" | |
exit | |
end | |
def self.check_url url | |
code = Util.call url | |
unless code == "200" | |
Util.fail url, code | |
else | |
Util.pass url | |
end | |
end | |
def self.parse_config | |
create_config unless File.exists? CONFIG_PATH | |
$config = JSON.parse File.read(CONFIG_PATH) | |
end | |
def self.create_config | |
f = File.open(CONFIG_PATH, "w") | |
f << DEFAULT_CONFIG | |
f.close | |
end | |
def self.create_logger | |
if @@args.include? "--stdout" | |
$logger = Logger.new STDOUT | |
else | |
$logger = Logger.new LOG_PATH | |
end | |
end | |
def self.run | |
$config["urls"].each do |url| | |
check_url url | |
end | |
end | |
def self.logs | |
puts File.read(LOG_PATH) | |
end | |
def self.main | |
parse_args | |
parse_config | |
create_logger | |
help unless self.methods.include? @@command.to_sym | |
send @@command.to_sym | |
end | |
end | |
Upd.main | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment