5 Copyright (c) 2003-2010, Andrew Dunstan
7 See accompanying License file for license details
13 use Digest::SHA1 qw(sha1_hex);
19 use Storable qw(thaw);
21 use vars qw($dbhost $dbname $dbuser $dbpass $dbport
22 $all_stat $fail_stat $change_stat $green_stat
27 use FindBin qw($RealBin);
28 require "$RealBin/BuildFarmWeb.pl";
30 die "no dbname" unless $dbname;
31 die "no dbuser" unless $dbuser;
33 # don't use configged dbuser/dbpass
35 $dbuser=""; $dbpass="";
37 my $dsn="dbi:Pg:dbname=$dbname";
38 $dsn .= ";host=$dbhost" if $dbhost;
39 $dsn .= ";port=$dbport" if $dbport;
41 my $db = DBI->connect($dsn,$dbuser,$dbpass);
43 die $DBI::errstr unless $db;
45 my $clear_old = $db->do(q[
49 (SELECT name FROM buildsystems WHERE no_alerts)
53 my $sth = $db->prepare(q[
55 SELECT DISTINCT ON (sysname, branch)
57 extract(epoch from snapshot at time zone 'GMT')::int as snapshot,
59 FROM build_status s join buildsystems b on (s.sysname = b.name)
60 WHERE NOT b.no_alerts and
61 snapshot > current_timestamp - interval '30 days'
62 ORDER BY sysname, branch, snapshot desc
70 while (my $row = $sth->fetchrow_hashref)
72 push(@last_heard, $row);
79 SELECT sysname, branch,
80 extract(epoch from first_alert) as first_alert,
81 extract(epoch from last_notification) as last_notification
86 my $alerts = $db->selectall_hashref($sql,['sysname','branch']);
91 my $clear_sth = $db->prepare(q[
98 my $update_sth = $db->prepare(q[
101 SET last_notification = timestamp '1970-01-01' + ( interval '1 second' * $1)
106 my $insert_sth = $db->prepare(q[
108 INSERT INTO alerts ( sysname, branch, first_alert, last_notification )
110 timestamp '1970-01-01' + ( interval '1 second' * $3),
111 timestamp '1970-01-01' + ( interval '1 second' * $4))
116 my $lts = scalar(localtime);
117 print "starting alert run: $lts\n";
119 foreach my $sysbranch (@last_heard)
121 my $client_conf = thaw $sysbranch->{config};
123 my %client_alert_settings = %{ $client_conf->{alerts} || {} };
124 my $setting = $client_alert_settings{$sysbranch->{branch}};
125 unless ($setting && $setting->{alert_after} && $setting->{alert_every})
127 # if no valid setting, clear any alert and keep going
128 if ($alerts->{$sysbranch->{sysname}}->{$sysbranch->{branch}})
130 $clear_sth->execute($sysbranch->{sysname},$sysbranch->{branch});
131 push(@need_cleared,[$sysbranch]);
135 # ok, we have valid settings. should the alert be on?
136 my $hours_since_heard = ($now - $sysbranch->{snapshot}) / 3600;
139 "have settings for $sysbranch->{sysname}:$sysbranch->{branch} ",
140 "hours since heard = $hours_since_heard, ",
141 "setting = $setting->{alert_after} / $setting->{alert_every} \n";
143 if ($hours_since_heard > $setting->{alert_after})
146 $alerts->{$sysbranch->{sysname}}->{$sysbranch->{branch}};
148 ($now - (3600 * $setting->{alert_every})) >
149 $known_alert->{last_notification})
151 # check if it's too old - 15 days and twice initial seems plenty
152 if ($hours_since_heard > 360 &&
153 $hours_since_heard > 2 * $setting->{alert_after} )
155 print "alert is too old ... giving up\n";
159 # old alert, but time to alert again
160 print "alert is on, but time to alert again\n";
161 $update_sth->execute($now,
162 $sysbranch->{sysname},
163 $sysbranch->{branch},
165 push(@need_alerts,[$sysbranch,$setting]);
166 print "alert updated\n";
168 elsif ( ! $known_alert )
171 print "new alert needed\n";
172 $insert_sth->execute($sysbranch->{sysname},
173 $sysbranch->{branch},
175 print "new record inserted\n";
176 push(@need_alerts,[$sysbranch,$setting]);
179 # nope, so clear the alert if it exists
180 elsif ($alerts->{$sysbranch->{sysname}}->{$sysbranch->{branch}})
182 print "clear exisiting alerts";
183 $clear_sth->execute($sysbranch->{sysname},$sysbranch->{branch});
184 push(@need_cleared,[$sysbranch,$setting]);
189 print "start emails\n";
191 my $addr_sth = $db->prepare(q[
199 my $me = `id -un`; chomp $me;
200 my $host = `hostname`; chomp ($host);
201 $host = $default_host unless ($host =~ m/[.]/ || !defined($default_host));
203 my $from_addr = "Exim BuildFarm <$me\@$host>";
204 $from_addr =~ tr /\r\n//d;
208 foreach my $clearme (@need_cleared)
210 my ($sysbranch, $setting) = @$clearme;
211 my ($animal, $branch) = ($sysbranch->{sysname},$sysbranch->{branch});
215 my $hours = sprintf("%.2f",($now - $sysbranch->{snapshot}) / 3600);
216 $text = "$sysbranch->{sysname} has now reported " .
217 "on $sysbranch->{branch} $hours hours ago.";
221 $text = "$sysbranch->{sysname} has lost alarm settings on branch: " .
222 "$sysbranch->{branch}. Resetting alarm to off.";
224 my $msg = new Mail::Send;
226 $msg->set('From',$from_addr);
228 $addr_sth->execute($animal);
230 my $mailto = $addr_sth->fetchrow_array;
232 print "sending clear to $mailto\n";
237 $msg->subject("Exim BuildFarm member $animal Branch $branch Alert cleared");
239 print $fh "\n\n$text\n";
242 print "alert cleared $animal $branch\n";
245 foreach my $needme (@need_alerts)
247 my ($sysbranch, $setting) = @$needme;
248 my ($animal, $branch) = ($sysbranch->{sysname},$sysbranch->{branch});
249 my $hours = sprintf("%.2f",($now - $sysbranch->{snapshot}) / 3600);
250 my $text = "$sysbranch->{sysname} has not reported " .
251 "on $sysbranch->{branch} for $hours hours.";
252 my $msg = new Mail::Send;
254 $msg->set('From',$from_addr);
256 $addr_sth->execute($animal);
258 my ($mailto) = $addr_sth->fetchrow_array;
262 print "sending alert to $mailto\n";
266 $msg->subject("Exim BuildFarm member $animal Branch $branch " .
267 "Alert notification");
269 print $fh "\n\n$text\n";
272 print "alert sent $animal $branch\n";
276 print "=================================\n";