Wednesday, February 18, 2009

EEM 3.0 – Variable Logic Pt #1

Latest IOS (12.4(22)T) from Cisco includes EEM 3.0, which is a major improvement over previous versions. Among the various enhancements introduced, the most interesting seem the interactive CLI support (a much requested feature) and the variable logic.

Here is a simple applet that displays a countdown timer before shutting down, using a little bit of variable logic.

Actions used : "set", "while/end", "wait", subtract", "decrement".


event manager applet WHILE-TEST-APPLET
! we run manually the applet, so no event detector is used
event none
! we set the variable $i equal to “10” ($i is a long-integer)
action 1.0 set i "10"
! the while loop starts here and lasts as long as $i is greater than or equal to 0
action 2.0 while $i ge 0
! we print a syslog message showing the variable value
action 2.2 syslog msg "$i secs till shutdown"
! we wait for 1 sec
action 2.3 wait 1
! we subtract 1 from $i (the result is not automatically saved to $i)
action 2.4 subtract $i 1
! we store the result of the previous operation to $i
action 2.6 set i "$_result"
! the while loop ends here
action 2.8 end
! we print the final syslog message
action 3.0 syslog msg "Shutting down now!!!"
! You can put other command (i.e. reload) here


Keep in mind that instead of

action 2.4 subtract $i 1
action 2.6 set i "$_result"

you can use just a single command

action 2.5 decrement i 1


Notes
a) All arithmetic calculations are performed as long integers without any checks for overflow.
b) If you need to break out of a loop you created by mistake, try pressing “Ctrl+Shift+6 + X” many times.
c) You get a warning in case you forget to close a loop. You don't get a warning in case you have used wrong logic inside the loop.
d) You can insert actual (that are saved in the configuration) comments between your actions, but it gets a little bit complicated, because you have to use extra actions ("action x comment xxx") in order to do so! C’mon Cisco, we all know you can do better.

At the end, we run the applet manually:

R1#event manager run WHILE-TEST-APPLET

*Feb 17 00:20:18.635: %HA_EM-6-LOG: WHILE-TEST-APPLET: 10 secs till shutdown
*Feb 17 00:20:19.635: %HA_EM-6-LOG: WHILE-TEST-APPLET: 9 secs till shutdown
*Feb 17 00:20:20.635: %HA_EM-6-LOG: WHILE-TEST-APPLET: 8 secs till shutdown
*Feb 17 00:20:21.635: %HA_EM-6-LOG: WHILE-TEST-APPLET: 7 secs till shutdown
*Feb 17 00:20:22.635: %HA_EM-6-LOG: WHILE-TEST-APPLET: 6 secs till shutdown
*Feb 17 00:20:23.635: %HA_EM-6-LOG: WHILE-TEST-APPLET: 5 secs till shutdown
*Feb 17 00:20:24.635: %HA_EM-6-LOG: WHILE-TEST-APPLET: 4 secs till shutdown
*Feb 17 00:20:25.643: %HA_EM-6-LOG: WHILE-TEST-APPLET: 3 secs till shutdown
*Feb 17 00:20:26.647: %HA_EM-6-LOG: WHILE-TEST-APPLET: 2 secs till shutdown
*Feb 17 00:20:27.651: %HA_EM-6-LOG: WHILE-TEST-APPLET: 1 secs till shutdown
*Feb 17 00:20:28.651: %HA_EM-6-LOG: WHILE-TEST-APPLET: 0 secs till shutdown
R1#
*Feb 17 00:20:29.651: %HA_EM-6-LOG: WHILE-TEST-APPLET: Shutting down now!!!


Here are another 2 applets that create or delete 10 loopbacks with ip addresses based on their number.


event manager applet CREATE-10-LOOPBACKS-APPLET
event none
action 1.1 set i "1"
action 2.1 cli command "enable"
action 2.3 cli command "conf t"
action 3.1 while $i le 10
action 3.3 cli command "interface Loopback $i"
action 3.5 cli command "ip address $i.$i.$i.$i 255.255.255.255"
action 3.7 increment i 1
action 3.9 end
action 4.1 cli command "end"

event manager applet DELETE-10-LOOPBACKS-APPLET
event none
action 1.1 set i "1"
action 2.1 cli command "enable"
action 2.3 cli command "conf t"
action 3.1 while $i le 10
action 3.3 cli command "no interface Loopback $i"
action 3.7 increment i 1
action 3.9 end
action 4.1 cli command "end"


First we create the loopback interfaces :

R1#event manager run CREATE-10-LOOPBACKS-APPLET

