Skip to content

Instantly share code, notes, and snippets.

@stevommmm
Created May 13, 2013 06:28
Show Gist options
  • Select an option

  • Save stevommmm/5566496 to your computer and use it in GitHub Desktop.

Select an option

Save stevommmm/5566496 to your computer and use it in GitHub Desktop.
Check cisco mds log files on remote switches
#!/usr/bin/python
from datetime import datetime, timedelta
import logging
import subprocess
import sys
import re
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.WARN)
# '(year ) (Month) (day ) (timestamp ) (sch) (err): (ed)
splitre = re.compile('(\d{4})\s(\w{3})\s+(\d{1,2})\s(\d{2}:\d{2}:\d{2})\s(\w+)\s\%(.+?):\s(.+)')
# Error message and handlers -------------------------------------------------------------------------------------------
def _ignore(reason=None):
logging.debug(reason)
return 0
def _warn(reason):
logging.warning(reason)
return 1
def _alert(reason):
logging.critical(reason)
return 2
# Special handlers
def _loginwarn(reason):
if 'Authentication failed for user root' in reason:
return _alert(reason)
return _warn(reason)
error_handles = {
'PORT-5-IF_DOWN_LINK_FAILURE': _alert,
'DEVICE-ALIAS-3-MERGE_FAILED': _warn,
'ETHPORT-5-IF_DOWN_LINK_FAILURE': _warn,
'FCDOMAIN-2-EPORT_ISOLATED': _warn,
'KERN-1-SYSTEM_MSG': _warn,
'MODULE-2-MOD_NOT_ALIVE': _alert,
'PLATFORM-3-MOD_PWRFAIL_MULTI': _alert,
'PORT-3-IF_UNSUPPORTED_TRANSCEIVER': _warn,
'PORT-5-IF_DOWN_ETH_LINK_DOWN': _alert,
'PORT-5-IF_DOWN_PEER_RESET': _warn,
'PORT-5-IF_DOWN_PORT_CHANNEL_MEMBERS_DOWN': _warn,
'SYSMGR-2-SERVICE_CRASHED': _alert,
'ZONE-2-ZS_MERGE_FULL_DATABASE_MISMATCH': _alert,
'AUTHPRIV-3-SYSTEM_MSG': _loginwarn,
}
# Log handlers ---------------------------------------------------------------------------------------------------------
def process_line(line):
"""Split our log file entry into time, host, error code and error message
For each error code we look for it in our known dictionary, if it is found
we process it with the key-value method defined in error_handles
'last message repeated' is an edge case that we try to handle gracefully
"""
m = splitre.match(line)
if m:
# 0-3 time, 4 host, 5 err code, 6 error message
hndlr = error_handles.get(m.group(6))
if hndlr:
return hndlr(m.group(7))
else:
return _ignore(m.group(7))
else:
if 'last message repeated' in line:
return 0
logging.critical(line)
return 2
def format_request(host):
"""Query cisco switch for log files beginning 1 hour ago"""
lh = datetime.today() - timedelta(hours = 1)
return ["ssh", "-q", host, "show", "logging", "logfile", "start-time", lh.strftime('%Y %b %d %H:%M:%S')]
def request_logs(host):
"""Start a ssh connection to the desired host
Each line of output is sent through process_line,
the return values are then used to determine the
exit value for the script
If no values are returned the log was probably empty
so we exit with OK / 0
"""
process = subprocess.Popen(format_request(host), stdout=subprocess.PIPE)
(output, errors) = process.communicate()
if errors:
logging.critical(errors)
sys.exit(1)
lines = output.splitlines()
retvals = map(process_line, lines)
if retvals and max(retvals) > 0:
sys.exit(max(retvals))
print "OK: no errors reported"
sys.exit(0)
if __name__ == '__main__':
if len(sys.argv) == 2:
request_logs(sys.argv[1])
else:
logging.critical("No host given. Usage: %s <host>" % sys.argv[0])
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment