Παρασκευή 6 Αυγούστου 2021

Get Telegram notifications for APC UPS using SNMP traps in Linux

In this post I am going to explain how I used SNMP traps on a Linux Debian system in order to get some telegram notifications. I used them in order to be informed in case our company UPS systems runs on battery.

First of all, you have to setup and test a Telegram Notification script.
I used the one here: https://github.com/samsulmaarif/telegram-notify , it is very easy to install and use.

The binary is installed in: /usr/local/bin/telegram-notify

We assume that you already have a telegram bot running.
The two things you 've got to find are your bot api-key and chat id or Channel id.
In case you are not yet using a telegram bot, there are plenty of guides showing how to setup one and how to get your user-id.

In order to get SNMP traps from your UPS Network management card, you should first enable them in the "Notifications". Just enter your Linux hostname as a trap receiver.


First install snmpd and snmptrapd

sudo apt install snmpd snmptrapd 


Edit your config files:

The only change I did to this file is the agentAddress.
/etc/snmp/snmpd.conf

agentAddress udp:161,udp6:161


Then, edit the configuration for the traps. The matching SNMPv1 OIDs for the online/offline battery status of an APC UPS are SNMPv2-SMI::enterprises.318.0.5 and SNMPv2-SMI::enterprises.318.0.9.

So, when each trap is received, my local script /usr/local/ups-telegram.sh  is ran, follwed by a string onbattery/offbattery used to identify the status.


/etc/snmp/snmptrapd.conf 

authcommunity log,execute,net public
snmpTrapdAddr udp:162
traphandle SNMPv2-SMI::enterprises.318.0.5 /usr/local/ups-telegram.sh "onbattery"  
traphandle SNMPv2-SMI::enterprises.318.0.9 /usr/local/ups-telegram.sh "offbattery"

Traps are enabled only for the matching OIDs. In order to de tests, you may use "traphandle default" instead of using specific OIDs.

Restart the services:

sudo service snmpd restart
sudo service snmptrapd restart


test the traps locally:

snmptrap -v 2c -c public localhost "" SNMPv2-SMI::enterprises.318.0.5
snmptrap -v 2c -c public localhost "" SNMPv2-SMI::enterprises.318.0.9

if everyting is fine, you should see some SNMP trap logs at your syslog.

In the second part, we will configure our "action" script. The variable $1 is passed from the snmptrapd.conf file.
In order to get the Telegram Notifications, I used a modified offical Example handler script:

ups-telegram.sh

#!/bin/bash

userid="myuserid"
apikey="myapikey"

read host
read ip
vars=
count=1

while read oid val
do
count=$[count+1]
if [ "$vars" = "" ]
then
vars="$oid = $val"
else
vars="$vars, $oid = $val"
fi
done

UPS_TIME="/tmp/UPS_TIME.tmp"

#echo a $1 trap from host=$host at IP $ip vars=$vars>>/var/log/messages
if [ "$1" = "onbattery" ]; then
starttime="$(TZ=UTC0 printf '%(%s)T\n' '-1')"
echo "$starttime" > "$UPS_TIME"

/usr/local/bin/telegram-notify --user "$userid" --key
"$apikey" --text "$host - On
battery power in response to an input power problem" --error

fi
if [ "$1" = "offbattery" ]; then
starttime=$(head -n 1 $UPS_TIME)
rm -f "$UPS_TIME"
elapsedseconds=$(( $(TZ=UTC0 printf '%(%s)T\n' '-1') - starttime ))

/usr/local/bin/telegram-notify --user "$userid" --key
"$apikey" --text "$host - No longer
on battery power - was on battery for $elapsedseconds secs" --success
fi

exit