3 # dqueued-watcher -- for regularily watching the queue daemon
5 # This script is intended to check periodically (e.g. started by cron) that
6 # everything is ok with debianqueued. If the daemon isn't running, it notifies
7 # the maintainer. It also checks if a new Debian keyring is available (in a
8 # Debian mirror aera, f.i.) and then updates the keyring used by debianqueued.
10 # Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
12 # This program is free software. You can redistribute it and/or
13 # modify it under the terms of the GNU General Public License as
14 # published by the Free Software Foundation: either version 2 or
15 # (at your option) any later version.
16 # This program comes with ABSOLUTELY NO WARRANTY!
18 # $Id: dqueued-watcher,v 1.28 1999/07/08 09:43:22 ftplinux Exp $
20 # $Log: dqueued-watcher,v $
21 # Revision 1.28 1999/07/08 09:43:22 ftplinux
22 # Bumped release number to 0.9
24 # Revision 1.27 1999/07/07 11:58:22 ftplinux
25 # Also update gpg keyring if $conf::gpg_keyring is set.
27 # Revision 1.26 1998/07/06 14:24:36 ftplinux
28 # Some changes to handle debian-keyring.tar.gz files which expand to a
29 # directory including a date.
31 # Revision 1.25 1998/05/14 14:21:45 ftplinux
32 # Bumped release number to 0.8
34 # Revision 1.24 1998/03/30 12:31:05 ftplinux
35 # Don't count "already reported" or "ignored for now" errors as .changes errors.
36 # Also list files for several error types.
37 # Also print out names of processed jobs.
39 # Revision 1.23 1998/03/30 11:27:37 ftplinux
40 # If called with args, make summaries for the log files given.
41 # make_summary: New arg $to_stdout, for printing report directly.
43 # Revision 1.22 1998/03/23 14:05:15 ftplinux
44 # Bumped release number to 0.7
46 # Revision 1.21 1997/12/16 13:19:29 ftplinux
47 # Bumped release number to 0.6
49 # Revision 1.20 1997/11/20 15:18:48 ftplinux
50 # Bumped release number to 0.5
52 # Revision 1.19 1997/10/31 12:26:31 ftplinux
53 # Again added new counters in make_summary: suspicious_files,
54 # transient_changes_errs.
55 # Extended tests for bad_changes.
56 # Quotes in pattern seem not to work, replaced by '.'.
58 # Revision 1.18 1997/10/30 14:17:32 ftplinux
59 # In make_summary, implemented some new counters for command files.
61 # Revision 1.17 1997/10/17 09:39:09 ftplinux
62 # Fixed wrong args to plural_s
64 # Revision 1.16 1997/09/25 11:20:42 ftplinux
65 # Bumped release number to 0.4
67 # Revision 1.15 1997/09/17 12:16:33 ftplinux
68 # Added writing summaries to a file
70 # Revision 1.14 1997/09/16 11:39:29 ftplinux
71 # In make_summary, initialize all counters to avoid warnings about uninited
74 # Revision 1.13 1997/09/16 10:53:36 ftplinux
75 # Made logging more verbose in queued and dqueued-watcher
77 # Revision 1.12 1997/08/18 13:07:15 ftplinux
78 # Implemented summary mails
80 # Revision 1.11 1997/08/18 12:11:44 ftplinux
81 # Replaced timegm by timelocal in parse_date; times in log file are
84 # Revision 1.10 1997/08/18 11:27:20 ftplinux
85 # Revised age calculation of log file for rotating
87 # Revision 1.9 1997/08/12 09:54:40 ftplinux
88 # Bumped release number
90 # Revision 1.8 1997/08/11 12:49:10 ftplinux
91 # Implemented logfile rotating
93 # Revision 1.7 1997/07/28 13:20:38 ftplinux
94 # Added release numner to startup message
96 # Revision 1.6 1997/07/25 10:23:04 ftplinux
97 # Made SIGCHLD handling more portable between perl versions
99 # Revision 1.5 1997/07/09 10:13:55 ftplinux
100 # Alternative implementation of status file as plain file (not FIFO), because
101 # standard wu-ftpd doesn't allow retrieval of non-regular files. New config
102 # option $statusdelay for this.
104 # Revision 1.4 1997/07/08 08:39:56 ftplinux
105 # Need to remove -z from tar options if --use-compress-program
107 # Revision 1.3 1997/07/08 08:34:15 ftplinux
108 # If dqueued-watcher runs as cron job, $PATH might not contain gzip. Use extra
109 # --use-compress-program option to tar, and new config var $gzip.
111 # Revision 1.2 1997/07/03 13:05:57 ftplinux
112 # Added some verbosity if stdin is a terminal
114 # Revision 1.1.1.1 1997/07/03 12:54:59 ftplinux
115 # Import initial sources
122 require "timelocal.pl";
125 my $batchmode = !(-t STDIN);
126 $main::curr_year = (localtime)[5];
130 ($version = 'Release: 0.9 $Revision: 1.28 $ $Date: 1999/07/08 09:43:22 $ $Author: ftplinux $') =~ s/\$ ?//g;
131 print "dqueued-watcher $version\n" if !$batchmode;
135 ($conf::queued_dir = (($0 !~ m,^/,) ? POSIX::getcwd()."/" : "") . $0)
137 require "$conf::queued_dir/config";
138 my # avoid spurious warnings about unused vars
140 $junk = $conf::maintainer_mail;
141 $junk = $conf::log_age;
146 sub daemon_running();
150 sub make_summary($$$);
160 # with arguments, make summaries (to stdout) for the logfiles given
162 make_summary( 1, undef, $_ );
166 # without args, just do normal maintainance actions
174 # check if the daemon is running, notify maintainer if not
177 my $daemon_down_text = "Daemon is not running\n";
178 my( $line, $reported );
180 if (daemon_running()) {
181 print "Daemon is running\n" if !$batchmode;
184 print "Daemon is NOT running!\n" if !$batchmode;
187 if ($conf::statusfile && -f $conf::statusfile && ! -p _ &&
188 open( STATUSFILE, "<$conf::statusfile" )) {
189 $line = <STATUSFILE>;
191 $reported = $line eq $daemon_down_text;
194 mail( "debianqueued down",
195 "The Debian queue daemon isn't running!\n",
196 "Please start it up again.\n" );
197 logger( "Found that daemon is not running\n" );
200 # remove unnecessary pid file
201 # also remove status FIFO, so opening it for reading won't block
203 unlink( $conf::pidfile, $conf::statusfile );
205 # replace status FIFO by a file that tells the user the daemon is down
206 if ($conf::statusfile) {
207 open( STATUSFILE, ">$conf::statusfile" )
208 or die "Can't open $conf::statusfile: $!\n";
209 print STATUSFILE $daemon_down_text;
215 # check if daemon is running
217 sub daemon_running() {
221 if (open( PIDFILE, "<$conf::pidfile" )) {
222 chomp( $pid = <PIDFILE> );
224 $main::daemon_pid = $pid, return 1 if $pid && kill( 0, $pid );
230 # check if new keyring is available, if yes extract it
234 my( $first_date, $f1, $f2, $i );
237 return if !defined $main::daemon_pid || !-f $conf::logfile;
239 open( F, "<$conf::logfile" ) or die "Can't open $conf::logfile: $!\n";
241 last if $first_date = parse_date( $_ );
244 # Simply don't rotate if nothing couldn't be parsed as date -- probably
246 return if !$first_date;
247 # assume year-wrap if $first_date is in the future
248 $first_date -= 365*24*60*60 if $first_date > time;
249 # don't rotate if first date too young
250 return if time - $first_date < $conf::log_age*24*60*60;
251 logger( "Logfile older than $conf::log_age days, rotating\n" );
254 $f1 = logf($conf::log_keep-1);
256 unlink( $f1 ) or warn "Can't remove $f1: $!\n";
260 for( $i = $conf::log_keep-2; $i > 0; --$i ) {
266 rename( $f1, $f2 ) or warn "Can't rename $f1 to $f2: $!\n";
270 # compress newest log
271 $f1 = "$conf::logfile.0";
272 $f2 = "$conf::logfile.1.gz";
274 system $conf::gzip, "-9f", $f1
275 and die "gzip failed on $f1 (status $?)\n";
276 rename( "$f1.gz", $f2 ) or warn "Can't rename $f1.gz to $f2: $!\n";
279 # rename current log and signal the daemon to open a new logfile
280 rename( $conf::logfile, $f1 );
281 kill( 1, $main::daemon_pid );
283 print "Rotated log files\n" if !$batchmode;
284 make_summary( 0, $first_date, $f1 )
285 if $conf::mail_summary || $conf::summary_file;
290 return sprintf( "$conf::logfile.%d.gz", $num );
295 my( $mon, $day, $hours, $mins, $month, $year, $secs );
296 my %month_num = ( "jan", 0, "feb", 1, "mar", 2, "apr", 3, "may", 4,
297 "jun", 5, "jul", 6, "aug", 7, "sep", 8, "oct", 9,
298 "nov", 10, "dec", 11 );
300 warn "Invalid date: $date\n", return 0
301 unless $date =~ /^(\w\w\w)\s+(\d+)\s+(\d+):(\d+):(\d+)\s/;
302 ($mon, $day, $hours, $mins, $secs) = ($1, $2, $3, $4, $5);
305 return 0 if !exists $month_num{$mon};
306 $month = $month_num{$mon};
307 return timelocal( $secs, $mins, $hours, $day, $month, $main::curr_year );
310 sub make_summary($$$) {
311 my $to_stdout = shift;
312 my $startdate = shift;
314 my( $starts, $statusd_starts, $suspicious_files, $transient_errs,
315 $upl_failed, $success, $commands, $rm_cmds, $mv_cmds, $msg,
317 my( @pgp_fail, %transient_errs, @changes_errs, @removed_changes,
318 @already_present, @del_stray, %uploaders, %cmd_uploaders );
321 if (!open( F, "<$file" )) {
322 mail( "debianqueued summary failed",
323 "Couldn't open $file to make summary of events." );
327 $starts = $statusd_starts = $suspicious_files = $transient_errs =
328 $upl_failed = $success = $commands = $rm_cmds = $mv_cmds = 0;
330 $startdate = parse_date( $_ ) if !$startdate;
331 ++$starts if /daemon \(pid \d+\) started$/;
332 ++$statusd_starts if /forked status daemon/;
333 push( @pgp_fail, $1 )
334 if /PGP signature check failed on (\S+)/;
335 ++$suspicious_files if /found suspicious filename/;
336 ++$transient_errs, ++$transient_errs{$1}
337 if /(\S+) (doesn.t exist|is too small) \(ignored for now\)/;
338 push( @changes_errs, $1 )
339 if (!/\((already reported|ignored for now)\)/ &&
340 (/(\S+) doesn.t exist/ || /(\S+) has incorrect (size|md5)/)) ||
341 /(\S+) doesn.t contain a Maintainer: field/ ||
342 /(\S+) isn.t signed with PGP/ ||
343 /(\S+) doesn.t mention any files/;
344 push( @removed_changes, $1 )
345 if /(\S+) couldn.t be processed for \d+ hours and is now del/ ||
346 /(\S+) couldn.t be uploaded for \d+ times/;
347 push( @already_present, $1 )
348 if /(\S+) is already present on master/;
349 ++$upl_failed if /Upload to \S+ failed/;
350 ++$success, push( @{$uploaders{$2}}, $1 )
351 if /(\S+) processed successfully \(uploader (\S*)\)$/;
352 push( @del_stray, $1 ) if /Deleted stray file (\S+)/;
353 ++$commands if /processing .*\.commands$/;
354 ++$rm_cmds if / > rm /;
355 ++$mv_cmds if / > mv /;
357 if /\(command uploader (\S*)\)$/;
361 $msg .= "Queue Daemon Summary from " . localtime($startdate) . " to " .
362 localtime(time) . ":\n\n";
364 $msg .= "Daemon started ".stimes($starts)."\n"
366 $msg .= "Status daemon restarted ".stimes($statusd_starts-$starts)."\n"
367 if $statusd_starts > $starts;
368 $msg .= @pgp_fail." job".plural_s(@pgp_fail)." failed PGP check:\n" .
369 format_list(2,@pgp_fail)
371 $msg .= "$suspicious_files file".plural_s($suspicious_files)." with ".
372 "suspicious names found\n"
373 if $suspicious_files;
374 $msg .= "Detected ".$transient_errs." transient error".
375 plural_s($transient_errs)." in .changes files:\n".
376 format_list(2,keys %transient_errs)
378 $msg .= "Detected ".@changes_errs." error".plural_s(@changes_errs).
379 " in .changes files:\n".format_list(2,@changes_errs)
381 $msg .= @removed_changes." job".plural_s(@removed_changes).
382 " removed due to persistent errors:\n".
383 format_list(2,@removed_changes)
385 $msg .= @already_present." job".plural_s(@already_present).
386 " were already present on master:\n".format_list(2,@already_present)
388 $msg .= @del_stray." stray file".plural_s(@del_stray)." deleted:\n".
389 format_list(2,@del_stray)
391 $msg .= "$commands command file".plural_s($commands)." processed\n"
393 $msg .= " ($rm_cmds rm, $mv_cmds mv commands)\n"
394 if $rm_cmds || $mv_cmds;
395 $msg .= "$success job".plural_s($success)." processed successfully\n";
398 $msg .= "\nPeople who used the queue:\n";
399 foreach $uploader ( keys %uploaders ) {
400 $msg .= " $uploader (".@{$uploaders{$uploader}}."):\n".
401 format_list(4,@{$uploaders{$uploader}});
405 if (%cmd_uploaders) {
406 $msg .= "\nPeople who used command files:\n";
407 foreach $uploader ( keys %cmd_uploaders ) {
408 $msg .= " $uploader ($cmd_uploaders{$uploader})\n";
416 if ($conf::mail_summary) {
417 mail( "debianqueued summary", $msg );
420 if ($conf::summary_file) {
422 open( F, ">>$conf::summary_file" ) or
423 die "Cannot open $conf::summary_file for appending: $!\n";
424 print F "\n", "-"x78, "\n", $msg;
432 return $num == 1 ? "once" : "$num times";
437 return $num == 1 ? "" : "s";
440 sub format_list($@) {
442 my( $i, $pos, $ret, $item, $len );
444 $ret = " " x $indent; $pos += $indent;
445 while( $item = shift ) {
446 $len = length($item);
447 $item .= ", ", $len += 2 if @_;
448 if ($pos+$len > LINEWIDTH) {
449 $ret .= "\n" . " "x$indent;
460 # send mail to maintainer
466 open( MAIL, "|$conf::mail -s '$subject' '$conf::maintainer_mail'" )
467 or (warn( "Could not open pipe to $conf::mail: $!\n" ), return);
469 print MAIL "\nGreetings,\n\n\tYour Debian queue daemon watcher\n";
471 or warn( "$conf::mail failed (exit status $?)\n" );
475 # log something to logfile
478 my $now = format_time();
481 if (!open( LOG, ">>$conf::logfile" )) {
482 warn( "Can't open $conf::logfile\n" );
485 print LOG "$now dqueued-watcher: ", @_;
490 # return current time as string
495 # omit weekday and year for brevity
496 ($t = localtime) =~ /^\w+\s(.*)\s\d+$/;