Category Archives: Mikrotik

Sending SMS using Infobip service and MikroTik tool / fetch feature

My recent article related to enhancement of Netwatch feature in MikroTik was created as a prerequisite for a simple alerting solution with e-mail / SMS notifications channels.

I am using Infobip SMS platform and they have clear and simple API solution (nicely documented) for sending SMS messages (I was able to make it work from Powershell – documented here.).

I was searching a bit and I saw that MikroTik changed something in the way tool called fetch works when we need to send header fields – as it can be read here, here and in official documentation here.

The working command – tested on MikroTik with RouterOS 6.44.3 (june 2019) is:

/tool fetch http-method=post mode=https http-header-field=”content-type:application/json,Authorization:Basic key23123832″ http-data=”{ \”from\”:\”MyMonitoring\”, \”to\”:[\”386xxyyyzzz\”], \”text\”:\”HOST x.x.x.x DOWN\”}” url=https://api.infobip.com/sms/1/text/single

So – the important thing to point out is the way you provide http-header-field:
http-header-field=”content-type:application/json,Authorization:Basic key23123832″

Hope it helps!

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

 

Mikrotik Netwatch ENHANCED!

UPDATED!  https://luka.manojlovic.net/2019/06/01/mikrotik-netwatch-enhanced-updated-june-2019/

Mikrotik has a great feature of NetWatch where you can ping something and if it is down you can run a script and if it comes back up you can run another script…

The problem is that sometimes you can lose only a ping or two and you would like your Mikrotik’s NetWatch to wait for a bit longer…

Becouse of that I have written a script that only after X seconds fires the configuration change and also checks if there is another script running (and it stops the execution).

So in Tools/Netwatch I have configured:
001
002
003
004
And now let’s go to the scripts:
005
006
007
008
And if we check the log how it works:

Example when NetWatch IP is down for more than 10 seconds and example wenh NetWatch IP is up for more than 10 seconds…

009

And here the example when during check something happens Рso when IP goes down just for a couple of seconds and then returns  Рand the second example when it is offline and it just comes up for a couple of seconds..

010

Scripts:

OnWlanUp:

:foreach j in=[/system script job find] do={:if ([/system script job get $j value-name=script] = “OnWlanDown”) do={/system script job remove $j;}}
:local countup value=0;
:local WLANSSID “wlan.novagorica.eu”;
:local SSIDUP [/interface wireless get “01-wlan.novagorica.eu” ssid];
:if ($SSIDUP != $WLANSSID) do={
:while (($countup < 10) && ([/ping address=10.10.0.1 interval=1 count=1]=1)) do={:set countup value=($countup+1); :delay 1000ms; :log warning message=”wlan.novagorica.eu – if UP check $countup”;};
:if ($countup=10) do={/interface wireless set “01-wlan.novagorica.eu” ssid=”wlan.novagorica.eu”; /interface wireless set “01-wlan.novagorica.eu” security-profile=01-wlan.novagorica.eu-noauth ; :log warning message=”wlan.novagorica.eu – gateway up!”;};
} else {:log warning message=”wlan.novagorica.eu – gateway up! – no need to change SSID”;}

onWlanDown:

:foreach i in=[/system script job find] do={:if ([/system script job get $i value-name=script] = “OnUp”) do={/system script job remove $i;}}
:local countdown value=0;
:local WLANSSID “wlan.novagorica.eu”;
:local SSIDUP [/interface wireless get “01-wlan.novagorica.eu” ssid];
:if ($SSIDUP = $WLANSSID) do={
:while (($countdown < 10) && ([/ping address=10.10.0.1 interval=1 count=1]=0)) do={:set countdown value=($countdown+1); :delay 1000ms; :log error message=”wlan.novagorica.eu – if DOWN check $countdown”;}; :if ($countdown=10) do={/interface wireless set “01-wlan.novagorica.eu” ssid=”wlan.novagorica.eu-offline”; /interface wireless set “01-wlan.novagorica.eu” security-profile=01-wlan.novagorica.eu-offline; :log error message=”wlan.novagorica.eu gateway down!”;};
} else {:log error message=”wlan.novagorica.eu – gateway still down! – no need to change SSID”;}