Community discussions

 
Ivoshiee
Member
Member
Topic Author
Posts: 470
Joined: Sat May 06, 2006 4:11 pm

Netwatch tool + script: Are global variables truly global?

Sun Jul 01, 2012 2:09 pm

I am experimenting with MT scripting and I've noticed that there are occasions when globally defined variables are not get updated by scripts when these are clearly being run.

As the MT scripting does not allow procedures nor calling scripts with parameters then it is relatively small playing space when global variables are not reliable as well. Are global variables truly global?

Running ROS v5.14 and beyond.
Last edited by Ivoshiee on Mon Jul 02, 2012 4:00 am, edited 1 time in total.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Are global variables truly global?

Sun Jul 01, 2012 2:20 pm

Are you sure the line that sets the variable is executed? I mean, maybe there was a runtime error in a script prior to that, which in turn makes the variable not being set.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
Ivoshiee
Member
Member
Topic Author
Posts: 470
Joined: Sat May 06, 2006 4:11 pm

Re: Are global variables truly global?

Sun Jul 01, 2012 2:39 pm

That given variable is being set up initially by a scheduler script at "startup" event.
In my other scripts I have a debug information posted to a log as variable getting updated, but when to verify it under terminal, it has still previous value and thus breaking my other scripts. If to run that given script by hand then that variable does get updated.
 
gotsprings
Forum Veteran
Forum Veteran
Posts: 760
Joined: Mon May 14, 2012 9:30 pm

Re: Are global variables truly global?

Sun Jul 01, 2012 2:45 pm

I found out the other day that a global variable is only accessible by the same user.

I wrote a script as admin that produced "previousIP".

Then when the system was put in the field the tech removed the admin account. He left one for him and one for me.

I needed to write a script that would fetch "previousIP".

Now since the script was written when I was logged in as Chris rather then Admin... guess where it failed.
"It ain't what you don't know that gets you into trouble. It's what you know for sure that just ain't so."
Mark Twain
 
Ivoshiee
Member
Member
Topic Author
Posts: 470
Joined: Sat May 06, 2006 4:11 pm

Re: Are global variables truly global?

Sun Jul 01, 2012 3:16 pm

I found out the other day that a global variable is only accessible by the same user.

I wrote a script as admin that produced "previousIP".

Then when the system was put in the field the tech removed the admin account. He left one for him and one for me.

I needed to write a script that would fetch "previousIP".

Now since the script was written when I was logged in as Chris rather then Admin... guess where it failed.
We have something here!

Does the system scheduler run all scripts with admin rights? It may be that even I have marked the owner as admin it is not and that is why my scheduled script is not updating that given variable and when I run it in terminal it does.

Is it a bug I am facing here?
 
Ivoshiee
Member
Member
Topic Author
Posts: 470
Joined: Sat May 06, 2006 4:11 pm

Re: Are global variables truly global?

Mon Jul 02, 2012 3:12 am

I've put debug messages all around my scripts now and I can conclude that is an issue with script either owner, a runner or scope. It seems the scheduled task is not being run as an admin or that scheduled task calling for another script owned by admin is not being run with admin context or something that. Another possibility is that even the calling script has the variable defined as global, the called script will not see it. I do not understand why and I can not read it out from the manual as well.

Edit: Another avenue for investigation it the Netwatch tool. Subset of these scripts are being started by Netwatch tool and it seems to not see globally defined variables. What ownership/running permissions/variable scope are being set when it will launch a script? How to change that?

Edit2: That seems to be it - Netwatch tool does not see global scope. The script it is running is owned by the admin (as the rest of these), but it does not see global variables set up by admin. What to do?


(I wonder if the scheduled tasks are within own separate world as well.)
 
Ivoshiee
Member
Member
Topic Author
Posts: 470
Joined: Sat May 06, 2006 4:11 pm

Re: Netwatch tool + script: Are global variables truly globa

Mon Jul 02, 2012 1:26 pm

Confirmation:
I did put a delay into the script and scripts->jobs does reveal that the owner of Netwatch tool started script is a "*sys" and that is why these scripts do not see global variables defined by an "admin".

How does one use global variables in Netwatch and within Netwatch triggered scripts?
I've yet to read about it from any MT manual I've searched. That owner/user issue is not being mentioned as well. Does it mean that it is yet another bug?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5921
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Netwatch tool + script: Are global variables truly globa

Mon Jul 02, 2012 2:21 pm

It is not a bug. global variables are global only for the user which created variables.
You can't read global variables created by different user.
 
Ivoshiee
Member
Member
Topic Author
Posts: 470
Joined: Sat May 06, 2006 4:11 pm

Re: Netwatch tool + script: Are global variables truly globa

Mon Jul 02, 2012 3:18 pm

