slides used in the hands-on lab. © 2011 cisco and/or its affiliates. all rights reserved. cisco...
TRANSCRIPT
Slides used in thehands-on lab
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 22
EEM TCL Event Register Keywords
CLI Event Keyword…
event_register_cli sync yes|no skip yes|no [occurs ?] [period ?] pattern ? [default ?][queue_priority low|normal|high] [maxrun ?] [nice 0|1]
SYNC is a mandatory parameter, SKIP is mandatory if SYNC is set to no - the rest are optional…OCCURS can be used to indicate the number of CLI command matches before an event is raised…PERIOD indicates the time interval between sampling
If you don’t want the CLI command to be executed, set SYNC as “no” and SKIP as “yes”…
::cisco::eem::event_register_cli sync no skip yes pattern “show ip interface brief” maxrun 20
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 2
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 33
EEM TCL Event Register Keywords
TIMER Event Keyword…
event_register_timer watchdog|countdown|absolute|cron [name ?] [cron_entry ?] [time ?] [queue_priority low|normal|high] [maxrun ?][nice 0|1]
The type of Timer event must be specified - all other keywords are optional…
::cisco::eem::event_register_timer cron cron_entry "2 9 * * *"
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 3
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 44
RESOURCE : CRON Information
Field Allowed Values
Minute 0-59
Hour 0-23
Day of month 1-31
Month 1-12 (or names, see table below)
Day of week 0-7 (0 or 7 is Sunday, or names, see table below)
String Meaning
@yearly Trigger once a year , “0 0 1 1 *”
@annually Same as @yearly
@monthly Trigger once a month, “0 0 1 * *”
@weekly Trigger once a week, “0 0 * * 0”
@daily Trigger once a day, “0 0 * * *”
@midnight Same as @daily
@hourly Trigger once an hour, “0 * * * *”© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 4
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 55
EEM TCL Event Register Keywords
NONE Event Keyword…
event_register_none [queue_priority low|normal|high] [maxrun ?] [nice 0|1]
The one key point about this one is the use of the MAXRUN command - it defaults to 20 seconds but might need ramping up especially if dealing with conditional loops and CLI connections…
::cisco::eem::event_register_none
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 5
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 66
EEM TCL Event Register Keywords
SYSLOG Event Keyword…
event_register_syslog [occurs ?] [period ?] pattern ? [priority all|emergencies|alerts|critical|errors|warnings|notifications| informational|debugging|0|1|2|3|4|5|6|7][queue_priority low|normal|high] [severity_fatal] [severity_critical] [severity_major][severity_minor] [severity_warning] [severity_notification][severity_normal] [severity_debugging][maxrun ?] [nice 0|1]
The pattern keyword must be specified with the SYSLOG message to monitor for - other optional keywords can be used to reduce the monitoring load based on message priority…
::cisco::eem::event_register_syslog pattern "%LINK-3-UPDOWN" maxrun 60
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 6
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 77
EEM Environment Variables
Setting/Updating Environment VariablesEnvironment Variables can be created or modified from the CLI using the following command…
Router(config)#event manager environment chassis 6509 Router(config)#do show event manager environmentNo. Name Value 1 module 5 2 intf GigabitEthernet6/2 3 chassis 6509
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 7
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 88
EEM Writing TCL Scripts
Namespace Import…Two namespace keywords should be specified in EVERY script that you load onto a Cisco switch or router…
namespace import ::cisco::eem::*namespace import ::cisco::lib::*
These two commands import the extensions into the TCL operating environment…
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 8
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 99
EEM TCL Basics—Creating a Variable
Variables…Can be created by using the “set” command…
set x 1
This creates the variable “x” and assigns it a value of 1
set init “abc”
This creates the variable “init” and assigns it a value of “abc”
Note: Assigning a text value necessitates enclosing the value in quotes…
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 9
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 1010
EEM TCL Basics—Referencing the Value in a Variable
Referencing the Value in a Variable…Referencing the variable value requires adding a “$” in front of the variable - consider the following…
set x 1set y $x
The second line assigns the value of variable $x to variable “y” - so $y will also inherit the value of 1
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 10
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 1111
EEM TCL Basics—Adding a Comment
Comments…Comments are invaluable to provide an understanding to other readers of the code for what the script is doing…
Comments are simply added by using a “#” at the beginning of the line
# This next block of code is used to# read in the contents of a file and place each# line into an array called $weekday
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 11
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 1212
EEM TCL Basics—Expressions
Expressions…To calculate math expressions, the “expr” command should be used - consider the following…
set x [expr 7 + 9 ]
The variable “x” is assigned the result of “7+9” - or another example
set y [expr [string length abc123] + 5 ]
The variable “y” is assigned the value of 11 as that is sum of the length in characters of the string “abc123” added to 5
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 12
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 1313
EEM TCL Basics—If-Then
If-Then…If-Then-Else is supported in TCL - a simple example
if { $name == “cisco”} { set init 1 }
The value of variable “init” will be changed to 1 if the variable $name has been set to “cisco” in an earlier part of the code (not shown)
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 13
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 1414
EEM TCL Basics—If-Then-Else
If-Then-Else…The “else” part of this option can also be used as shown in the following example…
if { $name == “cisco”} { set init 1} else { set init 2 }
Depending on the value of the variable $name, the variable $init will be set to either 1 or 2…
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 14
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 1515
EEM TCL Basics—More on Arrays
Arrays continued…
The values inside the array can also be accessed using an index as shown below…
set address “10.22.134.112”set x [split $address ,\.]# note that $x[0]=10, $x[1]=22, $x[2]=134, $x[3]=112set lastbyte [lindex $x 3]
This example splits out the ip address into the 4 bytes - it assigns each byte as a unique value in the array “$x” by splitting out the values using the “.” as the delimiter - the variable “lastbyte” is then assigned the last indexed value in the array (e.g. “112”)
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 15
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 1616
EEM TCL Basics—Reading in from a File
Reading Files…
The file format should be in plain ASCII (text) format - such a file can be accessed using the following command…
set infile [open disk0:/input.txt r]
This example opens a file “input.txt” that is located on the CF card in Disk0: - the file is opened in “read-only” format which is indicated by the “r” at the end of the line - as such, data cannot be written back out to this file
The command also sets up a file handle “infile” which is used as a pointer for the file contents that are read in…
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 16
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 1717
EEM TCL Basics—Reading in from a File
Reading Files…
Lets look at how the contents of a file can be read - lets assume the file “input.txt” is loaded with the following values…
set infile [open disk0:/input.txt r]set varindex 0; set finish 0while {[gets $infile line] >= 0} { if {$line == “end”} { set finish 1 } if {$finish == 0} { set data($varindex) $line incr varindex }}
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 17
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 1818
EEM TCL Basics—Writing to a File
Writing Files…
The file format will be in plain ASCII (text) format - such a file can be created using the following command…
set data [open disk0:/output.txt w]
This example opens a file “output.txt” that is located on the CF card in Disk0: - the file is opened in “write” format which is indicated by the “w” at the end of the line
The command also sets up a file handle “data” which is used as a pointer for the file contents that are written to the file…
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 18
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 1919
EEM TCL Basics—Writing to a File
Writing Files…
To write data to a file, the command “puts” is used…
puts <filehandle> <data to be written>
Using the file created on the previous slide, the following shows how data can be written
set x 10 puts $data $x
This example writes “10” into the file pointed to by the handle $data…
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 19
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 2020
EEM TCL Basics—Closing a File
Closing Files…
After extracting the data from a file, the file should be closed - this is done as follows…
close $infile
Using the previous file example, this closes the file represented by the handle “$infile”…
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 20
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 2121
EEM TCL Basics—How to Use Event Information
EVENT_REQINFO Event Keyword…The answer lays in using “event_reqinfo”…
array set syslog_details [event_reqinfo]
set answer $syslog_details(msg)
The variable “syslog_details” is primed with values (based on the SYSLOG event_reqinfo) with variables defining the state of the SYSLOG event - then we use…
Primes the variable “$answer” with the value of “msg” in the array “syslog_details”… In other words…
$answer = “%LINK-3-UPDOWN: Interface GigabitEthernet1/48, changed state to up”
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 21
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 2222
EEM TCL Basics—Regular Expressions
Regular Expressions…Are used to perform a pattern match on the whole (or part) value of a variable and takes the following format…
regexp { <pattern match>} $var abc
This command takes the value in variable $var, matches it to the regular expression <pattern match> and places the result of the inspection into the variable “abc”…
Next - how do we specify the “pattern match”…
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 22
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 2323
EEM TCL Basics—Regular Expressions
Some examples of pattern match…
^ match at beginning of string$ match at end of string. matches any literal character\x matches the character x[chars] matches any single character from char* matches zero or more of preceding atom+ matches one or more of preceding atom(abc) matches on expression abc
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 23
LAB
Example : regexp {(phone)} $Var1 Var2 will extract the word phone fromVar1 and assign it to Var2 if the value of Var1 is “I like my new cell phone”
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 2424
EEM TCL Basics—Invoking CLI Commands
Accessing the CLI…Before any commands can be invoked from a script, a connection (pipe) to the CLI must be opened with the following code…
if [catch {cli_open} result] { error $result $errorInfo } else { array set cli1 $result }
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 24
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 2525
EEM TCL Basics—Invoking CLI Commands
Invoking a command to the CLI…A command can be invoked as follows…
if [catch {cli_exec $cli1(fd) "enable"} result] { error $result $errorInfo}if [catch {cli_exec $cli1(fd) "conf t"} result] { error $result $errorInfo}
This example shows two commands being invoked one after the after…
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 25
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 2626
EEM TCL Basics—Invoking CLI Commands
Closing a connection to the CLI…After the CLI commands have been invoked - the connection to the CLI must be closed - use the following code to perform this action…
if [catch {cli_close $cli1(fd) $cli1(tty_id)} result] { error $result $errorInfo}
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 26
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 2727
EEM TCL Basics—Creating a Custom SYSLOG Message
Custom SYSLOG message…A custom SYSLOG message can be created as follows… action_syslog [priority emerg|alert|crit|err|warning|notice|info|debug] [msg ?]
An example is as follows…
if { $x == 1} { action_syslog msg “This is a test message”}
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 27
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 2828
EEM TCL Basics—Action Reload
Action Reload…This command can be used to force a reload of the entire switch…
An example of this follows…
if {$upgrade_complete == “yes”} { action_reload
}
action_reload
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 28
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 2929
EEM Creating a TCL User Directory
Script Directory…
Scripts MUST be loaded into a registered directory in order to be successfully invoked - creating the Directory can be done using the following…
Router# mkdir EEMCreate directory filename [EEM]? Created dir flash:EEMRouter#
This example creates a directory called “EEM” on flash:
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 29
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 3030
EEM Registering a TCL User Directory
Registering a Script Directory…
After creation, the Directory must be registered - this can be done using the following…
Router(config)# event manager directory user policy flash:/EEMRouter(config)# ^ZRouter# show event manager directory user policy flash:/EEM
The Directory is now registered and can be primed with TCL scripts
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 30
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 3131
EEM Registering TCL Script
Registering Scripts…
Scripts MUST be loaded into a registered directory in order to be successfully invoked - registering the script can be done using the following…
Router(config)# event manager policy <script-name> type user
Where <script-name> is the name of the script (e.g. cdp.tcl)
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 31
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 3232
EEM Registering TCL Script
Viewing Registering Scripts…Scripts that have been registered can be viewed using the following command…
Router#show event manager policy registeredNo. Class Type Event Type Trap Time Registered Name1 applet system cli Off Tue Dec 19 18:29:22 2006 my_applet pattern {conf t} sync no skip no
2 script user none Off Tue Dec 19 18:29:22 2006 autoqos.tcl policyname {autoqos.tcl} nice 0 queue-priority normal maxrun 20.000
3 script user none Off Tue Dec 19 18:29:22 2006 macip.tcl policyname {macip.tcl} nice 0 queue-priority normal maxrun 200.000
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 32
LAB
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 3333
EEM EEM Show Commands
Viewing Contents of Registering Scripts…The body of scripts that have been registered can be viewed using the following command…
Router#show event manager policy available detailed macip.tcl ::cisco::eem::event_register_none maxrun 200#------------------------------------------------------------------# EEM Add Static DHCP Snooping DB entries## December 2008# Copyright (c) 2008 by Cisco Systems, Inc.# All rights reserved.#------------------------------------------------------------------#namespace import ::cisco::eem::*namespace import ::cisco::lib::*# Open pipe to CLI and enter enable mode --More--
© 2011 Cisco and/or its affiliates. All rights reserved. Cisco PublicTECCRS-3000 33
LAB