[2251] | 1 | #!/usr/bin/python |
---|
[2252] | 2 | """ |
---|
| 3 | |
---|
| 4 | VM names on stdin. |
---|
| 5 | """ |
---|
| 6 | |
---|
[2250] | 7 | import smtplib |
---|
[2252] | 8 | import optparse |
---|
| 9 | import sys |
---|
[2250] | 10 | from email.mime.text import MIMEText |
---|
| 11 | |
---|
[2252] | 12 | from invirt import database |
---|
[2250] | 13 | |
---|
[2252] | 14 | def send_mail(smtp, opts, message, vm): |
---|
| 15 | contact = database.Machine.query.filter_by(name=vm).first().contact |
---|
[2250] | 16 | if '@' not in contact: |
---|
| 17 | contact += '@mit.edu' |
---|
| 18 | msg = MIMEText(message % vm) |
---|
| 19 | msg['To'] = contact |
---|
[2276] | 20 | msg['From'] = opts.from_addr |
---|
[2250] | 21 | msg['Reply-To'] = 'XVM <xvm@mit.edu>' |
---|
[2275] | 22 | msg['Subject'] = opts.subject % vm |
---|
[2252] | 23 | smtp.sendmail(opts.from_addr, |
---|
[2276] | 24 | [contact], |
---|
[2250] | 25 | msg.as_string()) |
---|
[2276] | 26 | return msg.as_string() |
---|
[2250] | 27 | |
---|
[2276] | 28 | def send_summary(smtp, opts, messages): |
---|
| 29 | msg = MIMEText('\n\n\n'.join(messages)) |
---|
| 30 | msg['To'] = 'xvm@mit.edu' |
---|
| 31 | msg['From'] = opts.from_addr |
---|
| 32 | msg['Subject'] = ("xvm outage-mail summary (%d): %s" |
---|
| 33 | % (len(messages), opts.subject)) |
---|
| 34 | smtp.sendmail(opts.from_addr, |
---|
| 35 | ['xvm@mit.edu'], |
---|
| 36 | msg.as_string()) |
---|
| 37 | |
---|
[2252] | 38 | def main(argv): |
---|
| 39 | parser = optparse.OptionParser( |
---|
| 40 | usage = '%prog {-m message, -f from} [options] <vm-list', |
---|
| 41 | description = __doc__.strip()) |
---|
| 42 | parser.add_option('-m', '--message', |
---|
| 43 | type = 'string', |
---|
| 44 | dest = 'message', |
---|
| 45 | help = 'filename with body of message') |
---|
[2275] | 46 | parser.add_option('-s', '--subject', |
---|
| 47 | type = 'string', |
---|
| 48 | dest = 'subject', |
---|
| 49 | default = '[xvm] Unexpected reboot of your VM %s', |
---|
| 50 | help = 'subject line of message; %s for VM name') |
---|
[2252] | 51 | parser.add_option('-f', '--from', |
---|
| 52 | type = 'string', |
---|
| 53 | dest = 'from_addr', |
---|
| 54 | help = 'for From: and envelope-from; first word is %(sig)s') |
---|
| 55 | parser.add_option('--host', |
---|
| 56 | type = 'string', |
---|
| 57 | dest = 'host', |
---|
| 58 | help = 'host that failed; %(host)s in message') |
---|
| 59 | parser.add_option('--time', |
---|
| 60 | type = 'string', |
---|
| 61 | dest = 'time', |
---|
| 62 | help = 'time of failure; %(time)s in message') |
---|
| 63 | opts, args = parser.parse_args() |
---|
[2250] | 64 | |
---|
[2252] | 65 | if len(args) or not opts.message or not opts.from_addr: |
---|
| 66 | parser.print_help(sys.stderr) |
---|
| 67 | return 2 |
---|
[2250] | 68 | |
---|
[2252] | 69 | opts.sig = opts.from_addr.split()[0] |
---|
| 70 | message = file(opts.message).read() % opts.__dict__ |
---|
| 71 | |
---|
| 72 | vms = sys.stdin.read().split() |
---|
| 73 | |
---|
| 74 | database.connect() |
---|
| 75 | s = smtplib.SMTP() |
---|
| 76 | s.connect() |
---|
| 77 | |
---|
[2276] | 78 | messages = [] |
---|
[2252] | 79 | for vm in vms: |
---|
[2276] | 80 | messages.append(send_mail(s, opts, message, vm)) |
---|
| 81 | send_summary(s, opts, messages) |
---|
[2252] | 82 | |
---|
| 83 | s.close() |
---|
| 84 | |
---|
| 85 | if __name__ == '__main__': |
---|
| 86 | sys.exit(main(sys.argv)) |
---|