Latency Monitoring Script Issue - Seeking Assistance

I am writing to seek assistance with a script I've been working on to monitor latency from different public IPs and enable source NAT to the LAN IP with the lowest latency to a target IP.

Below is the script:

\

Disable all NAT rules

/ip firewall nat set [find comment="SLT"] disable=yes
/ip firewall nat set [find comment="Dialog"] disable=yes
/ip firewall nat set [find comment="Airtel"] disable=yes

Define the target IP

:local targetIP "142.251.42.110"

Define the threshold for latency (30ms)

:local latencyThreshold 30

Ping the target IP from each source address and calculate average latency

:local avgLatency51 [/ping $targetIP interval=1 count=5 src-address=X.X.X1.5]
:local avgLatencyValue51 [:pick ([:find $avgLatency51 "avg-rtt="]) ([:find $avgLatency51 "ms"])]

:local avgLatency52 [/ping $targetIP interval=1 count=5 src-address=X.X.X2.5]
:local avgLatencyValue52 [:pick ([:find $avgLatency52 "avg-rtt="]) ([:find $avgLatency52 "ms"])]

:local avgLatency53 [/ping $targetIP interval=1 count=5 src-address=X.X.X3.5]
:local avgLatencyValue53 [:pick ([:find $avgLatency53 "avg-rtt="]) ([:find $avgLatency53 "ms"])]

Check the average latency values and enable/disable NAT accordingly

:if (($avgLatencyValue51 < $avgLatencyValue52) && ($avgLatencyValue51 < $avgLatencyValue53) && ($avgLatencyValue51 != "")) do={
/ip firewall nat set [find comment="SLT"] disabled=no
/ip address set [find interface=l1] disabled=yes
} else={
/ip firewall nat set [find comment="SLT"] disabled=yes
/ip address set [find interface=l1] comment=here1
}

:if (($avgLatencyValue52 < $avgLatencyValue51) && ($avgLatencyValue52 < $avgLatencyValue53) && ($avgLatencyValue52 != "")) do={
/ip firewall nat set [find comment="Dialog"] disabled=no
/ip address set [find interface=l2] disabled=yes
} else={
/ip firewall nat set [find comment="Dialog"] disabled=yes
/ip address set [find interface=l2] comment=here2
}

:if (($avgLatencyValue53 < $avgLatencyValue52) && ($avgLatencyValue53 < $avgLatencyValue51) && ($avgLatencyValue53 != "")) do={
/ip firewall nat set [find comment="Airtel"] disabled=no
/ip address set [find interface=l3] disabled=yes
} else={
/ip firewall nat set [find comment="Airtel"] disabled=yes
/ip address set [find interface=l3] comment=here3
}

Log the average latency values

:log info "avgLatencyValue51: $avgLatencyValue51"
:log info "avgLatencyValue52: $avgLatencyValue52"
:log info "avgLatencyValue53: $avgLatencyValue53"


While this script functions as expected in terms of enabling/disabling NAT rules based on latency values, it does not calculate the average latency as intended.

What may be causing this issue and any recommendations for resolving it...

You are getting the latency value incorrectly

:local pingfile “ping.txt”
:execute script=“{ping count=3 interval=1 1.1.1.1}” file=$pingfile

wait for execution

… just for example “delay”

delay 5
:local result [:file get $pingfile contents]
:local avgrttpos [:pick $result ([:find $result “avg-rtt”]+8) ([:find $result “max-rtt”])]
:local avgrtt [:pick $avgrttpos 0 ([:find $avgrttpos “ms”])]
:put $avgrtt ← > here is your variable

ps: under ideal conditions