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
26 require "$ENV{BFConfDir}/BuildFarmWeb.pl";
28 die "no dbname" unless $dbname;
29 die "no dbuser" unless $dbuser;
31 # don't use configged dbuser/dbpass
33 $dbuser=""; $dbpass="";
35 my $dsn="dbi:Pg:dbname=$dbname";
36 $dsn .= ";host=$dbhost" if $dbhost;
37 $dsn .= ";port=$dbport" if $dbport;
39 my $db = DBI->connect($dsn,$dbuser,$dbpass);
41 die $DBI::errstr unless $db;
43 my $clear_old = $db->do(q[
47 (SELECT name FROM buildsystems WHERE no_alerts)
51 my $sth = $db->prepare(q[
53 SELECT DISTINCT ON (sysname, branch)
55 extract(epoch from snapshot at time zone 'GMT')::int as snapshot,
57 FROM build_status s join buildsystems b on (s.sysname = b.name)
58 WHERE NOT b.no_alerts and
59 snapshot > current_timestamp - interval '30 days'
60 ORDER BY sysname, branch, snapshot desc
68 while (my $row = $sth->fetchrow_hashref)
70 push(@last_heard, $row);
77 SELECT sysname, branch,
78 extract(epoch from first_alert) as first_alert,
79 extract(epoch from last_notification) as last_notification
84 my $alerts = $db->selectall_hashref($sql,['sysname','branch']);
89 my $clear_sth = $db->prepare(q[
96 my $update_sth = $db->prepare(q[
99 SET last_notification = timestamp '1970-01-01' + ( interval '1 second' * $1)
104 my $insert_sth = $db->prepare(q[
106 INSERT INTO alerts ( sysname, branch, first_alert, last_notification )
108 timestamp '1970-01-01' + ( interval '1 second' * $3),
109 timestamp '1970-01-01' + ( interval '1 second' * $4))
114 my $lts = scalar(localtime);
115 print "starting alert run: $lts\n";
117 foreach my $sysbranch (@last_heard)
119 my $client_conf = thaw $sysbranch->{config};
121 my %client_alert_settings = %{ $client_conf->{alerts} || {} };
122 my $setting = $client_alert_settings{$sysbranch->{branch}};
123 unless ($setting && $setting->{alert_after} && $setting->{alert_every})
125 # if no valid setting, clear any alert and keep going
126 if ($alerts->{$sysbranch->{sysname}}->{$sysbranch->{branch}})
128 $clear_sth->execute($sysbranch->{sysname},$sysbranch->{branch});
129 push(@need_cleared,[$sysbranch]);
133 # ok, we have valid settings. should the alert be on?
134 my $hours_since_heard = ($now - $sysbranch->{snapshot}) / 3600;
137 "have settings for $sysbranch->{sysname}:$sysbranch->{branch} ",
138 "hours since heard = $hours_since_heard, ",
139 "setting = $setting->{alert_after} / $setting->{alert_every} \n";
141 if ($hours_since_heard > $setting->{alert_after})
144 $alerts->{$sysbranch->{sysname}}->{$sysbranch->{branch}};
146 ($now - (3600 * $setting->{alert_every})) >
147 $known_alert->{last_notification})
149 # check if it's too old - 15 days and twice initial seems plenty
150 if ($hours_since_heard > 360 &&
151 $hours_since_heard > 2 * $setting->{alert_after} )
153 print "alert is too old ... giving up\n";
157 # old alert, but time to alert again
158 print "alert is on, but time to alert again\n";
159 $update_sth->execute($now,
160 $sysbranch->{sysname},
161 $sysbranch->{branch},
163 push(@need_alerts,[$sysbranch,$setting]);
164 print "alert updated\n";
166 elsif ( ! $known_alert )
169 print "new alert needed\n";
170 $insert_sth->execute($sysbranch->{sysname},
171 $sysbranch->{branch},
173 print "new record inserted\n";
174 push(@need_alerts,[$sysbranch,$setting]);
177 # nope, so clear the alert if it exists
178 elsif ($alerts->{$sysbranch->{sysname}}->{$sysbranch->{branch}})
180 print "clear exisiting alerts";
181 $clear_sth->execute($sysbranch->{sysname},$sysbranch->{branch});
182 push(@need_cleared,[$sysbranch,$setting]);
187 print "start emails\n";
189 my $addr_sth = $db->prepare(q[
197 my $me = `id -un`; chomp $me;
198 my $host = `hostname`; chomp ($host);
199 $host = $default_host unless ($host =~ m/[.]/ || !defined($default_host));
201 my $from_addr = "PG Build Farm <$me\@$host>";
202 $from_addr =~ tr /\r\n//d;
206 foreach my $clearme (@need_cleared)
208 my ($sysbranch, $setting) = @$clearme;
209 my ($animal, $branch) = ($sysbranch->{sysname},$sysbranch->{branch});
213 my $hours = sprintf("%.2f",($now - $sysbranch->{snapshot}) / 3600);
214 $text = "$sysbranch->{sysname} has now reported " .
215 "on $sysbranch->{branch} $hours hours ago.";
219 $text = "$sysbranch->{sysname} has lost alarm settings on branch: " .
220 "$sysbranch->{branch}. Resetting alarm to off.";
222 my $msg = new Mail::Send;
224 $msg->set('From',$from_addr);
226 $addr_sth->execute($animal);
228 my $mailto = $addr_sth->fetchrow_array;
230 print "sending clear to $mailto\n";
235 $msg->subject("PGBuildfarm member $animal Branch $branch Alert cleared");
237 print $fh "\n\n$text\n";
240 print "alert cleared $animal $branch\n";
243 foreach my $clearme (@need_alerts)
245 my ($sysbranch, $setting) = @$clearme;
246 my ($animal, $branch) = ($sysbranch->{sysname},$sysbranch->{branch});
247 my $hours = sprintf("%.2f",($now - $sysbranch->{snapshot}) / 3600);
248 my $text = "$sysbranch->{sysname} has not reported " .
249 "on $sysbranch->{branch} for $hours hours.";
250 my $msg = new Mail::Send;
252 $msg->set('From',$from_addr);
254 $addr_sth->execute($animal);
256 my ($mailto) = $addr_sth->fetchrow_array;
260 print "sending alert to $mailto\n";
264 $msg->subject("PGBuildfarm member $animal Branch $branch " .
265 "Alert notification");
267 print $fh "\n\n$text\n";
270 print "alert sent $animal $branch\n";
274 print "=================================\n";