It is not a bug. global variables are global only for the user which created variables.
You can't read global variables created by different user.
Fine. Can you put it into the ROS Wiki manual as well?
For example here:
http://wiki.mikrotik.com/wiki/Manual:Scripting
http://wiki.mikrotik.com/wiki/Netwatch
and into other relevant places as well.

That put aside then how to use Netwatch tool, scheduler, or some other fancy ROS facility in a way that the global environment spaces are not separate (thus as every script is not being run so to speak in forced local mode)? Can I change Netwatch ownership from *sys to admin? Or should I somehow change other scripts over to *sys? Or is it designed to be separate and nothing can be done?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5921
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Netwatch tool + script: Are global variables truly globa

Mon Jul 02, 2012 4:49 pm

It is already mentione din the scripting manual

http://wiki.mikrotik.com/wiki/Manual:Sc ... #Variables
global - accessible from all scripts created by current user, defined by global keyword;
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5921
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Netwatch tool + script: Are global variables truly globa

Mon Jul 02, 2012 5:08 pm

Can I change Netwatch ownership from *sys to admin? Or should I somehow change other scripts over to *sys? Or is it designed to be separate and nothing can be done?
It is designed to be separate and you can't change ovnership of the variables or scripts.
 
Ivoshiee
Member
Member
Topic Author
Posts: 470
Joined: Sat May 06, 2006 4:11 pm

Re: Netwatch tool + script: Are global variables truly globa

Mon Jul 02, 2012 6:19 pm

It is already mentione din the scripting manual

http://wiki.mikrotik.com/wiki/Manual:Sc ... #Variables
global - accessible from all scripts created by current user, defined by global keyword;
Then it is a Network tool what is broken - it is being created by "current user admin", but instead of running under "admin" it is being run as "*sys". Please fix it.
 
Ivoshiee
Member
Member
Topic Author
Posts: 470
Joined: Sat May 06, 2006 4:11 pm

Re: Netwatch tool + script: Are global variables truly globa

Mon Jul 02, 2012 6:22 pm

Can I change Netwatch ownership from *sys to admin? Or should I somehow change other scripts over to *sys? Or is it designed to be separate and nothing can be done?
It is designed to be separate and you can't change ovnership of the variables or scripts.
Where can I read about it in the ROS manual? What is the point of having Netwatch event triggered scripts and not being able to use global variables to do some magic with those events?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5921
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Netwatch tool + script: Are global variables truly globa

Tue Jul 03, 2012 9:45 am

It is already mentione din the scripting manual

http://wiki.mikrotik.com/wiki/Manual:Sc ... #Variables
global - accessible from all scripts created by current user, defined by global keyword;
Then it is a Network tool what is broken - it is being created by "current user admin", but instead of running under "admin" it is being run as "*sys". Please fix it.
For netwatch, scheduler, etc current user is sys*. As I mentioned it is not a bug it is designed to work so.
 
Ivoshiee
Member
Member
Topic Author
Posts: 470
Joined: Sat May 06, 2006 4:11 pm

Re: Netwatch tool + script: Are global variables truly globa

Tue Jul 03, 2012 10:49 am

For netwatch, scheduler, etc current user is sys*. As I mentioned it is not a bug it is designed to work so.
"Scheduler current user as *sys" - that is not what I am experiencing here. I see scheduled scripts being run under admin and Netwatch under *sys. I do not care if these are *sys or admin as long as these are the same, but at the moment they are different for me. Why?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5921
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Netwatch tool + script: Are global variables truly globa

Tue Jul 03, 2012 11:26 am

We will see what we can do to change netwatch in the future versions , probably assign ownership.
 
Ivoshiee
Member
Member
Topic Author
Posts: 470
Joined: Sat May 06, 2006 4:11 pm

Re: Netwatch tool + script: Are global variables truly globa

Tue Jul 03, 2012 6:12 pm

We will see what we can do to change netwatch in the future versions , probably assign ownership.
It is probably the only thing to do it right. But one may ask when it shall happen?

Also, scheduler is not running in *sys ownership. If I set a schedule up as admin user then is is being run under admin ownership. That should be mentioned in http://wiki.mikrotik.com/wiki/Manual:System/Scheduler.
 
Ivoshiee
Member
Member
Topic Author
Posts: 470
Joined: Sat May 06, 2006 4:11 pm

Re: Netwatch tool + script: Are global variables truly globa

Tue Jul 03, 2012 8:24 pm

Until there is no proper fix I'll implement a workaround:

1. Netwatch tool is still in use, but anything it should run is set into separate scripts (device_is_up and device_is_down) and
2. set up a scheduler task (about every 5 seconds) what will run:
#
# As Netwatch is being run in *sys environment and thus not being able to see nothing else then to make it able to interact with other scripts we may use Scheduler.
#
#
# DevicePing -- a "my-device" to be monitored
# 1=up
# 0=down
#

