Community discussions

MUM Europe 2020
 
cicserver
Member
Member
Topic Author
Posts: 301
Joined: Sun Jul 24, 2011 12:04 pm

Internet Connectivity Monitor Script requierd [SOLVED]

Wed May 28, 2014 1:20 pm

Currently I am using Internet monitoring script (actually NETWATCH) which check Google Dns IP 8.8.8.8 and if receive no ping reply , it switches to my backup link by changing route distance. working fine.

But the issue is that sometime Google DNS ping timeout dueto my ISP issue and rest of internet works fine , but the netwatch consider it down (dueto single host checkup)

So how can I add at least 2 to 3 multiple hosts and if the script dont receive reply from all of them , then it should take action (like route change or whatever i want)

HELP please :(
Last edited by cicserver on Thu Jun 12, 2014 10:42 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 2950
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Internet Connectivity Monitor Script requierd

Wed May 28, 2014 3:28 pm

use 3 global variables on 3 netwatch different address, and when all 3 variables are set, change the route...
I'm Italian, not English. Sorry for my imperfect grammar.
 
cicserver
Member
Member
Topic Author
Posts: 301
Joined: Sun Jul 24, 2011 12:04 pm

Re: Internet Connectivity Monitor Script requierd

Thu May 29, 2014 6:30 am

I think I will not be able to understand the script coding :D as i m really dumb in this section.
I will be really thankful to you if you post an example code , Plzzz
 
cicserver
Member
Member
Topic Author
Posts: 301
Joined: Sun Jul 24, 2011 12:04 pm

Re: Internet Connectivity Monitor Script requierd

Sun Jun 08, 2014 12:18 pm

HELP Please :(
 
mainTAP
newbie
Posts: 36
Joined: Tue Oct 02, 2012 4:01 am

Re: Internet Connectivity Monitor Script requierd

Sun Jun 08, 2014 12:50 pm

Hi cicserver, instead of the inbuilt watchdog I am using a simple script which ping an IP address 5 times not just once and just after all 5 failed attempts it performs an action ( this can be adjusted to less than 5 ) . Hope this might help :
       :local i 0;
       :local F 0;
       :local date;
       :local time;
       
       :global InternetStatus;
       :global InternetLastChange;
       
       :for i from=1 to=5 do={
       \tif ([/ping 8.8.8.8 count=1]=0) do={:set F ($F + 1)}
       \t:delay 1;
       };
       \t\t\t\t
       :if (($F=5)) do={
       \t:if (($InternetStatus="UP")) do={
       \t\t:log info "WARNING : The INTERNET service's gone DOWN";
       \t\t:set InternetStatus "DOWN";
       
       ##do some action if needed    
 
       \t\t:set date [/system clock get date];
       \t\t:set time [/system clock get time];
       \t\t:set InternetLastChange ($time . " " . $date);
       \t\t\t\t
       \t} else={:set InternetStatus "DOWN";}
       } else={
       \t:if (($InternetStatus="DOWN")) do={\t
       \t\t:log info "WARNING : The INTERNET service's gone UP"; 
       \t\t:set InternetStatus "UP";
       
       ##do some action if needed
           
       \t\t:set date [/system clock get date];
       \t\t:set time [/system clock get time];
       \t\t:set InternetLastChange ($time . " " . $date);
       \t\t\t\t
       \t} else={:set InternetStatus "UP";}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 2950
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Internet Connectivity Monitor Script requierd

Sun Jun 08, 2014 1:04 pm

The syntax

:set variablename valueofthevariable

ARE WRONG,

must be

:set $variablename value=valueofthevariable;


:set not define new variable, like :local or :global, it use already defined variable, and $ must be put before variable name.

Actually it's working for compatibility, but in future version the support for wrong syntax can be removed.
I'm Italian, not English. Sorry for my imperfect grammar.
 
mainTAP
newbie
Posts: 36
Joined: Tue Oct 02, 2012 4:01 am

Re: Internet Connectivity Monitor Script requierd

Sun Jun 08, 2014 1:38 pm

Thanks for the correction. Unfortunately there are not many resources about mikrotik scripting language.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 2950
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Internet Connectivity Monitor Script requierd

Sun Jun 08, 2014 2:51 pm

Thanks for the correction. Unfortunately there are not many resources about mikrotik scripting language.
Right...
I'm Italian, not English. Sorry for my imperfect grammar.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Internet Connectivity Monitor Script requierd

Sun Jun 08, 2014 3:20 pm

The syntax

:set variablename valueofthevariable

ARE WRONG,

must be

:set $variablename value=valueofthevariable;


:set not define new variable, like :local or :global, it use already defined variable, and $ must be put before variable name.

Actually it's working for compatibility, but in future version the support for wrong syntax can be removed.
While both syntaxes work indeed, I must say that it seems the syntax you call "right" is actually violating normal mode of expression interpolating... making me hope the "right" syntax would be removed in favor of the "wrong" syntax.

Instead, one would expect that the "right" syntax would eventually be used for variable variables, so that for example:

ros code

:local varname oldValue
:local newvar varname

# EXPECTED:
# Because the value of $newvar is "varname",
# set the variable called "varname" to the value "newValue".
:set $newvar newValue

# ACTUAL:
# Sets the variable "newvar" to the value "newValue",
# and keeps "varname" unmodified.
:put $varname
:put $newvar
would output
newValue
varname
as opposed to the current
oldValue
newValue
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 2950
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Internet Connectivity Monitor Script requierd

Sun Jun 08, 2014 11:11 pm

It's perfect logical what you obtain from:
:local varname value=oldValue;
--> create new variable called varname and set it's value to string "oldValue"

:local newvar value=varname;
--> create new variable called newvar and set it's value to string "varname"

:set $newvar value=newValue;
--> now set newvar variable content to string "newValue"

:put $varname
--> varname containing "oldValue" and correctly display "oldValue"

:put $newvar
--> newvar containing "newValue" and correctly display "newValue"
if you use correct syntax you obtain the same:
:local varname value=oldValue;
--> create new variable called varname and set it's value to string "oldValue"

:local newvar value=$varname;
--> create new variable called newvar and set it's value equal to variable varname

:set $newvar value=newValue;
--> now set newvar variable content to string "newValue"

:put $varname
--> varname containing "oldValue" and correctly display "oldValue"

:put $newvar
--> newvar containing "newValue" and correctly display "newValue"
How you think to obtain
newValue
varname
as output of the two ":put"?

That variable are two distinct variables, if you need only one, use one.
The "pointer" type of variable do not exist on that scripting language.

And about share same value because one variable is previous set as another variable, I think is the worst way of programming code, without explicit use of pointer.
I'm Italian, not English. Sorry for my imperfect grammar.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Internet Connectivity Monitor Script requierd

Mon Jun 09, 2014 12:15 am

if you use correct syntax you obtain the same:
Yes, I got that. They indeed obtain the same in current versions.

I'm just saying that I don't think the so called "right" syntax makes sense.
:set $newvar value=newValue;
--> now set newvar variable content to string "newValue"
But see... THAT doesn't make sense... With all other commands, the normal process of interpretation is for
[command] ([unnamed parameter VALUE])* ([named parameter]=[value of named parameter])*
to become
[command] [unnamed parameter]=[value of unnamed parameter] [named parameter]=[value of named parameter]

So for example, in the code
:local interface LAN
/interface monitor-traffic $interface
the later command is interpolated to
/interface monitor-traffic interface="LAN"
and this is true even for the "local" and "global" commands, i.e.
:local varB $interface
is interpolated to
/local name="varB" value="LAN"
But with "set"... no... that normal flow is broken, and instead of
:set $varB $interface
being interpolated to
/set name="LAN" value="LAN"
it's suddenly
/set name="varB" value="LAN"
completely flying in the face of how any other command would deal with variables.
The "pointer" type of variable do not exist on that scripting language.
Neither does it exist in PHP... "variable variables" and "pointers" are two distinct features of a language, and in fact, PHP has the former - see here... In the case of PHP, there's a different syntax required, which can make code confusing, but in RouterOS' scripting language, it's the lack of a different syntax that seems to make things more confusing.
And about share same value because one variable is previous set as another variable, I think is the worst way of programming code, without explicit use of pointer.
I'll agree that "variable variables" aren't in general a good programming practice, and I for one avoid them in PHP, and would probably avoid them in RouterOS scripting too. But just like any programming tool, they should be available for use if needed, which would arguably be more often in RouterOS scripting that in PHP.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 2950
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Internet Connectivity Monitor Script requierd

Mon Jun 09, 2014 2:11 am

But with "set"... no... that normal flow is broken, and instead of
:set $varB $interface
being interpolated to
/set name="LAN" value="LAN"
it's suddenly
/set name="varB" value="LAN"
(really is interpolated to /set name=$varB value="LAN")
completely flying in the face of how any other command would deal with variables.
Now I catch your point...

Like

:set $varB $interface --> :set eval($varb) $interface

eval is a function to "evaluate" code ;)

But if you try to use "/set name=" you get one error:

ros code

[admin@MATRIX] > /set value=k name=x
syntax error (line 1 column 19)
[admin@MATRIX] > /set value=k name=$x
[admin@MATRIX] >
because also "/set name=" want as argument one variable, defined or not.

(The code are from RouterOS 6.14)
I'm Italian, not English. Sorry for my imperfect grammar.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 2950
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Internet Connectivity Monitor Script requierd

Mon Jun 09, 2014 2:30 am

But I hope you agree with me:

MikroTik must extend read/write support from/to file to more than 4096 Bytes.
Missing float numbers.
toarray function: must be passed as parameter what I want to split to array, not only ","
make possible to save function output, like "ping"

Surely I forget others...
I'm Italian, not English. Sorry for my imperfect grammar.
 
AlexS
Member Candidate
Member Candidate
Posts: 259
Joined: Thu Oct 10, 2013 7:21 am

Re: Internet Connectivity Monitor Script requierd

Mon Jun 09, 2014 10:03 am

Have to agree with boen_robot
 
cicserver
Member
Member
Topic Author
Posts: 301
Joined: Sun Jul 24, 2011 12:04 pm

Re: Internet Connectivity Monitor Script requierd

Mon Jun 09, 2014 1:37 pm

Lolz everyone have forgotten the real question asked by me in this thread, and started there conversation :P

Can any one please post a script which can check at least two or three wan hosts, and if found all of three dead, then its should change the route distance (or whatever action I would like it to take) and then if any single WAN host replies back , then it should change the distance back to normal.
 
mainTAP
newbie
Posts: 36
Joined: Tue Oct 02, 2012 4:01 am

Re: Internet Connectivity Monitor Script requierd

Mon Jun 09, 2014 3:45 pm

Man, try to read the reply after your previous post !
 
cicserver
Member
Member
Topic Author
Posts: 301
Joined: Sun Jul 24, 2011 12:04 pm

Re: Internet Connectivity Monitor Script requierd

Wed Jun 11, 2014 7:11 am

Man, try to read the reply after your previous post !
Dear mainTAP,

I am really grateful to you for the script. It's working fine, But the issue is that it checks only single / one HOST. I want to check at least 2 hosts for cross verification. Is it possible to add? how?

One question more, How many times does Mikrotik's NETWATCH pings the host before considering it UP or DOWN? one time or ??

Thank you for user friendly script post. :D
 
mainTAP
newbie
Posts: 36
Joined: Tue Oct 02, 2012 4:01 am

Re: Internet Connectivity Monitor Script requierd

Wed Jun 11, 2014 8:13 am

You are welcome, to add more hosts would be easy, how many would you like to check for how many repeats? Do you want to consider the link down only if all the attempts fail?

As for the netwach functionality, i believe it pings just once and makes its decision upon the outcome.
 
cicserver
Member
Member
Topic Author
Posts: 301
Joined: Sun Jul 24, 2011 12:04 pm

Re: Internet Connectivity Monitor Script requierd

Wed Jun 11, 2014 8:27 am

You are welcome, to add more hosts would be easy, how many would you like to check for how many repeats? Do you want to consider the link down only if all the attempts fail?
As for the netwach functionality, i believe it pings just once and makes its decision upon the outcome.
Ping at least 2 Hosts, and 5 times ping count. If it donot receive all ping replies from both hosts, then its should consider the link DOWN. but if any single host is reachable, then the link should be considered UP.

Thank you for being kind :D :)
 
mainTAP
newbie
Posts: 36
Joined: Tue Oct 02, 2012 4:01 am

Re: Internet Connectivity Monitor Script requierd

Wed Jun 11, 2014 9:41 am

I'll post the script in couple of hours when I get to a PC ;)
 
mainTAP
newbie
Posts: 36
Joined: Tue Oct 02, 2012 4:01 am

Re: Internet Connectivity Monitor Script requierd

Wed Jun 11, 2014 4:12 pm

Here is the code for 2 websites, it can be easily modified to test more addresses. It is rather simple and could be done better but it might help you.
Please be aware that the script expects the InternetStatus to be already set, meaning it runs the '#do something commands' just when the status changes , if you run the script for the first time it just populate the variable but would not action.
:local i 0;
:local F 0;
:local date;
:local time;


:local webpage1	"8.8.8.8"
:local webpage2	"www.google.com"

:global InternetStatus;
:global InternetLastChange;

:for i from=1 to=5 do={
	if ([/ping $webpage1 count=1]=0) do={:set F ($F + 1)}
	if ([/ping $webpage2 count=1]=0) do={:set F ($F + 1)}
	:delay 1;
};
				
:if (($F=10)) do={
	:if (($InternetStatus="UP")) do={
		:log info "WARNING : The INTERNET service's gone DOWN";
		:set InternetStatus "DOWN";

##		do something

				
		:set date [/system clock get date];
		:set time [/system clock get time];
		:set InternetLastChange ($time . " " . $date);
				
	} else={:set InternetStatus "DOWN";}
} else={
	:if (($InternetStatus="DOWN")) do={	
		:log info "WARNING : The INTERNET service's gone UP"; 
		:set InternetStatus "UP";

##		do something

		:set date [/system clock get date];
		:set time [/system clock get time];
		:set InternetLastChange ($time . " " . $date);
				
	} else={:set InternetStatus "UP";}
}
 
cicserver
Member
Member
Topic Author
Posts: 301
Joined: Sun Jul 24, 2011 12:04 pm

Re: Internet Connectivity Monitor Script requierd

Thu Jun 12, 2014 9:43 am

I am facing following issue.
After the script runs, it do logs, but no variable is displayed in VARIABLES section of the script.
The previous version of the script which check s only single host was making variables fine.
1.JPG
You do not have the required permissions to view the files attached to this post.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 2950
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Internet Connectivity Monitor Script requierd

Thu Jun 12, 2014 10:18 am

You can not ping http://www.google.com on script, you must resolve first the DNS to IP...

Is strange: where you have put the script?
I'm Italian, not English. Sorry for my imperfect grammar.
 
cicserver
Member
Member
Topic Author
Posts: 301
Joined: Sun Jul 24, 2011 12:04 pm

Re: Internet Connectivity Monitor Script requierd [SOLVED]

Thu Jun 12, 2014 10:39 am

You can not ping http://www.google.com on script, you must resolve first the DNS to IP...
Is strange: where you have put the script?
I am not using google.com, I am using PUBLIC IP addresses for test purpose like isp gateway IP and 8.8.8.8.
I add the script in SCRIPT section like I use to add other scripts which works fine.

UPDATE: I tested the same script on another RB, and it worked fine. So probably something is wrong in test RB (x86 version)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 2950
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Internet Connectivity Monitor Script requierd [SOLVED]

Thu Jun 12, 2014 10:52 am

the line on the script can be fix with:
:local website2 value=[:resolve domain-name="www.google.com" server=8.8.4.4 server-port=53];
I'm Italian, not English. Sorry for my imperfect grammar.
 
mainTAP
newbie
Posts: 36
Joined: Tue Oct 02, 2012 4:01 am

Re: Internet Connectivity Monitor Script requierd [SOLVED]

Thu Jun 12, 2014 12:37 pm

Thanks for the tip rextended, for some reason it also works with domain name and it resolves the IP automatically
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 2950
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Internet Connectivity Monitor Script requierd [SOLVED]

Thu Jun 12, 2014 12:41 pm

Thanks for the tip rextended, for some reason it also works with domain name and it resolves the IP automatically
I hope new RouterOS version can use dynamically the DNS name instead only IP... ;P
I'm Italian, not English. Sorry for my imperfect grammar.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 2950
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Internet Connectivity Monitor Script requierd [SOLVED]

Thu Jun 12, 2014 12:43 pm

P.S.:

I specify the part on red
:local website2 value=[:resolve domain-name="www.google.com" server=8.8.4.4 server-port=53];

because on this way, it work (obviously if 8.8.4.4 are reachable) without have previously configured correctly DNS on routerboard ;)
I'm Italian, not English. Sorry for my imperfect grammar.
 
cicserver
Member
Member
Topic Author
Posts: 301
Joined: Sun Jul 24, 2011 12:04 pm

Re: Internet Connectivity Monitor Script requierd [SOLVED]

Fri Jun 13, 2014 6:43 am

thank you both of you :)

Who is online

Users browsing this forum: No registered users and 7 guests