Skip to content

Instantly share code, notes, and snippets.

@MaiTrinh
Forked from codatory/Gemfile
Created May 21, 2020 03:38
Show Gist options
  • Save MaiTrinh/9ebaa5ce7711c4e1812e427aa3112d90 to your computer and use it in GitHub Desktop.
Save MaiTrinh/9ebaa5ce7711c4e1812e427aa3112d90 to your computer and use it in GitHub Desktop.
AWS Status Checker
require 'nokogiri'
require 'open-uri'
require './notifier'
@notifier = EmailNotifier.new
@error_level = 0
@watched_feeds = [
'http://status.aws.amazon.com/rss/ec2-us-east-1.rss',
'http://status.aws.amazon.com/rss/rds-us-east-1.rss',
'http://status.aws.amazon.com/rss/simpledb-us-east-1.rss',
'http://status.aws.amazon.com/rss/management-console.rss'
]
def upgrade_error(new_level)
if new_level > @error_level
@error_level = new_level
end
end
@notifier.colour(BLUE)
loop do
puts "Polling... #{Time.now}"
@error_level = 0
@watched_feeds.each do |feed|
open(feed) do |rss|
xml_doc = Nokogiri::XML(rss)
latest_status = xml_doc.css("item title").first.text
case latest_status
when /^Informational message/i
upgrade_error(1)
when /^Performance issues/i
upgrade_error(2)
when /^Service disruption/i
upgrade_error(3)
end
end
end
print "Polling Completed. "
case @error_level
when 0
puts "OK"
@notifier.colour(GREEN)
when 1
puts "INFO"
@notifier.colour(YELLOW)
when 2
puts "PERF"
@notifier.colour(ORANGE)
when 3
puts "OUTAGE"
@notifier.colour(RED)
end
sleep 240
end
source :rubygems
gem 'libusb'
gem 'nokogiri'
require 'libusb'
class Colour
attr_reader :red, :green, :blue
def initialize(red, green, blue)
@red = red
@green = green
@blue = blue
end
def hex_string
hexify(@red, @green, @blue)
end
def max_val
[@red, @green, @blue].max
end
private
def hexify(*items)
items.collect! { |x| x.chr }.join
end
end
WHITE = Colour.new(64, 64, 64)
RED = Colour.new(64, 0, 0)
GREEN = Colour.new(0, 64, 0)
BLUE = Colour.new(0, 0, 64)
MAGENTA = Colour.new(64, 0, 64)
YELLOW = Colour.new(64,64,0)
ORANGE = Colour.new(64,32,0)
CYAN = Colour.new(0,64,64)
OFF = Colour.new(0, 0, 0)
class EmailNotifier
def initialize
usb_context = LIBUSB::Context.new
# The hex value for Dream Cheeky on the device. Assuming only the email notifier is attached
@device = usb_context.devices(:idVendor => 0x1d34).first
reset_device
end
def colour!(colour)
@colour = colour
send(colour.hex_string + "\x00\x00\x00\x00\x05") # The 5 extra bytes apparently need to be sent
end
def colour(colour)
return true if @colour == colour
return colour!(colour) if @colour.nil?
mix_colours(@colour,colour).each do |i|
colour!(i)
sleep 0.01
end
colour! colour
@colour = colour
end
def turn_off
colour!(Colour.new(0, 0, 0))
end
private
def send(data)
@device.open do |handle|
request_type = LIBUSB::REQUEST_TYPE_CLASS | LIBUSB::RECIPIENT_INTERFACE # flags to send data to device
# other special flags that I don't know what they do
handle.control_transfer(:bmRequestType => request_type, :bRequest => 0x09, :wValue => 0x200, :wIndex => 0x00, :dataOut => data)
end
end
# Special codes that apparently need to be sent to initialise the device.
# The first three values of the last line also represent the RGB colours on initialisation
def reset_device
send "\x1f\x02\x00\x2e\x00\x00\x2b\x03"
send "\x00\x02\x00\x2e\x00\x00\x2b\x04"
send "\x00\x00\x00\x2e\x00\x00\x2b\x05"
end
def mix_colours(c1,c2)
current_color = c1.is_a?(Colour) ? [c1.red,c1.blue,c1.green] : c1.dup
destination = c2.is_a?(Colour) ? [c2.red,c2.blue,c2.green] : c2.dup
distance = []
until current_color == destination
[0,1,2].each do |i|
if current_color[i] > destination[i]
current_color[i] -= 1
elsif current_color[i] < destination[i]
current_color[i] += 1
end
end
distance << Colour.new(current_color[0],current_color[2],current_color[1])
end
distance
end
def limit_array(array,limit=150.0)
rate = array.length / limit
array.each_slice(rate.to_i).map(&:last)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment