Advertisement

How to monitor USB UPS status via SNMP

How to monitor USB UPS status via SNMP

There are several computers with USB-connected UPS. All the computers use the apcupsd program. Say you want to aggregate information about the state of the power supply for every unit in one place. Since snmpd is already running there, it would be logical to expand its functionality.

Advertisеment

The below solution, courtesy of the Admin's Blog, will collect the data from all snmpd units at once.

In order to monitor USB UPS status via SNMP, we need to first modify the snmp daemon config.

Preparations

Adds the following to the config snmpd.conf.

# APC UPS
pass .1.3.6.1.4.1.318.1.1.1 /bin/sh /etc/snmp/apcupsd.sh

The contents of the /etc/snmp/apcupsd.sh script is as follows.


#!/bin/sh -f

# Check apcupsd is online
apcaccess > /dev/null 2>&1 || exit 0

PLACE=".1.3.6.1.4.1.318.1.1.1"
REQ="$2" # Requested OID

#
# Process SET requests by simply logging the assigned value
# Note that such "assignments" are not persistent,
# nor is the syntax or requested value validated
#
if [ "$1" = "-s" ]; then
echo $* >> /tmp/passtest.log
exit 0
fi

#
# GETNEXT requests - determine next valid instance
#
if [ "$1" = "-n" ]; then
case "$REQ" in
$PLACE| \
$PLACE.0| \
$PLACE.0.*| \
$PLACE.1| \
$PLACE.1.1.0*) RET=$PLACE.1.1.1.0 ;;

$PLACE.1*| \
$PLACE.2.0| \
$PLACE.2.0.*| \
$PLACE.2.1| \
$PLACE.2.2.0*) RET=$PLACE.2.2.1.0 ;;

$PLACE.2.2.1*) RET=$PLACE.2.2.2.0 ;;

$PLACE.2.2.2*) RET=$PLACE.2.2.3.0 ;;

$PLACE.2.2.3*) RET=$PLACE.2.2.4.0 ;;

$PLACE.2*| \
$PLACE.3.0*| \
$PLACE.3.1*| \
$PLACE.3.2.0*) RET=$PLACE.3.2.1.0 ;;

$PLACE.3.2.1*| \
$PLACE.3.2.2*| \
$PLACE.3.2.3*) RET=$PLACE.3.2.4.0 ;;

$PLACE.3.2.4*) RET=$PLACE.3.2.5.0 ;;

$PLACE.3.2*| \
$PLACE.4.0*| \
$PLACE.4.1*| \
$PLACE.4.2.0*) RET=$PLACE.4.2.1.0 ;;

$PLACE.4.2.1*) RET=$PLACE.4.2.2.0 ;;

$PLACE.4.2.2*) RET=$PLACE.4.2.3.0 ;;

$PLACE.4.2.3*) RET=$PLACE.4.2.4.0 ;;

$PLACE.4.2.*| \
$PLACE.5*| \
$PLACE.6*| \
$PLACE.7.0*| \
$PLACE.7.1*| \
$PLACE.7.2.0*| \
$PLACE.7.2.1*| \
$PLACE.7.2.2*) RET=$PLACE.7.2.3.0 ;;

$PLACE.7.2.3*) RET=$PLACE.7.2.4.0 ;;

$PLACE.7*| \
$PLACE.8.0*) RET=$PLACE.8.1.0 ;;

*) exit 0 ;;
esac
else
#
# GET requests - check for valid instance
#
case "$REQ" in
$PLACE.1.1.1.0| \
$PLACE.2.2.1.0| \
$PLACE.2.2.2.0| \
$PLACE.2.2.3.0| \
$PLACE.2.2.4.0| \
$PLACE.3.2.1.0| \
$PLACE.3.2.4.0| \
$PLACE.3.2.5.0| \
$PLACE.4.2.1.0| \
$PLACE.4.2.2.0| \
$PLACE.4.2.3.0| \
$PLACE.4.2.4.0| \
$PLACE.7.2.3.0| \
$PLACE.7.2.4.0| \
$PLACE.8.1.0) RET=$REQ ;;
*) exit 0 ;;
esac
fi

