Use the correct form for cmp
[build-farm.git] / mail-dead-hosts.pl
1 #!/usr/bin/perl
2 # Samba.org buildfarm
3 # Copyright (C) 2008 Andrew Bartlett <abartlet@samba.org>
4 # Copyright (C) 2008 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 use FindBin qw($RealBin);
21 use POSIX qw(strftime);
22 use hostdb;
23 use Mail::Send;
24 use warnings;
25 use strict;
26
27 my $db = new hostdb("$RealBin/hostdb.sqlite") or die("Unable to connect to host database: $!");
28 my $dry_run = 0;
29
30 my $hosts = $db->dead_hosts(7 * 86400);
31 foreach (@$hosts) {
32         
33         my ($fh, $msg);
34         
35         $db->sent_dead_mail($_->{host}) or die "Could not update 'last dead mail sent' record for $_->{host}";
36
37         # send an e-mail to the owner
38         my $subject = "Your build farm host $_->{host} appears dead";
39         if ($dry_run) {
40                 print "To: $_->{owner} <$_->{owner_email}>\n";
41                 print "Subject: $subject\n";
42                 open(MAIL,"|cat");
43         } else {
44                 $msg = new Mail::Send(Subject=>$subject, To=>"build\@samba.org");
45                 $msg->set("From", "\"Samba Build Farm\" \<build\@samba.org\>");
46                 $fh = $msg->open; 
47         }
48
49         my $last_update;
50         if (defined($_->{last_update})) {
51             $last_update = strftime ("%a %b %e %H:%M:%S %Y", gmtime($_->{last_update}));
52         } else {
53             $last_update = "a long time";
54         }
55
56         my $body = << "__EOF__";        
57 Your host $_->{host} has been part of the Samba Build farm, hosted
58 at http://build.samba.org.
59
60 Sadly however we have not heard from it since $last_update.
61
62 Could you see if something has changed recently, and examine the logs
63 (typically in ~build/build_farm/build.log and ~build/cron.err) to see
64 why we have not heard from your host?
65
66 If you no longer wish your host to participate in the Samba Build
67 Farm, then please let us know so we can remove its records.
68
69 You can see the summary for your host at:
70 http://build.samba.org/?function=View+Host;host=$_->{host}
71
72 Thanks,
73
74 The Build Farm administration team.
75
76 __EOF__
77
78         if ($dry_run) {
79                 print MAIL $body;
80
81                 close(MAIL);
82         } else {
83                 print $fh "$body";
84                 $fh->close;
85         }
86 }
87
88 1;