Discussed in that thread is there is no need for :local variables in the first place! When a /system/script is called as action of /tool/netwatch up/down/test script, it will already have all the netwatch variables defined…
so all the :local that just do a find/get are unnecessary.
Also, the /system/script is marked as “Don’t Require Permissions”, correct?
Like a good portion of what I do, it’s mostly for learning.
In this case, I want to log the rtt-* values when Netwatch determines a DOWN situation so that I can tweak the thr-* values and come up with an overall combination threshold that works for me.
Yep, “in the script” does not mean “in terminal”.
Curly brackets are needed (in terminal).
@Amm0
It’s not like I have more patience, more simply you are way more experienced and knowledgeable than I am in Mikrotik scripts, so you automatically give for reknown basic things that a beginner needs to learn, and the only way to learn is IMHO to try (and fail, and try again). I am pretty sure that Josephny after this will remember curly brackets and how “local” means “local”.
My point is if you understand the problem, you MAY be able to avoid script. If goal is to JUST “tweak a netwatch”, then to output current values (rtt- loss- etc) AND configured “threshold”, the /system/logging topics=netwatch would do that. Not it’s not one line in logs, but so much easier than messing with any scripts… And, you can add a custom /system/logging/action just for netwatch to keep these netwatch debug items separate from main log. IMO that be a better approach for tweaking. Or if you want to get serious and track them, you can have the script append to file with CSV data to latter review in Excel.
If this is to learn scripting better… repeating the same get [find …] varname a bunch of times is inefficient. You can do ONE “get” without any attributes, and will return an array with all attributes. So as a learning exercise, it be better to learn arrays, which avoid needing so many locals. And there is a few videos on MiktoTik’s YouTube that fill in the blanks on array scripting usage more generally.
Thus you can use ONE local to get all the variables, then use array deconstruction to get particular values as needed (shown for CLI use … if using in /system/script the outer { } are NOT needed):
{
:local got [/tool/netwatch get [find host=8.8.8.8]]
:log info "status $($got->"status") stdev $($got->"rtt-stdev") / $($got->"thr-stdev") max $($got->"rtt-max") / $($got->"thr-max") "
}
Also, what may be happening… is netwatch will not “get” a threshold value if it is still default value / left unset. (More specifically, the value return of the get will be type “nil”, and NOT the default value).