source: trunk/scripts/outage/outage-mail @ 2275

Last change on this file since 2275 was 2275, checked in by price, 15 years ago

outage-mail: make subject line an option

File size: 2.1 KB
Line 
1#!/usr/bin/python
2"""
3
4VM names on stdin.
5"""
6
7import smtplib
8import optparse
9import sys
10from email.mime.text import MIMEText
11
12from invirt import database
13
14def 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['CC'] = 'XVM <xvm@mit.edu>'
21    msg['Reply-To'] = 'XVM <xvm@mit.edu>'
22    msg['From'] = opts.from_addr
23    msg['Subject'] = opts.subject % vm
24    smtp.sendmail(opts.from_addr,
25        [contact, 'xvm@mit.edu'],
26        msg.as_string())
27
28def main(argv):
29    parser = optparse.OptionParser(
30        usage = '%prog {-m message, -f from} [options] <vm-list',
31        description = __doc__.strip())
32    parser.add_option('-m', '--message',
33            type = 'string',
34            dest = 'message',
35            help = 'filename with body of message')
36    parser.add_option('-s', '--subject',
37            type = 'string',
38            dest = 'subject',
39            default = '[xvm] Unexpected reboot of your VM %s',
40            help = 'subject line of message; %s for VM name')
41    parser.add_option('-f', '--from',
42            type = 'string',
43            dest = 'from_addr',
44            help = 'for From: and envelope-from; first word is %(sig)s')
45    parser.add_option('--host',
46            type = 'string',
47            dest = 'host',
48            help = 'host that failed; %(host)s in message')
49    parser.add_option('--time',
50            type = 'string',
51            dest = 'time',
52            help = 'time of failure; %(time)s in message')
53    opts, args = parser.parse_args()
54
55    if len(args) or not opts.message or not opts.from_addr:
56        parser.print_help(sys.stderr)
57        return 2
58
59    opts.sig = opts.from_addr.split()[0]
60    message = file(opts.message).read() % opts.__dict__
61
62    vms = sys.stdin.read().split()
63
64    database.connect()
65    s = smtplib.SMTP()
66    s.connect()
67
68    for vm in vms:
69        send_mail(s, opts, message, vm)
70
71    s.close()
72
73if __name__ == '__main__':
74    sys.exit(main(sys.argv))
Note: See TracBrowser for help on using the repository browser.