# Advanced Netwatch

**URL:** <https://forum.mikrotik.com/t/advanced-netwatch/158909>\
**Category:** Scripting\
**Created:** [June 22, 2022, 8:38am UTC](https://forum.mikrotik.com/t/advanced-netwatch/158909 "2022-06-22T08:38:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zorger](https://forum.mikrotik.com/letter_avatar/zorger/32/6_449c533f586939b6e5f246632a46421f.png) [@zorger](https://forum.mikrotik.com/u/zorger)\
**Post date:** [June 22, 2022, 8:38am UTC](https://forum.mikrotik.com/t/advanced-netwatch/158909/1 "2022-06-22T08:38:58Z")

</div>

Hi!

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

netwatch

up:

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

```

down:

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

```

config:

```plaintext
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

```plaintext
: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

```plaintext
: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?

---

<div class="post-metadata">

**Author:** ![Jotne](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/jotne/32/56097_2.png) [@Jotne](https://forum.mikrotik.com/u/Jotne)\
**Post date:** [June 22, 2022, 11:01am UTC](https://forum.mikrotik.com/t/advanced-netwatch/158909/2 "2022-06-22T11:01:45Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![eworm](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/eworm/32/1441_2.png) [@eworm](https://forum.mikrotik.com/u/eworm)\
**Post date:** [June 22, 2022, 2:18pm UTC](https://forum.mikrotik.com/t/advanced-netwatch/158909/3 "2022-06-22T14:18:22Z")

</div>

Or have a look at my scripts, especially [Notify on host up and down](https://git.eworm.de/cgit/routeros-scripts/about/doc/netwatch-notify.md).

---

<div class="post-metadata">

**Author:** ![zorger](https://forum.mikrotik.com/letter_avatar/zorger/32/6_449c533f586939b6e5f246632a46421f.png) [@zorger](https://forum.mikrotik.com/u/zorger)\
**Post date:** [June 25, 2022, 1:59pm UTC](https://forum.mikrotik.com/t/advanced-netwatch/158909/4 "2022-06-25T13:59:05Z")

</div>

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

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

```

UP code:

```plaintext
: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:

```plaintext
: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 } }

```

---

<div class="post-metadata">

**Author:** ![Jotne](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/jotne/32/56097_2.png) [@Jotne](https://forum.mikrotik.com/u/Jotne)\
**Post date:** [June 25, 2022, 6:21pm UTC](https://forum.mikrotik.com/t/advanced-netwatch/158909/5 "2022-06-25T18:21:35Z")

</div>

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