Changeset 2252 for trunk/scripts/outage


Ignore:
Timestamp:
Mar 16, 2009, 8:26:21 PM (16 years ago)
Author:
price
Message:

make outage-mail fully reusable

Location:
trunk/scripts/outage
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/scripts/outage/outage-mail

    r2251 r2252  
    11#!/usr/bin/python
     2"""
     3
     4VM names on stdin.
     5"""
     6
    27import smtplib
    3 from invirt.database import *
     8import optparse
     9import sys
    410from email.mime.text import MIMEText
    511
    6 sender = 'Greg Price <price@mit.edu>'
    7 sendername = sender.split()[0]
    8 host = 'arklay-mansion'
     12from invirt import database
    913
    10 message = """\
    11 One of the four XVM host servers, %s, failed tonight at
    12 about 23:17 and was rebooted.  We're working to understand the cause.
    13 
    14 Your VM %%s was running on %s.  We have restarted it.
    15 
    16 We realize that this is an inconvenience for you, and we apologize for
    17 the unexpected downtime.
    18 
    19 - %s
    20 for the XVM team
    21 """ % (host, host, sendername)
    22 
    23 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()
    24 
    25 def send_mail(vm):
    26     contact = Machine.query.filter_by(name=vm).first().contact
     14def send_mail(smtp, opts, message, vm):
     15    contact = database.Machine.query.filter_by(name=vm).first().contact
    2716    if '@' not in contact:
    2817        contact += '@mit.edu'
     
    3120    msg['CC'] = 'XVM <xvm@mit.edu>'
    3221    msg['Reply-To'] = 'XVM <xvm@mit.edu>'
    33     msg['From'] = sender
     22    msg['From'] = opts.from_addr
    3423    msg['Subject'] = '[xvm] Unexpected reboot of your VM %s' % vm
    35     s.sendmail(sender,
     24    smtp.sendmail(opts.from_addr,
    3625        [contact, 'xvm@mit.edu'],
    3726        msg.as_string())
    3827
    39 connect()
    40 s = smtplib.SMTP()
    41 s.connect()
     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('-f', '--from',
     37            type = 'string',
     38            dest = 'from_addr',
     39            help = 'for From: and envelope-from; first word is %(sig)s')
     40    parser.add_option('--host',
     41            type = 'string',
     42            dest = 'host',
     43            help = 'host that failed; %(host)s in message')
     44    parser.add_option('--time',
     45            type = 'string',
     46            dest = 'time',
     47            help = 'time of failure; %(time)s in message')
     48    opts, args = parser.parse_args()
    4249
    43 for vm in vms:
    44     send_mail(vm)
     50    if len(args) or not opts.message or not opts.from_addr:
     51        parser.print_help(sys.stderr)
     52        return 2
    4553
    46 s.close()
     54    opts.sig = opts.from_addr.split()[0]
     55    message = file(opts.message).read() % opts.__dict__
     56
     57    vms = sys.stdin.read().split()
     58
     59    database.connect()
     60    s = smtplib.SMTP()
     61    s.connect()
     62
     63    for vm in vms:
     64        send_mail(s, opts, message, vm)
     65
     66    s.close()
     67
     68if __name__ == '__main__':
     69    sys.exit(main(sys.argv))
Note: See TracChangeset for help on using the changeset viewer.