ruby + x10 = crazy delicious

Posted on March 13, 2006

So I upgraded my Sprint DSL service to the 5.0 Mbps service about a month ago. At 3.0 Mbps, the service was rock solid, I never had downtime. Unfortunately, at 5.0 Mbps, that is no longer the case. My connection suffers from a condition called “Sync, No Surf.” Basically, my line is too noisy for that speed, and the DSL modem loses it’s connection, but doesn’t realize it, so it doesn’t try to reconnect, i.e. it still has “Sync”, but you can’t “Surf”.

Needless to say, this sucks. The fact that I host my own email in my basement makes it all the more annoying. I’ve called Sprint, and they sent somebody out who did something, that made it to where it only happens every few days rather than every few hours, but the line still isn’t up to snuff. I considered downgrading, but quite frankly, I really like the speed. Basically, I needed a little gnome to push the power button on the DSL modem whenever the connection was lost.

So two steps to the problem, first something to monitor whether the connection is dropped, and then something to restart the modem when it happens:

So the first player is Ruby. FreeBSD has a nifty little utility in ports called Daedalus. Daedalus simply sits in the background checking to see if things are as you want them, and if not, it runs a command. Although I haven’t had to hack it, it’s written in Ruby if I ever want to. Here’s what the relavant part of my configuration file looks like:

1
2
3
4
5
6
7

<name>dsl</name>
<checkinterval>5</checkinterval>
<checkcommand>ping -c 4 news.com</checkcommand>
<regex>/icmp_seq/</regex>
<failcommand>/root/restart_dsl_modem.rb</failcommand>
<commandtimeout>90</commandtimeout>

Pretty self explanatory. I’m not sure why I picked news.com, it’s just what I always ping to test a connection. But what’s that restart_dsl_modem.rb script…

I’m sure all of you are familiar with X10. They ran the most obnoxious pop-up/pop-under advertising campaign of all time selling their spy cameras and what not. Anyway, many years ago, they were giving out these free (plus shipping) home automation kits they called Firecracker. A little dongle that plugs into a serial port and little modules that you plug into the wall and then plug appliances into. You can still buy one here, but unfortunately they are not free anymore.

So I’ve had this box full of random X10 stuff that I’d been carting around for my last three moves, and I kept telling myself I should just throw it away, since I hadn’t used it. When I was trying to solve this DSL modem problem, I dug through the basement looking for it and was pissed cause I thought I had tossed it. I took one more look though, and it turned up, so all’s well.

Anyway, I plugged the dongle into the serial port of the FreeBSD box, and installed the x10-cm17a gem which provides an easy peasy Ruby interface to control the Firecracker module. So without further adieu, I present to you restart_dsl_modem.rb:

1
2
3
4
5
6
7
8
9
10
11

require x10/cm17a

X10.controller = X10::Cm17a::Controller.new("/dev/ttyd0")

dsl_modem = X10.device(a1)

dsl_modem.off
sleep 10
dsl_modem.on
sleep 60

Again, pretty self explanatory. I tested it out by pulling the phone cord on the DSL modem and then plugging it back in after the restart, and it worked like a champ. Here’s the Daedalus logs:


I, [2006-03-13T21:07:24.647592 #79938]  INFO -- :
    State changed from "found" on monitor dsl - the new state
    is "not found"
W, [2006-03-13T21:07:24.648690 #79938]  WARN -- :
    Regular expression /icmp_seq/ did not match the output of
    "ping -c 4 news.com" for monitor "dsl"
W, [2006-03-13T21:07:24.649623 #79938]  WARN -- :
    Running "/root/restart_dsl_modem.rb" (failcommand) for
    monitor "dsl"
I, [2006-03-13T21:08:44.315012 #79938]  INFO -- :
    State changed from "not found" on monitor dsl - the new
    state is "found"

Now to hook up ActionMailer to get notification emails, and I’ll be all set.

So geek. So awesome.

Comments
  1. asdfMarch 13, 2006 @ 9:33 PM

    Uh, why pay for the 5.0 Mbps service if they can’t provide it? Just sayin’

  2. anoopMarch 13, 2006 @ 9:33 PM

    It actually came out to the same price with the promotion they were running. I’d still pay more for it though, as the speed increase is pretty useful. I haven’t gotten off their back about actually fixing the problem though.