Community discussions

MikroTik App
 
cinders
newbie
Topic Author
Posts: 42
Joined: Fri Oct 28, 2011 3:09 am

Help with UPS monitoring script.

Thu Jun 20, 2013 11:53 am

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.
You do not have the required permissions to view the files attached to this post.
 
User avatar
c0d3rSh3ll
Long time Member
Long time Member
Posts: 558
Joined: Mon Jul 25, 2011 9:42 pm
Location: [admin@Chile] >

Re: Help with UPS monitoring script.

Fri Jun 21, 2013 7:06 am

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
nothing
 
cinders
newbie
Topic Author
Posts: 42
Joined: Fri Oct 28, 2011 3:09 am

Re: Help with UPS monitoring script.

Fri Jun 21, 2013 10:25 am

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!
 
cinders
newbie
Topic Author
Posts: 42
Joined: Fri Oct 28, 2011 3:09 am

Re: Help with UPS monitoring script.

Sat Jun 22, 2013 4:51 pm

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.
 
mdmimoso
newbie
Posts: 27
Joined: Sat Mar 28, 2009 2:30 pm
Location: Portugal

Re: Help with UPS monitoring script.

Tue Oct 15, 2013 12:44 am

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
 
cinders
newbie
Topic Author
Posts: 42
Joined: Fri Oct 28, 2011 3:09 am

Re: Help with UPS monitoring script.

Tue Oct 15, 2013 2:54 am

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.
 
mdmimoso
newbie
Posts: 27
Joined: Sat Mar 28, 2009 2:30 pm
Location: Portugal

Re: Help with UPS monitoring script.

Tue Oct 15, 2013 1:21 pm

Hello Matt

I want to buy this APC , do you think that works?
http://www.apc.com/resource/include/tec ... b=features
 
mdmimoso
newbie
Posts: 27
Joined: Sat Mar 28, 2009 2:30 pm
Location: Portugal

Re: Help with UPS monitoring script.

Tue Oct 15, 2013 1:31 pm

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
 
cinders
newbie
Topic Author
Posts: 42
Joined: Fri Oct 28, 2011 3:09 am

Re: Help with UPS monitoring script.

Tue Oct 15, 2013 3:40 pm

Should be fine.
 
mdmimoso
newbie
Posts: 27
Joined: Sat Mar 28, 2009 2:30 pm
Location: Portugal

Re: Help with UPS monitoring script.

Tue Oct 15, 2013 3:56 pm

Witch one?
 
cinders
newbie
Topic Author
Posts: 42
Joined: Fri Oct 28, 2011 3:09 am

Re: Help with UPS monitoring script.

Wed Oct 16, 2013 2:56 am

Both of them. :)
 
mdmimoso
newbie
Posts: 27
Joined: Sat Mar 28, 2009 2:30 pm
Location: Portugal

Re: Help with UPS monitoring script.

Wed Oct 16, 2013 1:19 pm

hello matt

a can't setup ups, due to this message

could you please help
You do not have the required permissions to view the files attached to this post.
 
mdmimoso
newbie
Posts: 27
Joined: Sat Mar 28, 2009 2:30 pm
Location: Portugal

Re: Help with UPS monitoring script.

Wed Oct 16, 2013 1:51 pm

resolved :)
 
cinders
newbie
Topic Author
Posts: 42
Joined: Fri Oct 28, 2011 3:09 am

Re: Help with UPS monitoring script.

Wed Oct 16, 2013 2:38 pm

Glad to hear!
 
mdmimoso
newbie
Posts: 27
Joined: Sat Mar 28, 2009 2:30 pm
Location: Portugal

Re: Help with UPS monitoring script.

Wed Oct 16, 2013 2:44 pm

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
 
solelunauno
Member Candidate
Member Candidate
Posts: 114
Joined: Mon Jul 16, 2012 7:00 pm
Location: Roseto Capo Spulico CS Italy
Contact:

Re: Help with UPS monitoring script.

Thu Nov 07, 2013 11:49 am

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.
SL1 Systems srl MTCNA MTCRE
 
Erlington
just joined
Posts: 8
Joined: Thu Mar 20, 2014 7:17 pm

Re: Help with UPS monitoring script.

Thu Mar 20, 2014 7:22 pm

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?
 
Erlington
just joined
Posts: 8
Joined: Thu Mar 20, 2014 7:17 pm

Re: Help with UPS monitoring script.

Thu Mar 20, 2014 7:57 pm

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
 
unridaz
newbie
Posts: 34
Joined: Tue Mar 19, 2013 11:48 pm

Re: Help with UPS monitoring script.

Fri Apr 18, 2014 1:45 am

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.
 
trottolino1970
Member Candidate
Member Candidate
Posts: 192
Joined: Thu May 17, 2007 4:25 pm
Contact:

Re: Help with UPS monitoring script.

Tue Oct 14, 2014 1:20 pm

this script work with apc smart sc-420?
 
smartmoney
just joined
Posts: 4
Joined: Sat Nov 15, 2014 5:38 pm

Re: Help with UPS monitoring script.

Mon Dec 29, 2014 4:52 am

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?
 
mareksuk
just joined
Posts: 4
Joined: Tue Feb 25, 2014 12:32 pm
Location: CZE
Contact:

Re: Help with UPS monitoring script.

Sat Feb 25, 2017 6:31 pm

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

Who is online

Users browsing this forum: No registered users and 34 guests