1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""SiteError
15
16 SiteError consolidates code used to handle and report site errors.
17 """
18 import Globals
19 import smtplib
20 import re
21 import cgi
22
24
25 SMTP_HOST = 'mail.zenoss.com'
26 ERRORS_ADDRESS = '[email protected]'
27
29 ''' Strip protocol and domain from the url
30 '''
31 stripDomain = False
32 if errorUrl.startswith('http://'):
33 errorUrl = errorUrl[len('http://'):]
34 stripDomain = True
35 elif errorUrl.startswith('https://'):
36 errorUrl = errorUrl[len('https://'):]
37 stripDomain = True
38 if stripDomain and '/' in errorUrl:
39 errorUrl = errorUrl[errorUrl.find('/'):]
40 return errorUrl
41 cleanUrl = classmethod(cleanUrl)
42
43
45 ''' Create the smnp header for an error email
46 '''
47 header = 'To: %s\nFrom: %s\nSubject: %s\n' % (
48 toAddress, fromAddress, subject)
49 return header
50 createEmailHeader = classmethod(createEmailHeader)
51
52
53 - def createReport(cls, errorType, errorValue, errorTrace, errorUrl, revision,
54 versionShort,
55 inHtml=True, contactName=None, contactEmail=None,
56 comments=None):
57 ''' Produce a summary of the given error details suitable for use
58 in an error email (inHtml=false) or on a page (inHtml=true)
59 '''
60 def StripTags(s):
61 ''' Strip html tags from string
62 '''
63 return re.sub('<[^>]*>', '', s)
64
65
66 if inHtml:
67 linebreak = '<br />\n'
68 contactName = cgi.escape(contactName)
69 contactEmail = cgi.escape(contactEmail)
70 comments = cgi.escape(comments)
71 else:
72 linebreak = '\n'
73 errorType = StripTags(errorType)
74 errorValue = StripTags(errorValue)
75 errorTrace = StripTags(errorTrace)
76 errorTrace = re.sub('\r\n\r\n', '\r\n', errorTrace)
77 msg = linebreak.join(['Type: %s' % errorType,
78 'Value: %s' % errorValue,
79 'URL: %s' % cls.cleanUrl(errorUrl),
80 'Revision: %s' % revision,
81 'Version: %s' % versionShort,
82 '%s' % errorTrace,
83 'Contact name: %s' % (contactName or ''),
84 'Email address: %s' % (contactEmail or ''),
85 'Comments: %s' % (comments or '')])
86 return msg
87 createReport = classmethod(createReport)
88
89
90 - def sendErrorEmail(cls, errorType, errorValue, errorTrace, errorUrl,
91 revision, versionShort,
92 contactName=None, contactEmail=None, comments=None):
93 ''' Attempt to send an email to the zenoss errors email address
94 with details of this error.
95 Returns true if mail was sent, false otherwise.
96 '''
97 import socket
98 fqdn = socket.getfqdn()
99 fromAddress = 'errors@%s' % fqdn
100 cleanUrl = cls.cleanUrl(errorUrl)
101 subject = '%s: %s (%s)' % (errorType, errorValue[:15], cleanUrl)
102 header = cls.createEmailHeader(
103 fromAddress, cls.ERRORS_ADDRESS, subject)
104 body = cls.createReport(errorType, errorValue, errorTrace, cleanUrl,
105 revision, versionShort,
106 0, contactName, contactEmail, comments)
107 mailSent = False
108 server = smtplib.SMTP(cls.SMTP_HOST)
109 try:
110 server.sendmail(fromAddress, cls.ERRORS_ADDRESS,
111 '%s\n\n%s' % (header, body))
112 mailSent = True
113 finally:
114 server.quit()
115 return mailSent
116 sendErrorEmail = classmethod(sendErrorEmail)
117