Monday, November 24, 2014

Cisco ASR9K L2VPN Mac Usage Script - TCL

If you manage a ASR9K router you must be sure that the mac address usage of the linecards do not exceed their capacity, otherwise based on the configuration traffic either going to be dropped or flooded. Trident based old line cards has a capacity of 128k Mac adress, and this limit could be a problem if you terminate too many l2 circuits.In such a scenerio, it is possible to write a TCL to create a syslog entry whenever the usage reaches a certain configurable amount. To do so, i wrote a TCL script, here are the details:

Command to Check Mac Usage
 You can check any of the linecards  resource like below:

RP/0/RSP0/CPU0:xxxx: show l2vpn forwarding resource hardware ingress detail location 0/0/CPU0 | include TOTAL MAC
Mon Nov 33 20:49:01.870 xxx
    TOTAL MAC     -/-         -/-         -/-         -/-         -/-         -/-      4325/2097152
    TOTAL MAC     -/-         -/-         -/-         -/-         -/-         -/-      4325/2097152
    TOTAL MAC     -/-         -/-         -/-         -/-         -/-         -/-      4325/2097152
    TOTAL MAC     -/-         -/-         -/-         -/-         -/-         -/-      4323/2097152
    TOTAL MAC     -/-         -/-         -/-         -/-         -/-         -/-      4323/2097152
    TOTAL MAC     -/-         -/-         -/-         -/-         -/-         -/-      4325/2097152
    TOTAL MAC     -/-         -/-         -/-         -/-         -/-         -/-      4323/2097152
    TOTAL MAC     -/-         -/-         -/-         -/-         -/-         -/-      4323/2097152


Prerequisites
For any script to work, router must have a hostname. Unless it is a lab router, you probably wouldn’t have such a problem, but keep in mind.

