Created
March 15, 2012 06:32
-
-
Save ulfmagnetics/2042498 to your computer and use it in GitHub Desktop.
Simple site pinging script in ruby
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
URL = "http://www.lumosity.com" | |
MATCH_STRING = "Start Training" | |
INTERVAL = 30 | |
DEBUG = false | |
def log(msg) | |
puts "[#{Time.now}] #{msg}" | |
end | |
def log_response(response) | |
puts "----------------------------" | |
puts response | |
puts "----------------------------" | |
end | |
def debug(msg) | |
log(msg) if DEBUG | |
end | |
def ping(cmd) | |
resp= `#{cmd}` | |
status_line = resp.split("\n").grep(/Status: \d{3}/) | |
status = resp.match(/Status: \d{3}/)[0].split(" ").last.to_i | |
@@counts[status] = (@@counts[status] || 0) + 1 | |
if status == 200 | |
debug "200 OK received :)" | |
elsif (300..399).include?(status) | |
log "ERROR: 30x code received!" | |
log_response(resp) | |
elsif (400..499).include?(status) | |
log "ERROR: 40x code received!" | |
log_response(resp) | |
elsif (500..599).include?(status) | |
log "ERROR: 50x code received!" | |
log_response(resp) | |
else | |
log "ERROR: no status code received" | |
end | |
rescue StandardError => ex | |
log "ERROR: exception while parsing response: #{ex.inspect}" | |
end | |
cmd = 'curl -0v -H "Cache-Control: no-cache, max-age=0" -H "User-Agent: LumosLabsPinger/1.0 (1)" -H "X-Newrelic-Ignore: true" ' + URL + ' 2>&1' | |
debug cmd | |
@@counts = {} | |
start = Time.now | |
log "Starting up pinger for URL #{URL} with interval #{INTERVAL} seconds" | |
while true do | |
begin | |
ping(cmd) | |
sleep(INTERVAL) | |
rescue SystemExit, Interrupt | |
puts "Exiting after #{(Time.now - start).ceil} seconds..." | |
puts @@counts.inspect | |
exit | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment