3 # Check what packages are needed to get upgraded on all machines
5 # Depends heavily on having ssh key authentication set up to all
6 # boxes. That's why I run it on my own workstation.
8 # Regular debian upgrades are detected by running
10 # on every machine, and parsing the output.
12 # We have decided to maintain some packages manually on some
13 # machines, so that system-level upgrades will not disturb
14 # applications, which may need more hand-holding. These are
15 # extracted from our apt repository, and queried on every
16 # server with apt-cache policy. This way, as soon as a package
17 # is released on our repo, it will get listed here.
19 # 11-Mar-2011 Heikki: Started this
20 # 22-Mar-2011 Heikki: Adding manually maintained packages
21 # 15-Aug-2011 Heikki: Adding a total in the headline, for nagiosgrapher
22 # 21-May-2012 Heikki: Added a date since when a package has been pending
23 # 31-May-2012 Heikki: Pointing to the new wiki
24 # 01-Jan-2013 Heikki: Get hosts from nagios-us as well.
26 # TODO: Assumes that we release our restricted packages for all versions
27 # and architectures at the same time. Gets only the highest version from
28 # all, and reports anything less than this. Good enough for now.
33 my $debug= $ARGV[0] || 0; # 0=none, 1=some, 2=more, 3=much
35 #my $wikilink = 'http://twiki.indexdata.dk/cgi-bin/twiki/view/ID/';
36 my $wikilink = 'https://twiki.indexdata.com/twiki/bin/view/ID/';
37 my $restrictedpackages = "ssh -q kebab cat /home/ftp/pub/debian/dists/*/restricted/*/Packages";
38 my $updlink="<i>-u</i>"; # to display after a name, liking to the upd page
42 "ariel" => "<i>Niels Erik</i> does the manual upgrades",
43 "bellone" => "<i>Niels Erik</i> does the manual upgrades",
44 "cfrepous" => "<i>Wolfram</i> does the manual upgrades",
45 "leopard" => "<i>Wolfram</i> does the manual upgrades",
46 "lsd" => "<i>Heikki</i> takes care of all upgrades",
50 #### Get list of hosts
51 # I could use a hard-coded list, but I would forget to maintain it.
52 # Nagios knows most of our hosts. It even knows which are worth
53 # checking, they have a command to check apts!
54 print "Getting hostlist from nagios\n" if $debug;
55 my $hostlist1 = `ssh nagios grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
56 or die "Could not get host list from nagios (dk)";
58 print "Getting hostlist from nagios-us\n" if $debug;
59 my $hostlist2 = `ssh nagios-us grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
60 or die "Could not get host list from nagios (dk)";
62 my $hostlist = $hostlist1 . $hostlist2;
63 print "Got list:\n$hostlist\n" if $debug>2;
65 ###### Get list of packages that can be manually maintained
66 print "getting restricted package versions\n" if $debug;
68 my $restplines = `$restrictedpackages`
69 or die "Could not get the list of restricted packages " .
70 "from $restrictedpackages: $! ";
71 print "Got package list: \n$restplines\n" if $debug>2;
74 for my $pline ( split("\n",$restplines) ) {
76 $pname = $1 if $pline =~ /^Package:\s+(\S*)\s*$/;
77 $pver = $1 if $pline =~ /^Version:\s+(\S*)\s*$/;
78 print "$pline: p=$pname v=$pver\n" if $debug>2;
79 if ( $pname && $pver ) {
80 print "\nPackage $pname version $pver \n" if $debug>2;
81 if ( ! $restrpkgs{$pname} ) {
82 $restrpkgs{$pname} = $pver;
83 print "found $pname, first version $pver\n" if $debug>1;
85 my $bver = $restrpkgs{$pname};
86 `dpkg --compare-versions "$bver" lt "$pver" 2>/dev/null `;
88 print "found $pname, better version $pver (better than $bver)\n"
90 $restrpkgs{$pname} = $pver;
92 print "found $pname, but version $pver is no better than $bver\n"
96 $pname = ""; # clear for the next one.
101 print "got " . scalar(keys(%restrpkgs)) . " restricted packages\n" if $debug;
103 for $pname ( sort (keys(%restrpkgs)) ) {
104 print " $pname " . $restrpkgs{$pname} . "\n";
110 my ( %sechosts, %secpkgs );
111 my ( %ownhosts, %ownpkgs );
112 my ( %manhosts, %manpkgs );
113 my ( %normhosts, %normpkgs );
124 # Pending modification dates
125 my %olddates; # Read in from the file
126 my %newdates; # To be written in the new version of the file
127 my $datefilename = "aptcheck.data";
128 my $dateoldfilename = "aptcheck.old";
129 my $thisdate = "*"; # indicates really old stuff
130 my $warndate; # Older than this will be boldfaced
132 if ( -f $datefilename ) {
133 print "Reading dates from $datefilename\n" if $debug;
134 open F, $datefilename or die "Could not open date file $datefilename: $!";
137 my ($pkg, $date) = split;
138 next unless $pkg; # skip empty lines
139 $olddates{$pkg} = $date;
140 print "Date for '$pkg' is '$date' \n" if $debug;
143 $thisdate = `date +%F`;
145 $warndate = `date +%F -d "30 days ago"` ; ;
147 print "Dates: now: '$thisdate' warn: '$warndate'\n" if $debug;
149 print "No datefile $datefilename found, starting from scratch\n";
153 my $table = "<table>\n";
155 #for my $hline ( split("\n",$hostlist) ) {
156 for my $hline ( sort( split("\n",$hostlist) ) ) {
157 next unless ( $hline =~ /\/([a-z0-9-]+)\.cfg$/ );
159 next if ($H =~ /^commands/ );
160 next if ($H =~ /^servicegroups/ );
161 print "Checking $H\n" if $debug;
163 my $cmd0 = "cat /etc/debian_version";
164 my $cmd1 = "apt-cache -q policy " . join(" ",sort(keys(%restrpkgs)));
165 my $cmd2 = "apt-get upgrade -s -o 'Debug::NoLocking=true' ";
166 # Note, do not append -qq, we want some output even when nothing to do
167 print "ssh -q $H \"$cmd0; $cmd1 ; $cmd2 \" 2>/dev/null \n" if ($debug>1);
168 my $apt = `ssh -q $H "$cmd0; $cmd1 ; $cmd2 " 2>/dev/null`;
170 $table .= "<tr><td colspan='3'> </td></tr>\n";
171 $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
175 print "Got apts for $H: \n$apt\n" if $debug>2;
176 my $det = ""; # detail lines
185 for my $p ( split("\n",$apt) ) {
186 if ( !$debver ) { # first line
188 $p =~ s/(5[0-9.]+)/$1 LENNY !!!/;
189 $p =~ s/(6[0-9.]+)/$1 squeeze/;
190 $p =~ s/(7[0-9.]+)/$1 wheezy/;
191 $p = " Debian $p";
192 $debversions{$H} = $p;
193 print "Deb version for $H is $p\n" if ($debug>1);
196 # parse apt-cache output
197 $restrname = $1 if $p =~ /^(\S+):$/;
198 $restrinst = $1 if $p =~ /^\s+Installed:\s+(\S+)$/;
199 $restrcand = $1 if $p =~ /^\s+Candidate:\s+(\S+)$/;
200 if ( $p =~ /^\s+Version table:/ ) { # have all for that package
201 my $bver = $restrpkgs{$restrname};
202 if ( ( $restrinst eq $restrcand ) &&
203 ( $restrinst ne $bver ) ) {
204 # if different, it is a regular apt upgrade, and will be seen
205 # later. AND we want to have a different version in our repo
206 `dpkg --compare-versions "$bver" lt "$restrinst" 2>/dev/null`;
207 if ( $? ) { # It was not a downgrade
208 # manual packages may be ahead of the repo!
213 $manpkgs{$restrname} = 1;
215 $det .= "<td> $restrname <b>(M)</b></td>";
216 $det .= "<td>". strdiff($bver,$restrinst)."</td>";
217 $det .= "<td>". strdiff($restrinst,$bver)."</td>";
218 my $datekey = "$H:$restrname";
219 if ( $olddates{$datekey} ) {
220 $newdates{$datekey} = $olddates{$datekey};
222 $newdates{$datekey} = $thisdate;
224 my $dispdate = $newdates{$datekey};
225 # if ( $dispdate lt $warndate ) {
226 if ( 0 ) { # manual packages don't need to be highlighted
227 $dispdate = "<b>$dispdate !</b>";
229 $det .= "<td>" . $dispdate . "</td>";
231 my $key = "$restrname";
232 if ( !$summary{$key} ) {
235 $summary{$key} .= "$H ";
238 $restrname = ""; # clear for next round
243 /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
244 my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
245 print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
246 $det .= "<tr><td> ";
249 if ( $src =~ /Security/ ) {
250 $det .= "<b>$pkg (s)</b> ";
255 } elsif ( $src =~ /Indexdata/ ) {
256 $det .= "<i><b>$pkg</b> (id) </i>";
267 if ( !$summary{$key} ) {
270 $summary{$key} .= "$H ";
271 $new = strdiff($cur,$new);
272 $cur = strdiff($new,$cur);
274 $det .= "<td>$cur</td> ";
275 $det .= "<td>$new</td> ";
276 my $datekey = "$H:$pkg";
277 if ( $olddates{$datekey} ) {
278 $newdates{$datekey} = $olddates{$datekey};
280 $newdates{$datekey} = $thisdate;
282 my $dispdate = $newdates{$datekey};
283 if ( ( $dispdate lt $warndate ) && ( $src =~ /Security/) ) {
284 $dispdate = "<b>$dispdate !</b>";
286 $det .= "<td>" . $dispdate . "</td>";
290 $table .= "<tr><td colspan='4'> </td></tr>\n";
291 $table .= "<tr><td colspan='4'><a name='$H'><b><u>$H</u></b></a> \n";
293 $table .= "<b>$pkgs</b> packages to upgrade. ";
294 $table .= "<b>$secs security</b>. " if $secs;
295 $table .= " $own from indexdata. " if $own;
296 $table .= " $man manual. " if $man;
301 my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
302 # Fix some pages that do not follow the convention.
303 # Mostly because the host names would not make proper WikiWords
304 $updlink =~ s/Bugzilla3Updates/BugzillaUpdates/;
305 $updlink =~ s/Opencontent-solrUpdates/OpenContentSolrUpdates/;
306 $updlinks{$H} = $updlink;
307 $table .= " <a href='$updlink' >Upd</a>";
308 $table .= " " . $debversions{$H};
309 $table .= "</td></tr>\n";
310 $table .= "<tr><td>$hostcomments{$H}</td></tr>\n"
311 if ( $hostcomments{$H} );
312 $table .= $det if $pkgs;
313 print "\n$table\n" if $debug>2;
314 last if $H =~/diane/ && $debug;
316 $table .= "</table>\n";
320 `mv -f $datefilename $dateoldfilename`;
321 open F, ">$datefilename" or die "Could not open date file $datefilename for writing";
322 for my $k (sort(keys(%newdates)) ) {
323 print F "$k " . $newdates{$k}. "\n";
324 print "date for '$k' '" . $newdates{$k}. "'\n" if $debug;
327 or die "Could not close date file $datefilename: $!";
329 print "Not updating the date file, this is a debug run\n";
333 my $outfile = "/tmp/aptcheck.html";
335 or die "Could not open $outfile for writing: $!";
337 print F "<head><title>Apt upgrade status</title></head>\n";
339 print F "<H1>Apt package status</H1>\n";
340 print F "<b>" . ( $sectot + $owntot + $mantot + $normtot ) .
341 "</b> packages pending (<b>$sectot</b> critical) \n";
343 print F "<H2>Debug run, many hosts missing!</H2>\n"
347 # Summary table: one row for per host group
349 print F "<table border='1' >\n";
350 print F "<tr><td> </td>" ;
351 print F "<td><b>Hosts</b></td>\n";
352 print F "<td><b>Packages</b></td></tr>\n";
355 print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) .
356 " / " . scalar(keys(%secpkgs)) . " / $sectot </td>\n" ;
358 for my $HH ( sort(keys(%sechosts)) ) {
359 my $upd = $updlinks{$HH} || "#" ;
360 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>$updlink</a> ";
364 for my $PP ( sort(keys(%secpkgs)) ) {
365 print F "<a href='#$PP'>$PP</a> ";
371 print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) .
372 " / " . scalar(keys(%ownpkgs)) . " / $owntot </td>\n" ;
374 for my $HH ( sort(keys(%ownhosts)) ) {
375 my $upd = $updlinks{$HH} || "#" ;
376 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>$updlink</a> ";
377 #print F "<a href='#$HH'><b>$HH</b></a> ";
381 for my $PP ( sort(keys(%ownpkgs)) ) {
382 print F "<a href='#$PP'>$PP</a> ";
388 print F "<tr><td><b>Manual</b><br/>" . scalar(keys(%manhosts)) .
389 " / " . scalar(keys(%manpkgs)) . " / $mantot </td>\n" ;
391 for my $HH ( sort(keys(%manhosts)) ) {
392 my $upd = $updlinks{$HH} || "#" ;
393 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>$updlink</a> ";
394 #print F "<a href='#$HH'><b>$HH</b></a> ";
398 for my $PP ( sort(keys(%manpkgs)) ) {
399 print F "<a href='#$PP'>$PP</a> ";
405 print F "<tr><td>Normal<br/>" . scalar(keys(%normhosts)) .
406 " / " . scalar(keys(%normpkgs)) . " / $normtot </td>\n" ;
408 for my $HH ( sort(keys(%normhosts)) ) {
409 my $upd = $updlinks{$HH} || "#" ;
410 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>$updlink</a> ";
411 #print F "<a href='#$HH'><b>$HH</b></a> ";
415 for my $PP ( sort(keys(%normpkgs)) ) {
416 print F "<a href='#$PP'>$PP</a> ";
422 print F "<tr><td>Skipped " . scalar(keys(%skiphosts)) . "</td>\n";
423 print F "<td colspan='2'>";
424 for my $HH ( sort(keys(%skiphosts)) ) {
425 my $upd = $updlinks{$HH} ||
426 $wikilink . ucfirst($HH) . "Updates" . $year;
427 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>$updlink</a> ";
428 #print F "<a href='#$HH'><b>$HH</b></a> ";
430 print F "</td></tr>\n";
433 print F "<tr><td>Ok <br/>" . scalar(keys(%okhosts)) .
434 " of " . scalar(keys(%allhosts)) . "</td>\n";
435 print F "<td colspan='2'>";
436 for my $HH ( sort(keys(%okhosts)) ) {
437 my $upd = $updlinks{$HH} || "#" ;
438 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>$updlink</a> ";
439 #print F "<a href='#$HH'><b>$HH</b></a> ";
442 print F "<b>None at all!</b>";
444 print F "</td></tr>\n";
446 print F "</table>\n";
448 print F "<p/><b>" . ( $sectot + $owntot + $mantot + $normtot ) .
449 "</b> packages pending (<b>$sectot</b> critical) \n";
452 #my $secs = 60*60*24 * 7 * 2; # 2 weeks in secods
453 #my $secs = "1m"; # one month, let nagios do the math
456 "<a href='http://nagios.indexdata.com/cgi-bin/nagios3/graphs.cgi?" .
457 "host=nagios&service=Apt%20Summary'>\n".
458 "<img src='http://nagios.indexdata.com/" .
459 "cgi-bin/nagios3/rrd2-system.cgi?" .
460 "host=nagios&service=Apt%20Summary&" .
462 "width=800&height=100&type=AVERAGE' /> ".
470 print F "<p/><b><u>Packages</u></b>\n";
472 for my $P ( sort(keys(%summary)) ) {
474 $PN = "<b>$P (s)</b>" if ($secpkgs{$P});
475 $PN = "<i>$P (id)</i>" if ($ownpkgs{$P});
476 $PN = "$P <b>(M)</b>" if ($manpkgs{$P});
477 print F "<tr><td><a name='$P'/>$PN</td>\n";
479 for my $HH ( split(' ',$summary{$P} )) {
480 print F "<a href=#$HH>$HH</a> ";
485 print F "</table>\n";
487 print F "<p/>Produced " . `date`.
488 " on " . `hostname` . " by " . `whoami` .
490 print F "</body></html>\n";
493 or die "Could not close $outfile: $!";
495 system "scp -q $outfile nagios:/var/www/heikki/index.html";
499 # Helper to take two strings and highligt that part of the second
500 # that is different from the first.
504 print "strdiff: '$x' '$y' \n" if $debug>2;
506 return "$x <b>??</b>";
509 while ( $a < length($y) &&
510 substr($x,$a,1) eq substr($y,$a,1) ) {
513 if ( $a == length($y) ) {
517 while ( $b < length($y)-$a &&
518 substr($x,-$b,1) eq substr($y, -$b,1) ) {
521 my $c = length($y) - $b +1;
522 print "strdiff: a=$a " . substr($y,0,$a) ."\n" if $debug>2;
523 print "strdiff: b=$b " . "\n" if $debug>2;
524 print "strdiff: c=$c " . substr($y,$c) ."\n" if $debug>2;
525 print "strdiff: " . substr($y,$a, $c-$a) ."\n" if $debug>2;
526 my $z = substr($y,0,$a) .
527 "<b>" . substr($y,$a, $c-$a) . "</b>" .
529 print "strdiff: " . $z ."\n" if $debug>2;
530 print "\n" if $debug>2;