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.
30 # TODO: Get the dates from ls --full-time /var/cache/apt/archives/
31 # and display next to the packages, so we can see how long they have
32 # been lingering. Boldface them if older than some limit
36 my $debug= $ARGV[0] || 0; # 0=none, 1=some, 2=more, 3=much
38 #my $wikilink = 'http://twiki.indexdata.dk/cgi-bin/twiki/view/ID/';
39 my $wikilink = 'https://twiki.indexdata.com/twiki/bin/view/ID/';
40 my $restrictedpackages = "ssh -q kebab cat /home/ftp/pub/debian/dists/*/restricted/*/Packages";
44 "ariel" => "<i>Niels Erik</i> does the manual upgrades",
45 "bellone" => "<i>Niels Erik</i> does the manual upgrades",
46 "cfrepous" => "<i>Wolfram</i> does the manual upgrades",
47 "leopard" => "<i>Wolfram</i> does the manual upgrades",
48 "lsd" => "<i>Heikki</i> takes care of all upgrades",
52 #### Get list of hosts
53 # I could use a hard-coded list, but I would forget to maintain it.
54 # Nagios knows most of our hosts. It even knows which are worth
55 # checking, they have a command to check apts!
56 print "Getting hostlist from nagios\n" if $debug;
57 my $hostlist1 = `ssh nagios grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
58 or die "Could not get host list from nagios (dk)";
60 print "Getting hostlist from nagios-us\n" if $debug;
61 my $hostlist2 = `ssh nagios-us grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
62 or die "Could not get host list from nagios (dk)";
64 my $hostlist = $hostlist1 . $hostlist2;
65 print "Got list:\n$hostlist\n" if $debug>2;
67 ###### Get list of packages that can be manually maintained
68 print "getting restricted package versions\n" if $debug;
70 my $restplines = `$restrictedpackages`
71 or die "Could not get the list of restricted packages " .
72 "from $restrictedpackages: $! ";
73 print "Got package list: \n$restplines\n" if $debug>2;
76 for my $pline ( split("\n",$restplines) ) {
78 $pname = $1 if $pline =~ /^Package:\s+(\S*)\s*$/;
79 $pver = $1 if $pline =~ /^Version:\s+(\S*)\s*$/;
80 print "$pline: p=$pname v=$pver\n" if $debug>2;
81 if ( $pname && $pver ) {
82 print "\nPackage $pname version $pver \n" if $debug>2;
83 if ( ! $restrpkgs{$pname} ) {
84 $restrpkgs{$pname} = $pver;
85 print "found $pname, first version $pver\n" if $debug>1;
87 my $bver = $restrpkgs{$pname};
88 `dpkg --compare-versions "$bver" lt "$pver" 2>/dev/null `;
90 print "found $pname, better version $pver (better than $bver)\n"
92 $restrpkgs{$pname} = $pver;
94 print "found $pname, but version $pver is no better than $bver\n"
98 $pname = ""; # clear for the next one.
103 print "got " . scalar(keys(%restrpkgs)) . " restricted packages\n" if $debug;
105 for $pname ( sort (keys(%restrpkgs)) ) {
106 print " $pname " . $restrpkgs{$pname} . "\n";
112 my ( %sechosts, %secpkgs );
113 my ( %ownhosts, %ownpkgs );
114 my ( %manhosts, %manpkgs );
115 my ( %normhosts, %normpkgs );
126 # Pending modification dates
127 my %olddates; # Read in from the file
128 my %newdates; # To be written in the new version of the file
129 my $datefilename = "aptcheck.data";
130 my $dateoldfilename = "aptcheck.old";
131 my $thisdate = "*"; # indicates really old stuff
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`;
146 print "No datefile $datefilename found, starting from scratch\n";
150 my $table = "<table>\n";
152 for my $hline ( split("\n",$hostlist) ) {
153 next unless ( $hline =~ /\/([a-z0-9-]+)\.cfg$/ );
155 next if ($H =~ /^commands/ );
156 next if ($H =~ /^servicegroups/ );
157 print "Checking $H\n" if $debug;
159 my $cmd0 = "cat /etc/debian_version";
160 my $cmd1 = "apt-cache -q policy " . join(" ",sort(keys(%restrpkgs)));
161 my $cmd2 = "apt-get upgrade -s -o 'Debug::NoLocking=true' ";
162 # Note, do not append -qq, we want some output even when nothing to do
163 print "ssh -q $H \"$cmd0; $cmd1 ; $cmd2 \" 2>/dev/null \n" if ($debug>1);
164 my $apt = `ssh -q $H "$cmd0; $cmd1 ; $cmd2 " 2>/dev/null`;
166 $table .= "<tr><td colspan='3'> </td></tr>\n";
167 $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
171 print "Got apts for $H: \n$apt\n" if $debug>2;
172 my $det = ""; # detail lines
181 for my $p ( split("\n",$apt) ) {
182 if ( !$debver ) { # first line
184 $p =~ s/(5[0-9.]+)/$1 LENNY !!!/;
185 $p =~ s/(6[0-9.]+)/$1 squeeze/;
186 $p =~ s/(7[0-9.]+)/$1 wheezy/;
187 $p = " Debian $p";
188 $debversions{$H} = $p;
189 print "Deb version for $H is $p\n" if ($debug>1);
192 # parse apt-cache output
193 $restrname = $1 if $p =~ /^(\S+):$/;
194 $restrinst = $1 if $p =~ /^\s+Installed:\s+(\S+)$/;
195 $restrcand = $1 if $p =~ /^\s+Candidate:\s+(\S+)$/;
196 if ( $p =~ /^\s+Version table:/ ) { # have all for that package
197 my $bver = $restrpkgs{$restrname};
198 if ( ( $restrinst eq $restrcand ) &&
199 ( $restrinst ne $bver ) ) {
200 # if different, it is a regular apt upgrade, and will be seen
201 # later. AND we want to have a different version in our repo
202 `dpkg --compare-versions "$bver" lt "$restrinst" 2>/dev/null`;
203 if ( $? ) { # It was not a downgrade
204 # manual packages may be ahead of the repo!
209 $manpkgs{$restrname} = 1;
211 $det .= "<td> $restrname <b>(M)</b></td>";
212 $det .= "<td>". strdiff($bver,$restrinst)."</td>";
213 $det .= "<td>". strdiff($restrinst,$bver)."</td>";
214 my $datekey = "$H:$restrname";
215 if ( $olddates{$datekey} ) {
216 $newdates{$datekey} = $olddates{$datekey};
218 $newdates{$datekey} = $thisdate;
220 $det .= "<td>" . $newdates{$datekey} . "</td>";
222 my $key = "$restrname";
223 if ( !$summary{$key} ) {
226 $summary{$key} .= "$H ";
229 $restrname = ""; # clear for next round
234 /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
235 my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
236 print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
237 $det .= "<tr><td> ";
240 if ( $src =~ /Security/ ) {
241 $det .= "<b>$pkg (s)</b> ";
246 } elsif ( $src =~ /Indexdata/ ) {
247 $det .= "<i><b>$pkg</b> (id) </i>";
258 if ( !$summary{$key} ) {
261 $summary{$key} .= "$H ";
262 $new = strdiff($cur,$new);
263 $cur = strdiff($new,$cur);
265 $det .= "<td>$cur</td> ";
266 $det .= "<td>$new</td> ";
267 my $datekey = "$H:$pkg";
268 if ( $olddates{$datekey} ) {
269 $newdates{$datekey} = $olddates{$datekey};
271 $newdates{$datekey} = $thisdate;
273 $det .= "<td>" . $newdates{$datekey} . "</td>";
277 $table .= "<tr><td colspan='4'> </td></tr>\n";
278 $table .= "<tr><td colspan='4'><a name='$H'><b><u>$H</u></b></a> \n";
280 $table .= "<b>$pkgs</b> packages to upgrade. ";
281 $table .= "<b>$secs security</b>. " if $secs;
282 $table .= " $own from indexdata. " if $own;
283 $table .= " $man manual. " if $man;
288 my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
289 # Fix some pages that do not follow the convention.
290 # Mostly because the host names would not make proper WikiWords
291 $updlink =~ s/Bugzilla3Updates/BugzillaUpdates/;
292 $updlink =~ s/Opencontent-solrUpdates/OpenContentSolrUpdates/;
293 $updlinks{$H} = $updlink;
294 $table .= " <a href='$updlink' >Upd</a>";
295 $table .= " " . $debversions{$H};
296 $table .= "</td></tr>\n";
297 $table .= "<tr><td>$hostcomments{$H}</td></tr>\n"
298 if ( $hostcomments{$H} );
299 $table .= $det if $pkgs;
300 print "\n$table\n" if $debug>2;
301 last if $H =~/diane/ && $debug;
303 $table .= "</table>\n";
306 `mv -f $datefilename $dateoldfilename`;
307 open F, ">$datefilename" or die "Could not open date file $datefilename for writing";
308 for my $k (sort(keys(%newdates)) ) {
309 print F "$k " . $newdates{$k}. "\n";
310 print "date for '$k' '" . $newdates{$k}. "'\n" if $debug;
313 or die "Could not close date file $datefilename: $!";
316 my $outfile = "/tmp/aptcheck.html";
318 or die "Could not open $outfile for writing: $!";
320 print F "<head><title>Apt upgrade status</title></head>\n";
322 print F "<H1>Apt package status</H1>\n";
323 print F "<b>" . ( $sectot + $owntot + $mantot + $normtot ) .
324 "</b> packages pending (<b>$sectot</b> critical) \n";
326 print F "<H2>Debug run, many hosts missing!</H2>\n"
330 # Summary table: one row for per host group
332 print F "<table border='1' >\n";
333 print F "<tr><td> </td>" ;
334 print F "<td><b>Hosts</b></td>\n";
335 print F "<td><b>Packages</b></td></tr>\n";
338 print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) .
339 " / " . scalar(keys(%secpkgs)) . " / $sectot </td>\n" ;
341 for my $HH ( sort(keys(%sechosts)) ) {
342 my $upd = $updlinks{$HH} || "#" ;
343 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
347 for my $PP ( sort(keys(%secpkgs)) ) {
348 print F "<a href='#$PP'>$PP</a> ";
354 print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) .
355 " / " . scalar(keys(%ownpkgs)) . " / $owntot </td>\n" ;
357 for my $HH ( sort(keys(%ownhosts)) ) {
358 my $upd = $updlinks{$HH} || "#" ;
359 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
360 #print F "<a href='#$HH'><b>$HH</b></a> ";
364 for my $PP ( sort(keys(%ownpkgs)) ) {
365 print F "<a href='#$PP'>$PP</a> ";
371 print F "<tr><td><b>Manual</b><br/>" . scalar(keys(%manhosts)) .
372 " / " . scalar(keys(%manpkgs)) . " / $mantot </td>\n" ;
374 for my $HH ( sort(keys(%manhosts)) ) {
375 my $upd = $updlinks{$HH} || "#" ;
376 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
377 #print F "<a href='#$HH'><b>$HH</b></a> ";
381 for my $PP ( sort(keys(%manpkgs)) ) {
382 print F "<a href='#$PP'>$PP</a> ";
388 print F "<tr><td>Normal<br/>" . scalar(keys(%normhosts)) .
389 " / " . scalar(keys(%normpkgs)) . " / $normtot </td>\n" ;
391 for my $HH ( sort(keys(%normhosts)) ) {
392 my $upd = $updlinks{$HH} || "#" ;
393 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
394 #print F "<a href='#$HH'><b>$HH</b></a> ";
398 for my $PP ( sort(keys(%normpkgs)) ) {
399 print F "<a href='#$PP'>$PP</a> ";
405 print F "<tr><td>Skipped " . scalar(keys(%skiphosts)) . "</td>\n";
406 print F "<td colspan='2'>";
407 for my $HH ( sort(keys(%skiphosts)) ) {
408 my $upd = $updlinks{$HH} ||
409 $wikilink . ucfirst($HH) . "Updates" . $year;
410 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
411 #print F "<a href='#$HH'><b>$HH</b></a> ";
413 print F "</td></tr>\n";
417 print F "<tr><td>Ok " . scalar(keys(%okhosts)) . "</td>\n";
418 print F "<td colspan='2'>";
419 for my $HH ( sort(keys(%okhosts)) ) {
420 my $upd = $updlinks{$HH} || "#" ;
421 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
422 #print F "<a href='#$HH'><b>$HH</b></a> ";
425 print F "<b>None at all!</b>";
427 print F "</td></tr>\n";
429 print F "</table>\n";
432 #my $secs = 60*60*24 * 7 * 2; # 2 weeks in secods
433 #my $secs = "1m"; # one month, let nagios do the math
436 "<a href='http://nagios.indexdata.com/cgi-bin/nagios3/graphs.cgi?" .
437 "host=nagios&service=Apt%20Summary'>\n".
438 "<img src='http://nagios.indexdata.com/" .
439 "cgi-bin/nagios3/rrd2-system.cgi?" .
440 "host=nagios&service=Apt%20Summary&" .
442 "width=800&height=100&type=AVERAGE' /> ".
450 print F "<p/><b><u>Packages</u></b>\n";
452 for my $P ( sort(keys(%summary)) ) {
454 $PN = "<b>$P (s)</b>" if ($secpkgs{$P});
455 $PN = "<i>$P (id)</i>" if ($ownpkgs{$P});
456 $PN = "$P <b>(M)</b>" if ($manpkgs{$P});
457 print F "<tr><td><a name='$P'/>$PN</td>\n";
459 for my $HH ( split(' ',$summary{$P} )) {
460 print F "<a href=#$HH>$HH</a> ";
465 print F "</table>\n";
467 print F "<p/>Packages marked with * are from the time before started to " .
468 "track package dates \n";
469 print F "<p/>Produced " . `date`.
470 " on " . `hostname` . " by " . `whoami` .
472 print F "</body></html>\n";
475 or die "Could not close $outfile: $!";
477 system "scp -q $outfile nagios:/var/www/heikki/index.html";
481 # Helper to take two strings and highligt that part of the second
482 # that is different from the first.
486 print "strdiff: '$x' '$y' \n" if $debug>2;
488 return "$x <b>??</b>";
491 while ( $a < length($y) &&
492 substr($x,$a,1) eq substr($y,$a,1) ) {
495 if ( $a == length($y) ) {
499 while ( $b < length($y)-$a &&
500 substr($x,-$b,1) eq substr($y, -$b,1) ) {
503 my $c = length($y) - $b +1;
504 print "strdiff: a=$a " . substr($y,0,$a) ."\n" if $debug>2;
505 print "strdiff: b=$b " . "\n" if $debug>2;
506 print "strdiff: c=$c " . substr($y,$c) ."\n" if $debug>2;
507 print "strdiff: " . substr($y,$a, $c-$a) ."\n" if $debug>2;
508 my $z = substr($y,0,$a) .
509 "<b>" . substr($y,$a, $c-$a) . "</b>" .
511 print "strdiff: " . $z ."\n" if $debug>2;
512 print "\n" if $debug>2;