EC2 Monitoring with Raspberry Pi

I’ve been doing a little Raspberry Pi hacking lately, and put together a neat way to have physical status LEDs on your desk for things like EC2 instances.

The Hardware

In its most basic form, you can simply hook up an LED and a bias resistor between a ground line and a GPIO line on the Pi, but that doesn’t scale especially well – You can run out of GPIO lines pretty quickly, especially if you’re doing different colors for each status. Plus, it’s not overly elegant.

The solution? Unicorns!

No, really. The fine folks at Pimoroni in Sheffield, UK have made a lovely little HAT device for the Pi called a Unicorn. Its primary purpose is lots of blinky lights to make pretty rainbows and stuff, hence the name. However, this HAT is a 4×8 (or an 8×8) array of RGB LEDs, addressable via the I2C bus, which doesn’t eat up a line per LED (good thing, otherwise it would require 96 analog lines). The unicornhat library (python3-unicornhat) is available for Python 2 and Python 3 in the Raspbian repo. When installed onto the Pi, the Unicorn will fit within a standard Raspberry Pi case.

The Code

This is my first foray into Python, so there was a bit of a learning curve. If you’re familiar with object-oriented code concepts, this should be easy for you. Python is much more parsimonious with punctuation than PHP or perl are.

For accessing the EC2 data, we’ll need Amazon’s boto3 library, also available in the Raspbian repo (python3-boto). One area where boto3 is really nice is that the data is returned directly as a dict object (what users of other languages would call an array), so you don’t have to mess with converting JSON or XML into an object structure, and it can be manipulated as you would any other associative array (or a hash for you old-timers that use perl). AWS returns a fairly complex object, so you kind of have to dig into it via a few iterative loops to extract the data you’re after.

From there, it’s a matter of assigning different RGB values to the states. I chose these ones:

  • stopped: red
  • pending: green
  • running: blue
  • stopping: yellow(ish)

I also discovered that I needed to assign a specific pixel to each instance ID, otherwise they tended to move around a bit depending on what order AWS returned them on a particular request.

Here’s what the second iteration looks like in action:

import boto3 as aws
import unicornhat as unicorn
import time

# Initialize the Unicorn
unicorn.clear()
unicorn.show()
unicorn.brightness(0.5)

# Create an EC2 object 
ec2 = aws.client('ec2')

# Define colors and positions
color = {}
color['stopped']={'red':255,'green':0,'blue':0}
color['pending']={'red':64,'green':255,'blue':0}
color['running']={'red':32,'green':32,'blue':255}
color['stopping']={'red':192,'green':128,'blue':32}
	
pixel = {}
pixel['i-0fa4ea2560aa17ffd']={'x':0,'y':0}
pixel['i-06b95cd864acb1a8c']={'x':0,'y':1}
pixel['i-0661da0f50ffb604c']={'x':0,'y':2}
pixel['i-063ec151e0f44ef9b']={'x':0,'y':3}
pixel['i-02c514ca567d8a033']={'x':0,'y':4}

# Loop until forever
while True:

	response = ec2.describe_instances()
		
	
	statetable = {}
	resarray = response['Reservations']
	for res in resarray:
		instarray = res['Instances']
		for inst in instarray:
			iid = inst['InstanceId']
			state = inst['State']['Name']
			# print(iid)
			# print(state)
			statetable[iid] = state
	
	
	for ec2inst in statetable:
		x = pixel[ec2inst]['x']
		y = pixel[ec2inst]['y']
		r = color[statetable[ec2inst]]['red']
		g = color[statetable[ec2inst]]['green']
		b = color[statetable[ec2inst]]['blue']
		# print(x,y,r,g,b)
	
		unicorn.set_pixel(x,y,r,g,b)
		unicorn.show()


	time.sleep(1)

For the moment, this is just monitoring EC2 status, but I’m going to be adding checks in the near future to do things like ping tests, HTTP checks, etc. Stay tuned.

Balloonar Eclipse!

Here in Kansas City, we’re on the very edge of the eclipse path, and it was a beautiful night for a balloon flight too. (KOJC 210053Z 34006KT 10SM CLR 22/09 A3013 RMK AO2 SLP196 T02170094). As I was out looking at the eclipse, along comes Jason Jones of Old World Balloonery with a load of passengers, and I got a balloonar eclipse as a bonus.

