* Emails PI, then Slices if the node does not come up after a certain number of days.
[monitor.git] / mailer.py
1 #!/usr/bin/python2
2 #
3 # Copyright (c) 2004  The Trustees of Princeton University (Trustees).
4 #
5 # Faiyaz Ahmed <faiyaza@cs.princeton.edu>
6 #
7 # $Id: $
8 from emailTxt import *
9 import xml, xmlrpclib
10 import smtplib
11
12 MTA="localhost"
13 FROM="support@planet-lab.org"
14
15 XMLRPC_SERVER = 'https://www.planet-lab.org/PLCAPI/'
16
17 def siteId(hostname):
18         api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
19         anon = {'AuthMethod': "anonymous"}
20         site_id = api.AnonAdmQuerySite (anon, {"node_hostname": hostname})
21         if len(site_id) == 1:  
22                 loginbase = api.AnonAdmGetSites (anon, site_id, ["login_base"])
23                 return loginbase[0]['login_base']
24
25
26 def slices(loginbase):
27         api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
28         return api.SliceListNames (auth.auth, loginbase)
29
30
31 def email (subject, text, to):
32         """Create a mime-message that will render HTML in popular
33         MUAs, text in better ones"""
34         import MimeWriter
35         import mimetools
36         import cStringIO
37
38         out = cStringIO.StringIO() # output buffer for our message 
39         txtin = cStringIO.StringIO(text)
40
41         writer = MimeWriter.MimeWriter(out)
42         #
43         # set up some basic headers... we put subject here
44         # because smtplib.sendmail expects it to be in the
45         # message body
46         #
47         writer.addheader("Subject", subject)
48         if to.__class__ == [].__class__ :       
49                 writer.addheader("To", to[0])
50                 cc = ""
51                 for dest in to[1:len(to)]:
52                         cc +="%s, " % dest
53                 cc = cc.rstrip(", ") 
54                 writer.addheader("CC", cc)
55         else:
56                 writer.addheader("To", to)
57                 
58         writer.addheader("MIME-Version", "1.0")
59         #
60         # start the multipart section of the message
61         # multipart/alternative seems to work better
62         # on some MUAs than multipart/mixed
63         #
64         writer.startmultipartbody("alternative")
65         writer.flushheaders()
66         #
67         # the plain text section
68         #
69         subpart = writer.nextpart()
70         subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
71         pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
72         mimetools.encode(txtin, pout, 'quoted-printable')
73         txtin.close()
74         #
75         # Now that we're done, close our writer and
76         # return the message body
77         #
78         writer.lastpart()
79         msg = out.getvalue()
80         out.close()
81         server = smtplib.SMTP(MTA)
82         server.sendmail(FROM, to,  msg)
83         server.quit()
84
85 if __name__=="__main__":
86         import smtplib
87         import emailTxt
88         id = siteId("alice.cs.princeton.edu")
89         print id
90         #if id:
91                 #email('TEST', emailTxt.mailtxt.ssh % {'hostname': "ALICE.cs.princeton.edu"}, "tech-" + id + "@sites.planet-lab.org")
92         #else:
93         #       print "No dice."
94         #email("TEST109", "THIS IS A TEST", ["faiyaza@cs.princeton.edu", "faiyaz@winlab.rutgers.edu", "faiyaza@gmail.com"])