Quantcast
Channel: MikroTik
Viewing all articles
Browse latest Browse all 22110

Scripting • Poor man's power failure monitoring

$
0
0
I did this before I noticed that /system ups ... even existed (insert :Sweating emoji here).
Since this will only work if the MikroTik has some power backup, maybe it's stupid of me to use this, instead of (now that I've noticed there is such a thing as /system ups ...), but, well, here it is if anyone cares.

This watches an ether* interface to see if the interface changes state. That interface should be connected to a simple router/switch/hub device which itself is NOT plugged into backup power. n.b. this is hardcoded to watch ether3, but of course that can be changed or made into a variable.
Code:
# Set up:local email "your@mail.org":local dateis [/system clock get date]:local timeis [/system clock get time]:local systemname [/system identity get name]:log info ("Power Monitor script starting at $dateis $timeis"):local lastpowerstate# Get the last known power state from the file "powerstate.txt", or set lastpowerstate to "up" because at some point it had to have been up or we wouldn't be here...:if ([:len [/file find where name="powerstate.txt"]]=0) do={ :log info ("Must create file powerstate.txt"); /file print file=powerstate.txt; # some MikroTik forum posts suggests that the devices may need a moment for the file creation effectuated by the above /file print file ... command to take effect, so do a delay :delay 2s; # Now set file contents to "up" /file set "powerstate.txt" contents="up"; # And set current state variable to "up" :set lastpowerstate "up";:log info ("created powerstate file and set it to up");} else={# Actually read last power state from the file:set lastpowerstate [/file get [find name=powerstate.txt] contents] ;#:log info ("last power state was $lastpowerstate");}# Get the current power state, which we assume matches whether ether3 is running (because we have a simple switch connected on ether3, which switch is NOT plugged into an Uninterruptable Power Supply):local powerstate ([/interface ethernet get ether3 running]=true)#:log info "Current power state based on Ether3 is $powerstate":if (($powerstate=true) && ($lastpowerstate="down")) do={   /tool e-mail send to="$email" subject="Power restored on router $systemname] at $dateis $timeis" body="Power restored on router $systemname at $dateis $timeis"   :log warning ("Power restored on router $[/system identity get name] at $dateis $timeis" body="Power restored on router $[/system identity get name] at $dateis $timeis")/file set "powerstate.txt" contents="up"} else= { :if (($powerstate=false) && ($lastpowerstate="up")) do={   /tool e-mail send to="$email" subject="Power failed on router $systemname at $dateis $timeis" body="Power failed on router $systemname at $dateis $timeis"   :log warning ("Power failed on router $systemname at $dateis $timeis" body="Power failed on router $systemname at $dateis $timeis")   /file set "powerstate.txt" contents="down" } else= {  :if ((($powerstate=false) && ($lastpowerstate="down")) || (($powerstate=true) && ($lastpowerstate="up"))) do={   :log info "Power Monitor nothing to do, no state change"  } }}

Statistics: Posted by libove — Thu Apr 17, 2025 5:39 pm



Viewing all articles
Browse latest Browse all 22110

Trending Articles