How to close IPSec automatically

If you know IP addresses for both ends of VPN tunell then you should ping the opposite end from each router.
When ping is not returning then consider VPN to be down so it should be restarted with
/ip ipsec installed-sa flush
or/and
/ip ipsec remote-peers kill-connections

See script for monitoring target IP
http://forum.mikrotik.com/t/logging-of-a-lost-connection/87840/2

This script should be scheduled to be run every ?? minutes…you need to choose the correct value depending on your needs

You could check “the other end” with netwatch and clear ipsec peer as above but netwatch is IMHO too dumb to decide when VPN is down. One dropped/overtimed packet starts restart procedure.
In my script you can tailor dropped to all packet treshold and this way choose when VPN is considered to be down.