A place where things actually work,
with a sense of humor.

Meuon
0 comments


keywords:

APC 10KVA UPS monitoring via SNMP
First you have to find the right MIB for your ups. This one is a "Smart-UPS RT 10000 XL" from APC. The real trick is using the right mib file with the -m "./mibfile.mib" command line syntax. The quotes make a lot of difference. The same trick works for snmpwalk and other snmp utilities. I'm running a much fancier version of what I have below in a production setting, but this is a good starting place and has the essentials working.

Sure beats serial cables and Java runtimes for the "official APC method" and you can run this on multiple servers, each with their own decision tree and set of commands. ie: Keep the mission critical stuff up the longest, drop the junk you don't need quickly and make that UPS last longer.
#!/usr/bin/perl
#This is as simple of an APC power monitor as I could make.
#Should be run as a cron job every 5 minutes or less. replace 192.168.192.4 with the IP of your UPS.

$shutdownthreshhold = 20 ;
open(IN,"/usr/bin/snmpget -v1 -c public -m "/home/juice/apc/powernet395.mib" 192.168.192.4 PowerNet-MIB::upsAdvBatteryCapacity.0 |") ;
while(<IN>) {
@d = split(/ /) ;
$capacity = $d[3] * 1 ;
} ;
open(IN,"/usr/bin/snmpget -v1 -c public -m "/home/juice/apc/powernet395.mib" 192.168.192.4 PowerNet-MIB::upsBasicOutputStatus.0 |") ;
while(<IN>) {
@d = split(/ /) ;
$status = $d[3] ;
chop($status) ;
} ;
open(OUT,">/tmp/powercheck") ;
print OUT "Battery Capacity: $capacity percent Battery Status: $status Shutdown at $shutdownthreshhold" ;
close(OUT) ;


if($status eq 'onBattery(3)') {
if($capacity < $shutdownthreshhold) {
print "SHUTDOWN
" ;
system("/sbin/shutdown -P now") ; #standard Linux shutdown, -P is power off
} ;

} ;



 

admin
0 comments


keywords:

Back to Perl: simplecsv2xml
i used to use Perl a lot, but I got rusty. Today I needed to convert a bunch of typical CSV files into "XML" Quick and Dirty, simple flat files.
It's a sad world when a professional $150+/hr developer only knows how to read XML. My guess is they code in Java or Dot.Not. Heck, Maybe MS-Visual Basic.

Mine is not to wonder why... mine is just to do or die. I went looking for what I wanted.. most of the solutions were just too complex, so I started from scratch and ened up with a simple Perl script to convert CSV to XML.. Why perl? Fast and easy parser of STDIN/STDOUT. This is NOT what is used in the final project, but it is something that works and many people (and sometimes me) just need a quick CSV to XML conversion.

Usage:   simplecsv2xml.pl <infile >outfile
 


#!/usr/bin/perl
# Mikes Uber Simple CSV to XML Parser
use Text::CSV;
$csv = Text::CSV->new();
$i = 0 ;
print "<?xml version="1.0" encoding="UTF-8"?> " ;
print "<document> " ;
while(<STDIN>) {
$status = $csv->parse($_); # parse a CSV string into fields
if($i < 1) {
@key = $csv->fields() ; # get the parsed fields
# assumes the first line is the key/field names
} else {
@data = $csv->fields(); # get the parsed fields
} ;
#now make it XML-ish.
if($i > 0) {
$r = 0 ;
print '<row>' ;
foreach(@data) {
if($key[$r] ne '') {
print '<'. $key[$r] . '>' . $_ . '</' . $key[$r] . '>' ;
} ;
$r++ ;
} ;
print '</row>' ." " ;
} ;
$i++ ;
} ;
print "</document> " ;


 
535 Chestnut Street - Suite 241 - Chattanooga TN 37402
423-605-6943