Page 1 of 1
Netwatch script variables
Posted: Thu Jan 04, 2024 3:07 am
by wojo
If I create a system script called NetwatchLog, which logs $id for example, it works if I set up the netwatch as such:
add ... up-script=NetwatchLog
However, it does not receive those variables in this format:
add ... up-script=/system script run NetwatchLog
I have a lot of code in that which I would like not to repeat. Any ideas? I tried a function global var but that doesn't work because netwatch doesn't allow access to global vars!
:local name "netwatch update: $host, comment=$comment status=$status"
:local email [/system logging action get [/system logging action find name=email] email-to]
:local done [$"done-tests"]
:local failed [$"failed-tests"]
:local sent [$"sent-count"]
:local responses [$"response-count"]
:local loss [$"loss-count"]
:local lossPercent [$"loss-percent"]
:local rttAvg [$"rtt-avg"]
:local rttMin [$"rtt-min"]
:local rttMax [$"rtt-max"]
:local rttJitter [$"rtt-jitter"]
:local rttStdev [$"rtt-stdev"]
:local lossPercent ($lossPercent / 10)
:local rttAvg ($rttAvg / 1000)
:local rttMin ($rttMin / 1000)
:local rttMax ($rttMax / 1000)
:local rttJitter ($rttJitter / 1000)
:local rttStdev ($rttStdev / 1000)
:local details "name = $name\n\ndone = $done\nfailed = $failed\nsent = $sent\nresponses = $responses\nloss = $loss\nloss% = $lossPercent\nrttAvg = $rttAvg\nrttMin = $rttMin\nrttMax = $rttMax\nrttJit = $rttJitter\nrttStdev = $rttStdev";
/tool e-mail send to=$email subject="$name" body="$details"
:log info $details
Re: Netwatch script variables
Posted: Thu Jan 04, 2024 4:04 am
by Amm0
[$"done-tests"] etc should be ($"done-tests") if you want to assign them . Square brackets [] are for commands only, so that your big problem — parenthesis () are for grouping & still need the rest ($"variable-with-space").
Re: Netwatch script variables
Posted: Thu Jan 04, 2024 4:30 am
by wojo
[$"done-tests"] etc should be ($"done-tests") if you want to assign them . Square brackets [] are for commands only, so that your big problem — parenthesis () are for grouping & still need the rest ($"variable-with-space").
Ah, yeah that was a mistake but the values still worked when this is placed in a netwatch itself.
I've updated to things like this, but they are still empty unless called as a "bare" script reference, not when called any other way.
:local done $"done-tests"
I really want to avoid repeating all this code over and over, but not sure how to do it. It seems the variables from netwatch will never propagate down to a called script unless referenced "bare".
Re: Netwatch script variables
Posted: Thu Jan 04, 2024 4:38 am
by wojo
I've inlined everything:
:local name "$comment $status"
:local email [/system logging action get [/system logging action find name=email] email-to]
:local details ("name = $name\n\ndone = $"done-tests"\nfailed = $"failed-tests"\nsent = $"sent-count"\nresponses = $"response-count"\nloss = $"loss-count"\nloss% = " . ($"loss-percent" / 10) . "\nrttAvg = " . ($"rtt-avg" / 1000) . "\nrttMin = " . ($"rtt-min" / 1000) . "\nrttMax = " . ($"rtt-max" / 1000) . "\nrttJit = " . ($"rtt-jitter" / 1000) . "\nrttStdev = " . ($"rtt-stdev" / 1000))
/tool e-mail send to=$email subject="$name" body="$details"
:log info $details
But for certain up/down on netwatch, I have it modify routes and kill connection tracked session, so I can't call the script for those and have to repeat this... at least it is shorter.
Re: Netwatch script variables
Posted: Thu Jan 04, 2024 4:54 am
by Amm0
Yeah the netwatch variable aren't going to pass via a /system/script/run.... The variables are already local to the netwatch script, so "passing" them another script isn't going to work.
While you should be able to assign them to globals (or uses a single array global with the values), that's not ideal either.
Another approach be to declare a global function that does the desired logging etc, and call that function from the netwatch with the $"done-tests", etc network variables as parameters to a NetwatchLog function. The save copying code to each netwatch since /system/script/run does not take arguments (but functions "exported" by script do allow parameters).
Re: Netwatch script variables
Posted: Thu Jan 04, 2024 5:39 am
by wojo
Yeah the netwatch variable aren't going to pass via a /system/script/run.... The variables are already local to the netwatch script, so "passing" them another script isn't going to work.
While you should be able to assign them to globals (or uses a single array global with the values), that's not ideal either.
Another approach be to declare a global function that does the desired logging etc, and call that function from the netwatch with the $"done-tests", etc network variables as parameters to a NetwatchLog function. The save copying code to each netwatch since /system/script/run does not take arguments (but functions "exported" by script do allow parameters).
tried those two in fact, but netwatch can't access globals, so I can't set nor call functions are are in globals
Re: Netwatch script variables
Posted: Thu Jan 04, 2024 10:09 am
by Amm0
Perhaps combine the approaches (e.g. script w/function). They changed the permission structure for these scripts recently.
/system script
add dont-require-permissions=yes name=defnwlog owner=skyfi source=\
":global NetwatchLog do={/log/info \"donetests: \$doneTests"}"
/tool netwatch
add disabled=no down-script="" host=8.8.8.8 http-codes="" interval=5s \
type=simple up-script=\
"/system/script/run defnwlog\r\
\n\$NetwatchLog doneTests=(\$\"done-tests\");"