
This is a script made to check router voltage and send mail if abnormal state is reached.
It has to be scheduled every 15 or 30 mins after startup initialization script. It can automatically learn correct threshold
after the first startup initialization. So, if router starts on battery power, it will set the correct threshold after battery
fully charged and some auto-reiterations of “lowvalarm script”;
It can also discriminate battery low but charging and send OK mail till if battery isn’t already fully charged (but mains is present).
The startup-alarm initializes varibles and sends a “reboot” mail notice, so you will know if router has been rebooted because of battery completely discharged or every other reason.
I made a fully automated configuration script (.rsc) to set a router with all necessary things, which are:
- startup-alarm script; scheduled at startup only; it initializes all variables, included mail recipient
- lowvalarm script; scheduled every 15 mins (or 30 , according to your power backup time capability).
- set_mail script; to be run only once to configure “/tool e-mail” with your sender data.
- smtp_resolve script; to be scheduled every hour to resolve smtp domain ip address ad eventually update.
So you only have to put your RECIPIENT address in startup-alarm script, SENDER data in set_mail script and SMTP server in smpt_resolve script.
After those customizations, you can load the customized .rsc and you will obtain this behavior: - script is loaded and set_mail, smtp_resolve and startup-alarm are launched in sequence;
- system will wait for 120 seconds (according to startup-alarm policy), and then send “reboot” mail notice;
- system will inform that script has been loaded and executed successfully.
This script can also be used with UPS power supply, without serial communication.
I explained here http://forum.mikrotik.com/t/supported-ups-s/44249/1 how this goal could be achieved.
/system script
add name=startup-alarm policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive source="#Script\
\_startup initialization for lowvalarm SL1Systems's script\r\
\n:log info message=startup_initialization_started\r\
\n#alarm is the current state memory variable (normal=0, low voltage=1)\r\
\n:global alarm\r\
\n#lowvalarm is the alarm low volt threshold; it's automatically \r\
\n:global lowvalarm\r\
\n#vbefore and vgrowth are check variables, needed to let script know\r\
\n#if voltage is decreasing (battery discharge), or increasing\r\
\n#(mains present, battery recharging);\r\
\n:global vbefore\r\
\n:global vgrowth\r\
\n:global voltage [/system health get voltage]\r\
\n:local thisbox [/system identity get name]\r\
\n:local thisdate [/system clock get date]\r\
\n:local thistime [/system clock get time]\r\
\n#initialization of state and check variables; lowvalarm is temporarily\r\
\n#set at current voltage -3 (-0.3V), but will be modified by lowvalarm\r\
\n#script if battery wasn't already fully charged at router startup.\r\
\n:set alarm 0\r\
\n:set lowvalarm (\$voltage - 3)\r\
\n:set vbefore \$voltage\r\
\n:set vgrowth \$voltage\r\
\n#Mail address used as recipient;\r\
\n#it's initialized here.\r\
\n:global recipient YOURADDRESS_RECIPIENT@TOURDOMAIN.COM\r\
\n:log info message=startup_initialization_completed\r\
\n#delay of 120 secs to wait for boot completed\r\
\n:delay 120\r\
\n#send mail to inform that router has been restarted:\r\
\n/tool e-mail send to=\"\$recipient\" \\\r\
\nsubject=\"\$thisbox rebooted on \$thisdate at \$thistime\";\r\
\n:log info message=startup_mail_sent"
add name=lowvalarm policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive source="#Script\
\_voltage check by Nicola Giampietro (SL1 Systems, Italy)\r\
\n#This script has to be scheduled every 15 or 30 mins after startup\r\
\n#initialization script. It can automatically learn correct threshold\r\
\n#after the initial startup initialization (if router starts on\r\
\n#battery power, it will set the correct threshold after battery\r\
\n#fully charged and some auto-reiterations of this script);\r\
\n#It can also discriminate battery low and charging and send OK mail\r\
\n#if battery isn't already fully charged (but mains is present).\r\
\n:log info message=lowvalarm_check_started;\r\
\n#alarm is the current state memory variable (normal=0, low voltage=1)\r\
\n:global alarm\r\
\n#lowvalarm is the alarm low volt threshold; it's automatically \r\
\n#learned after startup of router;\r\
\n:global lowvalarm\r\
\n#vbefore and vgrowth are check variables, needed to let script know\r\
\n#if voltage is decreasing (battery discharge), or increasing\r\
\n#(mains present, battery recharging);\r\
\n:global vbefore\r\
\n:global vgrowth\r\
\n:global voltage [/system health get voltage]\r\
\n:local thisbox [/system identity get name]\r\
\n:local thisdate [/system clock get date]\r\
\n:local thistime [/system clock get time]\r\
\n#Mail address used as recipient;\r\
\n#it's initialized by startup script, so it hasn't to be specified here.\
\r\
\n:global recipient\r\
\n#Check first if current voltage is under threshold:\r\
\n:if (\$voltage < \$lowvalarm) do= \\\r\
\n#Check if current voltage decreases:\r\
\n {:if (\$voltage < \$vgrowth) do= \\\r\
\n#Check if current voltage is really decreased, or if it's a spike!\r\
\n {:if (\$voltage < (\$vbefore-2)) do= \\\r\
\n#update vbefore, for future checking.\r\
\n {:set vbefore \$voltage; \\\r\
\n#Check if router is already in low voltage state (LOW mail already sent)\
;\r\
\n#if not (alarm=0), it will send mail and set alarm=1:\r\
\n :if (\$alarm=0) do= \\\r\
\n {/tool e-mail send to=\"\$recipient\" subject=\"\$thisbox \\\r\
\n\t LOW voltage on \$thisdate at \$thistime\"; :set alarm 1; \\\r\
\n\t}\r\
\n }\r\
\n }\\\r\
\n else= \\\r\
\n#Check if voltage increases;\r\
\n {:if (\$voltage > \$vgrowth) do= \\\r\
\n#Check if current voltage is really increased, or if it's a spike!\r\
\n {:if (\$voltage > (\$vbefore+2)) do= \\\r\
\n#update vbefore, for future checking.\r\
\n {:set vbefore \$voltage; \\\r\
\n#Check if router is already in normal state (OK mail already sent);\r\
\n#if not (alarm=1), it will send mail and reset alarm=0:\r\
\n\t :if (\$alarm=1) do= \\\r\
\n {/tool e-mail send to=\"\$recipient\" subject=\"\$thisbox \\\r\
\n\t voltage OK on \$thisdate at \$thistime\"; :set alarm 0; \\\r\
\n\t }\r\
\n\t}\r\
\n }\r\
\n }\r\
\n }\\\r\
\n#if voltage is over threshold:\r\
\nelse= \\\r\
\n#Check if router is already in normal state (OK mail already sent);\r\
\n#if not (alarm=1), it will send mail and reset alarm=0:\r\
\n {:if (\$alarm=1) do= \\\r\
\n {/tool e-mail send to=\"\$recipient\" subject=\"\$thisbox \\\r\
\n voltage OK on \$thisdate at \$thistime\"; :set alarm 0; \\\r\
\n }\r\
\n#Check if voltage increases;\r\
\n :if (\$voltage > \$vgrowth) do= \\\r\
\n#update vbefore, for future checking.\r\
\n {:set vbefore \$voltage; \\\r\
\n#follows battery recharging, to determine lowvalarm threshold,\r\
\n#which is normal voltage - 3 (- 0.3V).\r\
\n if (\$voltage > (\$lowvalarm+3)) do= \\\r\
\n#update lowvalarm threshold:\r\
\n {:set lowvalarm (\$voltage-3); \\\r\
\n }\r\
\n }\r\
\n }\r\
\n#update vgrowth, for future checking, at every script launch.\r\
\n:set vgrowth \$voltage;\r\
\n:log info message=lowvalarm_check_completed;\r\
\n"
add name=set_mail policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive source="#Script\
\_to configure /tool e-mail; to be run only once.\r\
\n#Mail address used as sender;\r\
\n#it's initialized here.\r\
\n:global sender YOURADDRESS_SENDER@YOURDOMAIN.COM\r\
\n#Password for smtp authentication:\r\
\n:global password YOURPASSWORD\r\
\n:global port 25\r\
\n#Set parameters in /tool e-mail only once:\r\
\n/tool e-mail set from=\$sender\r\
\n/tool e-mail set user=\$sender\r\
\n/tool e-mail set password=\$password\r\
\n/tool e-mail set port=\$port
\n:log info message=mail_setup_completed;\r\
\n"
add name=smtp_resolve policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive source="#Script\
\_to resolve smtp address and update if it's changed:\r\
\n#To be scheduled.\r\
\n:local ipsmtp\r\
\n:set ipsmtp [:resolve MAIL.YOURSMTP.COM]\r\
\nif (\$ipsmtp != [/tool e-mail get address]) do={ /tool e-mail set addres\
s=\$ipsmtp}
\n:log info message=smtp_resolve_completed;\r\
\n"
/system scheduler
add name=startup-alarm on-event=startup-alarm policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive start-time=\
startup
add interval=1h name=smtp-resolve on-event=smtp-resolve policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive start-time=\
startup
add interval=15m name=lowvalarm on-event=lowvalarm policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive start-time=\
startup
/system script run set_mail;
/system script run smtp_resolve;
/system script run startup-alarm;
I also made a flow chart to explain how lowvolt alarm script works. It’s attached here.