Community discussions

MikroTik App
 
NetWorker
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 98
Joined: Sun Jan 31, 2010 6:55 pm

UPS script

Thu Aug 31, 2017 8:14 pm

Hey yall!

The UPS script on the wiki https://wiki.mikrotik.com/wiki/UPS_scripts seems to be a little outdated when it comes to the online/onbattery flag.

On our APC SUA1000 attached to a RB750, the flag is called "on-line" instead of "on-battery":
Image
which causes the script to not initialize de global variables and to send a gazillion e-mails when the charge value goes below alarm value:
Image

Therefore, I've modified the script somewhat. Seems to be running ok on ROS 6.35 but I haven't tested during a real power outage (gear is mounted in a remote site and we didn't test the ups before leaving).
# UPS-Script powerfail
# (c) steinmann und weidinger OEG
# www.stone-rich.at
#
# Modified by networker for ROS v6
# All credits to the original author above
#
# WARNING, not lab tested!! Please report any bugs.
#
# Watches ups status and sends emails on power failure and low battery.
# This script will FAIL if:
# - Policies write, test, and read are not set
# - The system name contains non-standard characters (space, /, ...)
#
# user-configurable parameters below:

:local mailserver [:resolve smtp.wherever.com];
:local mailfrom "local@wherever.com";
:local mailto "admin@wherever.com";
:local upsName "ups1";

#
# do NOT make changes below!
#

:global flagonbatt;
:global flagbattlow;

:local battalarm 15;
:local battok 40;

:local curonline;
:local curcharge;

:local sysname [/system identity get name];
:local datetime "$[/system clock get date] $[/system clock get time]";

# First run? If so, we need to initialize the global flags
:if ([:typeof $flagonbatt]="nothing") do={:set flagonbatt 0}
:if ([:typeof $flagbattlow]="nothing") do={:set flagbattlow 0}

:set curonline true;
:set curcharge 100;
/system ups monitor [/system ups find name=$upsName] once do={
  :set curonline $"on-line"; :set curcharge $"battery-charge";
}

:if (($curonline=false) && ($flagonbatt=0)) do={
  :set flagonbatt 1;
 /tool e-mail send from=$mailfrom  to=$mailto server=$mailserver subject="$sysname: Power failure!" \
    body="$sysname  is on battery since $datetime";
  :log info "Power-Fail: EMail sent to $mailto";
}

:if (($curonline) && ($flagonbatt=1)) do={
 :set flagonbatt 0;
 /tool e-mail send from=$mailfrom  to=$mailto server=$mailserver subject="$sysname: Power is back" \
    body="$sysname is back on power since $datetime";
  :log info "Power-Restore: Email sent to $mailto";
}

:if (($curcharge <= $battalarm) && ($flagbattlow=0)) do={
  :set flagbattlow 1;
  /tool e-mail send from=$mailfrom  to=$mailto server=$mailserver subject="$sysname: Low battery!" \
    body="$sysname battery is at $curcharge %! $datetime";
  :log info "Batt-Low: Email sent to $mailto";
}

:if (($curcharge >= $battok) && ($flagbattlow=1)) do={
  :set flagbattlow 0;
  /tool e-mail send from=$mailfrom  to=$mailto server=$mailserver subject="$sysname: Battery recharged" \
    body="$sysname Battery recharged to $curcharge% $datetime";
  :log info "Batt-Recharged: Email sent to $mailto";
}
 
NetWorker
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 98
Joined: Sun Jan 31, 2010 6:55 pm

Re: UPS script

Mon Sep 04, 2017 7:06 pm

Guys, quick followup. The script won't read the previously run global variables if the policy policy is not set. So as it stands now, you need four of them for the script to behave: read, write, test and policy.

I've edited the comment in the script to reflect this:
# UPS-Script powerfail
# (c) steinmann und weidinger OEG
# www.stone-rich.at
#
# Modified by networker for ROS v6
# All credits to the original author above
#
# WARNING, not lab tested!! Please report any bugs.
#
# Watches ups status and sends emails on power failure and low battery.
# This script will FAIL if:
# - Policies read, write, test, and policy are not set
# - The system name contains non-standard characters (space, /, ...)
#
# user-configurable parameters below:

:local mailserver [:resolve smtp.wherever.com];
:local mailfrom "local@wherever.com";
:local mailto "admin@wherever.com";
:local upsName "ups1";

#
# do NOT make changes below!
#

:global flagonbatt;
:global flagbattlow;

:local battalarm 15;
:local battok 40;

:local curonline;
:local curcharge;

:local sysname [/system identity get name];
:local datetime "$[/system clock get date] $[/system clock get time]";

# First run? If so, we need to initialize the global flags
:if ([:typeof $flagonbatt]="nothing") do={:set flagonbatt 0}
:if ([:typeof $flagbattlow]="nothing") do={:set flagbattlow 0}

:set curonline true;
:set curcharge 100;
/system ups monitor [/system ups find name=$upsName] once do={
  :set curonline $"on-line"; :set curcharge $"battery-charge";
}

:if (($curonline=false) && ($flagonbatt=0)) do={
  :set flagonbatt 1;
 /tool e-mail send from=$mailfrom  to=$mailto server=$mailserver subject="$sysname: Power failure!" \
    body="$sysname  is on battery since $datetime";
  :log info "Power-Fail: EMail sent to $mailto";
}

:if (($curonline) && ($flagonbatt=1)) do={
 :set flagonbatt 0;
 /tool e-mail send from=$mailfrom  to=$mailto server=$mailserver subject="$sysname: Power is back" \
    body="$sysname is back on power since $datetime";
  :log info "Power-Restore: Email sent to $mailto";
}

:if (($curcharge <= $battalarm) && ($flagbattlow=0)) do={
  :set flagbattlow 1;
  /tool e-mail send from=$mailfrom  to=$mailto server=$mailserver subject="$sysname: Low battery!" \
    body="$sysname battery is at $curcharge %! $datetime";
  :log info "Batt-Low: Email sent to $mailto";
}

:if (($curcharge >= $battok) && ($flagbattlow=1)) do={
  :set flagbattlow 0;
  /tool e-mail send from=$mailfrom  to=$mailto server=$mailserver subject="$sysname: Battery recharged" \
    body="$sysname Battery recharged to $curcharge% $datetime";
  :log info "Batt-Recharged: Email sent to $mailto";
}

Who is online

Users browsing this forum: No registered users and 75 guests