Just ask on MikroTik Forum...
:global arrayofvalues
:if ([:typeof $arrayofvalues] = "nothing") do={:set arrayofvalues [:toarray ""]}
/ppp secret
:foreach item in=[find where profile="SIMRouters"] do={
:local pname [get $item name]
:local premip [get $item remote-address]
:if ([:ping $premip count=1] != 1) do={
:set ($arrayofvalues->$pname) (($arrayofvalues->$pname) + 1)
:if (($arrayofvalues->$pname) = 5) do={
:log error "Failed to PING 5 times on $premip $pname"
# INSERT WARNING ROUTINE HERE
}
} else={
:if (($arrayofvalues->$pname) >= 5) do={
:log warning "PING succeeded again on $premip $pname"
# INSERT UN-WARNING ROUTINE HERE
}
:set ($arrayofvalues->$pname) 0
}
:delay 1s
}
some suggestions:
do not use variable names than are like parameters (example name -> pname)
everytime use the ":" like on :log, :set, :if,
:ping etc.
if the operation is single and is easy understandeable:
:set pingip [/ping $address count=1]
:if ($pingip = 0) do={
better:
:if ([/ping $address count=1] = 0) do={
Commented version just for teaching
teaching code
# if exist I want use this variable
:global arrayofvalues
# if it is not already defined, I defined it as empty Array
:if ([:typeof $arrayofvalues] = "nothing") do={:set arrayofvalues [:toarray ""]}
# I work only on that section
/ppp secret
# select all items that have SIMRouters as profile
:foreach item in=[find where profile="SIMRouters"] do={
:local pname [get $item name]
:local premip [get $item remote-address]
:if ([:ping $premip count=1] != 1) do={
# if the ping fail, use the "username" as variable name and set it to "previous value + 1" (nothing + 1 = 1, no matter to set it first to 0)
:set ($arrayofvalues->$pname) (($arrayofvalues->$pname) + 1)
# I want be warned only one time, when the fail reach 5 times only
# no matter is the 6th, 7th, 8th time, I want receive only one warning
:if (($arrayofvalues->$pname) = 5) do={
:log error "Failed to PING 5 times on $premip $pname"
# INSERT WARNING ROUTINE HERE
}
} else={
# if I have been warned, I want be un-warned because the link work again
# if before have failed only 4 thimes, and now is working, I do not want any warn (because I receive warning only the 5th time...)
:if (($arrayofvalues->$pname) >= 5) do={
:log warning "PING succeeded again on $premip $pname"
# INSERT UN-WARNING ROUTINE HERE
}
# if ping successfully set / reset the variable to 0
:set ($arrayofvalues->$pname) 0
}
# prevent ping and CPU "storm"
:delay 1s
}