• No results found

Tcl Example Used to Check the CPU Utilization

In document Tcl Scripting for Cisco IOS (Page 121-128)

Step 3. Concatenate the show command output with a standard e-mail template from the library and get the e-mail server IP address:

if [catch {smtp_subst [file join $tcl_library email_template_cmd.tm]}

result] {

error $result $errorInfo }

Step 4. Send the e-mail message:

if [catch {smtp_send_email $result} result] { error $result $errorInfo

}

Figure 4-6 shows the e-mail received from the IOS device.

Tcl Example Used to Check the CPU Utilization

The sample script in this section checks the CPU, and if it exceeds a 60 percent thresh-old, generates an e-mail.

Step 1. The following event manager commands configured on the IOS device are used to define the environment variables in the Tcl script:

event manager environment _email_server e_mail_server.com event manager environment _email_to destination_account event manager environment _email_from source_account event manager environment _cron_entry 0-59/2 0-23/1 * * 0-7 From router TCLRouter: Periodic show interface | i errors Output

[email protected] <[email protected]>

To: Ray Blair <[email protected]>

1 input errors, 0 CRC, 1 frame, 0 overrun, 0 ignored 0 output errors, 0 collisions, 3 interface resets 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored 0 output errors, 0 colloisions, 0 interface resets

0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort 0 output errors, 0 colisions, 6 interface resets

0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort 0 output errors, 0 collisions, 6 interface resets

TCLRouter#

Figure 4-6 Script Error Results

Chapter 4: Embedded Event Manager (EEM) 105

event manager environment _show_cmd show proc cpu i five event manager environment _percent 60

event manager dir user pol flash:

event manager policy cpu_threshold_email.tcl

Note Because of an aspect of implementation (bug) in code prior to EEM Release 2.3, it is recommended to use the IP address of the mail server and not the name.

Step 2. The beginning of the Tcl script registers the CRON timer with user-defined parameters gleaned from the event manager variables:

::cisco::eem::event_register_timer cron name crontimer2 cron_entry

$_cron_entry maxrun_sec 240

Step 3. The following section checks whether the required environment variable _email_server exists:

if {![info exists _email_server]} {

Step 4. If an error is detected in the environment variable (_email_server), save the reason for the error and exit the script:

set result \

“Policy cannot be run: variable _email_server has not been set”

error $result $errorInfo }

Step 5. The following section checks whether the required environment variable _email_from exists:

if {![info exists _email_from]} {

Step 6. If an error is detected in the environment variable (_email_from), save the reason for the error and exit the script:

set result \

“Policy cannot be run: variable _email_from has not been set”

error $result $errorInfo }

Step 7. The following section checks whether the required environment variable _email_to exists:

if {![info exists _email_to]} {

Step 8. If an error is detected in the environment variable (_email_to), save the reason for the error and exit the script:

set result \

“Policy cannot be run: variable _email_to has not been set”

error $result $errorInfo }

Step 9. The following section checks whether the _email_cc environment variable exists. Because this is not a required parameter, the variable will be config-ured as an empty string if nothing has been entered:

if {![info exists _email_cc]} { set _email_cc ““

}

Step 10. Verify that the _show_cmd environment variable is set. This parameter will be used to collect the Cisco IOS show command output and send it in the e-mail body:

if {![info exists _show_cmd]} {

Step 11. If there is an error, collect the information and exit the script:

set result \

“Policy cannot be run: variable _show_cmd has not been set”

error $result $errorInfo }

Step 12. Load the standard EEM libraries. For example, the namespace import state-ment error and cli_open are standard EEM library procedures:

namespace import ::cisco::eem::*

namespace import ::cisco::lib::*

Step 13. Query the event info and log a message:

array set arr_einfo [event_reqinfo]

Step 14. If any problems were detected, the variable _cerrno is set to a nonzero value, and the variable named _cerr will contain the details of the problem:

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 }

Step 15. The following variables are used to store the time of the error:

global timer_type timer_time_sec set timer_type $arr_einfo(timer_type) set timer_time_sec $arr_einfo(timer_time_sec)

Step 16. Log a message:

set msg [format “timer event: timer type %s, time expired %s” \

$timer_type [clock format $timer_time_sec]]

Chapter 4: Embedded Event Manager (EEM) 107

Step 17. Send the message to the syslog process:

action_syslog priority info msg $msg

Step 18. If any problems were detected, the variable _cerrno is set to a nonzero value, and the variable named _cerr will contain the details of the problem:

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 }

Step 19. Execute the show command from the environment variable _show_cmd:

