Can anyone help? I’m trying to write a script that will notify me when there is a power failure and my UPS switches to battery.
So far if I run the command: /system ups monitor 0
This brings up the UPS monitoring. There is a line in the report called “on-line:” which shows “yes” when on mains AC and “no” when running on battery backup.
I would like to monitor that line and if it displays no, then send me an e-mail telling me the UPS is on battery back-up, then if it displays yes - an e-mail to say power has returned.
Thanks for the help. It works great, however I’d like to modify it to do a little more.
I messed around (took me 3 hours) and found a way to grab the variables I need. I wrote this small script:
:log info (“1: = $online”)
:log info (“2: = $runtimeleft”)
:log info (“3: = $battcharge”)
So now I can grab the required variables. How do I get a script to run so it:
1.) E-mails me when the battery goes off-line also telling me run-time and battery charge percentage (I can do this!)
2.) E-mails me when there is 5 minutes run-time left
3.) E-mails me when the mains power is restored
I’m guessing I would need to make some loops in the script? I’d like to avoid the script e-mailing me every minute when it’s run.
:global flagonbatt;
:global flagbattlow;
:global flagbattlowa;
:global shutdownin;
:global shutdown;
:global restored;
:local online;
:local runtimeleft;
:local battcharge;
:local sysname [/system identity get name];
:local datetime “$[/system clock get date] $[/system clock get time]”;
:if ([:typeof $flagonbatt]=“nothing”) do={:set flagonbatt 0}
:if ([:typeof $flagbattlow]=“nothing”) do={:set flagbattlow 0}
:if ([:typeof $flagbattlowa]=“nothing”) do={:set flagbattlowa 0}
:if ([:typeof $shutdownin]=“nothing”) do={:set shutdownin 100}
:if ([:typeof $shutdown]=“nothing”) do={:set shutdown 0}
:if ([:typeof $restored]=“nothing”) do={:set restored 0}
/system ups monitor ups1 once do={
:set online $“on-line”;
:set runtimeleft $“runtime-left”;
:set battcharge $“battery-charge”;
:set shutdownin $“offline-after”;
}
:if (($online=false) && ($flagonbatt=0)) do={
:set flagonbatt 1;
/tool e-mail send subject=“$sysname | Power Failure | $datetime | System will shutdown in $shutdownin | Battery Charge at $battcharge%” to=<your@email.com> body=“$sysname | Power Failure | $datetime | System will shutdown in $shutdownin | Battery Charge at $battcharge% - Good luck to you, let’s hope they fix it quickly!..”
:log info (“PowerFailure: EMail sent”)
}
:if (($online=true) && ($flagonbatt=1)) do={
:set flagonbatt 0;
:set shutdown 0;
:set flagbattlowa 0;
:set restored 1;
/tool e-mail send subject=“$sysname | Power Restored | $datetime | Battery Charge at $battcharge%” to=<your@email.com> body=“$sysname | Power Restored | $datetime | Battery Charge at $battcharge% - They fixed it! Well done to the electric company!..”
:log info (“PowerRestored: EMail sent”)
}
:if (($shutdownin <= 00:10:00) && ($shutdown=0)) do={
:set shutdown 1;
/tool e-mail send subject=“$sysname | System Warning | $datetime | System will shutdown in 10 minutes | Battery Charge at $battcharge%” to=<your@email.com> body=“$sysname | System Warning | $datetime | System will shutdown in 10 minutes | Battery Charge at $battcharge% - Better start praying!..”
:log info (“10MinLeft: Email sent”)
}
:if (($shutdownin <= 00:01:00) && ($flagbattlowa=0)) do={
:set flagbattlowa 1;
/tool e-mail send subject=“$sysname | System Warning | $datetime | System will shutdown in 1 minute | Battery Charge at $battcharge%” to=<your@email.com> body=“$sysname | System Warning | $datetime | System will shutdown in 1 minute | Battery Charge at $battcharge% - Pray for a miracle brother!..”
:log info (“1MinLeft: Email sent”)
}
:if (($battcharge=100) && ($restored=1)) do={
:set restored 0;
/tool e-mail send subject=“$sysname | Battery | $datetime | Battery is Fully Charged” to=<your@email.com> body=“$sysname | Battery | $datetime | Battery is Fully Charged - yeah, we’re ready to go again!..”
:log info (“BatteryFull: Email sent”)
}
This script will:
1.) Alert you when on battery power.
2.) Alert you when there is ten minutes of UPS runtime left.
3.) Alert you when there is one minute of UPS runtime left.
4.) Alert you when power is restored.
5.) Alert you when the battery is 100% charged.
I hope this might help someone in the future.
Matt.
There is a very simple solution to obtain a low mains alarm from mikrotik: you can use ad RB750UP or something else with voltage meter and power it with two power supplies; one power supply is a common 12Vdc adapter connected to power plug of the routerboard, and the second power supply is a 18V or 24V ethernet Poe, connected to “ether1” port.
If you connect the 12V adapter to UPS protected supply and the 18V Poe direct to mains, you obtain this situation: when mains is present, routerboard is supplied from 18V Poe and will read this voltage (about 17V inside router); when mains is lost and UPS is on battery, routerboard will be powered by 12Vdc adapter and will read this voltage (about 11.3V inside router), so you can use simple scripts to send alert mail when router power falls under 15Vdc.
The routerboard will not be affected by this supply variation because inside it has a redundant supply system made by diodes called “type OR diode logic” wich is also responsible of the 0.7 - 1V voltage reduction of power inside router.
Remember to connect the higher voltage to mains and the lower one to UPS, because “OR diode logic” works according to the simple policy “the higher voltage wins” ad you need mains supply victory when mains is present, to make working voltager fall reading.
For people having issues sending emails, ensure your have your mail server setup in /Tools → Email.
Alternatively, you can just edit the script and set it manually:
/tool e-mail send subject=“$sysname | Power Failure | $datetime | System will shutdown in $shutdownin | Battery Charge at $battcharge%” to=<your@email.com> body=“$sysname | Power Failure | $datetime | System will shutdown in $shutdownin | Battery Charge at $battcharge% - Good luck to you, let’s hope they fix it quickly!..”
changes to:
/tool e-mail send server=1.1.1.1port=25 subject=“$sysname | Power Failure | $datetime | System will shutdown in $shutdownin | Battery Charge at $battcharge%” to=<your@email.com> body=“$sysname | Power Failure | $datetime | System will shutdown in $shutdownin | Battery Charge at $battcharge% - Good luck to you, let’s hope they fix it quickly!..”