TCL on CUBE to check Telco SIP Trunk

The below script will check the status of the SIP trunk configured to the telco every hour. If its down it will send off an email

The TCL script. Write this in Notepad++ or something and save it.
::cisco::eem::event_register_timer cron name test cron_entry "0,5,10,15,20,25,30,35,40,45,50,55 * * * *" queue_priority low nice 1 maxrun 20
namespace import ::cisco::eem::*
namespace import ::cisco::lib::*
proc send_email {router sip clock} {
global errorInfo
set _email_server ""
set _email_from ""
set _email_to ""
set mail_pre "Mailservername: $_email_server\n"
append mail_pre "From: $_email_from\n"
append mail_pre "To: $_email_to\n"
append mail_pre "Cc: \n"
append mail_pre "Subject: SIP Trunk Down"
append mail_pre "\nHostname: $router\n"
append mail_pre "\nTime: $clock\n"
append mail_pre "\nSIP-Status: $sip\n"
set mail_msg [uplevel #0 [list subst -nobackslashes -nocommands $mail_pre]]
if [catch {smtp_send_email $mail_msg} result] {
error $result $errorInfo}
#open an exec session.
#catch return 1 if error occured
#result contains error or result of excuting script
if [catch {cli_open} result] {
error $result $errorInfo
} else {
array set cli $result
#one of the elemets in $cli array: fd=CLI channel handler
if { [catch {cli_exec $cli(fd) "enable"} result] } {
error $result $errorInfo
if { [catch {cli_exec $cli(fd) "show sip-ua register status"} result] } {
error $result $errorInfo
set show_sip_ua $result
regexp {.*(yes normal).*} $result a b
#puts "var a:$a"
#puts "var b:$b"
if {[info exists b]} {
#puts "LIV SIP Trunk is UP!"
} else {
#puts "LIV SIP Trunk is DOWN!"
#------------------ hostname -------------------
set routername [info hostname]
#------------------ time -------------------
if { [catch {cli_exec $cli(fd) "show clock"} result] } {
error $result $errorInfo
set clock $result
send_email $routername $show_sip_ua $clock
#--------------------- cli close ------------------------
cli_close $cli(fd) $cli(tty_id)

On the router create a dir to add all the TCL scripts in
#mkdir eem_policies
Upload the script.
#copy ftp://alexis:alexis@ flash:/eem_policies/sipTrunk.tcl
Define where the TCLs will be stored
config-t#event manager directory user policy flash:/eem_policies/
Register the script
config-t#event manager policy sipTrunk.tcl type user
You can manually run scripts that has no registered event with Event Detector eg:"::cisco::eem::event_register_none".
This not the case with this script. You either need to shorten the time it runs if you don’t want to wait the hour or replace the timer event with the event_register_none.
To manually run a script: #event manager run sipTrunk.tcl

To debug and check the script status
#more flash0:/eem_policies/sipTrunk.tcl
#show event manager history
#show event manager policy registered
#debug event manager tcl cli
#debug event manager tcl smtp

Tags: , ,

About Alexis Katsavras

Working as Freelance Cisco Unified Communications Consultant in the UK.