My crappy phone camera gave me neat lens flare showing the eclipse in progress next to the balloon:

 

Energy-Efficient Lighting: LED Bulbs

The biggest electrical energy drain in your home is usually lights. It goes without saying that if you can address that area, you’ll be a whole lot happier when the electric bill comes.

To this end, we’ve been replacing the lights in our home, starting with CFL a few years ago.Rather than an all-out replacement campaign, we simply replaced bulbs as they went out, under the premise that those are the ones that are on most of the time, and consequently consume more energy. There are several lights in our house that are incandescent, but are rarely used. As such, changing them is not a cost-effective proposition, given the higher cost of the bulbs. There are other areas in the house that are on dimmers, so CFL was not an option there.

One area where I absolutely love the CFL’s usually annoying habit of having to warm up to full brightness over a few minutes? The master bathroom. The overhead light there needs to be reasonably bright, but first thing in the morning, a 100W bulb is an assault on the senses. The 13W CFL there now warms up gently (slower on colder mornings) and isn’t quite so rough when you wake up.

Even CFL bulbs have a limited lifetime (about 2-3x that of incandescents), so those are starting to die now, and need to be replaced. Disposal of fluorescent lights is problematic due to a number of toxic substances involved. Over the past few years, LED bulbs have become a much more mature technology, and the price has come down substantially. Most residential LED bulbs are expected to last 20 years.

Where I’m using LED bulbs in my house:

Kitchen Track Lighting. Gradually replacing the eight halogen GU10 bulbs. So far, I’ve got three 4W Philips LED bulbs from Home Depot ($30). They’re just as bright as the 50W halogens, and they don’t emit any IR to speak of (the halogens would melt butter on the counter!). Net Cost savings over the 10-year lifetime of eight bulbs: around $1500.

LED StackMaster Bedroom. Just got a set of four Dimmable 8W LED lamps from LED Liquidators (please ignore the horrible web design – it seems to be a common problem with online LED retailers) that claim to be equivLED Stack Bulb in Ceiling fanalent to 60W. These go in the ceiling fan and are ideally suited to that application – they’re on a lot, on a dimmer, and the fan has a lot of vibration, which is really hard on incandescents. Net cost savings over 20-year lifetime of the bulbs: $800.

And if you’re into that sort of thing, it also reduces your carbon footprint.

Update, December 2010: All four of these bulbs have failed – they’ve got entire strips of LEDs that are flickering or flat out not working. An e-mail to customer service in October went unanswered. Called by phone and they said “customer service will call back and take care of it”. We shall see.

An interesting look at the Canadian economy

Take a look at this data coverage map from Sprint. Orange is EV-DO, Yellow is 1xRTT:

Wireless Data Coverage to the north

That spot with lots and lots of EV-DO, disproportionate to the rest of the country? That’s Alberta. The entire province is a mere 3.5 million people.

The isolated (but large) blob in the northeastern part of the province is centered on Fort McMurray, population 47,000. The EVDO coverage area is probably several thousand square miles.

Three guesses where all the oil is 🙂

Doh!

So, the rolling hotspot went dead just north of Wichita. All due to a dead battery.

It seems the Mogul, when running WiFi and EVDO, draws more power than my 12V charger can provide. I shoulda brought the 110V charger that came with the unit and plugged it into the power strip in the car. Once we got to the hotel, I borrowed one from Jason Lee, and battery levels increased when running WMWifiRouter on the AC adapter. I think the culprit is the cheap 12V adapter I got at Wal-Mart… it was $7, instead of the $40 that Sprint wanted. It was labeled as a BlackBerry charger, so my guess is there’s a current limiting circuit in there designed to keep the BB from incinerating itself, but is insufficient to power a Mogul running at full bore. I checked on JLee’s charger, and it sources 5V/1A, which is a pretty serious amount of juice.

Liveblogging from the road!

As promised, I’m rolling down I-35, chatting on IRC, and having a webcam chat over MSN with my dad (who uses a Linksys EVDO router for his access at home). Matt is hacking code from his laptop. Clif is paying attention to the road. Since we’re gonna be on interstate highway the whole way, we can pretty much count on a solid EVDO connection the whole way.

I’ve got a running ping going to 4.2.2.2 (a public DNS server). It’s interesting to watch the ping times start to get a little long, then we lose a packet, and then the ping times drop back down to the low 100s. I’m guessing those are tower handoffs. The fact that it works at all is nothing short of miraculous.

