11-05-2015 06:50 AM
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 !
11-05-2015 07:35 PM
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"
11-09-2015 02:29 AM
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.
11-09-2015 07:47 AM
Post the script as a file instead of copying the text into a post. It will be easier to spot the problem.
11-09-2015 08:14 AM
11-09-2015 08:19 AM
11-09-2015 08:46 AM
Sorry but I don't see any difference between the 2
11-09-2015 08:54 AM
11-09-2015 10:26 AM
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.
11-09-2015 11:47 AM
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.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide