showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 1 | import traceback, socket, os, time, smtplib, re, sys, getpass, logging |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2 | import common |
| 3 | from autotest_lib.client.common_lib import global_config |
| 4 | |
| 5 | CONFIG_SECTION = 'SCHEDULER' |
| 6 | |
showard | b6a186f | 2009-09-11 18:45:54 +0000 | [diff] [blame] | 7 | CONFIG_SECTION_SMTP = 'SERVER' |
| 8 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 9 | class EmailNotificationManager(object): |
| 10 | def __init__(self): |
| 11 | self._emails = [] |
| 12 | |
| 13 | self._from_address = global_config.global_config.get_config_value( |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 14 | CONFIG_SECTION, "notify_email_from", default=getpass.getuser()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 15 | |
| 16 | self._notify_address = global_config.global_config.get_config_value( |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 17 | CONFIG_SECTION, "notify_email", default='') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 18 | |
showard | b6a186f | 2009-09-11 18:45:54 +0000 | [diff] [blame] | 19 | 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='') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 30 | |
| 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: |
showard | b6a186f | 2009-09-11 18:45:54 +0000 | [diff] [blame] | 45 | mailer = smtplib.SMTP(self._smtp_server, self._smtp_port) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 46 | try: |
showard | b6a186f | 2009-09-11 18:45:54 +0000 | [diff] [blame] | 47 | if self._smtp_user: |
| 48 | mailer.login(self._smtp_user, self._smtp_password) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 49 | mailer.sendmail(self._from_address, to_list, msg) |
| 50 | finally: |
showard | b593fa8 | 2009-09-09 15:30:24 +0000 | [diff] [blame] | 51 | try: |
| 52 | mailer.quit() |
| 53 | except: |
| 54 | logging.exception('mailer.quit() failed:') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 55 | except Exception: |
showard | f098ebd | 2009-06-10 00:13:33 +0000 | [diff] [blame] | 56 | logging.exception('Sending email failed:') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 57 | |
| 58 | |
| 59 | def enqueue_notify_email(self, subject, message): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 60 | logging.error(subject + '\n' + message) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 61 | 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): |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 83 | logging.exception(reason) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 84 | message = "EXCEPTION: %s\n%s" % (reason, traceback.format_exc()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 85 | self.enqueue_notify_email("monitor_db exception", message) |
| 86 | |
| 87 | |
| 88 | manager = EmailNotificationManager() |