Removing WCCP redirect configuration

There are two sites interconnected by a high capacity leased line. 1st site (Router1) has an MPLS connection to corporate network with very limited capacity. 2nd site (Router2) has an internet connection via local ISP and backup (tunnel over internet) to a corporate VPN Hub. Also there is a proxy cache on the 2nd site to offload web traffic from both sites to ISP and reduce MPLS link utilization.
Corporate network has different priorities for different types of traffic. Everything is ok with business traffic when MPLS link is operational. If MPLS goes down traffic from the 1st site LAN will go directly to the tunnel but some traffic from the 2nd site will still be offloading through internet and will be in conflict with business traffic that goes with regard to corporate priority policies. This can lead to a serious service degradation in some cases.

TASK
Task here is to remove wccp configuration from Router2 when Router1's MPLS link goes down. (This can be also completed by moving wccp redirect from LAN-in to LL-out but the point for me was to write the script).

POSSIBLE SOLUTION
Lets say that there is a dynamic routing protocol (EIGRP, iBGP, ...) configured between Router1 and Router2 and between Router2 and VPN Hub. The easiest way to see if Router1 MPLS is operational will be to check the next-hop of a default route on Router2. If the next-hop is VPN Hub then MPLS is down and we can remove wccp configuration.

TCL SCRIPT

#Occurs every $EEM_WCCP_INTERVAL sec
::cisco::eem::event_register_timer watchdog time $EEM_WCCP_INTERVAL
#==============================================
#Script checks ip route 0.0.0.0 next-hop address every $EEM_WCCP_INTERVAL seconds.
#If next-hop is equal to $NEXT_HOP_ROUTER (considered as a backup next-hop) then
#script generates log message and removes wccp configuration from  $WCCP_INTERFACE interface.
#Script also changes global EEM variable to check if main link state changed to return wccp configuration back.
#This script do not parse static default routes.
#===============================================
#STATUS - global EEM variable;   1 - Main link UP    0 - Main link down
#RESULT - local script variable; 1 - Backup UP       0 - Backup down
# STATUS RESULT
#  1       1      - Main link went down. Remove WCCP redirect.
#  0       0      - Main link went up. Add WCCP redirect.
#  0\1     1\0    - Do nothing.

namespace import ::cisco::lib::*
namespace import ::cisco::eem::*

set NEXT_HOP_ROUTER 192.168.2.1
set WCCP_INTERFACE {Fa0/0}
set WCCP_ID 10
set WCCP_DIRECTION {in}
set PATTERN {(\d+\.\d+\.\d+\.\d+)+}
set _CMD {sh ip route 0.0.0.0 | i Last update}

if [catch {cli_open} result] {
    error $result $errorInfo
} else {
    array set cli1 $result
}

set ROUTE [cli_exec $cli1(fd) $_CMD]
set RES [regexp $PATTERN $ROUTE -> NEXT_HOP]

if {$RES == 0} {
 puts "Default route: Next hop is not found";
} else {
 set RESULT [string equal $NEXT_HOP $NEXT_HOP_ROUTER]
 #puts "$STATUS - $RESULT"
 if {$STATUS==1&&$RESULT==1} {
  #MAIN LINK DOWN
  if [catch {cli_exec $cli1(fd) "conf t"} result] {
   error $result $errorInfo
   exit 1
  }
  cli_exec $cli1(fd) "event manag environment STATUS 0"
  cli_exec $cli1(fd) "interface $WCCP_INTERFACE"
  cli_exec $cli1(fd) "no ip wccp $WCCP_ID redirect $WCCP_DIRECTION"
  action_syslog priority notifications msg "WCCP_MONITOR: MPLS DOWN, removing wccp configuration"
 } elseif {$STATUS==0&&$RESULT==0} {
  #MAIN LINK UP
  if [catch {cli_exec $cli1(fd) "conf t"} result] {
   error $result $errorInfo
   exit 1
  }
  cli_exec $cli1(fd) "event manag environment STATUS 1"
  cli_exec $cli1(fd) "interface $WCCP_INTERFACE"
  cli_exec $cli1(fd) "ip wccp $WCCP_ID redirect $WCCP_DIRECTION"
  action_syslog priority notifications msg "WCCP_MONITOR: MPLS UP, adding wccp configuration"
 }
}

USAGE


Set directory for user scripts:
event manager directory user policy flash:/
Set username to execute scripts:
event manager session cli username [username]

Set following variables:

set NEXT_HOP_ROUTER [Backup next-hop IP]
set WCCP_INTERFACE { [Interface with wccp redirect] }
set WCCP_ID [wccp service]
set WCCP_DIRECTION { [redirect direction on the interface] }


Upload script to router's flash:
copy  tftp://[IP]/myscript.tcl flash:

Set global variables:
Script will run every 50 seconds

event manager environment EEM_WCCP_INTERVAL 50
Main link is up by default
event manager environment STATUS 1



Register EEM policy:
event manager policy myscript.tcl