Before I had CyberCard, I still had a need to monitor if my system was running off the UPS power. If my server could detect power out and shut down other devices, my battery life would keep server up for longer.
If you have Mikrotik’s router with two power supplies and an SSH connection to the same there is a trick you can use – Mikrotik can show you each power supply state. If you take care to plug one power supply into the UPS and the other one into the non-UPS outlet, you suddenly have a detector.
ssh -i ~/.ssh/id_rsa firstname.lastname@example.org "/system health print"
Even better, the voltage doesn’t go immediately to 0 V as soon as power is out so there is a delay built-in. So, script is as easy as detecting 0V on the output. Something like this.
ssh -i ~/.ssh/id_rsa email@example.com "/system health print" \
| egrep 'psu-voltage' | grep -q '0V' && echo "Do Something!"
PS: If you’re interested in the whole script around this, you can download it here.