Uses CoMon's ability to find 'upness' to email. Changed queueing between threads...
[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 def email (subject, text, to, cc = None):
26         """Create a mime-message that will render HTML in popular
27         MUAs, text in better ones"""
28         import MimeWriter
29         import mimetools
30         import cStringIO
31
32         out = cStringIO.StringIO() # output buffer for our message 
33         txtin = cStringIO.StringIO(text)
34
35         writer = MimeWriter.MimeWriter(out)
36         #
37         # set up some basic headers... we put subject here
38         # because smtplib.sendmail expects it to be in the
39         # message body
40         #
41         writer.addheader("Subject", subject)
42         writer.addheader("To", to)
43         if cc: writer.addheader("CC", cc)
44         writer.addheader("MIME-Version", "1.0")
45         #
46         # start the multipart section of the message
47         # multipart/alternative seems to work better
48         # on some MUAs than multipart/mixed
49         #
50         writer.startmultipartbody("alternative")
51         writer.flushheaders()
52         #
53         # the plain text section
54         #
55         subpart = writer.nextpart()
56         subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
57         pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
58         mimetools.encode(txtin, pout, 'quoted-printable')
59         txtin.close()
60         #
61         # Now that we're done, close our writer and
62         # return the message body
63         #
64         writer.lastpart()
65         msg = out.getvalue()
66         out.close()
67
68         #server = smtplib.SMTP(MTA)
69         #server.sendmail(FROM, (to,cc), msg)
70         #server.quit()
71
72 if __name__=="__main__":
73         import smtplib
74         import emailTxt
75         id = siteId("alice.cs.princeeton.edu")
76         if id:
77                 email('TEST', emailTxt.mailtxt.STANDARD % {'hostname': "ALICE.cs.princeton.edu"}, "tech-" + id + "@sites.planet-lab.org")
78         else:
79                 print "No dice."