Last active
January 11, 2018 03:01
-
-
Save lisp-ceo/893d5c5625f1cd4c2a8f4a6891ee8da6 to your computer and use it in GitHub Desktop.
Hacky network probe script. Prints to terminal-notifier when done (it's notify-send for mac)
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
# frozen_string_literal: true | |
source "https://rubygems.org" | |
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } | |
gem "terminal-notifier" | |
gem "typhoeus" |
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 "typhoeus" | |
usage = <<-DOC | |
service_healthy | |
====== | |
Checks URL for response code. Prints to terminal-notifier when complete. | |
DOC | |
class HealthCheck | |
attr_accessor :healthy, :resp, :host, :timestamp | |
def initialize(host, healthy, resp, timestamp) | |
@host = host | |
@healthy = healthy | |
@resp = resp | |
@timestamp = timestamp | |
end | |
def healthy? | |
@healthy | |
end | |
def request_summary | |
summary = ["", "#{@host}"] | |
summary.push "======" | |
summary.push "Healthy?: #{healthy?}" | |
summary.push "Response code: #{@resp.response_code}" | |
summary.push "Response time: #{@resp.time}" | |
summary.push "Timestamp: #{@timestamp}" | |
summary.join("\n") | |
end | |
end | |
def log m, level | |
puts m if level == :info | |
end | |
def dirty_exit m, print_usage = false | |
puts m | |
puts usage if print_usage | |
exit 1 | |
end | |
def notify_healthy msg | |
`terminal-notifier -title #{"Service Healthy"} -message #{msg}` | |
end | |
def check_status url | |
t = Time.now | |
resp = Typhoeus.get(url, followlocation: true) | |
# TODO Build HealthDecisionLogic object to mediate between | |
# User-supplied health logic and HealthCheck health evaluation | |
is_200 = resp.response_code == 200 | |
health = HealthCheck.new(url, is_200, resp, t) | |
end | |
if ARGV.length == 0 | |
dirty_exit "First argument should be URL to service to monitor for 200's" | |
end | |
u_count = 0 | |
while (u = ARGV.pop; u_count += 1; u != nil) | |
log "processing #{u}", :info | |
until (health = check_status(u); health.healthy?) | |
log health.request_summary, :info | |
sleep 10 | |
end | |
notify_healthy "#{u} is now healthy" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment