2014/10/09

Automatic connectivity test on the router with EEM

Did you ever want to perform constant connectivity test to the different hosts on the Internet? You can use Embedded Event Manager for the task. EEM is part of the IOS software. I will show you how to use the EEM and TCL scripting for such task.

First you need to create TCL script, that will be used for connectivity test. You can find it bellow.

Router#more ping.tcl
set hosts {
{4.2.2.1}
{8.8.8.8}
{8.8.4.4}
}
set counter 0
set sucess_counter 0
set failed_hosts ""

foreach ip $hosts {
incr counter 1
set result [exec ping $ip]
puts $result
if {[regexp {!!!} $result]} {
incr sucess_counter 1
} else {
puts $ip
set failed_hosts "$failed_hosts  $ip"
}
}

set msg_output "Success rate $sucess_counter/$counter. Failed hosts: $failed_hosts"
[ios_config "event manager environment syslog_msg_output $msg_output"]

Let me first explain the flow of the script. The hosts variable is set with the command set host {}. You can include as many hosts on the Internet as you want. After that the three other variables are initialized. In the main part of the script ping command is performed for every hosts. If the result is at least three !, the test is successful and success_counter is increased. Otherwise failed host is added to the variable failed_hosts. After for loop is performed the text for the syslog message is created. The last step of the script is to add event manager environment variable to running config of the router. The variable name is syslog_msg_output and the value is the string msg_output.

After script is created and saved to the flash of the router, you need to tell your router what to do with the script. You can enter the following commands to the router running config.

event manager applet PING_TEST
 event timer watchdog time 300 maxrun 250
 action 0.5 cli command "enable"
 action 1.0 cli command "tclsh flash:ping.tcl"
 action 2.0 syslog msg "$syslog_msg_output$"
 action 3.0 cli command "configure terminal"
 action 4.0 cli command "no event manager environment syslog_msg_output"
 action 5.0 cli command "end"
 action 6.0 cli command "exit"


Let me explain what the commands above means. First create event manager applet. I have created the applet with the name PING_TEST. Next specify when this applet will be run. I run this applet every 300 seconds and let it run for the max of 250 seconds. It is important to trim these timers since you want to script to end before timeout (maxrun) expires. The default value is 20 seconds. After event timer has been set you can specify commands to be run in the applet. To run a script use "tclsh flash:ping.tcl" command and to create syslog message use syslog msg "$syslog_msg_output$". This will create syslog message with the text from event manager variable syslog_msg_output. After syslog message is created you can delete variable end exit.

It is time to test if script is working as expected. You can check logging buffer and you can see messages like this:

 %HA_EM-6-LOG: PING_TEST: Success rate 3/3. Failed hosts: $

If any of the hosts fail it will be listed under failed hosts and success rate will be adjusted accordingly to the test.

You are free to use this script in your environment. I hope you will find it usefully.

No comments:

Post a Comment