if [catch {cli_open} result] {

Step 20. If there is a problem with the show command, end the script because of the error:

error $result $errorInfo } else {

Step 21. With a success, save the results to the array cli1:

array set cli1 $result }

Step 22. Because the cli_open was successful, use the variable fd from now on to access the CLI.

Step 23. Enter IOS enable mode for proper display of many show commands:

if [catch {cli_exec $cli1(fd) “en”} result] {

Step 24. In the event of an error, end the script:

error $result $errorInfo }

if [catch {cli_exec $cli1(fd) $_show_cmd} result] {

Step 25. If a problem occurred executing the IOS show command, end the script now:

error $result $errorInfo } else {

Step 26. Save the output of the IOS show command in variable cmd_output:

set cmd_output $result }

if [catch {cli_close $cli1(fd) $cli1(tty_id)} result] {

Step 27. If a problem occurred closing access to the CLI, end the script now:

error $result $errorInfo }

Step 28. Log the success of the CLI command:

set msg [format “Command \”%s\” executed successfully” $_show_cmd]

Step 29. Send the message to the syslog process:

action_syslog priority info msg $msg

Step 30. If any problems were detected, the variable _cerrno is set to a nonzero value, and the variable named _cerr will contain the details of the problem:

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 }

Step 31. Collect the actual CPU percentage from the show command. The variable foundposition will point to the beginning of the word “five” in the string

“five minutes:”.

set foundposition [string first “five minutes: “ $cmd_output]

Step 32. We get the string length of “five minutes:” so that it can be easily removed later:

set cutoff [string length “five minutes: “]

Step 33. Only process the output if foundposition actually found the string “five minutes: ”:

if {$foundposition > -1} {

Step 34. Set the variable begin to the location of the beginning of the number after

“five minutes: ”:

set begin [expr $foundposition + $cutoff]

Step 35. Set the variable end to the location of the end of the number after “five min-utes: ”. This assumes that the number is no longer than three digits; it could be one, two, or three digits long:

set end [expr $begin + 3]

Step 36. Save the number variable realcpu. This variable might or might not contain the % character, depending on how many digits long it is. For example, if the variable was at 100, the realcpu variable will not have the % character present

Chapter 4: Embedded Event Manager (EEM) 109

because only three digits were saved. If it were a two-digit number like 50, you would have the % character in the variable realcpu:

set realcpu [string range $result $begin $end]

Step 37. Check whether the % character was found. If it was, remove it:

set foundpercent [string first “%” $realcpu]

if {$foundpercent > -1} {

Step 38. Remove any trailing white space characters:

set realcpu [string trimright $realcpu]

Step 39. Remove the % character if it is present:

set realcpu [string trimright $realcpu “%?”]

} }

Step 40. Compare the CPU usage to the %60 value specified. If the CPU usage exceeded the set max, the variable realcpu contains the actual number from the show command:

if {$realcpu < $_percent} {

set result “CPU did not exceed the set percent”

error $result }

Step 41. Collect the name of the Cisco IOS device:

set routername [info hostname]

if {[string match ““ $routername]} {

Step 42. If the IOS device is missing the hostname configuration command, display the following error message:

error “Host name is not configured”

}

Step 43. Send an e-mail indicating that the CPU was above the configured limit.

Concatenate the show command output with a standard e-mail template from the library and get the e-mail server IP address:

if [catch {smtp_subst [file join $tcl_library email_template_cmd.tm]}

result] {

error $result $errorInfo }

Step 44. Send the e-mail message:

if [catch {smtp_send_email $result} result] { error $result $errorInfo

}

Summary

The Embedded Event Manager (EEM) includes detectors for notification of a particular event, the event manager that controls the operation of predetermined actions, and the policies that can be either an applet or Tcl script. EEM has continued to evolve, with more feature functionality with each subsequent release.

From the examples in this chapter, you can see the differences in applets and Tcl scripts and the power you have to customize your own applications to automate monitoring and troubleshooting processes.

References

Cisco Beyond (a repository for user-contributed EEM policies):

http://tinyurl.com/yetbqgq

Cisco Embedded Automation Systems: http://www.cisco.com/go/easy Cisco IOS Embedded Event Manager (EEM): http://www.cisco.com/go/eem Writing Embedded Event Manager Policies Using Cisco IOS CLI:

http://tinyurl.com/yjveq6q

Cisco IOS IP Service Level Agreements (SLAs): http://www.cisco.com/go/ipsla

Chapter 5

Advanced Tcl Operation in

In document Tcl Scripting for Cisco IOS (Page 121-128)