blob: d504fbd429422410ebafe1dbc439b6a1d04d164f [file] [log] [blame]
showard27f33872009-04-07 18:20:53 +00001import traceback, socket, os, time, smtplib, re, sys, getpass, logging
showard170873e2009-01-07 00:22:26 +00002import common
3from autotest_lib.client.common_lib import global_config
4
5CONFIG_SECTION = 'SCHEDULER'
6
showardb6a186f2009-09-11 18:45:54 +00007CONFIG_SECTION_SMTP = 'SERVER'
8
showard170873e2009-01-07 00:22:26 +00009class EmailNotificationManager(object):
10 def __init__(self):
11 self._emails = []
12
13 self._from_address = global_config.global_config.get_config_value(
showardd1ee1dd2009-01-07 21:33:08 +000014 CONFIG_SECTION, "notify_email_from", default=getpass.getuser())
showard170873e2009-01-07 00:22:26 +000015
16 self._notify_address = global_config.global_config.get_config_value(
showardd1ee1dd2009-01-07 21:33:08 +000017 CONFIG_SECTION, "notify_email", default='')
showard170873e2009-01-07 00:22:26 +000018
showardb6a186f2009-09-11 18:45:54 +000019 self._smtp_server = global_config.global_config.get_config_value(
20 CONFIG_SECTION_SMTP, "smtp_server", default='localhost')
21
22 self._smtp_port = global_config.global_config.get_config_value(
23 CONFIG_SECTION_SMTP, "smtp_port", default=None)
24
25 self._smtp_user = global_config.global_config.get_config_value(
26 CONFIG_SECTION_SMTP, "smtp_user", default='')
27
28 self._smtp_password = global_config.global_config.get_config_value(
29 CONFIG_SECTION_SMTP, "smtp_password", default='')
showard170873e2009-01-07 00:22:26 +000030
31 def send_email(self, to_string, subject, body):
32 """Mails out emails to the addresses listed in to_string.
33
34 to_string is split into a list which can be delimited by any of:
35 ';', ',', ':' or any whitespace
36 """
37 # Create list from string removing empty strings from the list.
38 to_list = [x for x in re.split('\s|,|;|:', to_string) if x]
39 if not to_list:
40 return
41
42 msg = "From: %s\nTo: %s\nSubject: %s\n\n%s" % (
43 self._from_address, ', '.join(to_list), subject, body)
44 try:
showardb6a186f2009-09-11 18:45:54 +000045 mailer = smtplib.SMTP(self._smtp_server, self._smtp_port)
showard170873e2009-01-07 00:22:26 +000046 try:
showardb6a186f2009-09-11 18:45:54 +000047 if self._smtp_user:
48 mailer.login(self._smtp_user, self._smtp_password)
showard170873e2009-01-07 00:22:26 +000049 mailer.sendmail(self._from_address, to_list, msg)
50 finally:
showardb593fa82009-09-09 15:30:24 +000051 try:
52 mailer.quit()
53 except:
54 logging.exception('mailer.quit() failed:')
showard170873e2009-01-07 00:22:26 +000055 except Exception:
showardf098ebd2009-06-10 00:13:33 +000056 logging.exception('Sending email failed:')
showard170873e2009-01-07 00:22:26 +000057
58
59 def enqueue_notify_email(self, subject, message):
showarded2afea2009-07-07 20:54:07 +000060 logging.error(subject + '\n' + message)
showard170873e2009-01-07 00:22:26 +000061 if not self._notify_address:
62 return
63
64 body = 'Subject: ' + subject + '\n'
65 body += "%s / %s / %s\n%s" % (socket.gethostname(),
66 os.getpid(),
67 time.strftime("%X %x"), message)
68 self._emails.append(body)
69
70
71 def send_queued_emails(self):
72 if not self._emails:
73 return
74 subject = 'Scheduler notifications from ' + socket.gethostname()
75 separator = '\n' + '-' * 40 + '\n'
76 body = separator.join(self._emails)
77
78 self.send_email(self._notify_address, subject, body)
79 self._emails = []
80
81
82 def log_stacktrace(self, reason):
showard27f33872009-04-07 18:20:53 +000083 logging.exception(reason)
showard170873e2009-01-07 00:22:26 +000084 message = "EXCEPTION: %s\n%s" % (reason, traceback.format_exc())
showard170873e2009-01-07 00:22:26 +000085 self.enqueue_notify_email("monitor_db exception", message)
86
87
88manager = EmailNotificationManager()