11-13-2014 12:27 PM
I have a script written in the TCLSH of IOS, I can test this script from the shell using the following commands
LAB-RTR#tclsh
LAB-RTR(tcl)#source flash:test.tcl
NO CALLS
Is there a way I can run this test.tcl script every 60 seconds on the router.
SCRIPT test.tcl
proc get_ints {} {
set syslog [open "syslog:" w+]
#puts "BEFORE\n"
set check ""
set int_out [ exec "show voice call status\n" ]
#puts "INTOUT $int_out\n"
set mylist [regexp -nocase -line -inline {(^([0-9]*|No) active calls? found)} $int_out]
#puts "MYLIST $mylist\n"
foreach int $mylist {
#puts "INT $int\n"
if {$int == "No"} {
puts " NO CALLS\n";
puts $syslog "NO CALLS"
} elseif {[string is integer -strict $int]} {
puts "$int CALLS\n";
puts $syslog "$int CALLS"
} else {
#puts "GARBAGE\n";
}
}
close $syslog
}
#puts ""
puts [eval get_ints]
Solved! Go to Solution.
11-13-2014 01:01 PM
Convert this to an EEM Tcl policy. Just add:
proc exec { cmd } {
global errorInfo
if { [catch {cli_open} result] } {
error $result $errorInfo
}
array set cli $result
set output [cli_exec $cli(fd) $cmd]
catch {cli_close $cli(fd) $cli(tty_id)}
return $output
}
Then register this as an EEM Tcl policy with this at the top:
::cisco::eem::event_register_timer watchdog time 60
namespace import ::cisco::eem::*
namespace import ::cisco::lib::*
It will then run every 60 seconds.
11-13-2014 01:01 PM
Convert this to an EEM Tcl policy. Just add:
proc exec { cmd } {
global errorInfo
if { [catch {cli_open} result] } {
error $result $errorInfo
}
array set cli $result
set output [cli_exec $cli(fd) $cmd]
catch {cli_close $cli(fd) $cli(tty_id)}
return $output
}
Then register this as an EEM Tcl policy with this at the top:
::cisco::eem::event_register_timer watchdog time 60
namespace import ::cisco::eem::*
namespace import ::cisco::lib::*
It will then run every 60 seconds.
11-14-2014 09:51 AM
Hey Joseph, Thanks a lot for pointing me in the right direction. I have my script running the way I want. I was able to generate a syslog using the script, now I need to generate a SNMP trap can you give me an example of how to do it.
action_syslog priority emergencies msg "TOO MANY CALLS" -- This works
11-14-2014 11:24 AM
Something like this would work:
action_snmp_trap strdata "TOO MANY CALLS"
11-21-2014 07:08 AM
Hey Joseph, Thanks for all your help. I have one last question, so now that I am running my script every 60 seconds if there is an issue I send multiple traps to our monitoring system. Is there any way you can keep track if the trap was already sent I will not send another trap again.
One way I can think of is using a loopback interface. But I would prefer not shutting down the loopback interface just to keep track of what my script is doing.
IF issue {
IF Loop1 is up {
don't send trap
} ELSE {
bring Loop1 up
Send Trap
}
} ELSE (No issue) {
shutdown Loop1
}
11-21-2014 08:28 AM
EEM has the concept of contexts where you can store data between executions. You save data using context_save and retrieve it using context_retrieve. Note: context data is volatile, so when it is retrieved, the context is destroyed. See http://www.cisco.com/c/en/us/td/docs/ios/netmgmt/configuration/guide/12_2sx/nm_12_2sx_book/nm_eem_policy_tcl.html#wp1210964 for more details.
11-21-2014 10:03 AM
Thanks lot Joseph.
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