Page 1 of 1

Help with UPS monitoring script.

Posted: Thu Jun 20, 2013 11:53 am
by cinders
Guys,

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.

Can anyone help?

All the best,
Cinders.

Re: Help with UPS monitoring script.

Posted: Fri Jun 21, 2013 7:06 am
by c0d3rSh3ll
try this
{
local status [system ups get value-name=on-line 0]
if ($status = yes ) do={/system script run script1} else={/system script run scrip2}
}
you can change the script1 and script2 for your script for send email or anything

Re: Help with UPS monitoring script.

Posted: Fri Jun 21, 2013 10:25 am
by cinders
c0d3rSh3ll,

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:

:local online;
:local runtimeleft;
:local battcharge;

/system ups monitor ups1 once do={
:set online $"on-line";
:set runtimeleft $"runtime-left";
:set battcharge $"battery-charge";
}

: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.

Thanks again for getting me on the right track!

Re: Help with UPS monitoring script.

Posted: Sat Jun 22, 2013 4:51 pm
by cinders
Well it took me a few hours and here it is...

: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.

Re: Help with UPS monitoring script.

Posted: Tue Oct 15, 2013 12:44 am
by mdmimoso
Hello Matt

Thanks for this very useful script.

Could you please tell me What is the ROS version And UPS that you are using ?

Thanks in advance
David

Re: Help with UPS monitoring script.

Posted: Tue Oct 15, 2013 2:54 am
by cinders
Sure. I originally wrote this on 5.2 and have since upgraded to 6.1.

This works with APC UPS"s - Smart-UPS 1000VA, Also Back-UPS 500VA, 650VA.

Re: Help with UPS monitoring script.

Posted: Tue Oct 15, 2013 1:21 pm
by mdmimoso
Hello Matt

I want to buy this APC , do you think that works?
http://www.apc.com/resource/include/tec ... b=features

Re: Help with UPS monitoring script.

Posted: Tue Oct 15, 2013 1:31 pm
by mdmimoso
what about this one? the price is better but the isterface is usb...

Back-UPS Pro 550
http://www.apc.com/products/resource/in ... tab=models

Re: Help with UPS monitoring script.

Posted: Tue Oct 15, 2013 3:40 pm
by cinders
Should be fine.

Re: Help with UPS monitoring script.

Posted: Tue Oct 15, 2013 3:56 pm
by mdmimoso
Witch one?

Re: Help with UPS monitoring script.

Posted: Wed Oct 16, 2013 2:56 am
by cinders
Both of them. :)

Re: Help with UPS monitoring script.

Posted: Wed Oct 16, 2013 1:19 pm
by mdmimoso
hello matt

a can't setup ups, due to this message

could you please help

Re: Help with UPS monitoring script.

Posted: Wed Oct 16, 2013 1:51 pm
by mdmimoso
resolved :)

Re: Help with UPS monitoring script.

Posted: Wed Oct 16, 2013 2:38 pm
by cinders
Glad to hear!

Re: Help with UPS monitoring script.

Posted: Wed Oct 16, 2013 2:44 pm
by mdmimoso
hello matt

i tested your script but i cant receive any mail :(

do i have to have a scheduler , but if i run the script nothing happens

thanks

Re: Help with UPS monitoring script.

Posted: Thu Nov 07, 2013 11:49 am
by solelunauno
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.

Re: Help with UPS monitoring script.

Posted: Thu Mar 20, 2014 7:22 pm
by Erlington
Same thing happened to me, no sended email. My version of Router OS is 6.10, and I monitoring with the USB port, is there any help?

Re: Help with UPS monitoring script.

Posted: Thu Mar 20, 2014 7:57 pm
by Erlington
i tested your script but i cant receive any mail :(

do i have to have a scheduler , but if i run the script nothing happens

thanks
Same thing happened to me my RouterOS version is 6.10

Re: Help with UPS monitoring script.

Posted: Fri Apr 18, 2014 1:45 am
by unridaz
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.1 port=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!..."

add the variables you need.

Re: Help with UPS monitoring script.

Posted: Tue Oct 14, 2014 1:20 pm
by trottolino1970
this script work with apc smart sc-420?

Re: Help with UPS monitoring script.

Posted: Mon Dec 29, 2014 4:52 am
by smartmoney
I'm getting an error using this script on rb750up using v6.23

: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";

on this line my error is "syntax error (line 1 column 6)" and the o in :set online is red

also on this next line
:set runtimeleft $"runtime-left";
my error is "syntax error (line 1 column 6)" and the r in :set runtimeleft is red

lastly again
:set battcharge $"battery-charge";
my error is "syntax error (line 1 column 6)" and the b in :set battcharge is red

how do i fix these?

Re: Help with UPS monitoring script.

Posted: Sat Feb 25, 2017 6:31 pm
by mareksuk
Some UPS, even APC SmartUPS 1500 do not report OnBattery, but they still report OnLine.

In such case, use this script driven by OnLine value instead of OnBattery :
:local mailto "email@address";
:local online;
:local sysname [/system identity get name];
:local datetime "$[/system clock get date] $[/system clock get time]";
:local upsname
:set upsname [system ups get value-name=name 0];

:global oldonline;

:if ([:typeof $oldonline]="nothing") do={:set $oldonline true};

:set online true;
:set online [system ups get value-name=on-line 0];

:if ( ( $online = false ) && ( $oldonline = true ) ) do={
  :set oldonline false;

  /tool e-mail send to=$mailto subject="$upsname on $sysname : Power failed!" body="$upsname on $sysname is on battery since $datetime";
  :log info "Power-Failed: EMail sent to $mailto";
}

:if ( ( $online = true ) && ( $oldonline = false ) ) do={
  :set oldonline true;

  /tool e-mail send to=$mailto subject="$upsname on $sysname : Power restored!" body="$upsname on $sysname is back online since $datetime";
  :log info "Power-Restored: EMail sent to $mailto";
}