*Feb 18 01:23:19.999: %LINK-3-UPDOWN: Interface Loopback1, changed state to up
*Feb 18 01:23:20.315: %LINK-3-UPDOWN: Interface Loopback2, changed state to up
*Feb 18 01:23:20.675: %LINK-3-UPDOWN: Interface Loopback3, changed state to up
*Feb 18 01:23:20.987: %LINK-3-UPDOWN: Interface Loopback4, changed state to up
*Feb 18 01:23:21.027: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback1, changed state to up
*Feb 18 01:23:21.259: %SYS-5-CONFIG_I: Configured from console by eem-user on vty0 (EEM:CREATE-10-LOOPBACKS-APPLET)
*Feb 18 01:23:21.267: %LINK-3-UPDOWN: Interface Loopback5, changed state to up
*Feb 18 01:23:21.315: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback2, changed state to up
*Feb 18 01:23:21.651: %LINK-3-UPDOWN: Interface Loopback6, changed state to up
*Feb 18 01:23:21.675: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback3, changed state to up
*Feb 18 01:23:21.943: %LINK-3-UPDOWN: Interface Loopback7, changed state to up
*Feb 18 01:23:21.987: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback4, changed state to up
*Feb 18 01:23:22.267: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback5, changed state to up
*Feb 18 01:23:22.287: %LINK-3-UPDOWN: Interface Loopback8, changed state to up
*Feb 18 01:23:22.647: %LINK-3-UPDOWN: Interface Loopback9, changed state to up
*Feb 18 01:23:22.655: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback6, changed state to up
*Feb 18 01:23:22.943: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback7, changed state to up
*Feb 18 01:23:22.959: %LINK-3-UPDOWN: Interface Loopback10, changed state to up
*Feb 18 01:23:23.287: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback8, changed state to up
*Feb 18 01:23:23.647: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback9, changed state to up
*Feb 18 01:23:23.959: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback10, changed state to up
R1#
R1#sh ip int br
Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 unassigned YES NVRAM up up
FastEthernet0/1 unassigned YES NVRAM up up
Loopback1 1.1.1.1 YES manual up up
Loopback2 2.2.2.2 YES manual up up
Loopback3 3.3.3.3 YES manual up up
Loopback4 4.4.4.4 YES manual up up
Loopback5 5.5.5.5 YES manual up up
Loopback6 6.6.6.6 YES manual up up
Loopback7 7.7.7.7 YES manual up up
Loopback8 8.8.8.8 YES manual up up
Loopback9 9.9.9.9 YES manual up up
Loopback10 10.10.10.10 YES manual up up


Then we delete them :

R1#event manager run DELETE-10-LOOPBACKS-APPLET

*Feb 18 01:24:30.455: %LINK-5-CHANGED: Interface Loopback1, changed state to administratively down
*Feb 18 01:24:30.499: %LINK-5-CHANGED: Interface Loopback2, changed state to administratively down
*Feb 18 01:24:30.835: %LINK-5-CHANGED: Interface Loopback3, changed state to administratively down
*Feb 18 01:24:31.171: %LINK-5-CHANGED: Interface Loopback4, changed state to administratively down
*Feb 18 01:24:31.195: %SYS-5-CONFIG_I: Configured from console by eem-user on vty0 (EEM:DELETE-10-LOOPBACKS-APPLET)
*Feb 18 01:24:31.307: %LINK-5-CHANGED: Interface Loopback5, changed state to administratively down
*Feb 18 01:24:31.455: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback1, changed state to down
*Feb 18 01:24:31.499: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback2, changed state to down
*Feb 18 01:24:31.591: %LINK-5-CHANGED: Interface Loopback6, changed state to administratively down
*Feb 18 01:24:31.835: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback3, changed state to down
*Feb 18 01:24:31.871: %LINK-5-CHANGED: Interface Loopback7, changed state to administratively down
*Feb 18 01:24:32.167: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback4, changed state to down
*Feb 18 01:24:32.187: %LINK-5-CHANGED: Interface Loopback8, changed state to administratively down
*Feb 18 01:24:32.307: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback5, changed state to down
*Feb 18 01:24:32.543: %LINK-5-CHANGED: Interface Loopback9, changed state to administratively down
*Feb 18 01:24:32.591: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback6, changed state to down
*Feb 18 01:24:32.871: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback7, changed state to down
*Feb 18 01:24:32.919: %LINK-5-CHANGED: Interface Loopback10, changed state to administratively down
*Feb 18 01:24:33.187: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback8, changed state to down
*Feb 18 01:24:33.543: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback9, changed state to down
*Feb 18 01:24:33.919: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback10, changed state to down
R1#
R1#sh ip int br
Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 unassigned YES NVRAM up up
FastEthernet0/1 unassigned YES NVRAM up up


I know i might be dreaming, but the days of TCL are coming to an end (just kidding) :-p
Anyhow, the more applet functionality we get, the less scripting we need.

1 comment:

  1. Great examples TASSOS!!
    Thank you very much.

    - Juan

    ReplyDelete

 
Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Greece License.