# 20020620 bog@deol.ru htp://www.bog.pp.ru/hard/UPS.html#rrdtool
# $1 is UPS number

if [ -z "$1" ]
then
  echo Usage: ups_graph.sh ups-number start interval
  exit
fi
if [ "$1" = "1" ]
then
  host=адрес-UPS
  community=имя-сообщества
  index=имя-UPS
else
  echo Wrong UPS number
  exit
fi
if [ -z "$3" ]
then
  start=-2days
  int=5min
else
  start=$2
  int=$3
fi
HTMLDIR=директория-для-картинок

snmpget -On -Oq -m RFC1213-MIB $host $community sysDescr.0 sysUpTime.0 sysName.0 | awk '{for (i=2;i<NF;i++) printf $i " "; print $NF}' | (read sysDescr; read sysUpTime; read sysName; echo "UPS name: $sysName<br>Uptime: $sysUpTime<br>SNMP agent model: $sysDescr<br>" > $HTMLDIR/ups_model.$index)

snmpget -On -Oq -m PowerNet-MIB $host $community upsBasicIdentModel.0  | awk '{for (i=2;i<NF;i++) printf $i " "; print $NF}' | ( read upsBasicIdentModel;  echo "UPS model: $upsBasicIdentModel" >> $HTMLDIR/ups_model.$index)

/usr/local/bin/rrdtool graph $HTMLDIR/ups_capacity_$int.$index.png --start $start --vertical-label percents --width 700 --step 60 --height 185 --imgformat PNG --title="$index UPS load and capacity" DEF:BatteryCapacity=ups.$index.rrd:BatteryCapacity:AVERAGE DEF:OutputLoad=ups.$index.rrd:OutputLoad:AVERAGE 'LINE1:BatteryCapacity#FF0000:Battery Capacity' 'GPRINT:BatteryCapacity:AVERAGE:Avg %lf' 'GPRINT:BatteryCapacity:LAST:Cur %lf\l' 'LINE1:OutputLoad#00FF00:Output Load     ' 'GPRINT:OutputLoad:AVERAGE:Avg %lf' 'GPRINT:OutputLoad:LAST:Cur %lf\l' > /dev/null

/usr/local/bin/rrdtool graph $HTMLDIR/ups_voltage_$int.$index.png --start $start --vertical-label Volts --width 700 --step 60 --height 185 --imgformat PNG --title="$index UPS voltage" DEF:InputLineVoltage=ups.$index.rrd:InputLineVoltage:AVERAGE DEF:InputMaxLineVoltage=ups.$index.rrd:InputMaxLineVoltage:AVERAGE DEF:InputMinLineVoltage=ups.$index.rrd:InputMinLineVoltage:AVERAGE DEF:OutputVoltage=ups.$index.rrd:OutputVoltage:AVERAGE 'LINE1:InputLineVoltage#FF0000:Input Line' 'LINE1:InputMaxLineVoltage#0000FF:Input Line max' 'LINE1:InputMinLineVoltage#00FF00:Input Line min' 'LINE1:OutputVoltage#000000:Output Line   ' > /dev/null

/usr/local/bin/rrdtool graph $HTMLDIR/ups_freq_$int.$index.png --start $start --vertical-label Hz --width 700 --step 60 --height 185 --imgformat PNG --title="$index UPS Frequency" DEF:InputFrequency=ups.$index.rrd:InputFrequency:AVERAGE DEF:OutputFrequency=ups.$index.rrd:OutputFrequency:AVERAGE 'LINE2:InputFrequency#FF0000:Input ' 'LINE2:OutputFrequency#00FF00:Output' > /dev/null

/usr/local/bin/rrdtool graph $HTMLDIR/ups_temp_$int.$index.png --start $start --vertical-label grad --width 700 --step 60 --height 185 --imgformat PNG --title="$index UPS Temperature" DEF:BatteryTemperature=ups.$index.rrd:BatteryTemperature:AVERAGE 'LINE2:BatteryTemperature#00FF00:Temperature' > /dev/null

/usr/local/bin/rrdtool graph $HTMLDIR/ups_bstatus_$int.$index.png --start $start --vertical-label status --width 700 --step 60 --height 185 --imgformat PNG --title="$index UPS Battery Status" DEF:BatteryStatus=ups.$index.rrd:BatteryStatus:AVERAGE 'LINE1:BatteryStatus#00FF00:Status - unknown(1), batteryNormal(2), batteryLow(3)' > /dev/null

/usr/local/bin/rrdtool graph $HTMLDIR/ups_breplace_$int.$index.png --start $start --vertical-label replace --width 700 --step 60 --height 185 --imgformat PNG --title="$index UPS Battery Replace" DEF:BatteryReplace=ups.$index.rrd:BatteryReplace:AVERAGE 'LINE1:BatteryReplace#00FF00:Replace - noBatteryNeedsReplacing(1), batteryNeedsReplacing(2)' > /dev/null

/usr/local/bin/rrdtool graph $HTMLDIR/ups_fail_$int.$index.png --start $start --vertical-label 'Fail Cause' --width 700 --step 60 --height 185 --imgformat PNG --title="$index UPS Input Line Fail Cause" DEF:InputLineFailCause=ups.$index.rrd:InputLineFailCause:AVERAGE 'LINE2:InputLineFailCause#00FF00:noTransfer(1),highVoltage(2),brownout(3),blackout(4),smallSag(5),deepSag(6),smallSpike(7),largeSpike(8),selfTest(9),rateOfVoltageChange(10)' > /dev/null

/usr/local/bin/rrdtool graph $HTMLDIR/ups_ostatus_$int.$index.png --start $start --vertical-label 'Output Status' --width 700 --step 60 --height 185 --imgformat PNG --title="$index UPS Output Status" DEF:OutputStatus=ups.$index.rrd:OutputStatus:AVERAGE 'LINE2:OutputStatus#00FF00:unknown(1),onLine(2),onBattery(3),onSmartBoost(4),timedSleeping(5),off(7),rebooting(8),sleepingUntilPowerReturn(11),onSmartTrim(12)' > /dev/null

/usr/local/bin/rrdtool graph $HTMLDIR/ups_selftest_$int.$index.png --start $start --vertical-label results --width 700 --step 60 --height 185 --imgformat PNG --title="$index UPS Self Test Results" DEF:DiagnosticsResults=ups.$index.rrd:DiagnosticsResults:AVERAGE DEF:CalibrationResults=ups.$index.rrd:CalibrationResults:AVERAGE 'LINE2:DiagnosticsResults#00FF00:Diagnostics Results - ok(1), failed(2), invalidTest(3), testInProgress(4)' 'LINE2:CalibrationResults#0000FF:Calibration Results - ok(1), invalidCalibration(2), calibrationInProgress(3)' > /dev/null

/usr/local/bin/rrdtool graph $HTMLDIR/ups_battime_$int.$index.png --start $start --vertical-label 0.01s --width 700 --step 60 --height 185 --imgformat PNG --title="$index UPS Time" DEF:BatteryTimeOnBatt=ups.$index.rrd:BatteryTimeOnBatt:AVERAGE DEF:BatteryRemaining=ups.$index.rrd:BatteryRemaining:AVERAGE 'LINE2:BatteryTimeOnBatt#00FF00:Time on Battery' 'LINE2:BatteryRemaining#0000FF:runtime remaining' > /dev/null


