MikroTik – Netwatch enhanced (updated June 2019)

With MikroTik one can create an excellent e-mail / SMS alerting system when a host goes down or returns up.
In Tools there is Netwatch feature – but it has one disadvantage – it triggers “up” or “down” commands / scripts – but sometimes one missed ping does not mean that the host is permanently offline / online. Because of that I have written a script that can extend the ping checks (in my example for another 10 seconds – after first ping failed (triggered by Netwatch)) and only after being absolutely sure that host is offline or online triggers an event – e-mail message or SMS message (using some SMS gateway – covered in this article).

As you can see scripts can successfully handle event when host goes offline and when it comes online again:
example

Scripts can also handle “flapping host” (host going down and returning up in less then 10 seconds) behavior:

example2

What do you need to setup such monitoring system:
1. Tools / Netwatch – create two entries for same host:
example 3

2. System / Scripts – you will need to create two scripts – for down and up events:
scripts

on-down – script:
:log error message=”Host x.x.x.x is down! Disabling Netwatch host down monitoring – taking over with script on-down – checking reachability of host x.x.x.x each second for ten seconds!”
:tool netwatch disable numbers=0
:local countup value=0
:while (($countup < 10) && ([:ping address=x.x.x.x interval=1 count=1]=0)) do={:set countup value=($countup+1); :delay 1000ms; :log error message=”Host x.x.x.x is offline. Check number: $countup” };
:if ($countup < 10) do={:log warning message=”Host x.x.x.x online again in less than ten seconds/checks – up on check number: $countup. Enabling netwatch.”; :tool netwatch enable numbers=0; :tool e-mail send to=my.email@gmail.com subject=”Host Up after $countup” body=”Host Up after $countup”; } else={:log error message=”After ten seconds/checks host x.x.x.x is still offline – probably there is an major issue/outage – sending e-mail/SMS.”; :tool e-mail send to=my.email@gmail.com subject=”Host x.x.x.x Down” body=”Host x.x.x.x Down! Host x.x.x.x Down!”;}
:tool netwatch enable numbers=1

on-up – script:
:log warning message=”Host x.x.x.x is up! Disabling Netwatch host up monitoring – taking over with script on-up – checking reachability of host x.x.x.x each second for ten seconds!”
:tool netwatch enable numbers=0
:local countdown value=0
:while (($countdown < 10) && ([:ping address=x.x.x.x interval=1 count=1]=1)) do={:set countdown value=($countdown+1); :delay 1000ms; :log warning message=”Host x.x.x.x is online. Check number: $countdown” };
:if ($countdown < 10) do={:log error message=”Host x.x.x.x offline again in less than ten seconds/checks – down on check number: $countdown. Enabling netwatch monitoring.”; :tool netwatch enable numbers=1; :tool e-mail send to=luka@manojlovic.net subject=”Host x.x.x.x Down after $countup” body=”Host x.x.x.x Down after $countup”; } else={:log warning message=”After ten seconds/checks host x.x.x.x is still online – probably everything is ok – sending e-mail/SMS.”; :tool e-mail send to=luka@manojlovic.net subject=”Host x.x.x.x Up” body=”Host x.x.x.x Up! Up!”;}
:tool netwatch enable numbers=0

 

6 thoughts on “MikroTik – Netwatch enhanced (updated June 2019)

  1. Pingback: Mikrotik Netwatch ENHANCED! | Luka Manojlovic

  2. Pingback: Scripting • MikroTik – Netwatch enhanced (updated June 2019) – Mags Forum Technology

  3. Pingback: Sending SMS using Infobip service and MikroTik tool / fetch feature | Luka Manojlovic

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.