on 05-19-2012 11:30 PM
In preparing for CiscoLive! in San Diego, I am provisioning our access layer 3560-E switches. Since things have a tendency to change a lot at an event like CiscoLive! I thought it would be best to make sure our port descriptions are always up-to-date when it comes to reflecting what devices are connected. To help me do that, I wrote up this small EEM applet policy. It will update the port's description based on the CDP neighbor learned on that port. This policy requires EEM 3.2, so you're looking at 12.2(55)SE or higher for the 3560s. It will also work on 3750s and ISR G2 routers running 15.x code.
event manager applet update-port-description
event neighbor-discovery interface regexp GigabitEthernet.* cdp add
action 1.0 cli command "enable"
action 2.0 cli command "config t"
action 3.0 cli command "interface $_nd_local_intf_name"
action 4.0 cli command "description $_nd_cdp_entry_name:$_nd_port_id"
The result of this will be a description like the following on switch ports:
description SDCC_IDF_1.11:TenGigabitEthernet0/1
A very pretty script!!
Will this script change the port´s description every 180 seconds?
No, it changes the port description when a new CDP neighbor connects to it.
I like it!
Have made an improved version, please comment and/or improve further:
regexp in the event are for the uplink module on 3750-X/3560-X, adapt to your own need.
event manager applet auto-update-port-description authorization bypass
description "Auto-update port-description based on CDP neighbors info"
action 0.0 comment "Event line regexp: Deside which interface to auto-update description on"
event neighbor-discovery interface regexp .*GigabitEthernet[1-9]/1/[1-4]$ cdp add
action 1.0 comment "Trim domain name"
action 1.1 string trimright "$_nd_cdp_entry_name" ".your.domain.name"
action 1.2 string trimright "$_string_result" ".another.domain.name"
action 1.3 set _host "$_string_result"
action 2.0 comment "Convert long interface name to short"
action 2.1 string first "Ethernet" "$_nd_port_id"
action 2.2 if "$_string_result" eq 7
action 2.21 string replace "$_nd_port_id" 0 14 "Gi"
action 2.3 elseif "$_string_result" eq 10
action 2.31 string replace "$_nd_port_id" 0 17 "Te"
action 2.4 elseif "$_string_result" eq 4
action 2.41 string replace "$_nd_port_id" 0 11 "Fa"
action 2.5 end
action 2.6 set _int "$_string_result"
action 3.0 comment "Actual config of port description"
action 3.1 cli command "enable"
action 3.2 cli command "config t"
action 3.3 cli command "interface $_nd_local_intf_name"
action 3.4 cli command "description $_host:$_int"
action 3.5 cli command "do write"
action 4.0 syslog msg "EEM script updated description on $_nd_local_intf_name and saved config"
Description would be something like: my-test-sw:Gi1/1/1 instead of my-test-sw.my.domain.name:GigabitEthernet1/1/1
Question: When (and why) are "action exit" required at the end of an applet? Could not find any good information on this.
Regards
Tommy V
Improved even further, please comment and/or improve further:
regexp in the event are for the uplink module on 3750-X/3560-X, adapt to your own need.
event manager applet auto-update-port-description authorization bypass
description "Auto-update port-description based on CDP neighbors info"
action 0.0 comment "Event line regexp: Deside which interface to auto-update description on"event neighbor-discovery interface regexp .*GigabitEthernet[1-9]/1/[1-4]$ cdp add
action 1.0 comment "Verify CDP neighbor to be Switch or Router"
action 1.1 regexp "(Switch|Router)" $_nd_cdp_capabilities_string
action 1.2 if $_regexp_result eq 1
action 2.0 comment "Trim domain name"
action 2.1 regexp "^([^\.]+)\." $_nd_cdp_entry_name match host
action 3.0 comment "Convert long interface name to short"
action 3.1 string first "Ethernet" "$_nd_port_id"
action 3.2 if "$_string_result" eq 7
action 3.21 string replace "$_nd_port_id" 0 14 "Gi"
action 3.3 elseif "$_string_result" eq 10
action 3.31 string replace "$_nd_port_id" 0 17 "Te"
action 3.4 elseif "$_string_result" eq 4
action 3.41 string replace "$_nd_port_id" 0 11 "Fa"
action 3.5 end
action 3.6 set int "$_string_result"
action 4.0 comment "Check old description if any, and do no change if same host:int"
action 4.1 cli command "enable"
action 4.11 cli command "config t"
action 4.2 cli command "do show interface $_nd_local_intf_name | incl Description:"
action 4.21 set olddesc "<none>"
action 4.22 set olddesc_sub1 "<none>"
action 4.23 regexp "Description: ([a-zA-Z0-9:/\-]*)([a-zA-Z0-9:/\-\ ]*)" "$_cli_result" olddesc olddesc_sub1
action 4.24 if "$olddesc_sub1" eq "$host:$int"
action 4.25 syslog msg "EEM script did NOT change desciption on $_nd_local_intf_name, since remote host and interface is unchanged"
action 4.26 exit 10
action 4.27 end
action 4.3 cli command "interface $_nd_local_intf_name"
action 4.4 cli command "description $host:$int"
action 4.5 cli command "do write"
action 4.6 syslog msg "EEM script updated description on $_nd_local_intf_name from $olddesc to Description: $host:$int and saved config"
action 5.0 end
action 6.0 exit 1
Description would be something like: my-test-sw:Gi1/1/1 instead of my-test-sw.my.domain.name:GigabitEthernet1/1/1
Does someone have a better way to shorten interface names?
Regards
Tommy V
Thanks for the mod!
Why not do something like this to shorten the port names:
action 3.1 string range $_nd_port_id 0 1
action 3.6 set int "$_string_result"
I'm trying to run this on a stack of 3750-X's running 15.0(2)SE, but I'm getting the following errors-
002407: Apr 9 16:41:33.564 CDT: %HA_EM-3-FMPD_UNKNOWN_ENV: fh_parse_var: could not find environment variable: _nd_cdp_capabilities_string
002408: Apr 9 16:41:33.564 CDT: %HA_EM-3-FMPD_ERROR: Error executing applet auto-update-port-description statement 1.1
Any suggestions on where to start looking?
In looking at the code, this variable should be set and set even in the case of capabilities being empty. If you enable "debug event manager detector nei" do you see any messages about capabilities when the event is triggered?
I do-
002611: Apr 10 09:05:41.302 CDT: ND Update CDP Notification Event for STACK.corp.com on Gi1/0/17
002612: Apr 10 09:05:41.302 CDT: ND Update CDP Notification Event for STACK.corp.com on Gi2/0/17
002613: Apr 10 09:05:41.302 CDT: ND Update CDP Notification Event for STACK.corp.com on Gi1/0/16
002614: Apr 10 09:05:41.302 CDT: ND Update CDP Notification Event for STACK.corp.com on Gi2/0/16
002615: Apr 10 09:05:41.310 CDT: fh_fd_nd_event_match: num_matches = 0
002616: Apr 10 09:05:41.310 CDT: fh_fd_nd_event_match: num_matches = 0
002617: Apr 10 09:05:41.310 CDT: fh_fd_nd_event_match: num_matches = 0
002618: Apr 10 09:05:41.310 CDT: fh_fd_nd_event_match: num_matches = 0
Please let me preface with something that could be causing the problem. I copied the script and then added 'event none'. Is that the correct procedure?
Hi Collin,
No, this variable will not be set for the none event detector. It will only be set for the neighbor discovery event detector.
Mike
If I remove the event none and run it (event manager run [applet name]) I get this
EEM policy update-port-description not registered with event none Event Detector
The policy in the comments as well as the policy in this doc are designed to only be run on the reception of a new CDP neighbor event. They cannot be run manually as they are designed.
OK. Stupid question then Joe :-) How do I register it so a new CDP neighbor event changes the description?
If you take either of these examples and simply configure them on your device as-is, then they will be listening for CDP events. You can test by clearing your CDP neighbor table.
Thanks Joe...it's slowly starting to sink in. I cleaned up the mess I was making. I cleared the cdp table. I see in the log that it sees a new event but it still does not name the port.
015051: Apr 10 13:38:19.867 CDT: ND Add CDP Notification Event for STACK.corp.com on Gi2/0/16
015052: Apr 10 13:38:19.867 CDT: Event type = CDP ADD
015053: Apr 10 13:38:19.867 CDT: Interface spec is a regexp
015054: Apr 10 13:38:19.867 CDT: Interface did not Match regexp
015055: Apr 10 13:38:19.867 CDT: nd_match_one_event: ret=FALSE
015056: Apr 10 13:38:19.867 CDT: fh_fd_nd_event_match: num_matches = 0
The regexp in the comment would not have matched. You want something like:
event neighbor-discovery interface regexp .*GigabitEthernet[1-9]/[0-9]/[0-9]+$ cdp add
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: