Created
May 4, 2017 20:39
-
-
Save adamjonas/978b3e054561c888769397ae065d2aea to your computer and use it in GitHub Desktop.
This file contains hidden or 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/local/rvm/rubies/ruby-2.3.3/bin/ruby | |
require 'net/http' | |
require 'json' | |
MONITORED_HOSTGROUPS = { | |
ironboard: 'Web Servers', | |
postgres: 'Database', | |
flatchat: 'Chat', | |
ide: 'IDE' | |
} | |
url = 'http://localhost:8080/state' | |
uri = URI(url) | |
response = Net::HTTP.get(uri) | |
data = JSON.parse(response) | |
host_status = data['content'].map do |host, info| | |
service_status = info['services'].map do |service, check| | |
check['current_state'].to_i | |
end | |
{ host => { host_check: info['current_state'].to_i, service_checks: service_status } } | |
end | |
def check_status(hostgroup, payload) | |
hostgroup_status = payload.map do |host| | |
status = host.map do |key, value| | |
if key.include?(hostgroup) | |
if value[:host_check] != 0 | |
"major_outage" | |
elsif value[:service_checks].reject(&:zero?).count > 0 | |
"partial_outage" | |
else | |
"operational" | |
end | |
end | |
end | |
status | |
end | |
hostgroup_status.flatten.uniq.compact | |
end | |
def status_monitor(name, hostgroup, payload) | |
status = check_status(hostgroup, payload) | |
if status.include?('major_outage') | |
service(name, '', 'Major Outage', '#E74C3C') | |
elsif status.include?('partial_outage') | |
service(name, '', 'Partial Outage', '#E67E22') | |
else | |
service(name, '', 'Operational', '#2FCC66') | |
end | |
end | |
def set_summary(summaries) | |
if summaries.include?('major_outage') | |
summary('Learn is currently experiencing a major service outage', '#FE8484', '#DC143C') | |
elsif summaries.include?('partial_outage') | |
summary('Learn is currently experiencing degraded service', '#FCBC6E', '#FF510C') | |
else | |
summary('All Learn.co services are operational', '#f3ffe1', '#719904') | |
end | |
end | |
def header | |
<<-EOF | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Learn.co Status</title> | |
<link href='//fonts.googleapis.com/css?family=Open+Sans:400,600,700,300|Droid+Sans+Mono' rel='stylesheet' type='text/css'> | |
<link rel="stylesheet" media="screen" href="assets/default.css" /> | |
<script src="assets/default.js"></script> | |
<link rel="shortcut icon" href="https://flatironschool-static.s3.amazonaws.com/favicons/favicon.ico"/> | |
<meta content='width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1' name='viewport' /> | |
</head> | |
<body> | |
<header class='siteHeader'> | |
<p class='siteHeader__subscribe'> | |
<a class='siteHeader__button' href='https://learn.co'>Go To Learn</a> | |
</p> | |
<div class='container'> | |
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" style="z-index:-100;" height="100" width="100" | |
viewBox="0 0 37.2 57.2" enable-background="new 0 0 37.2 57.2" xml:space="preserve"> | |
<g> | |
<defs> | |
<path id="SVGID_1_" d="M20.1,29.9l11.8,0L20.1,49.1L20.1,29.9z M17.1,7.9l0,19.2l-11.8,0L17.1,7.9z M17.3,1.7L1.2,27.8 | |
c-0.3,0.5-0.3,0.9,0,1.4c0.3,0.5,0.8,0.6,1.3,0.7l14.5,0l0,24.7c0,0.7,0.4,1.3,1.1,1.5c0.1,0,0.3,0.1,0.4,0.1c0.5,0,1-0.2,1.3-0.7 | |
l16.1-26.1c0.3-0.5,0.3-1.1,0-1.5c-0.3-0.5-0.8-0.8-1.3-0.8l-14.5,0l0-24.6c0-0.7-0.4-1.3-1.1-1.5C18.9,1,18.7,1,18.6,1 | |
C18.1,1,17.6,1.3,17.3,1.7"/> | |
</defs> | |
<use xlink:href="#SVGID_1_" overflow="visible" fill="#4EBFE2"/> | |
<clipPath id="SVGID_2_"> | |
<use xlink:href="#SVGID_1_" overflow="visible"/> | |
</clipPath> | |
<use xlink:href="#SVGID_1_" overflow="visible" fill="none" stroke="#4EBFE2" stroke-width="2" stroke-miterlimit="10"/> | |
</g> | |
</svg> | |
<p class='siteHeader__title'>Learn.co Status</p> | |
<p class='siteHeader__subTitle'>Learn.co is Flatiron School's online campus. This is our status site. Contact us at [email protected] with any questions or problems.</p> | |
</div> | |
</header> | |
<div class='container'> | |
EOF | |
end | |
def summary(message, background_color, summary_color) | |
<<-EOF | |
<div class='staytusDemo' style='background:#{background_color};'> | |
<p> | |
<p style='color:#{summary_color};'> | |
<b >#{message}.</b> | |
<p>If you find otherwise, please contact [email protected]. Thanks!</p> | |
</p> | |
</p> | |
</div> | |
<ul class='serviceList'> | |
EOF | |
end | |
def service(name, description, status, color) | |
<<-EOF | |
<li class='serviceList__item'> | |
<p class='serviceList__status'><span class="serviceStatusTag serviceStatusTag--minor" style="color:#{color};">#{status}</span></p> | |
<p class='serviceList__name'> | |
#{name} | |
<span class='serviceList__description'>#{description}</span> | |
</p> | |
</li> | |
EOF | |
end | |
def last_updated_at | |
<<-EOF | |
</ul> | |
</div> | |
<div style='text-align:center;margin-top:30px;'> | |
<p> | |
<b>Last Updated At:</b> #{Time.now.utc} | |
</p> | |
</div> | |
EOF | |
end | |
def footer | |
<<-EOF | |
<footer class='siteFooter'> | |
<div class='container'> | |
<ul class='siteFooter__nav'> | |
<li><a href='https://flatironschool.com'>Our Homepage</a></li> | |
<li><a href='mailto:[email protected]'>Contact Us</a></li> | |
</ul> | |
<p class='siteFooter__poweredBy'> | |
Start Learning on <a href='https://learn.co'>Learn</a> | |
</p> | |
</div> | |
</footer> | |
</body> | |
</html> | |
EOF | |
end | |
summary_of_statuses = MONITORED_HOSTGROUPS.keys.map do |hostgroup| | |
check_status(hostgroup.to_s, host_status) | |
end | |
puts header | |
puts set_summary(summary_of_statuses.flatten) | |
MONITORED_HOSTGROUPS.each do |key, value| | |
puts status_monitor(value, key.to_s, host_status) | |
end | |
puts last_updated_at | |
puts footer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment