Fang Deng | 8177dfd | 2015-04-10 11:57:33 -0700 | [diff] [blame] | 1 | """Scheduler email manager.""" |
| 2 | |
| 3 | |
| 4 | import logging |
| 5 | import os |
| 6 | import re |
| 7 | import socket |
| 8 | import time |
| 9 | import traceback |
| 10 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 11 | import common |
| 12 | from autotest_lib.client.common_lib import global_config |
Fang Deng | 8177dfd | 2015-04-10 11:57:33 -0700 | [diff] [blame] | 13 | from autotest_lib.site_utils import gmail_lib |
| 14 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 15 | |
| 16 | CONFIG_SECTION = 'SCHEDULER' |
| 17 | |
showard | b6a186f | 2009-09-11 18:45:54 +0000 | [diff] [blame] | 18 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 19 | class EmailNotificationManager(object): |
Fang Deng | 8177dfd | 2015-04-10 11:57:33 -0700 | [diff] [blame] | 20 | """Scheduler email notification manager.""" |
| 21 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 22 | def __init__(self): |
Fang Deng | 8177dfd | 2015-04-10 11:57:33 -0700 | [diff] [blame] | 23 | """Initialize the manager.""" |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 24 | self._emails = [] |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 25 | self._notify_address = global_config.global_config.get_config_value( |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 26 | CONFIG_SECTION, "notify_email", default='') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | def send_email(self, to_string, subject, body): |
| 30 | """Mails out emails to the addresses listed in to_string. |
| 31 | |
Fang Deng | 8177dfd | 2015-04-10 11:57:33 -0700 | [diff] [blame] | 32 | @param to_string: is split into a list which can be delimited by any of: |
| 33 | ';', ',', ':' or any whitespace. |
| 34 | @param subject: String, email subject. |
| 35 | @param body: String, message body |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 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 |
Fang Deng | 8177dfd | 2015-04-10 11:57:33 -0700 | [diff] [blame] | 41 | to_string = ','.join(to_list) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 42 | try: |
Fang Deng | 8177dfd | 2015-04-10 11:57:33 -0700 | [diff] [blame] | 43 | gmail_lib.send_email(to_string, subject, body) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 44 | except Exception: |
showard | f098ebd | 2009-06-10 00:13:33 +0000 | [diff] [blame] | 45 | logging.exception('Sending email failed:') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | def enqueue_notify_email(self, subject, message): |
Fang Deng | 8177dfd | 2015-04-10 11:57:33 -0700 | [diff] [blame] | 49 | """Enqueue a message that will be sent later. |
| 50 | |
| 51 | @param subject: String, subject of the message. |
| 52 | @param message: String, message to enqueue. |
| 53 | """ |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 54 | logging.error(subject + '\n' + message) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 55 | if not self._notify_address: |
| 56 | return |
| 57 | |
| 58 | body = 'Subject: ' + subject + '\n' |
| 59 | body += "%s / %s / %s\n%s" % (socket.gethostname(), |
| 60 | os.getpid(), |
| 61 | time.strftime("%X %x"), message) |
| 62 | self._emails.append(body) |
| 63 | |
| 64 | |
| 65 | def send_queued_emails(self): |
Fang Deng | 8177dfd | 2015-04-10 11:57:33 -0700 | [diff] [blame] | 66 | """Send queued emails.""" |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 67 | if not self._emails: |
| 68 | return |
| 69 | subject = 'Scheduler notifications from ' + socket.gethostname() |
| 70 | separator = '\n' + '-' * 40 + '\n' |
| 71 | body = separator.join(self._emails) |
| 72 | |
| 73 | self.send_email(self._notify_address, subject, body) |
| 74 | self._emails = [] |
| 75 | |
| 76 | |
| 77 | def log_stacktrace(self, reason): |
Fang Deng | 8177dfd | 2015-04-10 11:57:33 -0700 | [diff] [blame] | 78 | """Log an exception and enqueue it. |
| 79 | |
| 80 | @param reason: An exception to log and send. |
| 81 | """ |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 82 | logging.exception(reason) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 83 | message = "EXCEPTION: %s\n%s" % (reason, traceback.format_exc()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 84 | self.enqueue_notify_email("monitor_db exception", message) |
| 85 | |
| 86 | |
| 87 | manager = EmailNotificationManager() |