check connection quality

Hey guys,

we need a script that turn off our WAN connection if the package loss is to high. But we don’t know if it is working or if we made some errors since we don’t get any feedback from the RouterOS.

:local pingTargetIn;
:local pingTargetOut;
:local ispIn;
:local ispOut;

# SAVE WAN STATUS BEFORE RUNNING SCRIPT (enabled/disabled)
:local E [/interface get WAN running]

:if ($E=false) do={
# IF WAN IS DOWN, FIRST ENABLE IT
       /interface ethernet enable WAN;
 }

 # RUN CONNECTION TEST ON WAN

 /tool flood-ping 176.221.xxx.xxx count=10 do={
	:if ($sent >= 10) do={
		:set pingTargetOut $sent;
		:set pingTargetIn $received;
	}
}

:local packageloss (100 - (($pingTargetIn * 100) / $pingTargetOut));

# CHECK RESULT OF TEST CONNECTION

:if ($packageloss >= 30 && ([/interface get [find name="WAN"] running]=true)) do={

	# DISABLE WAN
	/interface ethernet disable WAN;
	:if ($E=false) do={
	# STAY DOWN, NO NEED TO SEND NOTIFICATION
	}else={
	# WAN IS DISABLED, NOTIFY USER
		/tool e-mail send to="user@company.com" from=mikrotik@company.com subject=("CONNECTION BAD") body="Currently we have badly RTO when ping to www.pingTarget.colm. Our main internet trouble, Please use the internet wisely";
	}	
}else={
	:if ($E=false) do={
		# WAN IS ENABLED; NOTIFY USER
		/tool e-mail send to="user@company.com" from=mikrotik@company.com subject=("[RTO]") body="WAN UP. Thank You";
	}else={
		# STAY UP, NO NEED TO SEND NOTIFICATION
	}
}

Any help is appreciated. Thank you

I did some clean up of the code and further testing and found the part thats not working but I still don’t know why
This is the part that’s not working as intend:

:set packageloss (100 - (($pingIn * 100) / $pingOut));
:log info $packageloss;
#CHECK RESULT OF TEST CONNECTION
:if ($packageloss >= 30) do={
        # DISABLE WAN
        /interface ethernet disable WAN;
        :log info "WAN disabled";
        /tool e-mail send to="admin@company.com" from=mikrotik@company.com subject="www.company.de" body="Currently we have badly RTO when ping to http://www.company.de. Our main internet trouble, Please use the internet wisely";
        :log info "NOTIFICATION sent";
}else={
         # WAN IS ENABLED; NOTIFY USER
         /tool e-mail send to="admin@company.com" from=mikrotik@company.com subject="www.company.de" body="Ping http://www.company.de already UP. Thank You";
         :log info "WAN enabled";
}

Even after further testing I can’t narrow down the exact problem. For me it seems to be within the :if part.

Cheers