Thursday, May 4, 2017

Python script on EXOS - Ping test to Backbone device and disable the port if ping failed

EXOS support python script from 15.6. Below script is one of example can be used on EXOS.
In below example, switch will test ping every 10 seconds and disable uplink ports and generate the log if ping to backbone device failed.

# vi pingCheck.py


import exos.api
import time
import re
import sys


lagPort = sys.argv[1]
ip_addr_list = [(sys.argv[2], sys.argv[3]), (sys.argv[4], sys.argv[5])]
timeSleep = 10


def pingCheck(ip_addr, vr_name):
   pingCMD = 'ping count 3 vr {} {}'.format(vr_name, ip_addr)
   cmdOutput = exos.api.exec_cli([pingCMD]).splitlines()[-2]
   pingResult = re.search(r'received,\s+(\d+)%\s+loss', cmdOutput).group(1)
   return True if pingResult == '0' else False


def lagCheck(lagPort):
   lagCMD = 'show lacp lag {} | include {}'.format(lagPort, lagPort)
   cmdOutput = exos.api.exec_cli([lagCMD]).splitlines()[0]
   lagResult = re.search(r'\s+(\d+)\s+(\w{2}:?){6}', cmdOutput).group(1)
   return True if int(lagResult) > 0 else False


if __name__ == '__main__':
   result = True
   while True:
       if result:
           for ip_addr, vr_name in ip_addr_list:
               result = pingCheck(ip_addr, vr_name)
               if not result:
                   runCMD1 = 'create log message "WARNING!! Ping failure to {}, Disabling the LAG port ({}) to upstream Cisco router"'.format(ip_addr, lagPort)
                   runCMD2 = 'disable ports {}'.format('85-96')
                   runCMD3 = 'create log message "Process PingCheck will stop until the LAG port {} is manually re-enabled"'.format(lagPort)
                   exos.api.exec_cli([runCMD1, runCMD2, runCMD3])
                   break
       else:
           result = lagCheck(lagPort)
           if result:
               runCMD = 'create log message "The LAG port {} has been manually re-enabled and brought up. Process PingCheck has started again"'.format(lagPort)
               exos.api.exec_cli([runCMD])
       time.sleep(timeSleep)

From EXOS version 15.7, user can create process running based on user created python script. With below configuration on the switch, process will be created to run the script named pingcheck, and will use 4 values argument.

# create process "pingCheck" python-module "pingCheck" start auto "85" "115.164.3.131" "VR-accessPODSHT" "115.164.3.163" "VR-accessPODSHT"

* 85 is the lag port to disable when failure is detected. "115.164.3.131" "VR-accessPODSHT" is the ip address and vr name to monitor by ping.


No comments:

Post a Comment