A Perl script to control an APC 9606 non-interactively
A while back i bought an APC 9606 on eBay, because i needed a way to cycle my DSL modem when it hung. It tends to hang when the line drops temporarily and as i'm not always around to do this manually, i needed a way for this to happen automagically.
My server no longer has 2 serial ports and one is used by the UPS, so i decided to try SNMP/Web. Getting this set up wasn't too hard, but i found no way to control the APC via SNMP. I could connect and snmpwalk, but didn't see any MIBs that would do the trick. As my SNMP knowledge was very limited, and i had the feeling i needed some proprietary code to access the MIBs, i decided to give the HTTP web interface a whirl.
I used the good old "Live Headers" Firefox plugin to see what got posted as i toggled things on the web interface. Unfortunately it was rather tedious to figure out what field did what, so it became a trial and error ordeal. Turn off outlet 1, look at the post. Turn on outlet 1 look at the post. Reboot outlet 1... You get the picture.
Obviously i tried to find documentation on the web but didn't find anything in that regard. This is why i posted this in the hope that i might save someone else some grunt work time.
The script works as follows
Performs action on given outlet. -o Outlet to reboot [1-8] -a Action to perform [off,on,reboot] Usage: apctoggle -o 2 -a reboot
The reboot is immediate. If you want the delayed actions feel free to add them.
The script uses cURL to do the posts, so in addition to Perl you need cURL installed. I also use Getopt::Std, so you'll need that.
So as mentioned above, to turn off outlet number 5 you issue
apctoggle -o 5 -a off
To turn it back on issue
apctoggle -o 5 -a on
Ok, enough verbosity. Here's the actual script.
I plugged this in using a cron job that pings my gateway and cycles the modem if the ping fails. The side effect is that i get an email notifying me, which helps documentation purposes. This is the crontab line i use
5,15,25,35,45,55 * * * * ping -c1 10.0.0.1 > /dev/null || (echo "line down"; /usr/local/sbin/apctoggle -o8 -a reboot )
The crontab line assumes that you stored acptoggle under /usr/local/sbin and made it executable.