ASR9k EEM + TCL Interactive Scripting

Requirement:
1. Capture interface tunnel port each 5 minutes, if traffics > X, will capture other information.
2. Store those information to disk0/harddisk.

In fact, the requirement is very easy by Python + CRT, but customer couldn’t find a PC to continue to run python script, so only use EEM + TCL on ASR9k. And in TCL script, I use two function: foreach and scan.

Follow CLI need config before do script, if you change any variable or script, you need re-config “event manager policy tac_te.tcl username cisco”:

aaa authorization eventmanager default local
event manager environment _cron_entry1 */5 * * * *
event manager directory user policy disk0:
event manager policy tac_te.tcl username cisco persist-time 3600 type user


Follow output result, a,b,x,y… are test flag:

RP/0/RSP0/CPU0:Jan  5 16:13:38.545 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: Starting scripts! 
RP/0/RSP0/CPU0:Jan  5 16:13:39.557 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: a 
RP/0/RSP0/CPU0:Jan  5 16:13:40.041 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: b 
RP/0/RSP0/CPU0:Jan  5 16:13:40.042 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.043 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.044 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.045 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.045 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.046 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.047 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.048 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.825 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: b 
RP/0/RSP0/CPU0:Jan  5 16:13:40.826 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.827 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.828 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.828 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.829 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.830 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.831 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.832 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.832 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.833 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.834 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.835 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.835 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.836 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.837 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.838 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.839 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.839 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.840 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.841 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.842 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.842 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.843 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.844 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.845 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.846 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.846 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.847 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.848 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.849 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.850 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.851 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.851 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.852 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.853 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: z 
RP/0/RSP0/CPU0:Jan  5 16:13:40.854 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: xx 
RP/0/RSP0/CPU0:Jan  5 16:13:40.855 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: x 
RP/0/RSP0/CPU0:Jan  5 16:13:40.856 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: y 
RP/0/RSP0/CPU0:Jan  5 16:13:40.856 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: z 
RP/0/RSP0/CPU0:Jan  5 16:13:40.857 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: xx 
RP/0/RSP0/CPU0:Jan  5 16:13:40.858 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: zz 
RP/0/RSP0/CPU0:Jan  5 16:13:40.859 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: Writing Capture Info... 
RP/0/RSP0/CPU0:Jan  5 16:13:40.860 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: c 
RP/0/RSP0/CPU0:Jan  5 16:13:41.424 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: e 
RP/0/RSP0/CPU0:Jan  5 16:13:41.501 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: f 
RP/0/RSP0/CPU0:Jan  5 16:13:41.508 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: g 
RP/0/RSP0/CPU0:Jan  5 16:13:41.508 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: c 
RP/0/RSP0/CPU0:Jan  5 16:13:45.077 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: e 
RP/0/RSP0/CPU0:Jan  5 16:13:45.080 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: f 
RP/0/RSP0/CPU0:Jan  5 16:13:45.166 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: g 
RP/0/RSP0/CPU0:Jan  5 16:13:45.167 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: c 
RP/0/RSP0/CPU0:Jan  5 16:13:55.027 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: e 
RP/0/RSP0/CPU0:Jan  5 16:13:55.145 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: f 
RP/0/RSP0/CPU0:Jan  5 16:13:55.160 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: g 
RP/0/RSP0/CPU0:Jan  5 16:13:55.161 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: h 
RP/0/RSP0/CPU0:Jan  5 16:13:56.466 : tclsh[65783]: %HA-HA_EEM-6-ACTION_SYSLOG_LOG_INFO : tac_te.tcl: End script 

RP/0/RSP0/CPU0:ASR9001-1#dir disk0:CASE-TS.txt
Mon Jan  5 16:14:24.840 UTC
Directory of disk0:

11726670    -rw-  44276       Mon Jan  5 16:13:55 2015  CASE-TS.txt
4026515456 bytes total (2994521088 bytes free)

RP/0/RSP0/CPU0:ASR9001-1#dir disk0:CASE-TS.txt
Mon Jan  5 16:42:51.630 UTC
Directory of disk0:

11726670    -rw-  1195452     Mon Jan  5 16:39:55 2015  CASE-TS.txt
4026515456 bytes total (2993374208 bytes free)

Follow script:

#
# XR TCL Offical Document
# http://www.cisco.com/c/en/us/td/docs/routers/crs/software/crs_r4-2/system_monitoring/configuration/guide/b_sysmon_cg42crs/b_sysmon_cg42crs_chapter_010.html#con_1219664
#

#::cisco::eem::event_register_timer watchdog name timer sec 60 maxrun_sec 900
::cisco::eem::event_register_timer cron name crontimer1 cron_entry $_cron_entry1

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

#
# errorInf 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 a ""
set b ""
set condition_commond_list [list \
    " sh int MgmtEth0/RSP0/CPU0/0" \
]
set command_list [list \
	" show platform"  \
	" show ins ac sum" \
	" show interface" \
]

# Notify users that we're collecting
action_syslog priority info msg "Starting scripts!"

# Query the event info, check error, you should not change
array set arr_einfo [event_reqinfo]

if {$_cerrno != 0} {
    set result [format "component=%s; subsys err=%s; posix err=%s;\n%s" \
        $_cerr_sub_num $_cerr_sub_err $_cerr_posix_err $_cerr_str]
    error $result 
}

# Open a cli connection
if [catch {cli_open} result] {
	error $result $errorInfo
} else {
	array set cli1 $result
}

# Loop through the command list1
action_syslog priority info msg "a"
foreach comm_temp1 $condition_commond_list {
	if [catch {cli_exec $cli1(fd) $comm_temp1} cli_show] {
		error $result $errorInfo
	}
    action_syslog priority info msg "b" 
	foreach line [split $cli_show \n] {
		action_syslog priority info msg "x" 
		# If find "input rate", match1 is available variable, so that should 1
		# If not find, match1 is unavailable variable, so that should 0
		# Attention: the regexp() couldn't have bank! 
	    set match1 [regexp (rate) $line match]
		action_syslog priority info msg "y" 
		# Trigger condition is bobcat port traffics > 10M
	    if {$match1 == 1} {
			action_syslog priority info msg "z"
			# String must exactly match $line, include bank! 
	        scan $line "  5 minute input rate %d bits/sec, %d packets/sec" a b
			action_syslog priority info msg "xx" 
	        if {$a > 500} {
	             set condition 1
				 action_syslog priority info msg "zz" 
				 break
	        }
	    } 
	}
}

# Capture information when issue happened
set filename "/disk0:/CASE-TS.txt"

if {$condition==1} {
	action_syslog priority info msg "Writing Capture Info..."
	foreach comm_temp_2 $command_list {
		action_syslog priority info msg "c"
		if [catch {cli_exec $cli1(fd) $comm_temp_2} cli_show_2] {
			action_syslog priority info msg "d"
			error $result $errorInfo
		}
		action_syslog priority info msg "e"
		# must re-set outfile before puts
		set outfile [open $filename a+]
		puts $outfile "$cli_show_2"
		action_syslog priority info msg "f"
		close $outfile
		action_syslog priority info msg "g"
	}
	action_syslog priority info msg "h"	
}

#close the cli connection
if [catch {cli_close $cli1(fd) $cli1(tty_id)} result] {
    error $result $errorInfo
}

#Notify users that we have finished checking the memory
set output_msg "End script"
action_syslog priority info msg $output_msg

Follow good site, can reference convert from EEM to TCL script:
Convert an EEM Applet to a Tcl Policy

anyShare分享到:
你可以留言,或者trackback 从你的网站

留言哦