Determine a folder  in router  to copy the script, and make that directory.
#cd disk0:
#pwd
Mon Nov 21 15:37:24.393 UTC
disk0:
#mkdir tcl_scripts
Mon Nov 21 15:37:30.098 UTC
Create directory filename [tcl_scripts?
Created dir disk0:/ tcl_scripts


Point your user scripts folder to event manager.
(config)#event manager directory user policy disk0:/tcl_scripts

Configure AAA, so that event manager use locally configured users.
(config)#aaa authorization eventmanager default local

Under “admin” hierarchy create a user to execute scripts.
(config)#admin
(config)#username eem
(config-un)#group root-system
(config-un)#group cisco-support

The Script File:
I am a beginner if the case is writing a TCL script. In the script below i did not use loop/while cyles, as a result some part of the code repeats itself for every line card, moreover anyone experienced in regexps could shorten the code.

If you are not going to do any change, copy the script below into a text file and save it as a file. In my case i named it as “”.

  • Script should work on ASR9010 and ASR9006.
  • Script checks only the cards that are in “IOS-XR RUN” state. So there should not be a problem if linecard slots are empty or  the linecards are off due to any reason. 
  • Do not use tcl operators supported by TCL versions newer than 8.3.4


::cisco::eem::event_register_timer cron name l2maccheck cron_entry $l2maccheck maxrun_sec 240
namespace import ::cisco::eem::*
namespace import ::cisco::lib::*
################################################################################################
#                                                                                              #
#  Revision #          :1.2                                                                    #
#  Last Updated        :01.10.2014                                                             #  
#  Author/Contributor  :Ercin TORUN                                                            #
#                                                                                              #
#  Description         :Script checks if any of the linecards reaches its MAC capacity         #
#                       and creates a syslog message.                                          #
#                                                                                              #
#  Requirements        :                                                                       #
#                                                                                              #                   
#                                                                                              #
#                       Example: event manager environment l2vpnmac_threshold_percent 85       #   
#                                event manager environment l2maccheck 0 * * * *                #
#                                                                                              #
#  Cisco Products tested : ASR9010-9006                                                        #   
#                                                                                              #
#  Cisco IOS-XR Version tested : 4.2.3                                                         #  
#                                                                                              #
################################################################################################
##########################################################
#####################SET VARIABLES########################
##########################################################
# errorInfo gets set by namespace if any of the auto_path directories do not
# contain a valid tclIndex file. It is not an error just left over stuff.
# so we set the errorInfo value to null so that we don't have left
# over errors in it.
#
set errorInfo ""
set USEDMAC_LINECARD0_INGRESS "0"
set TOTALMAC_LINECARD0_INGRESS "0"
set USEDMAC_LINECARD1_INGRESS "0"
set TOTALMAC_LINECARD1_INGRESS "0"
set USEDMAC_LINECARD2_INGRESS "0"
set TOTALMAC_LINECARD2_INGRESS "0"
set USEDMAC_LINECARD3_INGRESS "0"
set TOTALMAC_LINECARD3_INGRESS "0"
set USEDMAC_LINECARD4_INGRESS "0"
set TOTALMAC_LINECARD4_INGRESS "0"
set USEDMAC_LINECARD5_INGRESS "0"
set TOTALMAC_LINECARD5_INGRESS "0"
set USEDMAC_LINECARD7_INGRESS "0"
set TOTALMAC_LINECARD7_INGRESS "0"
set USEDMAC_LINECARD7_INGRESS "0"
set TOTALMAC_LINECARD7_INGRESS "0"
set card0check "0"
set card1check "0"
set card2check "0"
set card3check "0"
set card4check "0"
set card5check "0"
set card6check "0"
set card7check "0"
##########################################################
#Check required environment variable(s) has been defined##
##########################################################
if {![info exists l2vpnmac_threshold_percent]} {
    set result "EEM Policy Error: variable l2vpnmac_threshold_percent has not been set"
    error $result $errorInfo
}
if {![info exists l2maccheck]} {
    set result "EEM Policy Error: variable l2maccheck has not been set"
    error $result $errorInfo
}
##############LINECARD-EXIST-CHECK####################
#------------------- "cli open"-------------------
if [catch {cli_open} result] {
    error $result $errorInfo
} else {
    array set cli $result
}
if [catch {cli_exec $cli(fd) "show platform | i IOS XR RUN "} result] {
    error $result $errorInfo
} else { 
 set line_card_exist_check $result
 regexp {0/0/CPU0} $line_card_exist_check card0check
 regexp {0/1/CPU0} $line_card_exist_check card1check
 regexp {0/2/CPU0} $line_card_exist_check card2check
 regexp {0/3/CPU0} $line_card_exist_check card3check
 regexp {0/4/CPU0} $line_card_exist_check card4check
 regexp {0/5/CPU0} $line_card_exist_check card5check
 regexp {0/6/CPU0} $line_card_exist_check card6check
 regexp {0/7/CPU0} $line_card_exist_check card7check 
 }
##########################################################
############MAC LINECARD0 INGRESS#########################
##########################################################
if { [ string length $card0check ] > 2 } {
if [catch {cli_open} result] {
    error $result $errorInfo
} else {
    array set cli $result
}
if [catch {cli_exec $cli(fd) "show l2vpn forwarding resource hardware ingress detail location 0/0/CPU0 | include TOTAL MAC"} result] { 
    error $result $errorInfo
} else { 
 set linecard0_output $result
 regsub -all { TOTAL MAC } $linecard0_output "USEDMAC" linecard0_output
 regsub -all { -/- } $linecard0_output "" linecard0_output
 regsub -all "\\s" $linecard0_output "" linecard0_output
 regsub -all "/" $linecard0_output "TOTALMAC" linecard0_output
 regexp {USEDMAC([0-9]*)TOTALMAC([0-9]*)} $linecard0_output linecard0_output
 regexp {USEDMAC([0-9]*)} $linecard0_output USEDMAC_LINECARD0_INGRESS
 regexp {TOTALMAC([0-9]*)} $linecard0_output TOTALMAC_LINECARD0_INGRESS
 regsub -all {[A-Z]} $USEDMAC_LINECARD0_INGRESS "" USEDMAC_LINECARD0_INGRESS
 regsub -all {[A-Z]} $TOTALMAC_LINECARD0_INGRESS "" TOTALMAC_LINECARD0_INGRESS
}
 if {$TOTALMAC_LINECARD0_INGRESS > 63999} {
 set linecard0_ingress_percent [ expr ($USEDMAC_LINECARD0_INGRESS*100.0)/($TOTALMAC_LINECARD0_INGRESS)]
}
 if { $linecard0_ingress_percent > $l2vpnmac_threshold_percent } {
    action_syslog msg "LINECARD0 Mac Adress usage is $USEDMAC_LINECARD0_INGRESS and exceeded the threshold % $l2vpnmac_threshold_percent ! If mac address usage on LINECARD0 reaches up to $TOTALMAC_LINECARD0_INGRESS , traffic might get flooded or dropped depending on the l2vpn configuration of the router"
}
if [catch {cli_close $cli(fd) $cli(tty_id)} result] {
    error $result $errorInfo
}
}
##########################################################
############MAC LINECARD1 INGRESS#########################
##########################################################
if { [ string length $card1check ] > 2 } {
if [catch {cli_open} result] {
    error $result $errorInfo
} else {
    array set cli $result
}
if [catch {cli_exec $cli(fd) "show l2vpn forwarding resource hardware ingress detail location 0/1/CPU0 | include TOTAL MAC"} result] { 
    error $result $errorInfo
} else { 
 set linecard1_output $result
 regsub -all { TOTAL MAC } $linecard1_output "USEDMAC" linecard1_output
 regsub -all { -/- } $linecard1_output "" linecard1_output
 regsub -all "\\s" $linecard1_output "" linecard1_output
 regsub -all "/" $linecard1_output "TOTALMAC" linecard1_output
 regexp {USEDMAC([0-9]*)TOTALMAC([0-9]*)} $linecard1_output linecard1_output
 regexp {USEDMAC([0-9]*)} $linecard1_output USEDMAC_LINECARD1_INGRESS
 regexp {TOTALMAC([0-9]*)} $linecard1_output TOTALMAC_LINECARD1_INGRESS
 regsub -all {[A-Z]} $USEDMAC_LINECARD1_INGRESS "" USEDMAC_LINECARD1_INGRESS
 regsub -all {[A-Z]} $TOTALMAC_LINECARD1_INGRESS "" TOTALMAC_LINECARD1_INGRESS
}
 if {$TOTALMAC_LINECARD1_INGRESS > 63999} {
 set linecard1_ingress_percent [ expr ($USEDMAC_LINECARD1_INGRESS*100.0)/($TOTALMAC_LINECARD1_INGRESS)]
}
 if { $linecard1_ingress_percent > $l2vpnmac_threshold_percent } {
    action_syslog msg "LINECARD1 Mac Adress usage is $USEDMAC_LINECARD1_INGRESS and exceeded the threshold % $l2vpnmac_threshold_percent ! If mac address usage on LINECARD1 reaches up to $TOTALMAC_LINECARD1_INGRESS , traffic might get flooded or dropped depending on the l2vpn configuration of the router"
}
if [catch {cli_close $cli(fd) $cli(tty_id)} result] {
    error $result $errorInfo
}
}
##########################################################
############MAC LINECARD2 INGRESS#########################
##########################################################
if { [ string length $card2check ] > 2 } {
if [catch {cli_open} result] {
    error $result $errorInfo
} else {
    array set cli $result
}
if [catch {cli_exec $cli(fd) "show l2vpn forwarding resource hardware ingress detail location 0/2/CPU0 | include TOTAL MAC"} result] { 
    error $result $errorInfo
} else { 
 set linecard2_output $result
 regsub -all { TOTAL MAC } $linecard2_output "USEDMAC" linecard2_output
 regsub -all { -/- } $linecard2_output "" linecard2_output
 regsub -all "\\s" $linecard2_output "" linecard2_output
 regsub -all "/" $linecard2_output "TOTALMAC" linecard2_output
 regexp {USEDMAC([0-9]*)TOTALMAC([0-9]*)} $linecard2_output linecard2_output
 regexp {USEDMAC([0-9]*)} $linecard2_output USEDMAC_LINECARD2_INGRESS
 regexp {TOTALMAC([0-9]*)} $linecard2_output TOTALMAC_LINECARD2_INGRESS
 regsub -all {[A-Z]} $USEDMAC_LINECARD2_INGRESS "" USEDMAC_LINECARD2_INGRESS
 regsub -all {[A-Z]} $TOTALMAC_LINECARD2_INGRESS "" TOTALMAC_LINECARD2_INGRESS
}
 if {$TOTALMAC_LINECARD2_INGRESS > 63999} {
 set linecard2_ingress_percent [ expr ($USEDMAC_LINECARD2_INGRESS*100.0)/($TOTALMAC_LINECARD2_INGRESS)]
}
 if { $linecard2_ingress_percent > $l2vpnmac_threshold_percent } {
    action_syslog msg "LINECARD2 Mac Adress usage is $USEDMAC_LINECARD2_INGRESS and exceeded the threshold % $l2vpnmac_threshold_percent ! If mac address usage on LINECARD2 reaches up to $TOTALMAC_LINECARD2_INGRESS , traffic might get flooded or dropped depending on the l2vpn configuration of the router"
}
if [catch {cli_close $cli(fd) $cli(tty_id)} result] {
    error $result $errorInfo
}
}
##########################################################
############MAC LINECARD3 INGRESS#########################
##########################################################
if { [ string length $card3check ] > 2 } {
if [catch {cli_open} result] {
    error $result $errorInfo
} else {
    array set cli $result
}
if [catch {cli_exec $cli(fd) "show l2vpn forwarding resource hardware ingress detail location 0/3/CPU0 | include TOTAL MAC"} result] { 
    error $result $errorInfo
} else { 
 set linecard3_output $result
 regsub -all { TOTAL MAC } $linecard3_output "USEDMAC" linecard3_output
 regsub -all { -/- } $linecard3_output "" linecard3_output
 regsub -all "\\s" $linecard3_output "" linecard3_output
 regsub -all "/" $linecard3_output "TOTALMAC" linecard3_output
 regexp {USEDMAC([0-9]*)TOTALMAC([0-9]*)} $linecard3_output linecard3_output
 regexp {USEDMAC([0-9]*)} $linecard3_output USEDMAC_LINECARD3_INGRESS
 regexp {TOTALMAC([0-9]*)} $linecard3_output TOTALMAC_LINECARD3_INGRESS
 regsub -all {[A-Z]} $USEDMAC_LINECARD3_INGRESS "" USEDMAC_LINECARD3_INGRESS
 regsub -all {[A-Z]} $TOTALMAC_LINECARD3_INGRESS "" TOTALMAC_LINECARD3_INGRESS
}
 if {$TOTALMAC_LINECARD3_INGRESS > 63999} {
 set linecard3_ingress_percent [ expr ($USEDMAC_LINECARD3_INGRESS*100.0)/($TOTALMAC_LINECARD3_INGRESS)]
}
 if { $linecard3_ingress_percent > $l2vpnmac_threshold_percent } {
    action_syslog msg "LINECARD3 Mac Adress usage is $USEDMAC_LINECARD3_INGRESS and exceeded the threshold % $l2vpnmac_threshold_percent ! If mac address usage on LINECARD3 reaches up to $TOTALMAC_LINECARD3_INGRESS , traffic might get flooded or dropped depending on the l2vpn configuration of the router"
}
if [catch {cli_close $cli(fd) $cli(tty_id)} result] {
    error $result $errorInfo
}
}
##########################################################
############MAC LINECARD4 INGRESS#########################
##########################################################
if { [ string length $card4check ] > 2 } {
if [catch {cli_open} result] {
    error $result $errorInfo
} else {
    array set cli $result
}
if [catch {cli_exec $cli(fd) "show l2vpn forwarding resource hardware ingress detail location 0/4/CPU0 | include TOTAL MAC"} result] { 
    error $result $errorInfo
} else { 
 set linecard4_output $result
 regsub -all { TOTAL MAC } $linecard4_output "USEDMAC" linecard4_output
 regsub -all { -/- } $linecard4_output "" linecard4_output
 regsub -all "\\s" $linecard4_output "" linecard4_output
 regsub -all "/" $linecard4_output "TOTALMAC" linecard4_output
 regexp {USEDMAC([0-9]*)TOTALMAC([0-9]*)} $linecard4_output linecard4_output
 regexp {USEDMAC([0-9]*)} $linecard4_output USEDMAC_LINECARD4_INGRESS
 regexp {TOTALMAC([0-9]*)} $linecard4_output TOTALMAC_LINECARD4_INGRESS
 regsub -all {[A-Z]} $USEDMAC_LINECARD4_INGRESS "" USEDMAC_LINECARD4_INGRESS
 regsub -all {[A-Z]} $TOTALMAC_LINECARD4_INGRESS "" TOTALMAC_LINECARD4_INGRESS
}
 if {$TOTALMAC_LINECARD4_INGRESS > 63999} {
 set linecard4_ingress_percent [ expr ($USEDMAC_LINECARD4_INGRESS*100.0)/($TOTALMAC_LINECARD4_INGRESS)]
}
 if { $linecard4_ingress_percent > $l2vpnmac_threshold_percent } {
    action_syslog msg "LINECARD4 Mac Adress usage is $USEDMAC_LINECARD4_INGRESS and exceeded the threshold % $l2vpnmac_threshold_percent ! If mac address usage on LINECARD4 reaches up to $TOTALMAC_LINECARD4_INGRESS , traffic might get flooded or dropped depending on the l2vpn configuration of the router"
}
if [catch {cli_close $cli(fd) $cli(tty_id)} result] {
    error $result $errorInfo
}
}
##########################################################
############MAC LINECARD5 INGRESS#########################
##########################################################
if { [ string length $card5check ] > 2 } {
if [catch {cli_open} result] {
    error $result $errorInfo
} else {
    array set cli $result
}
if [catch {cli_exec $cli(fd) "show l2vpn forwarding resource hardware ingress detail location 0/5/CPU0 | include TOTAL MAC"} result] { 
    error $result $errorInfo
} else { 
 set linecard5_output $result
 regsub -all { TOTAL MAC } $linecard5_output "USEDMAC" linecard5_output
 regsub -all { -/- } $linecard5_output "" linecard5_output
 regsub -all "\\s" $linecard5_output "" linecard5_output
 regsub -all "/" $linecard5_output "TOTALMAC" linecard5_output
 regexp {USEDMAC([0-9]*)TOTALMAC([0-9]*)} $linecard5_output linecard5_output
 regexp {USEDMAC([0-9]*)} $linecard5_output USEDMAC_LINECARD5_INGRESS
 regexp {TOTALMAC([0-9]*)} $linecard5_output TOTALMAC_LINECARD5_INGRESS
 regsub -all {[A-Z]} $USEDMAC_LINECARD5_INGRESS "" USEDMAC_LINECARD5_INGRESS
 regsub -all {[A-Z]} $TOTALMAC_LINECARD5_INGRESS "" TOTALMAC_LINECARD5_INGRESS
}
 if {$TOTALMAC_LINECARD0_INGRESS > 63999} {
 set linecard5_ingress_percent [ expr ($USEDMAC_LINECARD5_INGRESS*100.0)/($TOTALMAC_LINECARD5_INGRESS)]
}
 if { $linecard5_ingress_percent > $l2vpnmac_threshold_percent } {
    action_syslog msg "LINECARD5 Mac Adress usage is $USEDMAC_LINECARD5_INGRESS and exceeded the threshold % $l2vpnmac_threshold_percent ! If mac address usage on LINECARD5 reaches up to $TOTALMAC_LINECARD5_INGRESS , traffic might get flooded or dropped depending on the l2vpn configuration of the router"
}
if [catch {cli_close $cli(fd) $cli(tty_id)} result] {
    error $result $errorInfo
}
}
##########################################################
############MAC LINECARD6 INGRESS#########################
##########################################################
if { [ string length $card6check ] > 2 } {
if [catch {cli_open} result] {
    error $result $errorInfo
} else {
    array set cli $result
}
if [catch {cli_exec $cli(fd) "show l2vpn forwarding resource hardware ingress detail location 0/6/CPU0 | include TOTAL MAC"} result] { 
    error $result $errorInfo
} else { 
 set linecard6_output $result
 regsub -all { TOTAL MAC } $linecard6_output "USEDMAC" linecard6_output
 regsub -all { -/- } $linecard6_output "" linecard6_output
 regsub -all "\\s" $linecard6_output "" linecard6_output
 regsub -all "/" $linecard6_output "TOTALMAC" linecard6_output
 regexp {USEDMAC([0-9]*)TOTALMAC([0-9]*)} $linecard6_output linecard6_output
 regexp {USEDMAC([0-9]*)} $linecard6_output USEDMAC_LINECARD6_INGRESS
 regexp {TOTALMAC([0-9]*)} $linecard6_output TOTALMAC_LINECARD6_INGRESS
 regsub -all {[A-Z]} $USEDMAC_LINECARD6_INGRESS "" USEDMAC_LINECARD6_INGRESS
 regsub -all {[A-Z]} $TOTALMAC_LINECARD6_INGRESS "" TOTALMAC_LINECARD6_INGRESS
}
 if {$TOTALMAC_LINECARD6_INGRESS > 63999} {
 set linecard6_ingress_percent [ expr ($USEDMAC_LINECARD6_INGRESS*100.0)/($TOTALMAC_LINECARD6_INGRESS)]
}
 if { $linecard6_ingress_percent > $l2vpnmac_threshold_percent } {
    action_syslog msg "LINECARD6 Mac Adress usage is $USEDMAC_LINECARD6_INGRESS and exceeded the threshold % $l2vpnmac_threshold_percent ! If mac address usage on LINECARD6 reaches up to $TOTALMAC_LINECARD6_INGRESS , traffic might get flooded or dropped depending on the l2vpn configuration of the router"
}
if [catch {cli_close $cli(fd) $cli(tty_id)} result] {
    error $result $errorInfo
}
}
##########################################################
############MAC LINECARD7 INGRESS#########################
##########################################################
if { [ string length $card7check ] > 2 } {
if [catch {cli_open} result] {
    error $result $errorInfo
} else {
    array set cli $result
}
if [catch {cli_exec $cli(fd) "show l2vpn forwarding resource hardware ingress detail location 0/7/CPU0 | include TOTAL MAC"} result] { 
    error $result $errorInfo
} else { 
 set linecard7_output $result
 regsub -all { TOTAL MAC } $linecard7_output "USEDMAC" linecard7_output
 regsub -all { -/- } $linecard7_output "" linecard7_output
 regsub -all "\\s" $linecard7_output "" linecard7_output
 regsub -all "/" $linecard7_output "TOTALMAC" linecard7_output
 regexp {USEDMAC([0-9]*)TOTALMAC([0-9]*)} $linecard7_output linecard7_output
 regexp {USEDMAC([0-9]*)} $linecard7_output USEDMAC_LINECARD7_INGRESS
 regexp {TOTALMAC([0-9]*)} $linecard7_output TOTALMAC_LINECARD7_INGRESS
 regsub -all {[A-Z]} $USEDMAC_LINECARD7_INGRESS "" USEDMAC_LINECARD7_INGRESS
 regsub -all {[A-Z]} $TOTALMAC_LINECARD7_INGRESS "" TOTALMAC_LINECARD7_INGRESS
}
 if {$TOTALMAC_LINECARD7_INGRESS > 63999} {
 set linecard7_ingress_percent [ expr ($USEDMAC_LINECARD7_INGRESS*100.0)/($TOTALMAC_LINECARD7_INGRESS)]
}
 if { $linecard7_ingress_percent > $l2vpnmac_threshold_percent } {
    action_syslog msg "LINECARD7 Mac Adress usage is $USEDMAC_LINECARD7_INGRESS and exceeded the threshold % $l2vpnmac_threshold_percent ! If mac address usage on LINECARD7 reaches up to $TOTALMAC_LINECARD7_INGRESS , traffic might get flooded or dropped depending on the l2vpn configuration of the router"
}
if [catch {cli_close $cli(fd) $cli(tty_id)} result] {
    error $result $errorInfo
}
}
############CLOSE#########################

Activating the Script

After copying the script to a folder, you must define the environment variables  For this script, you must define the warning thresold and a cron entry for repetitive tasks.

(config)# event manager environment l2vpnmac_threshold_percent 85   
(config)# event manager environment l2maccheck 0 * * * * 
(config)# event manager policy  asr9010_l2vpn_mac_check username eem type user

NOTE: make a Google search if you are not familiar with cron entries.  Some examples:

0 * * * *            ------->  0. Minute of every hour
15 * * * *          ------->  15. Minute of every hour
*/10 * * * *    ------->  e very 10 minutes

0 comments:

Post a Comment

 

Internetworking Hints Copyright © 2011 -- Template created by O Pregador -- Powered by Blogger