Start of topic | Skip to actions
PythonReportingReporting ModuleThis module is taken from the identity project and is tailored to that. Some of the wording should be updated if you use this.
"""
This module provides an email based error reporting system in addition
to overloading the python logging system. This module adds a 'failure'
logging level which mimics the 'error' logging level as well as sends
an email error report.
@author: cpepe@tearsoffire.org
"""
import logging, logging.config
from ConfigParser import ConfigParser
configFile = '/etc/identity/conf/server.conf'
class eLogger(logging.getLoggerClass()):
"""
This class redefines the logger to include a failure method which sends
an error report to alert sysadmins
"""
def failure(self, *args, **kwargs):
from socket import gethostname
hostname = gethostname()
msg = """
The Identity manager encountered an error and cannot proceed without help.
If the error is related to a provisioning request please use the PSR ID presented
in the error report below to look up the provisioning trace
in syslog. Logs are available on this host listed in the email.
If the error is not related to a PSR then the IDM service may not be fully operational.
Please attend to this issue as soon as possible.\n\n"""
msg += '-------------- Error Report --------------\n'
msg += 'For IDM server running on %s:\n' % (hostname)
for arg in args:
msg += arg
msg += '\n------------------------------------------\n'
sendErrorReport(msg)
logging.error(*args, **kwargs)
#use custom logger to handle failures by submitting help requests
logging.setLoggerClass(eLogger)
def sendErrorReport(msg):
"""
send email error report to alert sysadmins
"""
###############################################################################
### Configuration Section ###
###############################################################################
#[ email configuration ]#
try:
parser = ConfigParser()
result = parser.read( configFile )
#if the config file doesn't exist parser.read returns an empty list
if len(result) is 0:
msg = 'Report Email: Failed to load config file: %s' % (configFile)
logger.error(msg)
raise Excpetion(msg)
smtpServer = parser.get('reporting', 'smtpServer')
recipients = parser.get('reporting', 'recipients')
sender = parser.get('reporting', 'sender')
subject = parser.get('reporting', 'subject')
except Exception, e:
logger.error('Could not read email configuration options: %s' % (e))
###############################################################################
try:
import smtplib
from socket import gethostname
except Exception, e:
logging.error('Could not email error report: %s' % (str(e)))
else:
email = 'From: %s\r\nSubject: %s\r\n' % (sender, subject)
email += msg
email += '\n\n\nMessage from: %s' % (gethostname())
try:
session = smtplib.SMTP(host=smtpServer)
sendresult = session.sendmail(sender, recipients, email)
except Exception, e:
logging.error('Could not email error report because %s. Report: %s' % (str(e), email))
return True
UsageUse this module by like this:
import reporting
#In a single script logging needs to be configured either from a file
#or in the script itself. In a larger project this only needs to be done
#once. Other modules just need to import reporting and get the logger.
reporting.logging.config.fileConfig('/etc/identity/conf/logging.conf')
logger = reporting.logging.getLogger('pa_server')
logger.failure('This message will be emailed to you and logged as an error')
| |||||||