Community discussions

MikroTik App
 
zorger
just joined
Topic Author
Posts: 2
Joined: Wed Jun 22, 2022 11:30 am

Advanced Netwatch

Wed Jun 22, 2022 11:38 am

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?
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Advanced Netwatch

Wed Jun 22, 2022 2:01 pm

I do use same script for both up and down in netwatch.
viewtopic.php?p=888800#p888800

You can just change :log to telegram
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Advanced Netwatch

Wed Jun 22, 2022 5:18 pm

Or have a look at my scripts, especially Notify on host up and down.
 
zorger
just joined
Topic Author
Posts: 2
Joined: Wed Jun 22, 2022 11:30 am

Re: Advanced Netwatch

Sat Jun 25, 2022 4:59 pm

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 } }
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Advanced Netwatch

Sat Jun 25, 2022 9:21 pm

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:
viewtopic.php?p=888800#p888800

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.

Who is online

Users browsing this forum: No registered users and 17 guests