:global DevicePing;

:local devicestatus;

:set devicestatus [/tool netwatch get [/tool netwatch find comment="my-device"] status];

#Detecting device pinging ability change:

:if ($DevicePing!=0 and $devicestatus="down") do={:set $DevicePing 0; /system script run device_is_down;} else={:if ($DevicePing!=1 and $devicestatus="up") do={:set $DevicePing 1; /system script run device_is_up;}};
Note: != notation is in use to negate any ill effects from $DevicePing initialization errors or it being put to some invalid value down the line.
 
Institor
just joined
Posts: 9
Joined: Sat Apr 29, 2017 3:28 pm

Re: Netwatch tool + script: Are global variables truly global?

Thu Oct 19, 2017 8:58 am

I have found very strange issue with global variables in "netwatch" scripts. When user logged in (via winbox or ssh, doesn't matter) global variables in "up" and "down" scripts works fine, as expected.
But if user not logged in, global variable created by script lasts only about 10 seconds, and then disappears. (for example, netwatch executes "down" script normally (i see email sent by that script), but if i wait for about 15-20 seconds and log in, there are no variables in "environment". BUT! if I log in within 10 sec. after script execution, variables exist!)

Scripts
Down script
:global isdown;

:local email "<my email>";
:local downtime [/system clock get time];
:local downdate [/system clock get date];

:set $isdown "true";
/tool e-mail send to=$email subject="subject" body="host is down :-( Date: $downdate Time: $downtime";

Up script
:global isdown;

:local email "<my email>";
:local uptime [/system clock get time];
:local update [/system clock get date];

if ($isdown="true") do={
:set $isdown "false";
/tool e-mail send to=$email subject="subject" body="host is UP! Date: $update Time: $uptime";
}

Any ideas where did it go wrong?
RouterOs ver. 6.40.4, hap ac lite...
 
User avatar
abrodkin
just joined
Posts: 12
Joined: Wed Nov 08, 2017 10:30 am

Re: Netwatch tool + script: Are global variables truly global?

Sat Dec 02, 2017 1:56 am

I have found very strange issue with global variables in "netwatch" scripts. When user logged in (via winbox or ssh, doesn't matter) global variables in "up" and "down" scripts works fine, as expected.
But if user not logged in, global variable created by script lasts only about 10 seconds, and then disappears. (for example, netwatch executes "down" script normally (i see email sent by that script), but if i wait for about 15-20 seconds and log in, there are no variables in "environment". BUT! if I log in within 10 sec. after script execution, variables exist!)

Any ideas where did it go wrong?
RouterOs ver. 6.40.4, hap ac lite...
That's exactly what I see on 6.40.5 on "hex poe".
I was trying to solve the same simple task - log connection failure time and then mention it in email on connection restore.

Really super simple thing but it doesn't work!
 
Institor
just joined
Posts: 9
Joined: Sat Apr 29, 2017 3:28 pm

Re: Netwatch tool + script: Are global variables truly global?

Mon Dec 04, 2017 4:17 pm

Looks like strange and serious bug. I ended up using temp file to store information... Very annoying. Can't see any errors in my scripts.
 
User avatar
abrodkin
just joined
Posts: 12
Joined: Wed Nov 08, 2017 10:30 am

Re: Netwatch tool + script: Are global variables truly global?

Mon Dec 04, 2017 5:02 pm

Looks like strange and serious bug. I ended up using temp file to store information... Very annoying. Can't see any errors in my scripts.
That's what I was going to implement: dump connection down time to file and then print contents of the file in email on connection up.
Care to share your netwatch scripts doing that?
 
Institor
just joined
Posts: 9
Joined: Sat Apr 29, 2017 3:28 pm

Re: Netwatch tool + script: Are global variables truly global?

Tue Dec 05, 2017 3:22 am

I only use file as indicator/switch (something like boolean var, exist/not exist), but i think command
/file set filename.txt contents="downtime=..."
and then
/file get filename.txt contents
should do the job
 
User avatar
SiB
Member Candidate
Member Candidate
Posts: 148
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: Netwatch tool + script: Are global variables truly global?

Mon Aug 19, 2019 3:33 am

NetWatch run up-script and down-script as *sys user who have some limitation.
You can create own "/system script add dont-require-permissions=yes ...." then this way you can call it from NetWatch up/down-scripts and your global's etc. start working fine.

WARNING: https://wiki.mikrotik.com/wiki/Manual:S ... ermissions
MTCNA + MTCRE + MTCINE | ~600 users at ~150 RouterBoards in EMEA | Telegram: @SiB_PL | WebChat: Tokonda

Who is online

Users browsing this forum: No registered users and 8 guests