Using Mikrotik’s Router to Detect Power Outage

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 admin@router.home "/system health print"
voltage: 23.6V
current: 426mA
temperature: 50C
power-consumption: 10W
psu1-voltage: 24.4V
psu2-voltage: 0V

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 admin@router.home "/system health print" \
| egrep 'psu[12]-voltage' | grep -q '0V' && echo "Do Something!"

PS: If you're interested in the whole script around this, you can download it here.

Leave a Reply

Your email address will not be published. Required fields are marked *