X-Git-Url: http://xvm.mit.edu/gitweb/invirt/scripts/outage.git/blobdiff_plain/cb73e2a3963392c6670bc3de3a3db3993126bba7..18db0434c020e34755f691da2443445ac664b93f:/outage-mail diff --git a/outage-mail b/outage-mail index ed75737..4731cb5 100644 --- a/outage-mail +++ b/outage-mail @@ -1,46 +1,86 @@ #!/usr/bin/python -import smtplib -from invirt.database import * -from email.mime.text import MIMEText - -sender = 'Greg Price <price@mit.edu>' -sendername = sender.split()[0] -host = 'arklay-mansion' - -message = """\ -One of the four XVM host servers, %s, failed tonight at -about 23:17 and was rebooted. We're working to understand the cause. +""" -Your VM %%s was running on %s. We have restarted it. +VM names on stdin. +""" -We realize that this is an inconvenience for you, and we apologize for -the unexpected downtime. - -- %s -for the XVM team -""" % (host, host, sendername) +import smtplib +import optparse +import sys +from email.mime.text import MIMEText -vms = "amd64-test brain-trust cluster-test david-vista debathena-livecd-build ecprice gradbook greg-ns1 greg-ns2 groovy htns iodine jsoltren latex liftm medkaz mhd mksvn niska polarix price-lenny price-test8 qren2 shawn-of-awesome staxowax tdc tdc-secure thesecrete tor transistor watmap".split() +from invirt import database -def send_mail(vm): - contact = Machine.query.filter_by(name=vm).first().contact +def send_mail(smtp, opts, message, vm): + contact = database.Machine.query.filter_by(name=vm).first().contact if '@' not in contact: contact += '@mit.edu' msg = MIMEText(message % vm) msg['To'] = contact - msg['CC'] = 'XVM <xvm@mit.edu>' + msg['From'] = opts.from_addr msg['Reply-To'] = 'XVM <xvm@mit.edu>' - msg['From'] = sender - msg['Subject'] = '[xvm] Unexpected reboot of your VM %s' % vm - s.sendmail(sender, - [contact, 'xvm@mit.edu'], + msg['Subject'] = opts.subject % vm + smtp.sendmail(opts.from_addr, + [contact], msg.as_string()) + return msg.as_string() + +def send_summary(smtp, opts, messages): + msg = MIMEText('\n\n\n'.join(messages)) + msg['To'] = 'xvm@mit.edu' + msg['From'] = opts.from_addr + msg['Subject'] = ("xvm outage-mail summary (%d): %s" + % (len(messages), opts.subject)) + smtp.sendmail(opts.from_addr, + ['xvm@mit.edu'], + msg.as_string()) + +def main(argv): + parser = optparse.OptionParser( + usage = '%prog {-m message, -f from} [options] <vm-list', + description = __doc__.strip()) + parser.add_option('-m', '--message', + type = 'string', + dest = 'message', + help = 'filename with body of message') + parser.add_option('-s', '--subject', + type = 'string', + dest = 'subject', + default = '[xvm] Unexpected reboot of your VM %s', + help = 'subject line of message; %s for VM name') + parser.add_option('-f', '--from', + type = 'string', + dest = 'from_addr', + help = 'for From: and envelope-from; first word is %(sig)s') + parser.add_option('--host', + type = 'string', + dest = 'host', + help = 'host that failed; %(host)s in message') + parser.add_option('--time', + type = 'string', + dest = 'time', + help = 'time of failure; %(time)s in message') + opts, args = parser.parse_args() + + if len(args) or not opts.message or not opts.from_addr: + parser.print_help(sys.stderr) + return 2 + + opts.sig = opts.from_addr.split()[0] + message = file(opts.message).read() % opts.__dict__ + + vms = sys.stdin.read().split() + + database.connect() + s = smtplib.SMTP() + s.connect() -connect() -s = smtplib.SMTP() -s.connect() + messages = [] + for vm in vms: + messages.append(send_mail(s, opts, message, vm)) + send_summary(s, opts, messages) -for vm in vms: - send_mail(vm) + s.close() -s.close() +if __name__ == '__main__': + sys.exit(main(sys.argv))