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 for my $hline ( sort( split("\n",$hostlist) ) ) {
154 next unless ( $hline =~ /\/([a-z0-9-]+)\.cfg$/ );
156 next if ($H =~ /^commands/ );
157 next if ($H =~ /^servicegroups/ );
158 print "Checking $H\n" if $debug;
160 my $cmd0 = "cat /etc/debian_version";
161 my $cmd1 = "apt-cache -q policy " . join(" ",sort(keys(%restrpkgs)));
162 my $cmd2 = "apt-get upgrade -s -o 'Debug::NoLocking=true' ";
163 # Note, do not append -qq, we want some output even when nothing to do
164 print "ssh -q $H \"$cmd0; $cmd1 ; $cmd2 \" 2>/dev/null \n" if ($debug>1);
165 my $apt = `ssh -q $H "$cmd0; $cmd1 ; $cmd2 " 2>/dev/null`;
167 $table .= "<tr><td colspan='3'> </td></tr>\n";
168 $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
172 print "Got apts for $H: \n$apt\n" if $debug>2;
173 my $det = ""; # detail lines
182 for my $p ( split("\n",$apt) ) {
183 if ( !$debver ) { # first line
185 $p =~ s/(5[0-9.]+)/$1 LENNY !!!/;
186 $p =~ s/(6[0-9.]+)/$1 squeeze/;
187 $p =~ s/(7[0-9.]+)/$1 wheezy/;
188 $p = " Debian $p";
189 $debversions{$H} = $p;
190 print "Deb version for $H is $p\n" if ($debug>1);
193 # parse apt-cache output
194 $restrname = $1 if $p =~ /^(\S+):$/;
195 $restrinst = $1 if $p =~ /^\s+Installed:\s+(\S+)$/;
196 $restrcand = $1 if $p =~ /^\s+Candidate:\s+(\S+)$/;
197 if ( $p =~ /^\s+Version table:/ ) { # have all for that package
198 my $bver = $restrpkgs{$restrname};
199 if ( ( $restrinst eq $restrcand ) &&
200 ( $restrinst ne $bver ) ) {
201 # if different, it is a regular apt upgrade, and will be seen
202 # later. AND we want to have a different version in our repo
203 `dpkg --compare-versions "$bver" lt "$restrinst" 2>/dev/null`;
204 if ( $? ) { # It was not a downgrade
205 # manual packages may be ahead of the repo!
210 $manpkgs{$restrname} = 1;
212 $det .= "<td> $restrname <b>(M)</b></td>";
213 $det .= "<td>". strdiff($bver,$restrinst)."</td>";
214 $det .= "<td>". strdiff($restrinst,$bver)."</td>";
215 my $datekey = "$H:$restrname";
216 if ( $olddates{$datekey} ) {
217 $newdates{$datekey} = $olddates{$datekey};
219 $newdates{$datekey} = $thisdate;
221 $det .= "<td>" . $newdates{$datekey} . "</td>";
223 my $key = "$restrname";
224 if ( !$summary{$key} ) {
227 $summary{$key} .= "$H ";
230 $restrname = ""; # clear for next round
235 /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
236 my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
237 print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
238 $det .= "<tr><td> ";
241 if ( $src =~ /Security/ ) {
242 $det .= "<b>$pkg (s)</b> ";
247 } elsif ( $src =~ /Indexdata/ ) {
248 $det .= "<i><b>$pkg</b> (id) </i>";
259 if ( !$summary{$key} ) {
262 $summary{$key} .= "$H ";
263 $new = strdiff($cur,$new);
264 $cur = strdiff($new,$cur);
266 $det .= "<td>$cur</td> ";
267 $det .= "<td>$new</td> ";
268 my $datekey = "$H:$pkg";
269 if ( $olddates{$datekey} ) {
270 $newdates{$datekey} = $olddates{$datekey};
272 $newdates{$datekey} = $thisdate;
274 $det .= "<td>" . $newdates{$datekey} . "</td>";
278 $table .= "<tr><td colspan='4'> </td></tr>\n";
279 $table .= "<tr><td colspan='4'><a name='$H'><b><u>$H</u></b></a> \n";
281 $table .= "<b>$pkgs</b> packages to upgrade. ";
282 $table .= "<b>$secs security</b>. " if $secs;
283 $table .= " $own from indexdata. " if $own;
284 $table .= " $man manual. " if $man;
289 my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
290 # Fix some pages that do not follow the convention.
291 # Mostly because the host names would not make proper WikiWords
292 $updlink =~ s/Bugzilla3Updates/BugzillaUpdates/;
293 $updlink =~ s/Opencontent-solrUpdates/OpenContentSolrUpdates/;
294 $updlinks{$H} = $updlink;
295 $table .= " <a href='$updlink' >Upd</a>";
296 $table .= " " . $debversions{$H};
297 $table .= "</td></tr>\n";
298 $table .= "<tr><td>$hostcomments{$H}</td></tr>\n"
299 if ( $hostcomments{$H} );
300 $table .= $det if $pkgs;
301 print "\n$table\n" if $debug>2;
302 last if $H =~/diane/ && $debug;
304 $table .= "</table>\n";
307 `mv -f $datefilename $dateoldfilename`;
308 open F, ">$datefilename" or die "Could not open date file $datefilename for writing";
309 for my $k (sort(keys(%newdates)) ) {
310 print F "$k " . $newdates{$k}. "\n";
311 print "date for '$k' '" . $newdates{$k}. "'\n" if $debug;
314 or die "Could not close date file $datefilename: $!";
317 my $outfile = "/tmp/aptcheck.html";
319 or die "Could not open $outfile for writing: $!";
321 print F "<head><title>Apt upgrade status</title></head>\n";
323 print F "<H1>Apt package status</H1>\n";
324 print F "<b>" . ( $sectot + $owntot + $mantot + $normtot ) .
325 "</b> packages pending (<b>$sectot</b> critical) \n";
327 print F "<H2>Debug run, many hosts missing!</H2>\n"
331 # Summary table: one row for per host group
333 print F "<table border='1' >\n";
334 print F "<tr><td> </td>" ;
335 print F "<td><b>Hosts</b></td>\n";
336 print F "<td><b>Packages</b></td></tr>\n";
339 print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) .
340 " / " . scalar(keys(%secpkgs)) . " / $sectot </td>\n" ;
342 for my $HH ( sort(keys(%sechosts)) ) {
343 my $upd = $updlinks{$HH} || "#" ;
344 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
348 for my $PP ( sort(keys(%secpkgs)) ) {
349 print F "<a href='#$PP'>$PP</a> ";
355 print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) .
356 " / " . scalar(keys(%ownpkgs)) . " / $owntot </td>\n" ;
358 for my $HH ( sort(keys(%ownhosts)) ) {
359 my $upd = $updlinks{$HH} || "#" ;
360 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
361 #print F "<a href='#$HH'><b>$HH</b></a> ";
365 for my $PP ( sort(keys(%ownpkgs)) ) {
366 print F "<a href='#$PP'>$PP</a> ";
372 print F "<tr><td><b>Manual</b><br/>" . scalar(keys(%manhosts)) .
373 " / " . scalar(keys(%manpkgs)) . " / $mantot </td>\n" ;
375 for my $HH ( sort(keys(%manhosts)) ) {
376 my $upd = $updlinks{$HH} || "#" ;
377 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
378 #print F "<a href='#$HH'><b>$HH</b></a> ";
382 for my $PP ( sort(keys(%manpkgs)) ) {
383 print F "<a href='#$PP'>$PP</a> ";
389 print F "<tr><td>Normal<br/>" . scalar(keys(%normhosts)) .
390 " / " . scalar(keys(%normpkgs)) . " / $normtot </td>\n" ;
392 for my $HH ( sort(keys(%normhosts)) ) {
393 my $upd = $updlinks{$HH} || "#" ;
394 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
395 #print F "<a href='#$HH'><b>$HH</b></a> ";
399 for my $PP ( sort(keys(%normpkgs)) ) {
400 print F "<a href='#$PP'>$PP</a> ";
406 print F "<tr><td>Skipped " . scalar(keys(%skiphosts)) . "</td>\n";
407 print F "<td colspan='2'>";
408 for my $HH ( sort(keys(%skiphosts)) ) {
409 my $upd = $updlinks{$HH} ||
410 $wikilink . ucfirst($HH) . "Updates" . $year;
411 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
412 #print F "<a href='#$HH'><b>$HH</b></a> ";
414 print F "</td></tr>\n";
418 print F "<tr><td>Ok " . scalar(keys(%okhosts)) . "</td>\n";
419 print F "<td colspan='2'>";
420 for my $HH ( sort(keys(%okhosts)) ) {
421 my $upd = $updlinks{$HH} || "#" ;
422 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
423 #print F "<a href='#$HH'><b>$HH</b></a> ";
426 print F "<b>None at all!</b>";
428 print F "</td></tr>\n";
430 print F "</table>\n";
433 #my $secs = 60*60*24 * 7 * 2; # 2 weeks in secods
434 #my $secs = "1m"; # one month, let nagios do the math
437 "<a href='http://nagios.indexdata.com/cgi-bin/nagios3/graphs.cgi?" .
438 "host=nagios&service=Apt%20Summary'>\n".
439 "<img src='http://nagios.indexdata.com/" .
440 "cgi-bin/nagios3/rrd2-system.cgi?" .
441 "host=nagios&service=Apt%20Summary&" .
443 "width=800&height=100&type=AVERAGE' /> ".
451 print F "<p/><b><u>Packages</u></b>\n";
453 for my $P ( sort(keys(%summary)) ) {
455 $PN = "<b>$P (s)</b>" if ($secpkgs{$P});
456 $PN = "<i>$P (id)</i>" if ($ownpkgs{$P});
457 $PN = "$P <b>(M)</b>" if ($manpkgs{$P});
458 print F "<tr><td><a name='$P'/>$PN</td>\n";
460 for my $HH ( split(' ',$summary{$P} )) {
461 print F "<a href=#$HH>$HH</a> ";
466 print F "</table>\n";
468 print F "<p/>Produced " . `date`.
469 " on " . `hostname` . " by " . `whoami` .
471 print F "</body></html>\n";
474 or die "Could not close $outfile: $!";
476 system "scp -q $outfile nagios:/var/www/heikki/index.html";
480 # Helper to take two strings and highligt that part of the second
481 # that is different from the first.
485 print "strdiff: '$x' '$y' \n" if $debug>2;
487 return "$x <b>??</b>";
490 while ( $a < length($y) &&
491 substr($x,$a,1) eq substr($y,$a,1) ) {
494 if ( $a == length($y) ) {
498 while ( $b < length($y)-$a &&
499 substr($x,-$b,1) eq substr($y, -$b,1) ) {
502 my $c = length($y) - $b +1;
503 print "strdiff: a=$a " . substr($y,0,$a) ."\n" if $debug>2;
504 print "strdiff: b=$b " . "\n" if $debug>2;
505 print "strdiff: c=$c " . substr($y,$c) ."\n" if $debug>2;
506 print "strdiff: " . substr($y,$a, $c-$a) ."\n" if $debug>2;
507 my $z = substr($y,0,$a) .
508 "<b>" . substr($y,$a, $c-$a) . "</b>" .
510 print "strdiff: " . $z ."\n" if $debug>2;
511 print "\n" if $debug>2;