1 | #!/usr/bin/python |
---|
2 | """ |
---|
3 | |
---|
4 | VM names on stdin. |
---|
5 | """ |
---|
6 | |
---|
7 | import smtplib |
---|
8 | import optparse |
---|
9 | import sys |
---|
10 | from email.mime.text import MIMEText |
---|
11 | |
---|
12 | from invirt import database |
---|
13 | |
---|
14 | def send_mail(smtp, opts, message, vm): |
---|
15 | contact = database.Machine.query.filter_by(name=vm).first().contact |
---|
16 | if '@' not in contact: |
---|
17 | contact += '@mit.edu' |
---|
18 | msg = MIMEText(message % vm) |
---|
19 | msg['To'] = contact |
---|
20 | msg['From'] = opts.from_addr |
---|
21 | msg['Reply-To'] = 'XVM <xvm@mit.edu>' |
---|
22 | msg['Subject'] = opts.subject % vm |
---|
23 | smtp.sendmail(opts.from_addr, |
---|
24 | [contact], |
---|
25 | msg.as_string()) |
---|
26 | return msg.as_string() |
---|
27 | |
---|
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 | |
---|
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') |
---|
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') |
---|
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() |
---|
64 | |
---|
65 | if len(args) or not opts.message or not opts.from_addr: |
---|
66 | parser.print_help(sys.stderr) |
---|
67 | return 2 |
---|
68 | |
---|
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 | |
---|
78 | messages = [] |
---|
79 | for vm in vms: |
---|
80 | messages.append(send_mail(s, opts, message, vm)) |
---|
81 | send_summary(s, opts, messages) |
---|
82 | |
---|
83 | s.close() |
---|
84 | |
---|
85 | if __name__ == '__main__': |
---|
86 | sys.exit(main(sys.argv)) |
---|