Last active
December 27, 2015 10:09
-
-
Save dennispaagman/7309395 to your computer and use it in GitHub Desktop.
Small script to check the http status code of multiple app servers for a certain URL.
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 'open-uri' | |
require 'thread' | |
require 'benchmark' | |
# AppServerChecker checks the response for a specific URL on each app server. | |
# It does so by overwriting the Host header to the original hostname to trick | |
# the app server in serving the correct content. It also speeds up the check | |
# by creating a seperate thread for each request and outputs to stdout. | |
# | |
# Usage: | |
# `AppServerChecker.new('htp://www.domain.tld').run!` | |
# | |
# Example output: | |
# Checking 4 servers for http://www.springest.nl: | |
# app01.springe.st: 200 OK 470ms | |
# app02.springe.st: 200 OK 459ms | |
# app03.springe.st: 200 OK 459ms | |
# app04.springe.st: 200 OK 491ms | |
# | |
# The URL will default to www.springest.nl when none is given. | |
# | |
# You can run this script from the command line directly: | |
# `ruby check_app_servers.rb http://www.springest.nl` | |
# | |
# Change the `SERVERS` constant if you want to add or remove servers. | |
# Change the `DEFAULT_URL` constant if you want to change the default url. | |
# | |
class AppServerChecker | |
SERVERS = [ | |
'app01.springe.st', | |
'app02.springe.st', | |
'app03.springe.st', | |
'app04.springe.st', | |
] | |
DEFAULT_URL = 'http://www.springest.nl' | |
attr_accessor :uri, :threads | |
def initialize(url) | |
url ||= DEFAULT_URL | |
@uri = URI.parse(url) | |
@threads = [] | |
end | |
def run! | |
puts "Checking #{SERVERS.size} servers for #{uri}:" | |
SERVERS.each do |server| | |
threads << Thread.new do | |
status = '' | |
benchmark = Benchmark.measure do | |
begin | |
open("http://#{server}#{uri.path}", 'Host' => uri.host) do |request| | |
code, message = request.status | |
status = "#{code} #{message}" | |
end | |
rescue OpenURI::HTTPError => e | |
status = e | |
end | |
end | |
puts "#{server}: #{status} #{(benchmark.real*1000).round}ms" | |
end | |
threads.each(&:join) | |
end | |
end | |
end | |
AppServerChecker.new(ARGV.first).run! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment