Last active
April 17, 2017 13:50
-
-
Save ericpulvino/0e4711bbeedba84b4b9009703480b850 to your computer and use it in GitHub Desktop.
Quick script to enable ports which have been set to err-disabled by mstpctl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Quick Script to Recover Error-Disabled Ports | |
# Originally written by Eric Pulvino (2017-04-17) | |
import time | |
import json | |
import subprocess | |
import logging | |
import logging.handlers | |
## DEBUGGING To print full STP State | |
#import pprint | |
## PrettyPrint Setup | |
#pp = pprint.PrettyPrinter(indent=4) | |
#pp.pprint(stp_info[u'bridge']) | |
def recover_port(port): | |
try: | |
recovery_output = subprocess.check_output(['ip','link','set',port,'up',]) | |
except: | |
message="attempt to recover port %s was unsuccessful." %(port) | |
print(message) | |
log.warn(message) | |
return | |
message = "recovery of port %s was successful." %(port) | |
print(message) | |
log.info(message) | |
def collect_stp_state(): | |
# Collect Current STP State | |
try: | |
value = subprocess.check_output('mstpctl showall json',shell=True) | |
except: | |
message="ERROR -- Could not query STP process for state!" | |
print(message) | |
log.error(message) | |
exit(1) | |
# Convert JSON to Python Dict | |
try: | |
stp_info = json.loads(value) | |
except: | |
message="ERROR -- STP JSON output could not be converted to python dict!" | |
print(message) | |
log.error(message) | |
exit(1) | |
return stp_info | |
def process_stp_state(stp_info): | |
for port in stp_info[u'bridge']: | |
if "swp" not in port: continue | |
for stp_instance in stp_info[u'bridge'][port]: | |
if u'bpduGuardPort' not in stp_info[u'bridge'][port][stp_instance]: continue | |
if u'bpduGuardError' not in stp_info[u'bridge'][port][stp_instance]: continue | |
if stp_info[u'bridge'][port][stp_instance][u'bpduGuardPort'] == True and stp_info[u'bridge'][port][stp_instance][u'bpduGuardError'] == True: | |
message="port %s is in err-disabled state. Attempting to recover." %(port) | |
print(message) | |
log.warn(message) | |
recover_port(port) | |
def main(): | |
# Recovery Interval (in sec) | |
interval=60 | |
# Logging Setup | |
log = logging.getLogger(__name__) | |
log.setLevel(logging.DEBUG) | |
handler = logging.handlers.SysLogHandler(address = '/dev/log') | |
formatter = logging.Formatter('Err-Disable-Recovery: %(message)s') | |
handler.setFormatter(formatter) | |
log.addHandler(handler) | |
global log | |
while True: | |
process_stp_state(collect_stp_state()) | |
time.sleep(interval) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment