A while back I needed to get the input voltage from one of our UPSes, so I used bsnmpwalk(1)
to get the information needed and ran it in a script with a loop and sleep. Running it in tmux(1)
, of course.
#!/bin/sh
lastoff="maybe"
while true;
do
inpvol=$(bsnmpwalk -o quiet -s public@172.20.42.101 1.3.6.1.2.1.33.1.3.3.1.3)
[ $? != 0 ] && \
curl -s -X POST \
https://api.telegram.org/botXXX:YYY/sendMessage \
-d chat_id=-ZZZ \
-d text="Something is wrong with the SNMP server"
[ "${inpvol}" -lt 200 ] && \
curl -s -X POST \
https://api.telegram.org/botXXX:YYY/sendMessage \
-d chat_id=-ZZZ \
-d text="Power seems to be off. I see Input Voltage as ${inpvol}" && \
lastoff="true"
[ "${inpvol}" -ge 200 ] && [ "${lastoff}" == "true" ] && \
curl -s -X POST \
https://api.telegram.org/botXXX:YYY/sendMessage \
-d chat_id=-ZZZ \
-d text="Power back on. I see Input Voltage as ${inpvol}" && \
lastoff="false"
printf "%s --- %s\n" "$(date)" "${inpvol}"
sleep 60
done
This got the job done, but I guess there’s place for improvement (leave a reply).
Anyways, I kept forgetting that I need to run the script in tmux
after reboots, so I decided to use daemon(8)
.
touch /etc/rc.local
chmod +x /etc/rc.local
cat - >> /etc/rc.local
#!/bin/sh
daemon -u nobody -r -R 5 -f -t ups-notifier -o /var/log/ups_notifier.log /usr/local/bin/ups_notifier.sh
Again, there’s a place for improvement, specifically I can use a proper rc.d(8)
script, yet again, this gets the job done.
Gotta say, I love the simplicity of FreeBSD.