script for checking if there is traffic really

Hi,

Since I have started using Mikrotik there was a problem with OVPN, some of the users when disconnected suddenly (power outage, hibernation/sleep, else) are getting stuck on the mikrotik. That is, their interface is still there, their session is open on the radius. Tried with various timeout settings, no luck. Support has no idea, also ovpn is not supported meh. So for solving this problem, I would like to write a script that checks all OVPN interfaces if there was traffic on them in the last 1 minute and if not, perform a disconnect that will close the interface and the radius session as well. But I am really not adept on mikrotik scripts.

How would you go about this?

Thank you,
Andras

Try this script, we are using it in the “wireless” part of the forum to enable/disable a wireless interface if it stalls.

Sounds like just what you need. Just modify the appropriate lines to enable or disable your OVPN interfaces.
Also make sure that name of your interface is correct according to the variable.

Run the script from a scheduler every 20 secs or more.. (but no less than 20 secs)

Ohara is the genius that wrote the script, so credit is due to him.

Simon



\

variable for intervals

:local i 0

variable for traffic

:local txrx 0

name of the wireless interface

:local iface “wlan1”

check if interface exists, otherwise don’t continue

:if ([:len [/interface find name=$iface]] > 0) do={

check if connection is running, otherwise don’t continue

:while ([/interface get [find name=$iface] running]=true) do={

we will do 10 snapshots of the network traffic

:if ($i<10) do={

each time we do a snapshot, we increment the counter

:set i ($i+1)

snapshots are done in 1 second intervals

:delay 1

take traffic snapshot and increment txrx variable by current bps

to validate TX only replace line with :set txrx ($txrx+$(“tx-bits-per-second”))

/interface monitor-traffic $iface once do={
:set txrx ($txrx+$(“tx-bits-per-second”)+$(“rx-bits-per-second”))
}

print information in the log window, this line can be removed to keep the logs clean

:log info ($i . " - txrx: " . $txrx)

execute this part of script after 10 snapshots

} else {

if the sum of all 10 traffic snapshots is 0 then disable and enable interface

:if ($txrx=0) do={
:log warning “Resetting interface”
/interface disable [/interface find name=$iface]
/interface enable [/interface find name=$iface]
}

stop the script

stop
}
}
}

Thank you Simon, this looks great!

The problem is that I use dynamic interfaces for hundreds of VPN connections, but this is a great start. I should be able make a script that lists all the ovpn interfaces and foreach run this script on all.

Thanks,
Andras