Community discussions

MikroTik App
 
xzsdre
just joined
Topic Author
Posts: 6
Joined: Tue Jul 20, 2021 9:45 pm

Incomprehensible behavior of Netwatch and a script that imitates it.

Tue Jul 20, 2021 10:07 pm

Hello everyone. There are 2 similar questions.
1. If I configure a netwatch to ping a host on the network, even if mikrotik own local IP-address with the action to write to the log (for example Host online, Host offline), then immediately after rebooting microtik apparently cannot determine the availability of an IP-address (even its own) and writes to the log that the Host is offline, although in fact the host is online. Someone in the know what could be wrong?
2. To work around this problem, I tried using a script that monitors the previous availability state.

:global vdsdown
:local host 192.168.1.252
:local checkvds [/ping $host count=3]

:if (checkvds = 0) do={
:if ($vdsdown != true) do={
/log warning "Host $host is offline";
:set $vdsdown true
}
} else={
:if ($vdsdown = true) do={
/log info "Host $host is online";
:set $vdsdown false
}
}

But there is a nuance. On mikrotiK 962UiGS-5HacT2HnT, the script runs without problems, the entries in the log are one-time. On mikrotik RBLtAP-2HnD this script, if the host is not available, writes a message "Host 192.168.1.252 is offline" every time until the host becomes available again (https://prnt.sc/1dluijn). Firmware on 962UiGS-5HacT2HnT - 6.48.3 stable, firmware on RBLtAP-2HnD - 6.47.10 long term.

Maybe someone can help figure it out?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Incomprehensible behavior of Netwatch and a script that imitates it.  [SOLVED]

Wed Jul 21, 2021 7:52 pm

search tag # rextended check host status

evidenced errors on your script:

original code

:global vdsdown
:local host 192.168.1.252 # better put the editable things up
:local checkvds [/ping $host count=3]

	:if ($checkvds = 0) do={  # $ must used for call variables
		:if ($vdsdown != true) do={
		/log warning "Host $host is offline"; # useless ";" 
		:set $vdsdown true # $ must not be used on set
		}
	} else={
		:if ($vdsdown != false = true) do={ # better != false instead of =true
		/log info "Host $host is online"; # useless ";" 
		:set $vdsdown false # $ must not be used on set
		}
	}


My revised version:
:local host 192.168.0.101

:global vdsdown
:global vdsstatus

# for test "warning unstable" set the count to 2
:local checkvds [/ping $host count=3]

:if ($checkvds = 0) do={
    :if ($vdsstatus != "offline") do={ /log error "Host $host change status from $vdsstatus to offline" }
    :set vdsstatus "offline"
    :set vdsdown true
}

:if (($checkvds > 0) && ($checkvds < 3)) do={
    :if ($vdsstatus != "unstable") do={ /log warning "Host $host change status from $vdsstatus to unstable" }
    :set vdsstatus "unstable"
    :set vdsdown false
}

:if ($checkvds = 3) do={
    :if ($vdsstatus != "online") do={ /log info "Host $host change status from $vdsstatus to online" }
    :set vdsstatus "online"
    :set vdsdown false
}
 
xzsdre
just joined
Topic Author
Posts: 6
Joined: Tue Jul 20, 2021 9:45 pm

Re: Incomprehensible behavior of Netwatch and a script that imitates it.

Thu Jul 22, 2021 10:38 am

Thanks for the hints regarding the script. In this case, since the script works as it should, the question about the Netwatch has become irrelevant.
Thank you!
 
xzsdre
just joined
Topic Author
Posts: 6
Joined: Tue Jul 20, 2021 9:45 pm

Re: Incomprehensible behavior of Netwatch and a script that imitates it.

Thu Jul 22, 2021 3:44 pm

search tag # rextended check host status

evidenced errors on your script:

original code

:global vdsdown
:local host 192.168.1.252 # better put the editable things up
:local checkvds [/ping $host count=3]

	:if ($checkvds = 0) do={  # $ must used for call variables
		:if ($vdsdown != true) do={
		/log warning "Host $host is offline"; # useless ";" 
		:set $vdsdown true # $ must not be used on set
		}
	} else={
		:if ($vdsdown != false = true) do={ # better != false instead of =true
		/log info "Host $host is online"; # useless ";" 
		:set $vdsdown false # $ must not be used on set
		}
	}


My revised version:
:local host 192.168.0.101

:global vdsdown
:global vdsstatus

# for test "warning unstable" set the count to 2
:local checkvds [/ping $host count=3]

:if ($checkvds = 0) do={
    :if ($vdsstatus != "offline") do={ /log error "Host $host change status from $vdsstatus to offline" }
    :set vdsstatus "offline"
    :set vdsdown true
}

:if (($checkvds > 0) && ($checkvds < 3)) do={
    :if ($vdsstatus != "unstable") do={ /log warning "Host $host change status from $vdsstatus to unstable" }
    :set vdsstatus "unstable"
    :set vdsdown false
}

:if ($checkvds = 3) do={
    :if ($vdsstatus != "online") do={ /log info "Host $host change status from $vdsstatus to online" }
    :set vdsstatus "online"
    :set vdsdown false
}
I ask you to clarify one nuance. I corrected my version of the script and also tried yours. Both scripts, if run manually, work as expected. But if the same scripts are launched by the scheduler, the status of the host, online or offline, is constantly written to the log. I just can't understand what's the matter ...
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Incomprehensible behavior of Netwatch and a script that imitates it.

Thu Jul 22, 2021 3:53 pm

what's exactly is wroted inside script and scheduler?

/sys script export

/sys scheduler export
 
xzsdre
just joined
Topic Author
Posts: 6
Joined: Tue Jul 20, 2021 9:45 pm

Re: Incomprehensible behavior of Netwatch and a script that imitates it.

Thu Jul 22, 2021 4:23 pm

what's exactly is wroted inside script and scheduler?

/sys script export

/sys scheduler export
[mikrot@666] > /system scheduler export
# jul/22/2021 20:13:57 by RouterOS 6.47.10
# software id = S4YS-CZ1S
#
# model = RBLtAP-2HnD
# serial number = D7290CECC82A
/system scheduler
add interval=10s name=schedule_ping on-event="/system script run ping_192.168.1.252;" policy=\
reboot,read,write,test,password,sniff,sensitive,romon start-date=jul/12/2021 start-time=09:00:00


# jul/22/2021 20:15:44 by RouterOS 6.47.10
# software id = S4YS-CZ1S
#
# model = RBLtAP-2HnD
# serial number = D7290CECC82A
/system script
add dont-require-permissions=yes name=ping_192.168.1.252 owner=mikrot policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":\
global vdsdown\r\
\n:local host 192.168.1.252\r\
\n:local checkvds [/ping \$host count=3]\r\
\n\r\
\n\t:if (\$checkvds = 0) do={\r\
\n\t\t:if (\$vdsdown != true) do={\r\
\n/log warning \"Host \$host is offline\"\r\
\n\t\t:set \$vdsdown true\r\
\n\t\t}\r\
\n\t} else={\r\
\n\t\t:if (\$vdsdown != false) do={\r\
\n/log info \"Host \$host is online\"\r\
\n\t\t:set \$vdsdown false\r\
\n\t\t}\r\
\n\t}"

Here is the script and the scheduler, nothing special. When the scheduler runs this script every 10 seconds, we have in the log (screenshot https://prnt.sc/1ed2ifr). When manually started, the result is the same. What has changed or broken, I do not understand ...
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Incomprehensible behavior of Netwatch and a script that imitates it.

Thu Jul 22, 2021 4:31 pm

RouterOS version? I try on 6.47.10 and my version work as expected, the script do not create useless log.

10 seconds are too close, can overlap the executin. Use 20 seconds at least.

Try to paste it directly on scheduler but...

the $ after :set is still present!!!

xzsdre code

:global vdsdown
:local host 192.168.1.252
:local checkvds [/ping $host count=3]

	:if ($checkvds = 0) do={
		:if ($vdsdown != true) do={
/log warning "Host $host is offline"
		:set $vdsdown true
		}
	} else={
		:if ($vdsdown != false) do={
/log info "Host $host is online"
		:set $vdsdown false
		}
	}
 
xzsdre
just joined
Topic Author
Posts: 6
Joined: Tue Jul 20, 2021 9:45 pm

Re: Incomprehensible behavior of Netwatch and a script that imitates it.

Thu Jul 22, 2021 5:14 pm

RouterOS is 6.47.10 version (long-term). Thank you for the hint, I didn’t notice the "$" symbol. But it also didn't help for some reason. Log entries about the state of the hots are still written every time the script is executed ...
Okay, I'll try to figure out what's the matter. Thanks for the tips.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Incomprehensible behavior of Netwatch and a script that imitates it.

Thu Jul 22, 2021 5:18 pm

You use same global variable name vdsdown on two script or inside another active netwatch???
 
xzsdre
just joined
Topic Author
Posts: 6
Joined: Tue Jul 20, 2021 9:45 pm

Re: Incomprehensible behavior of Netwatch and a script that imitates it.

Thu Jul 22, 2021 5:27 pm

You use same global variable name vdsdown on two script or inside another active netwatch???
I use global variable name vdsdown on two script, but I turn them on in turn, so as not to interfere with each other.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Incomprehensible behavior of Netwatch and a script that imitates it.

Thu Jul 22, 2021 5:27 pm

try to change the var name......

Who is online

Users browsing this forum: NxtGen [Bot], Rhydu and 17 guests