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 | |
| 7 | class EmailNotificationManager(object): |
| 8 | def __init__(self): |
| 9 | self._emails = [] |
| 10 | |
| 11 | self._from_address = global_config.global_config.get_config_value( |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 12 | CONFIG_SECTION, "notify_email_from", default=getpass.getuser()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 13 | |
| 14 | self._notify_address = global_config.global_config.get_config_value( |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 15 | CONFIG_SECTION, "notify_email", default='') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | def send_email(self, to_string, subject, body): |
| 19 | """Mails out emails to the addresses listed in to_string. |
| 20 | |
| 21 | to_string is split into a list which can be delimited by any of: |
| 22 | ';', ',', ':' or any whitespace |
| 23 | """ |
| 24 | # Create list from string removing empty strings from the list. |
| 25 | to_list = [x for x in re.split('\s|,|;|:', to_string) if x] |
| 26 | if not to_list: |
| 27 | return |
| 28 | |
| 29 | msg = "From: %s\nTo: %s\nSubject: %s\n\n%s" % ( |
| 30 | self._from_address, ', '.join(to_list), subject, body) |
| 31 | try: |
| 32 | mailer = smtplib.SMTP('localhost') |
| 33 | try: |
| 34 | mailer.sendmail(self._from_address, to_list, msg) |
| 35 | finally: |
showard | b593fa8 | 2009-09-09 15:30:24 +0000 | [diff] [blame^] | 36 | try: |
| 37 | mailer.quit() |
| 38 | except: |
| 39 | logging.exception('mailer.quit() failed:') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 40 | except Exception: |
showard | f098ebd | 2009-06-10 00:13:33 +0000 | [diff] [blame] | 41 | logging.exception('Sending email failed:') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | def enqueue_notify_email(self, subject, message): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 45 | logging.error(subject + '\n' + message) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 46 | if not self._notify_address: |
| 47 | return |
| 48 | |
| 49 | body = 'Subject: ' + subject + '\n' |
| 50 | body += "%s / %s / %s\n%s" % (socket.gethostname(), |
| 51 | os.getpid(), |
| 52 | time.strftime("%X %x"), message) |
| 53 | self._emails.append(body) |
| 54 | |
| 55 | |
| 56 | def send_queued_emails(self): |
| 57 | if not self._emails: |
| 58 | return |
| 59 | subject = 'Scheduler notifications from ' + socket.gethostname() |
| 60 | separator = '\n' + '-' * 40 + '\n' |
| 61 | body = separator.join(self._emails) |
| 62 | |
| 63 | self.send_email(self._notify_address, subject, body) |
| 64 | self._emails = [] |
| 65 | |
| 66 | |
| 67 | def log_stacktrace(self, reason): |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 68 | logging.exception(reason) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 69 | message = "EXCEPTION: %s\n%s" % (reason, traceback.format_exc()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 70 | self.enqueue_notify_email("monitor_db exception", message) |
| 71 | |
| 72 | |
| 73 | manager = EmailNotificationManager() |