Skip to content

Instantly share code, notes, and snippets.

@aleks-mariusz
Created May 1, 2014 04:52
Show Gist options
  • Save aleks-mariusz/ebbed4a2081165676382 to your computer and use it in GitHub Desktop.
Save aleks-mariusz/ebbed4a2081165676382 to your computer and use it in GitHub Desktop.
Oct 15 2008 - i almost forgot i even dabbled a little in tcl to aid my network engineering pursuits. Here i am using the event manager environment capabilities on higher end cisco IOS-based devices to monitor link status and set up alerts
::cisco::eem::event_register_timer watchdog name linkmon time $linkmon_period maxrun 45
# _________________________________________________________________________#
# | |#
# | cogent-monitor |#
# | |#
# | by: Alex Koralewski |#
# | (c) 2008, Forex Capital Markets, LLC. |#
# | |#
# | Version 0.8 03/14/2008 |#
# | Version 1.0 04/03/2008 |#
# |_______________________________________________________________________|#
#
# linkmon - timed link monitor
#
# desc: The purpose is to monitor the packet loss based on the output
# of the ping command, if packet loss found, we will set delay on the
# interface higher so the link is avoided
#
# trigger: Watchdog timer - set from env variable
# action: raise delay metric, send email
# env vars: linkmon_period - time interval for execution (seconds)
# linkmon_int - interface to monitor
# linkmon_ip_dest - ip address to ping
# linkmon_ip_src - ip address to source ping packets from
#
# restrictions:
#
# To change execution interval or other parms, you must re-register
# the policy.
#
# The policy will continue indefinitely once registered.
#
# ios config:
#
# linkmon setup:
#
# Router#copy ftp://10.60.4.131/link-monitor.eem-script.tcl disk0:/linkmon.tcl
# Router(config)#event manager directory user policy disk0:/
# Router(config)#event manager environment linkmon_period 15
# Router(config)#event manager environment linkmon_addr_dst 10.0.12.11
# Router(config)#event manager environment linkmon_addr_src 10.0.12.1
# Router(config)#event manager environment linkmon_int GigabitEthernet3/12
#
# optionally, setting this will cause this neighbor to be cleared when the interface's delay is raised
# Router(config)#event manager environment linkmon_eigrp_neighbor 10.0.12.11
#
# linkmon enable:
#
# Router(config)#event manager policy linkmon.tcl type user
#
# linkmon disable
#
# Router(config)#no event manager policy linkmon.tcl type user
# Check for required environment variables
# If any required vars are not available, print error and quit
if {![info exists linkmon_period]} {
set result "EEM Policy Error: variable linkmon_period has not been set"
error $result $errorInfo
}
if {![info exists linkmon_addr_dst]} {
set result "EEM Policy Error: variable linkmon_addr_dst has not been set"
error $result $errorInfo
}
if {![info exists linkmon_addr_src]} {
set result "EEM Policy Error: variable linkmon_addr_src has not been set"
error $result $errorInfo
}
if {![info exists linkmon_int]} {
set result "EEM Policy Error: variable linkmon_int has not been set"
error $result $errorInfo
}
# namespace imports
namespace import ::cisco::eem::*
namespace import ::cisco::lib::*
set numFail 0
get getNumFail [ appl_reqinfo key "failures" ]
if { [ lindex $getNumFail 0 ] == "data" } {
set numFail [ lindex $getNumFail 1 ]
}
# save exact execution time for command
#set time_now [clock seconds]
set _status_file "disk0:packet_loss_exists"
if {![file exists $_status_file]} {
# issue the cli command
if [catch {cli_open} result] {
error $result $errorInfo
} else {
array set cli1 $result
}
# execute command
if [catch {cli_exec $cli1(fd) "en"} result] {
error $result $errorInfo
}
if [catch {cli_exec $cli1(fd) "term len 0"} result] {
error $result $errorInfo
}
if [catch {cli_exec $cli1(fd) "ping $linkmon_addr_dst source $linkmon_addr_src size 1325 repeat 10"} result] {
error $result $errorInfo
}
set cmd_output $result
set routername [info hostname]
if {[string match "" $routername]} {
error "Host name is not configured"
}
regexp {(Success rate is .*[0-9] percent)} $cmd_output ping_result
set success_rate [lindex $ping_result 3]
if {$success_rate < 85} {
incr numFail
action_syslog priority info msg "linkmon.tcl: ping success rate was only $success_rate"
} else {
set numFail 0
}
appl_setinfo key "failures" data $numFail
if {$numFail > 1} {
action_syslog priority info msg "linkmon.tcl: ping failed more than twice in a row.. shutting interface $linkmon_int down"
set myfileid [open $_status_file w+]
foreach outs $cmd_output {
puts $myfileid $outs
}
close $myfileid
if [catch {cli_exec $cli1(fd) "config t"} result] {
error $result $errorInfo
}
if [catch {cli_exec $cli1(fd) "interface $linkmon_int"} result] {
error $result $errorInfo
}
if [catch {cli_exec $cli1(fd) "shut"} result] {
error $result $errorInfo
}
if [catch {cli_exec $cli1(fd) "end"} result] {
error $result $errorInfo
}
set _email_server "mail-out.nydc.fxcorp.prv"
set _email_from "[email protected]"
set _email_to "[email protected]"
set _email_cc ""
set _email_template "disk0:linkmon.tm"
if [catch {smtp_subst $_email_template} result] {
error $result $errorInfo
}
if [catch {smtp_send_email $result} result] {
error $result $errorInfo
}
}
if [catch {cli_close $cli1(fd) $cli1(tty_id)} result] {
error $result $errorInfo
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment