Netwatch script

I would like to create a script that would take all static ip addresses and monitor them for up or down status. Upon a change in up or down, the script would email me the device status. I would like the subject to include the name of the device, and I would like the name to be whatever I have named the static ip address in the comments.

For example, if I have a printer at a static address 10.0.1.20, and in the comments it says “Printer”, I would like the script to have a subject of Printer, either up or down.

Is this something that can be done within the Mikrotik OS?

Thanks.

Yes. Have each netwatch run a unique script on up/down which sets some variables (name, up/down, date/time etc. etc.), then each of these scripts calls one script which does the e-mailing. Easy-peasy.

Ok, can you show me a similar script so I can learn from it and try to reverse engineer it?

I don’t know where to start.

In a word, no - I’d pretty much have to write it for you. However, the wiki pages on scripting should give you enough information to be able to write it yourself. If you can’t grok it from them, then I am afraid you will probably have to find a consultant to do it for you. The other option is to go for a third party service such as https://www.pingdom.com/ which does it all for you.

You have to write it for me then. I won’t take no for an answer!

Just kidding. Can you point me in the right direction on the variables for the name in the comments section? I can’t find any info on that at all.

The other stuff I can probably put together.

Maybe you can check it as i go along.

Thanks.

Try:

[admin@MikroTik] > /tool netwatch add host=10.0.1.20 comment="Printer" interval=1m down-script=printerdown     
[admin@MikroTik] > :put [/tool netwatch get value-name=comment [find host=10.0.1.20]]                              
Printer

Although I suspect you’re better off using the ‘printerdown’ script to set the description of the host.

Although scripts can help you in getting out of the box solution, but still i suggest you to go with DUDE, it can do much more in a more sophisticated way with details logging , SMS / Email / Voice and other customized actions,
Also Mikrotik DUDE is a FREE Centralized Network Monitoring/Management system. I am using in my company from past few years, and every one including me and management is pleased with its easy accessibility via web/client and extremely modified functions.
You can take a look at its working here
http://aacable.wordpress.com/2012/07/02/the-dude-show-us-your-map-series/

You could either use netwatch or just look through the dhcp leases and ping them. I have two scripts that do a lot of that…

Sent from my SCH-I545 using Tapatalk

Looking at my scripts I could most likely merge them and do what your looking for… would have to be on a recent version of RouterOS so I can use hash arrays… just let me know if you would be interested.

Got bored... Haven't tested it... but something like this..

Dead Peer Detector

Set Comments (Tunnel, Peer, Policy) = "+DPD++"

Variables

:local failureThreshold 3

:local pingResults {""}

:local linkDown do={
:put ("LINK DOWN - " . $1)
}

:global linkUp do={
:put ("LINK UP - " . $1)
}

Main Loop

/ip dhcp-server lease {
:foreach i in=[find (!dynamic && comment~"^([^+]\+DPD\+([^+])$"] do={
:local leaseComment [get $i comment]

	:local tempComment [:pick $leaseComment ([:find $leaseComment "+"]+1) [:len $leaseComment]]
	:local peerString [:pick $tempComment ([:find $tempComment "+"]+1) [:len $tempComment]]

	:local peerAddress [get $i address]

	:if ([:typeof ($pingResults->$leaseComment)] = "nothing") do={:set ($pingResults->$leaseComment) 0}

	:set pingResult [ping $peerAddress count=1]

	:if ($pingResult = 0) do={
		:if (($pingResults->$leaseComment) < ($failureThreshold + 2)) do={
			:set ($pingResults->$leaseComment) (($pingResults->$leaseComment) + 1)
			
			:if (($pingResults->$leaseComment) = $failureThreshold) do={
				$linkDown ($peerComment)
			}
		}
	}

	:if ($pingResult = 1) do={
		:if (($pingResults->$leaseComment) > 0) do={
			:set ($pingResults->$leaseComment) (($pingResults->$leaseComment) - 1)
			
			:if (($pingResults->$leaseComment) = ($failureThreshold - 1)) do={
				$linkUp ($peerComment)
			}
		}
	}
}

}

Eric,

Thanks for doing this. I am on version 5.26. I haven’t upgraded yet because I’m not sure of the stability of the newer versions. Do I need to upgrade to make this work?

Next, is the script going through all of the ip address on the network, or just the static ip addresses?

Next, does this script work with net watch, or is it stand alone? I didn’t see any email output in the script.

Thanks Eric.

  1. Yes, it requires 6.6 (or something) or higher. It uses associative arrays and functions. You could get rid of the functions, but it won’t really help you because it still uses the associative arrays and that will be much harder to get rid of.

  2. Basically the script looks for dhcp-leases that are BOTH STATIC and have a comment that looks like +DPD+. DPD meaning Dead Peer Detection. What it does is finds those hosts… pings them… if they are down it adds one to the counter, if they are up it doesn’t… and then once a host has three sequential dead pings it triggers the linkDown function with the string. Once it recovers it triggers the linkUp function with the string.

  3. You are correct. Just add whatever you want into the linkUp and LinkDown functions… e.g. swap put for /tools email etc…

-Eric

Eric,

I am going to hold off on this for now. I want to wait until version 6 (or 7) becomes more stable. I am going to stick with version 5 for now. For now I will just use net watch.

Thanks for all of your help as always.

Yep.

sir how bout ping is high and ISP is slow connection, how to monitor it send notification via sms?