
Nut - a module to talk to a UPS via NUT (Network UPS Tools) upsd

use UPS::Nut;
$ups = new UPS::Nut( NAME => "myups",
HOST => "somemachine.somewhere.com",
PORT => "3305",
USERNAME => "upsuser",
PASSWORD => "upspasswd",
TIMEOUT => 30,
DEBUG => 0,
DEBUGOUT => "/some/file/somewhere");
if ($ups->Status() =~ /OB/) {
print "Oh, no! Power failure!\n";
}

This is an object-oriented (whoo!) interface between Perl and upsd from the Network UPS Tools package (http://www.exploits.org/nut/). It only does the things that it can do talking to upsd. It won't monitor your UPS continually - you'll have to write something that does that, like:
for (;;) { if ($ups->Status() =~ /OB/) { # Ack! There's a power failure! Sure wish I had written some code to # deal with that situation... } }

Shown with defaults: new UPS::Nut( NAME => "default", HOST => "localhost", PORT => "3305", USERNAME => "", PASSWORD => "", DEBUG => 0, DEBUGOUT => ""); * NAME is the name of the UPS to monitor, as specified in ups.conf * HOST is the host running upsd * PORT is the port that upsd is running on * USERNAME and PASSWORD are those specified in upsd.conf. If these aren't specified, then you will only have access to the level of privileges in upsd.conf that do not require a password. This is configured in upsd.conf. * DEBUG turns on debugging output * DEBUGOUT is de thing you do when the s*** hits the fan. Actually, it's the filename where you want debugging output to go. If it's not specified, debugging output comes to STDOUT.

Query(varname) returns value of the specified variable, if supported
BattPercent() returns % of battery left
LoadPercent() returns % of load, the UPS' available capacity
LineVoltage() returns line voltage, useful if your UPS doesn't do voltage regulation.
Status() returns status, one of "OL," "OB," or "LB," which are online (power OK), on battery (power failure), or low battery, respectively.
Temperature() returns UPS temperature
These all operate on the UPS specified in the NAME argument to the constructor.
Master() Use this to find out whether or not we have MASTER privileges for this UPS. Returns 1 if we have MASTER privileges, returns 0 otherwise.
ListVars() Returns a list of all variables supported by the UPS.
ListInstCmds() Returns a list of all instant commands supported by the UPS.
InstCmd (command) Send an instant command to the UPS. Returns undef if the command can't be completed for whatever reason, otherwise returns 1.
FSD() Set the FSD (forced shutdown) flag for the UPS. This means that we're planning on shutting down the UPS very soon, so the attached load should be shut down as well. Returns 1 on success, returns undef on failure for any reason. This cannot be unset, so don't set it unless you mean it.
Error() why did the previous operation fail? The answer is here. It will return a concise, well-written, and brilliantly insightful few words as to why whatever you just did went bang. I promise that this method will never return "Error: This module doesn't like you. Go away."
TimeLeft() at current load, how much time before battery is depleted. This isn't very accurate at loads other than 25% and 50%, (the data points that I got from CyberPower Systems) but it will give you a number.

These are things that are listed in "protocol.txt" in the Nut distribution that I haven't implemented yet. Consult "protocol.txt" (in the Nut distribution, under the docs/ subdirectory) to see what these commands do. SET ENUM VARDESC LISTRW VARTYPE INSTCMDDESC

Unless the load is at or very near 25% or 50%, this isn't accurate. According to Cyberpower, at 50% load, the battery will last approximately 40 minutes at full charge. at 25% load, the battery will last approximately 100 minutes at full charge. At loads higher than 50%, I'm told that the bettery life decreases "exponentially." So I drew a line, connecting the two points, which is described by the function Battery life (minutes) = (-2.4 * Load Percentage) + 160. The accuracy will improve as I get more data points.

Kit Peters kpeters@iname.com http://www.awod.com/staff/kpeters/perl/

Developed with the kind support of A World Of Difference, Inc. <http://www.awod.com/>
Many thanks to Ryan Jessen <rjessen@cyberpowersystems.com> at CyberPower Systems for much-needed assistance.

This module is distributed under the same license as Perl itself.