add missing format string
[amitay/build-farm.git] / mail-dead-hosts.py
1 #!/usr/bin/python
2 # Samba.org buildfarm
3 # Copyright (C) 2008 Andrew Bartlett <abartlet@samba.org>
4 # Copyright (C) 2008-2010 Jelmer Vernooij <jelmer@samba.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 from buildfarm import BuildFarm
21 from buildfarm.web import host_uri
22 import optparse
23 import smtplib
24 from email.MIMEText import MIMEText
25 import time
26
27 parser = optparse.OptionParser()
28 parser.add_option("--dry-run", help="Don't actually send any emails.", action="store_true")
29 (opts, args) = parser.parse_args()
30
31 buildfarm = BuildFarm(timeout=40.0)
32
33 smtp = smtplib.SMTP()
34 smtp.connect()
35
36 hosts = buildfarm.hostdb.dead_hosts(7 * 86400)
37 for host in hosts:
38     if host.last_update:
39         last_update = time.strftime("%a %b %e %H:%M:%S %Y", time.gmtime(host.last_update))
40     else:
41         last_update = "a long time"
42
43     body = """
44 Your host %s has been part of the Samba Build farm, hosted
45 at http://build.samba.org/.
46
47 Sadly however we have not heard from it since %s.
48
49 Could you see if something has changed recently, and examine the logs
50 (typically in ~build/build_farm/build.log and ~build/cron.err) to see
51 why we have not heard from your host?
52
53 If you no longer wish your host to participate in the Samba Build
54 Farm, then please let us know so we can remove its records.
55
56 You can see the summary for your host at: %s
57
58 Thanks,
59
60 The Build Farm administration team.
61
62 """ % (host.name, last_update, host_uri("http://build.samba.org/build.cgi", host.name))
63
64     msg = MIMEText(body)
65
66     # send an e-mail to the owner
67     msg["Subject"] ="Your build farm host %s appears dead" % host.name
68     msg["From"] = "\"Samba Build Farm\" <build@samba.org>"
69     msg["To"] = "\"%s\" <%s>" % host.owner
70     msg["Bcc"] = "\"Samba Build Farm\" <build@samba.org>"
71
72     if opts.dry_run:
73         print msg.as_string()
74     else:
75         smtp.sendmail(msg["From"], [msg["To"], msg["Bcc"]], msg.as_string())
76         host.dead_mail_sent()
77 buildfarm.commit()
78 smtp.quit()