Advanced Netwatch

Hi!

I’m writing some simple netwatch code to monitor some network device state with mikrotik NetWatch and telegram.

netwatch

up:

/tool fetch url="telegram bot get api url with message - link up" keep-result=no

down:

/tool fetch url="telegram bot get api url with message - link down" keep-result=no

config:

ip = some ip
interval = 60 sec
timeout = 5 sec

and all work, but it to often send me message, because of 1 ping packet droped in a minute when it check connection.

now i rewrite it, with

down

:if ([/ping IP interval=1 count=10] = 0) do={
/tool fetch url="telegram bot get api url with message - link down" keep-result=no
}

up

:if ([/ping IP interval=1 count=10] != 0) do={
/tool fetch url="telegram bot get api url with message - link up" keep-result=no
}

And now it send message about down only if realy down, more than 10 seconds.
But it stay to often send about link UP.


How properly solve this case?

I do use same script for both up and down in netwatch.
http://forum.mikrotik.com/t/tool-using-splunk-to-analyse-mikrotik-logs-4-0-graphing-everything/153043/3

You can just change :log to telegram

Or have a look at my scripts, especially Notify on host up and down.

Thanks to all! I make decision to write code in netwatch up/down, without script
Prepare:

:global filename "hcoreisup.txt"
/file print file=$filename where name=""
/file set $filename contents="UP"

UP code:

:global filename "hcoreisup.txt"
:global hcoreisup ""
:global hcoreisup [/file get $filename contents];
:if ($hcoreisup="") do={ log/info message="hcoreisup not defined"
	:if ([/ping 172.16.33.1 interval=1 count=10] = 0 ) do={ :global hcoreisup "DOWN" } else={ :global hcoreisup "UP" }
	  /file print file=$filename where name=""
	  :delay 1
	  /file set $filename contents=$hcoreisup
	  log/info message=$hcoreisup
} else={ :if ($hcoreisup="DOWN") do={:if ([/ping 172.16.33.1 interval=1 count=10] = 0 ) do={:global hcoreisup "DOWN"} else={ :global hcoreisup "UP"
/tool fetch url="https://api.telegram.org/BOT_API_KEY/sendMessage\?chat_id=-CHAT_1_ID&text=URLENCODED_TEXT" keep-result=no
/tool fetch url="https://api.telegram.org/BOT_API_KEY/sendMessage\?chat_id=-CHAT_2_ID&text=URLENCODED_TEXT" keep-result=no
				/file set $filename contents=$hcoreisup
				log/info message=$hcoreisup } } else={ log/info message=$hcoreisup } }

DOWN code:

:global filename "hcoreisup.txt"
:global hcoreisup ""
:global hcoreisup [/file get $filename contents];
:if ($hcoreisup="") do={ log/info message="hcoreisup not defined"
	:if ([/ping 172.16.33.1 interval=1 count=10] = 0 ) do={ :global hcoreisup "DOWN" } else={ :global hcoreisup "UP" }
		/file print file=$filename where name=""
		:delay 1
		/file set $filename contents=$hcoreisup
		log/info message=$hcoreisup
} else={ :if ($hcoreisup="UP") do={:if ([/ping 172.16.33.1 interval=1 count=10] = 0 ) do={ :global hcoreisup "DOWN"
/tool fetch url="https://api.telegram.org/BOT_API_KEY/sendMessage\?chat_id=-CHAT_1_ID&text=URLENCODED_TEXT" keep-result=no
/tool fetch url="https://api.telegram.org/BOT_API_KEY/sendMessage\?chat_id=-CHAT_2_ID&text=URLENCODED_TEXT" keep-result=no
				/file set $filename contents=$hcoreisup
				log/info message=$hcoreisup } } else={ log/info message=$hcoreisup } }

You do not need two script. Use same script for both up/down and then use the status variable to see if its up/down as I already posted above:
http://forum.mikrotik.com/t/tool-using-splunk-to-analyse-mikrotik-logs-4-0-graphing-everything/153043/3

Any reason for using global variable in the script? Use local variable if you do not intend to store the data for later use or use in another script.