cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1798
Views
0
Helpful
9
Replies

Need help modifying TCL script with a loop.

Cisco Ham
Level 1
Level 1

Hi,

I'm new to TCL/EEM, with some help from my friend Google I was able to create a small script that resides on the flash of a switch and it looks like this :

::cisco::eem::event_register_timer cron name ipsla_to_syslog.tcl  cron_entry "59 23 * * *"
namespace import ::cisco::eem::*
namespace import ::cisco::lib::*
array set arr_einfo [event_reqinfo]
# Open the CLI
if [catch {cli_open} result] {
   error $result $errorInfo
} else {
    array set cli1 $result
}

# Go into enable mode
if [catch {cli_exec $cli1(fd) "en"} result] {
    error $result $errorInfo
}

if [catch {cli_exec $cli1(fd) "sh ip sla enhanced-history distribution-statistics" } command ] {
        error $command $errorInfo
}
action_syslog priority info msg "$command"

I have registered it on the switch as follows :

    event manager directory user policy "bootflash:/"
    event manager policy ipsla_to_syslog.tcl type user

One minute before midnight it will run and write all statistic information of the IP SLA instances to the syslog server, the problem is that this is a lot of information because of a lot of IP SLA instances and it seems that most of the time we miss a lot of lines.

So what I would like to do is limit this to a certain number of IP SLA instances that are specified in the script and then run a loop.

I mean something like : set ip_sla_instance  { 103 205 302} and then a loop that would run the following commands

sh ip sla enhanced-history distribution-statistics 103

sh ip sla enhanced-history distribution-statistics 205

sh ip sla enhanced-history distribution-statistics 302

Hopefully it is clear what I would like to have, English is not my native language.

Another thing I would like to do is at the start of the script change the logging level of the switch to informational with the command "logging trap informational" and at the end of the script switch it back to "logging trap notifications".

Any help would be very much appreciated !

9 Replies 9

Joe Clarke
Cisco Employee
Cisco Employee

You can do that:

set ip_sla_instances [list 103 205 302]

set buffer {}

foreach instance $ip_sla_instance {

    if [catch {cli_exec $cli1(fd) "sh ip sla enhanced-history distribution-statistics $instance" } command ] {
        error $command $errorInfo
    }

    append buffer $command

}

puts "Command output is $buffer"

Hi Joe,

Thank you for your help, I've modified the script, it looks like this now :

::cisco::eem::event_register_timer cron name ipsla_to_syslog.tcl cron_entry "55 10 * * *"
namespace import ::cisco::eem::*
namespace import ::cisco::lib::*
array set arr_einfo [event_reqinfo]
# Open the CLI
if [catch {cli_open} result] {
error $result $errorInfo
} else {
array set cli1 $result
}

# Go into enable mode
if [catch {cli_exec $cli1(fd) "en"} result] {
error $result $errorInfo
}

set ip_sla_instance [list 103 106 201]
set buffer {}
foreach instance $ip_sla_instance {
if [catch {cli_exec $cli1(fd) "sh ip sla enhanced-history distribution-statistics $instance" } command ] {
error $command $errorInfo

}

append buffer $command
}
puts "Command output is $buffer"
action_syslog priority info msg "$buffer"

There is a problem somewhere when I try to run it, this is the Event Manager debug output :

Nov 9 10:55:00.573 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: invalid command name " "
Nov 9 10:55:00.573 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: while executing
Nov 9 10:55:00.573 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: " if [catch {cli_exec $cli1(fd) "sh ip sla enhanced-history distribution-statistics $instance" } command ] {
Nov 9 10:55:00.573 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: error $command $errorInfo
Nov 9 10:55:00.573 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: }"
Nov 9 10:55:00.573 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: ("foreach" body line 2)
Nov 9 10:55:00.573 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: invoked from within
Nov 9 10:55:00.573 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: "foreach instance $ip_sla_instance {
Nov 9 10:55:00.573 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: if [catch {cli_exec $cli1(fd) "sh ip sla enhanced-history distribution-statistics $instance" } command ] {
Nov 9 10:55:00.573 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: ..."
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: invoked from within
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: "$slave eval $Contents"
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: (procedure "eval_script" line 7)
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: invoked from within
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: "eval_script slave $scriptname"
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: invoked from within
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: "if {$security_level == 1} { #untrusted script
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: interp create -safe slave
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: interp share {} stdin slave
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: interp share {} stdout slave
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: ..."
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: (file "tmpsys:/lib/tcl/base.tcl" line 50)
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: Tcl policy execute failed:
Nov 9 10:55:00.574 CET: %HA_EM-6-LOG: ipsla_to_syslog.tcl: invalid command name " "

Could you tell me what is wrong ? I have absolutely no coding experience, thank you for your patience.

Post the script as a file instead of copying the text into a post.  It will be easier to spot the problem.

Here you go.

Use this one.

Sorry but I don't see any difference between the 2

There are. Don't copy and paste. Take the file I posted, change the extension, and register that. It should work.

Wow, it certainly did the trick and I don't even see what you've changed.

One last problem : with "debug event-manager" I can see that the script goes through the loop for each of the ip sla instances specified in the list, however it seems to write only the output of the command from the first ip sla instance ( 103 in our example ) in the syslog file. Any idea how to fix this ? And I am very curious to what you did to make this work.

Syslog is not a good medium to send information.  The CLI output contains multiple lines, and most syslog applications assume one line per message.  Some even limit total message length to 1024 bytes.  But the $buffer variable holds all of the output.  If you write it to a file on flash or email it, you should see everything.

Review Cisco Networking for a $25 gift card