netwatch does not extract the value from parameter

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

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\
                    "
}

thank you rextended,

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

P

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

The keep of $host is wanted, because is converted dynamically on netwatch event script run.

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