#
# "Process" GET* requests - return hard-coded value
#
echo "$RET"
case "$RET" in
$PLACE.1.1.1.0) echo "string"; apcaccess -u -p MODEL ; exit 0 ;;
$PLACE.2.2.1.0) echo "Gauge32"; apcaccess -u -p BCHARGE ; exit 0 ;;
$PLACE.2.2.2.0) echo "Gauge32"; apcaccess -u -p ITEMP ; exit 0 ;;
$PLACE.2.2.3.0) echo "Timeticks"; echo $(($(LC_ALL=C printf "%.*f" 0 $(apcaccess -u -p TIMELEFT)) * 6000)) ; exit 0 ;;
$PLACE.2.2.4.0) echo "string"; apcaccess -u -p BATTDATE ; exit 0 ;;
$PLACE.3.2.1.0) echo "Gauge32"; apcaccess -u -p LINEV ; exit 0 ;;
$PLACE.3.2.4.0) echo "Gauge32"; apcaccess -u -p LINEFREQ ; exit 0 ;;
$PLACE.3.2.5.0) echo "string"; apcaccess -u -p LASTXFER ; exit 0 ;;
$PLACE.4.2.1.0) echo "Gauge32"; apcaccess -u -p OUTPUTV ; exit 0 ;;
$PLACE.4.2.2.0) echo "Gauge32"; apcaccess -u -p LINEFREQ ; exit 0 ;;
$PLACE.4.2.3.0) echo "Gauge32"; apcaccess -u -p LOADPCT ; exit 0 ;;
$PLACE.4.2.4.0) echo "Gauge32"; apcaccess -u -p LOADPCT ; exit 0 ;;
$PLACE.7.2.3.0) echo "string"; apcaccess -u -p SELFTEST ; exit 0 ;;
$PLACE.7.2.4.0) echo "string"; apcaccess -u -p SELFTEST ; exit 0 ;;
$PLACE.8.1.0) echo "Gauge32"; echo 1 ; exit 0 ;;
*) echo "string"; echo "ack... $RET $REQ"; exit 0 ;; # Should not happen
esac

If you are wondering about OIDs, you can find them HERE. Note that some UPS models do not return all parameters or in the wrong format, so the code may contain inaccuracies and stubs.

Now you are ready.

How to monitor USB UPS status via SNMP

To monitor USB UPS status via SNMP, run the following command: snmpwalk -v 1 -c public 127.0.0.1 .1.3.6.1.4.1.318.1.1.1. The output will be like that:

SNMPv2-SMI::enterprises.318.1.1.1.1.1.1.0 = STRING: "Smart-UPS 750 "
SNMPv2-SMI::enterprises.318.1.1.1.2.2.1.0 = Gauge32: 100
SNMPv2-SMI::enterprises.318.1.1.1.2.2.2.0 = Gauge32: 36
SNMPv2-SMI::enterprises.318.1.1.1.2.2.3.0 = Timeticks: (432000) 1:12:00.00
SNMPv2-SMI::enterprises.318.1.1.1.2.2.4.0 = STRING: "2020-05-14"
SNMPv2-SMI::enterprises.318.1.1.1.3.2.1.0 = Gauge32: 227
SNMPv2-SMI::enterprises.318.1.1.1.3.2.4.0 = Gauge32: 50
SNMPv2-SMI::enterprises.318.1.1.1.3.2.5.0 = STRING: "Automatic or explicit self test"
SNMPv2-SMI::enterprises.318.1.1.1.4.2.1.0 = Gauge32: 227
SNMPv2-SMI::enterprises.318.1.1.1.4.2.2.0 = Gauge32: 50
SNMPv2-SMI::enterprises.318.1.1.1.4.2.3.0 = Gauge32: 13
SNMPv2-SMI::enterprises.318.1.1.1.4.2.4.0 = Gauge32: 13
SNMPv2-SMI::enterprises.318.1.1.1.7.2.3.0 = STRING: "NO"
SNMPv2-SMI::enterprises.318.1.1.1.7.2.4.0 = STRING: "NO"
SNMPv2-SMI::enterprises.318.1.1.1.8.1.0 = Gauge32: 1

If you further configure the monitoring script, you can get these nifty graphs:

UpsloadVoltageBatterytempRuntime

 

 

Support us

Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:

If you like this article, please share it using the buttons below. It won't take a lot from you, but it will help us grow. Thanks for your support!

Advertisеment

Author: Sergey Tkachenko

Sergey Tkachenko is a software developer who started Winaero back in 2011. On this blog, Sergey is writing about everything connected to Microsoft, Windows and popular software. Follow him on Telegram, Twitter, and YouTube.

Leave a Reply

Your email address will not be published.

css.php
Using Telegram? Subscribe to the blog channel!
Hello. Add your message here.