Community discussions

MikroTik App
 
tabate47
Long time Member
Long time Member
Topic Author
Posts: 510
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

Netwatch script

Thu Mar 06, 2014 9:16 am

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.
 
User avatar
NAB
Trainer
Trainer
Posts: 542
Joined: Tue Feb 10, 2009 4:08 pm
Location: UK
Contact:

Re: Netwatch script

Thu Mar 06, 2014 11:32 am

Is this something that can be done within the Mikrotik OS?
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.
 
tabate47
Long time Member
Long time Member
Topic Author
Posts: 510
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

Re: Netwatch script

Thu Mar 06, 2014 4:49 pm

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.
 
User avatar
NAB
Trainer
Trainer
Posts: 542
Joined: Tue Feb 10, 2009 4:08 pm
Location: UK
Contact:

Re: Netwatch script

Thu Mar 06, 2014 4:58 pm

Ok, can you show me a similar script so I can learn from it and try to reverse engineer it?
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.
 
tabate47
Long time Member
Long time Member
Topic Author
Posts: 510
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

Re: Netwatch script

Thu Mar 06, 2014 5:04 pm

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.
 
User avatar
NAB
Trainer
Trainer
Posts: 542
Joined: Tue Feb 10, 2009 4:08 pm
Location: UK
Contact:

Re: Netwatch script

Sat Mar 08, 2014 2:08 am

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.
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.
 
User avatar
aacable
Member
Member
Posts: 435
Joined: Wed Sep 17, 2008 11:58 am
Location: ISLAMIC Republic of PAKISTAN
Contact:

Re: Netwatch script

Sat Mar 08, 2014 1:17 pm

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 ... ap-series/
 
efaden
Forum Guru
Forum Guru
Posts: 1708
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Netwatch script

Sat Mar 08, 2014 1:58 pm

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
 
efaden
Forum Guru
Forum Guru
Posts: 1708
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Netwatch script

Sun Mar 09, 2014 8:10 pm

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.
 
efaden
Forum Guru
Forum Guru
Posts: 1708
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Netwatch script

Sun Mar 09, 2014 8:47 pm

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..

ros code

#
# Dead Peer Detector
#

# Set Comments (Tunnel, Peer, Policy) = "+DPD+<STRING>+"

#
# 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)
				}
			}
		}
	}
}
 
tabate47
Long time Member
Long time Member
Topic Author
Posts: 510
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

Re: Netwatch script

Mon Mar 10, 2014 4:52 am

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.
 
efaden
Forum Guru
Forum Guru
Posts: 1708
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Netwatch script

Mon Mar 10, 2014 1:56 pm

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+<SOME STRING>. 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
 
tabate47
Long time Member
Long time Member
Topic Author
Posts: 510
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

Re: Netwatch script

Tue Mar 11, 2014 7:18 am

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.
 
efaden
Forum Guru
Forum Guru
Posts: 1708
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Netwatch script

Tue Mar 11, 2014 4:52 pm

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.
 
kcavery001
just joined
Posts: 9
Joined: Mon Feb 22, 2021 5:59 am

Re: Netwatch script

Tue Feb 23, 2021 9:08 am

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..

ros code

#
# Dead Peer Detector
#

# Set Comments (Tunnel, Peer, Policy) = "+DPD+<STRING>+"

#
# 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)
				}
			}
		}
	}
}
sir how bout ping is high and ISP is slow connection, how to monitor it send notification via sms?

Who is online

Users browsing this forum: Bing [Bot] and 115 guests