Recipe For a Road Trip

Half the Resurrection IT crew is getting into the LovingWaiterMobile and hitting the road tomorrow for MinistryTECH/Spring RoundTable. Being geeks, we need net on the go. Here’s how:

Take one Sprint Mogul (or equivalent):

Add the following:

Version 3.0 Rev.A/GPS/WiMo 6.1 firmware update
WMWiFiRouter software
Google Maps for Windows Mobile
An external power source (not strictly necessary, but highly recommended if you’re going to do this for more than about 20 minutes)

Stir carefully and set up the ad-hoc connection on your client devices, and connect.

The result:

Beats the pants off this for sheer elegance, while providing the same functionality:

So where does Google Maps come in? Simple – for GPS Navigation. Which it can do while dishing up wi-fi to our laptops. Now we just need something that will upload our position to a live map 🙂

In Honour of Pi Day

I present to you the Cadaeic Cadenza. This both math nerdy and word nerdy all in the same breath. It’s a magnificent piece of work.

I’m glad Mike keeps his page updated, but he should really put it somewhere other than AOL 🙂

In 7 years, we get a few extra digits on Pi day… 3/14/15. I guess April 4 is sort of a Pi day as well.

Ian And His Amazing Electric Penguins!

Today, we put up our Christmas tree, which the girls enjoyed immensely. I took on the task of polishing the Wallace silver bells, which have been a family tradition since I was a wee lad. We have the annual bell from 1981 through 1991.

While I was on the subject of polishing silver, my thoughts turned to my penguins, a set that I got from Restoration Hardware about 10 years ago. I have 2 of them that hold a small dish, and another 2 that hold a pair of candles each. They’re cast brass with silver plate, and the silver is badly tarnished. A few years ago, I tried to polish them using silver polish, but that required enough elbow grease to lube a semi, so I gave up on that idea.

So, I googled for a way to do it by soaking them in something. I found a tip that involved lining a container with aluminum foil and making a solution of hot water, salt, and banking soda. After about 5 minutes, I pulled the first penguin out of the soup, and wiped off a swath of tarnish with a light pass of my thumb. “Whoa,” I say, and start attacking the bird with a cloth. I replenish the salt and baking soda and put it in for a while longer. By the time I got done wiping it the second time, it was back to its original shiny glory. I am excited.

<nerd>

Since I’m a big ol’ geek, I start to wonder what the chemistry of this is, and how damaging it is to the silver, as the page where I found it recommended not going overboard with it on silverplated items.

Luckily, my dad shows up on IM and I ask him. I’m not even as big a geek as my parents are, if you can believe that. Both my parents have graduate degrees in scientific fields, and my mom has a PhD as well. My grandfather on my dad’s side was a metallurgist, so my dad more than likely knew about this particular trick.

Sure enough, he tells me that this was a trick his dad used all the time. I figure if a metallurgist used this, it can’t be all bad. As it turns out, unlike using silver polish, which removes the tarnished silver altogether, this process retains all the original silver.

Science Content

Tarnished metals are usually sulfides. In this case, the tarnish is silver sulfide (Ag2S). The solution described above sets up a cathodic battery reaction, wherein the alkaline water (due to the baking soda) is the electrolyte. The salt isn’t strictly necessary, but it may help the electrolytic process.

Here’s where the magic happens. In a alkaline solution, the aluminum is electrochemically more aggressive than the silver, and actually goes in and displaces the silver in the tarnish and replaces it with aluminum atoms, forming aluminum sulfide (Al2S3). The silver atoms then return back to the plating on the object and the aluminum sulfide wipes off quite easily.

When I dumped the water out, it stunk of sulfur, with an odor that was very similar to methyl mercaptan, which is what they use to make natural gas smell bad. Given that we have a natgas stove, furnace, water heater, and dryer, I have a strong suspicion where the sulfur that tarnished the penguins came from 🙂 Removing the foil showed a lot of pitting where the metal had been removed to go join its sulfur buddies.

For grins, I stuck a voltmeter with the anode probe on the aluminum and the cathode probe in the water, and it started out at about .3V, and it steadily kept climbing. After about half an hour, the meter was reading about half a volt.

So, I got to have science fun, AND get my penguins shiny.