Community discussions

MikroTik App
 
prawira
Trainer
Trainer
Topic Author
Posts: 357
Joined: Fri Feb 10, 2006 5:11 am

netwatch does not extract the value from parameter

Sat Apr 09, 2022 7:36 am

hi there,

i made the following script :
/tool netwatch
:for i from=31 to=225 do={
:local nhost "10.1.0.$i";
add host="$nhost" interval=1s timeout=100ms disabled=yes \
    up-script="{:local nIP \$nhost;\r\n\
                :local nDevice [/ip dns static get [find address=\$nIP] name];\r\n\
                :log info \"### \$nDevice IP Address \$nIP UP ###\";}" \
    down-script="{:local nIP \$nhost;\r\n\
                  :local nDevice [/ip dns static get [find address=\$nIP] name];\r\n\
                  :log info \"### \$nDevice IP Address \$nIP DOWN ###\";}"
}

the result on export like follow :
/tool netwatch
add disabled=yes down-script=\
    "{:local nIP \$nhost;\r\
    \n:local nDevice [/ip dns static get [find address=\$nIP] name];\r\
    \n:log info \"### \$nDevice IP Address \$nIP DOWN ###\";\r\
    \n\
    \n}" host=10.1.0.31 interval=1s timeout=100ms up-script=\
    "{:local nIP \$nhost;\r\
    \n:local nDevice [/ip dns static get [find address=\$nIP] name];\r\
    \n:log info \"### \$nDevice IP Address \$nIP UP ###\";\r\
    \n}"

the parameter host is replaced with the correct value of $nhost, but why both parameter $nhost on both UP and DOWN scripts does not replaced with the value declared ?

thank you

P
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: netwatch does not extract the value from parameter

Sat Apr 09, 2022 12:07 pm

remove the \ before $ ?

But, the code can be optimized...
:for i from=31 to=225 do={
    :local nhost "10.1.0.$i"
    /tool netwatch
    add host=$nhost interval=1s timeout=100ms disabled=yes \
          up-script=":local nDevice [/ip dns static get [find address=\$host] name]\r\n\
                     :log info \"### \$nDevice IP Address \$host UP ###\"\r\n\
                    " \
        down-script=":local nDevice [/ip dns static get [find address=\$host] name]\r\n\
                     :log info \"### \$nDevice IP Address \$host DOWN ###\"\r\n\
                    "
}
 
prawira
Trainer
Trainer
Topic Author
Posts: 357
Joined: Fri Feb 10, 2006 5:11 am

Re: netwatch does not extract the value from parameter

Mon Apr 11, 2022 8:20 am

thank you rextended,

i try your suggestion but $host still stay the same after excecuted, it does not changed into the ip address.

P
 
prawira
Trainer
Trainer
Topic Author
Posts: 357
Joined: Fri Feb 10, 2006 5:11 am

Re: netwatch does not extract the value from parameter

Mon Apr 11, 2022 8:25 am

dear rextended,

i just re-alter the code and it works. thank you.

the code become as follow :
:for i from=31 to=225 do={
    :local host "10.1.0.$i"
    /tool netwatch
    add host=$host interval=1s timeout=100ms disabled=yes \
          up-script=":local nDevice [/ip dns static get [find address=$host] name]\r\n\
                     :log info \"### \$nDevice IP Address $host UP ###\"\r\n\
                    " \
        down-script=":local nDevice [/ip dns static get [find address=$host] name]\r\n\
                     :log info \"### \$nDevice IP Address $host DOWN ###\"\r\n\
                    "
}
thank you

P
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: netwatch does not extract the value from parameter

Mon Apr 11, 2022 11:03 am

The keep of $host is wanted, because is converted dynamically on netwatch event script run.
 
prawira
Trainer
Trainer
Topic Author
Posts: 357
Joined: Fri Feb 10, 2006 5:11 am

Re: netwatch does not extract the value from parameter  [SOLVED]

Tue Apr 12, 2022 6:05 am

here are my final codes...
:for i from=2 to=17 do={
  :local host "10.100.1.$i";
  :local hostname [/ip dhcp-server lease get [find address=$host] comment];
  /tool netwatch
  add host=$host interval=1s timeout=100ms disabled=no comment=$hostname \
      up-script=":local nSince [/tool netwatch get [find host=$host] since]\r\n\
                 :log warning \"### Device $hostname (IP Address $host) indicated UP on \$nSince ###\"\r\n\
                 /system scheduler set [find name=\"Monitoring Device $host\"] disable=yes" \
    down-script=":local nSince [/tool netwatch get [find host=$host] since]\r\n\
                 :log error \"### Device $hostname (IP Address $host) indicated DOWN on \$nSince ###\"\r\n\
                 /system scheduler set [find name=\"Monitoring Device $host\"] disable=no start-date=[/system clock get date] start-time=[/system clock get time]"

  /system scheduler
    add disabled=yes interval=5m name="Monitoring Device $host" on-event="{\r\n\
      :local sDate [/system scheduler get [find name=\"Monitoring Device $host\"] start-date]\r\n\
      :local sTime [/system scheduler get [find name=\"Monitoring Device $host\"] start-time]\r\n\
      :local sResult [/ping $host interval=0.05s size=28 count=30]\r\n\
      :if (\$sResult < 10) do={:log error \"### Monitoring Device $hostname (IP Address $host) every 5 minutes still indicated DOWN since \$sDate \$sTime ###\";}\r\n}"
}
feel free to try and evaluate the above script codes.

P

Who is online

Users browsing this forum: alexantao, hmdslm and 27 guests