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(self, errorType, errorValue, errorTrace, errorUrl,
91 revision, versionShort,
92 contactName=None, contactEmail=None, comments=None,
93 smtphost=None, smtpport=25, usetls=False, usr='',
94 pwd=''):
95 ''' Attempt to send an email to the zenoss errors email address
96 with details of this error.
97 Returns true if mail was sent, false otherwise.
98 '''
99 import socket
100 fqdn = socket.getfqdn()
101 fromAddress = 'errors@%s' % fqdn
102 cleanUrl = self.cleanUrl(errorUrl)
103 subject = '%s: %s (%s)' % (errorType, errorValue[:15], cleanUrl)
104 header = self.createEmailHeader(
105 fromAddress, self.ERRORS_ADDRESS, subject)
106 body = self.createReport(errorType, errorValue, errorTrace, cleanUrl,
107 revision, versionShort,
108 0, contactName, contactEmail, comments)
109 mailSent = False
110
111
112 if not smtphost:
113 smtphost = self.SMTP_HOST
114 server = smtplib.SMTP(smtphost, smtpport)
115 if usetls:
116 server.ehlo()
117 server.starttls()
118 server.ehlo()
119 if usr: server.login(usr, pwd)
120 try:
121 server.sendmail(fromAddress, self.ERRORS_ADDRESS,
122 '%s\n\n%s' % (header, body))
123 mailSent = True
124 finally:
125 try: server.quit()
126 except: pass
127 return mailSent
128 sendErrorEmail = classmethod(sendErrorEmail)
129