Useful scripts

This script is courtesy of user ukzerosniper from http://forum.mikrotik.com/t/alert-on-as-route-becomes-inactive/124643/3

This script searches a route based on it’s comment and sends an email based on status changes (UP / DOWN).

In my case my office has 3 WAN routes. Because they are fail-over and load-balanced, if one ISP fails, internet access is not affected and nobody knows. So I needed a script which would detect when a route fails and sends an email.


{
:global prevstatus;
:global status [:ip route get value-name=active [:ip route find comment="3.  Recursive lookup for Virgin connection (Via WAN routing table)"]]
:if ( "$status" != "$prevstatus" ) do={
:log warn "Virgin gateway changed status from \"$prevstatus\" -> \"$status\" (true=up, false=down, nothing=unintialized)";
:tool e-mail send to=user1@jdomain1.com subject="Virgin WAN gateway changed status" body=( [ :system clock get date ] . " " . [ :system clock get time ] . " gateway changed status from \"$prevstatus\" -> \"$status\" (true=up, false=down, nothing=unintialized)" )
:tool e-mail send to=user2@domain2.com subject="Virgin WAN gateway changed status" body=( [ :system clock get date ] . " " . [ :system clock get time ] . " gateway changed status from \"$prevstatus\" -> \"$status\" (true=up, false=down, nothing=unintialized)" )
}
:global prevstatus $status
}

All I needed to do was:

  1. Change the email address in the script
  2. Change the comment search text
  3. Make sure the SMTP server is setup properly so that the mikrotik can send emails
  4. Schedule the script to run every few minutes.
  5. Repeat for WAN-2 and WAN-3 (I assume you need to chg the global variables for status and prevstatus for each wan. For example statusw1 and prevtatusw1 for WAN-1, and so on)

I am a beginner to MT scripting and this script worked flawlessly. I don’t know if it is the optimal way to do so but for now it is perfect.

Note: The above script sends an email to 2 different email addresses. If you only need to send one email, delete the corresponding line(s).