Embedded Event Manager simple example

EEM can be used for monitoring, or triggered by specified conditions (change configuration, etc.).
The following is Cisco official document.

Embedded Event Manager (EEM) is a distributed and customized approach to event detection and recovery offered directly in a Cisco IOS device. EEM offers the ability to monitor events and take informational, corrective, or any desired EEM action when the monitored events occur or when a threshold is reached. An EEM policy is an entity that defines an event and the actions to be taken when that event occurs.

https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/eem/configuration/xe-16/eem-xe-16-book/eem-overview.html

1. Simple Python script

import cli

config_interface = cli.configurep(
    ["interface GigabitEthernet3", "ip add 1.1.1.1 255.255.255.0", "no shutdown", "end"])

print(config_interface)

2. Copy to csr1000v flash

haifeli-csr1kv#copy tftp://10.2.2.2/haifeli/haifeng.py flash:
Destination filename [haifeng.py]? 
Accessing tftp://10.2.2.2/haifeli/haifeng.py...
Loading haifeli/haifeng.py from 10.2.2.2 (via GigabitEthernet1): !
[OK - 262 bytes]

262 bytes copied in 0.353 secs (742 bytes/sec)
haifeli-csr1kv#

3. Configure IP SLA

haifeli-csr1kv#sh run | sec sla
track 1 ip sla 1 reachability
ip sla 1
 icmp-echo 1.1.1.1 source-interface GigabitEthernet3
ip sla schedule 1 life forever start-time now

4. Configure EEM

haifeli-csr1kv#sh run | sec event
event manager applet track-GigabitEthernet3
 event syslog pattern "%TRACK-6-STATE: 1 ip sla 1 reachability Up -> Down"
 action 0.0 cli command "en"
 action 1.0 cli command "guestshell run python /flash/haifeng.py"

5. Verification

haifeli-csr1kv#show ip int brief
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       10.124.53.53    YES other  up                    up
GigabitEthernet2       100.100.100.2   YES other  up                    up
GigabitEthernet3       1.1.1.1         YES TFTP   up                    up
VirtualPortGroup0      192.168.2.1     YES manual up                    up
haifeli-csr1kv#
haifeli-csr1kv#show track 1
Track 1
  IP SLA 1 reachability
  Reachability is Up
    10 changes, last change 00:00:40
  Latest operation return code: OK
  Latest RTT (millisecs) 1
haifeli-csr1kv#
haifeli-csr1kv#terminal monitor
haifeli-csr1kv#
haifeli-csr1kv#
haifeli-csr1kv#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
haifeli-csr1kv(config)#default int gigabitEthernet 3
Interface GigabitEthernet3 set to default configuration
haifeli-csr1kv(config)#
*Apr 14 10:57:20.584: %DMI-5-SYNC_START: R0/0: syncfd: External change to running configuration detected. The running configuration will be synchronized to the NETCONF running data store.
*Apr 14 10:57:24.397: %DMI-5-SYNC_COMPLETE: R0/0: syncfd: The running configuration has been synchronized to the NETCONF running data store.
*Apr 14 10:57:24.868: %TRACK-6-STATE: 1 ip sla 1 reachability Up -> Down
*Apr 14 10:57:24.869: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : CTL : cli_open called.
*Apr 14 10:57:24.870: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : OUT : haifeli-csr1kv>
*Apr 14 10:57:24.870: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : IN  : haifeli-csr1kv>en
*Apr 14 10:57:24.881: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : OUT : haifeli-csr1kv#
*Apr 14 10:57:24.881: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : IN  : haifeli-csr1kv#guestshell run python /flash/haifeng.py
*Apr 14 10:57:28.806: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : OUT : Line 1 SUCCESS: interface GigabitEthernet3
*Apr 14 10:57:28.806: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : OUT : Line 2 SUCCESS: ip add 1.1.1.1 255.255.255.0
*Apr 14 10:57:28.806: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : OUT : Line 3 SUCCESS: no shutdown
*Apr 14 10:57:28.807: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : OUT : Line 4 SUCCESS: end
*Apr 14 10:57:28.807: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : OUT : None
*Apr 14 10:57:28.807: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : OUT :
*Apr 14 10:57:28.807: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : OUT :
*Apr 14 10:57:28.807: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : OUT : haifeli-csr1kv#
*Apr 14 10:57:28.807: %HA_EM-6-LOG: track-GigabitEthernet3 : DEBUG(cli_lib) : : CTL : cli_close called.
*Apr 14 10:57:28.807:
*Apr 14 10:57:28.807: tty is now going through its death sequence
haifeli-csr1kv(config)#
haifeli-csr1kv(config)#
haifeli-csr1kv(config)#
haifeli-csr1kv(config)#end
haifeli-csr1kv#
haifeli-csr1kv#
haifeli-csr1kv#show ip int brief
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       10.124.53.53    YES other  up                    up
GigabitEthernet2       100.100.100.2   YES other  up                    up
GigabitEthernet3       1.1.1.1         YES TFTP   up                    up
VirtualPortGroup0      192.168.2.1     YES manual up                    up
haifeli-csr1kv#
haifeli-csr1kv#
*Apr 14 10:58:24.869: %TRACK-6-STATE: 1 ip sla 1 reachability Down -> Up

Leave a Reply

Your email address will not be published.