I have been through several network monitoring packages and most of them are pretty terrible. I was happy to have discovered FreeNATS which was not overly complicated, and yet still provided the simple monitoring features that everybody needs (as in is it up or not?).
A few years ago I started buying up Emerson Liebert UPS systems to replace our mix of APC and Tripp Lite models. Liebert sells a web card option which provides you with SNMP monitoring and limited management. I am just now getting around to monitoring these units and I needed something very simple to do it with. And that is where this test comes from.
This is a "first effort" from a non-programmer. So if you spot some sloppy code and have some ideas on how to improve this script, please share your thoughts! Hopefully there is someone else out there monitoring Liebert UPS's and using FreeNATS who can make use of this.
This test provides you with a battery status (good, or needs replaced), time left on battery (you hope it's zero, otherwise you are running on battery), and temperature (how hot in Fahrenheit degrees). The temperature has been the most useful reading to me, because it tells me which of my wiring closets are running too hot and will kill my batteries prematurely.
Thanks to Dave (the admin and creator of FreeNATS) I was about to get hints on how I could also display the model number and serial number on the testing screen. This should be useful if you need to call into support for battery replacement, etc.
And now, the code:
Code: Select all
<?php // liebertups.inc.php -- Liebert UPS Module
// This is to pull info from a Liebert UPS with a web card
// installed to it. As you can tell, it's a customization
// made upon the tcp.inc.php included with FreeNATS.
// -Steve Ballantyne 6/3/2012
/* -------------------------------------------------------------
This file is part of FreeNATS
FreeNATS is (C) Copyright 2008 PurplePixie Systems
FreeNATS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FreeNATS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FreeNATS. If not, see www.gnu.org/licenses
For more information see www.purplepixie.org/freenats
-------------------------------------------------------------- */
if (isset($NATS))
{
class FreeNATS_LiebertUPS_Test extends FreeNATS_Local_Test
{
function DoTest($testname,$param,$hostname,$timeout,$params)
{
global $NATS;
if ($timeout<=0) $timeout=$NATS->Cfg->Get("test.liebertups.timeout",0); // if no test-specific param use sys default
if ($timeout<=0) $timeout=60; // if sys default is <=0 then default to 60 seconds
$ip=ip_lookup($hostname);
if ($ip=="0") return -2; // lookup failed
$errno=0;
$errstr="";
$ups_snmp = @snmpget($ip, $params[1], $params[0]);
$snmp_value = preg_split('/[\s:]+/', $ups_snmp, -1, PREG_SPLIT_NO_EMPTY);
// Check to see if this data needs converted into an Integer or not.
if (is_numeric($snmp_value[1])) {
// This is an integer
$snmp_value_int = (int)$snmp_value[1];
return $snmp_value_int;
}
else {
// This is NOT an integer
return $snmp_value[1];
}
}
function Evaluate($result)
{
if ($result<0) return 2; // failure
return 0; // else success
}
function DisplayForm(&$row)
{
global $NATS;
// Try to offer up goodies like the serial number and model number of this unit.
$data=$NATS->GetNode($row['nodeid']);
$ip=ip_lookup($data['hostname']);
echo "<strong>INFO COLLECTED FROM THIS UNIT</strong><br>";
echo "<i>These values may be unknwon until you fill in your community password, save this test, and return to this screen.</i><br>";
echo "<strong>Hostname : </strong>".$data['hostname']."<br>";
echo "<strong>IP is : </strong>$ip<br>";
// We will not try to run SNMP queries if there is no community password set
if ($row['testparam1']!="" || $row['testparam1']!=NULL) {
$upsmodel_snmp = @snmpget($ip, $row['testparam1'], "1.3.6.1.2.1.33.1.1.2.0");
$upsmodel_value = preg_split('/[\s:]+/', $upsmodel_snmp, -1, PREG_SPLIT_NO_EMPTY);
$upsserial_snmp = @snmpget($ip, $row['testparam1'], "1.3.6.1.4.1.476.1.42.2.4.2.1.7.1");
$upsserial_value = preg_split('/[\s:]+/', $upsserial_snmp, -1, PREG_SPLIT_NO_EMPTY);
}
else {
$upsmodel_value[1]="Unknown";
$upsserial_value[1]="Unknown";
}
echo "<strong>Model Number</strong>: $upsmodel_value[1]<br>\n";
echo "<strong>Serial Number</strong>: $upsserial_value[1]<br><hr>\n";
echo "<table border=0>";
echo "<tr><td align=left>";
echo "<strong>Choose an SNMP OID :</strong>";
echo "<tr><td align=left>";
// Determine what to set the dropdown box default to ... is this a saved test we are revising or viewing? -
$option="".$row['testparam']."";
if ($option=="1.3.6.1.4.1.476.1.42.3.4.1.2.3.1.3.1")
$selected=1;
elseif ($option=="1.3.6.1.2.1.33.1.2.1.0")
$selected=2;
elseif ($option=="1.3.6.1.2.1.33.1.2.2.0")
$selected=3;
echo "<center><select name=testparam style=\"background-color: #FFFFFF; color: #000000; font-family: Arial; font-weight: none; font-size: 12; width: 375px;\">";
if ($selected==1)
echo "<option value=\"1.3.6.1.4.1.476.1.42.3.4.1.2.3.1.3.1\" selected=\"selected\">Temperature in Fahrenheit (1.3.6.1.4.1.476.1.42.3.4.1.2.3.1.3.1)</option>";
else
echo "<option value=\"1.3.6.1.4.1.476.1.42.3.4.1.2.3.1.3.1\">Temperature in Fahrenheit (1.3.6.1.4.1.476.1.42.3.4.1.2.3.1.3.1)</option>";
if ($selected==2)
echo "<option value=\"1.3.6.1.2.1.33.1.2.1.0\" selected=\"selected\">Battery Status (2=okay 3=low 4=depleted) (1.3.6.1.2.1.33.1.2.1.0)</option>";
else
echo "<option value=\"1.3.6.1.2.1.33.1.2.1.0\">Battery Status (2=okay 3=low 4=depleted) (1.3.6.1.2.1.33.1.2.1.0)</option>";
if ($selected==3)
echo "<option value=\"1.3.6.1.2.1.33.1.2.2.0\" selected=\"selected\">Seconds on Battery (0=not on battery) (1.3.6.1.2.1.33.1.2.2.0)</option>";
else
echo "<option value=\"1.3.6.1.2.1.33.1.2.2.0\">Seconds on Battery (0=not on battery) (1.3.6.1.2.1.33.1.2.2.0)</option>";
// Some older units do NOT report seconds left, but DO report a time remaining value
echo "<option value=\"1.3.6.1.4.1.476.1.42.3.5.1.18.0\">BatteryTimeRemaining (1.3.6.1.4.1.476.1.42.3.5.1.18.0)</option>";
echo "</select><br /><br />";
echo "<tr><td align=left>";
echo "<strong>Community Password :</strong>";
echo "<br><i>Usually this is the word public</i>";
echo "</td></tr>";
echo "</td><td align=left>";
echo "<tr><td align=left>";
echo "<input type=text name=testparam1 size=30 maxlength=255 value=\"".$row['testparam1']."\">";
echo "</td></tr>";
echo "</table>";
}
}
$params=array();
$NATS->Tests->Register("liebertups","FreeNATS_LiebertUPS_Test",$params,"Liebert UPS Testing",1,"Liebert UPS SNMP Testing");
}
?>
Enjoy!