--- /dev/null
+#!/usr/bin/perl -w
+#
+# Check what packages are needed to get upgraded on all machines
+#
+# Depends heavily on having ssh key authentication set up to all
+# boxes. That's why I run it on my own workstation.
+#
+# 11-Mar-2011 Heikki: Started this
+
+#### Init
+my $debug= $ARGV[0] || 0; # 0=none, 1=some, 2=more, 3=much
+my $year =`date +%Y`;
+my $wikilink = 'http://twiki.indexdata.dk/cgi-bin/twiki/view/ID/';
+
+#### Get list of hosts
+# I could use a hard-coded list, but I would forget to maintain it.
+# Nagios knows most of our hosts.
+
+my $hostlist = `ssh nagios grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
+ or die "Could not get host list";
+
+print "Got list:\n$hostlist\n" if $debug>2;
+my $table = "<table>\n";
+my %summary;
+my %sechosts;
+my %secpkgs;
+for $hline ( split("\n",$hostlist) ) {
+ next unless ( $hline =~ /\/([a-z0-9]+)\.cfg$/ );
+ my $H = $1;
+ next if ($H =~ /^commands/ );
+ next if ($H =~ /^servicegroups/ );
+ print "Checking $H\n" if $debug;
+ my $apt = `ssh $H apt-get upgrade -s -o 'Debug::NoLocking=true' `;
+ # Note, do not append -qq, we want some output even when nothing to do
+ if ( !$apt ) {
+ $table .= "<tr><td colspan='3'> </td></tr>\n";
+ $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
+ next;
+ }
+ print "Got apts for $H: \n$apt\n" if $debug>2;
+ my $det = "";
+ my $pkgs = 0;
+ my $secs = 0;
+ my $own = 0;
+ for $p ( split("\n",$apt) ) {
+ next unless $p =~
+ /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
+ my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
+ print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
+ $det .= "<tr><td> ";
+ $pkgs++;
+ my $key = $pkg;
+ if ( $src =~ /Security/ ) {
+ $det .= "<b>$pkg</b>";
+ $key = "<b>$pkg</b>";
+ $sechosts{$H}=1;
+ $secpkgs{$pkg}=1;
+ $secs++;
+ } elsif ( $src =~ /Indexdata/ ) {
+ $det .= "<b><i>$pkg</i></b>";
+ $key = "<i>$pkg</i>";
+ $own++;
+ } else {
+ $det .= "$pkg";
+ }
+ if ( !$summary{$key} ) {
+ $summary{$key} = "";
+ }
+ $new = strdiff($cur,$new);
+ $cur = strdiff($new,$cur);
+ $summary{$key} .= "$H ";
+ $det .= "</td> ";
+ $det .= "<td>$cur</td> ";
+ $det .= "<td>$new</td> ";
+ $det .= "</tr>\n";
+ }
+ $table .= "<tr><td colspan='3'> </td></tr>\n";
+ $table .= "<tr><td colspan='3'><b><u>$H</u></b> \n";
+ if ( $pkgs ) {
+ $table .= "<b>$pkgs</b> packages to upgrade. ";
+ $table .= "<b>$secs security</b>. " if $secs;
+ $table .= " $own from indexdata " if $own;
+ } else {
+ $table .= "ok";
+ }
+ my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
+ $table .= " <a href='$updlink' >Upd</a>";
+ $table .= "</td></tr>\n";
+ $table .= $det if $pkgs;
+ print "\n$table\n" if $debug>2;
+ last if $H =~/dart/ && $debug;
+}
+$table .= "</table>\n";
+
+# Produce page
+my $outfile = "/tmp/aptcheck.html";
+open F, ">$outfile"
+ or die "Could not open $outfile for writing: $!";
+print F "<html>\n";
+print F "<head><title>Apt upgrade status</title></head>\n";
+print F "<body>\n";
+print F "<H1>Apt package status</H1>\n";
+
+if ( %sechosts ) {
+ print F "<h2>Security updates for " .
+ scalar(keys(%sechosts)) ." hosts: </h2>\n";
+ print F "<b>" . join(" ", sort(keys(%sechosts)))."</b><br/> ".
+ join(" ", sort(keys(%secpkgs))). "<p/>\n";
+}
+
+print F $table;
+
+print F "<p/>Produced " . `date`.
+ " on " . `hostname` . " by " . `whoami` .
+ "<br/>\n";
+print F "</body></html>\n";
+
+close(F)
+ or die "Could not close $outfile: $!";
+
+system "scp -q $outfile nagios:/var/www/heikki/index.html";
+
+# Helper to take two strings and highligt that part of the second
+# that is different from the first.
+sub strdiff {
+ my $x = shift;
+ my $y = shift;
+ print "strdiff: '$x' '$y' \n" if $debug>1;
+ my $a = 0;
+ while ( $a < length($y) &&
+ substr($x,$a,1) eq substr($y,$a,1) ) {
+ $a++;
+ }
+ if ( $a == length($y) ) {
+ return "$y ???";
+ }
+ my $b = 1;
+ while ( $b < length($y)-$a &&
+ substr($x,-$b,1) eq substr($y, -$b,1) ) {
+ $b++;
+ }
+ my $c = length($y) - $b +1;
+ print "strdiff: a=$a " . substr($y,0,$a) ."\n" if $debug>1;
+ print "strdiff: b=$b " . "\n" if $debug>1;
+ print "strdiff: c=$c " . substr($y,$c) ."\n" if $debug>1;
+ print "strdiff: " . substr($y,$a, $c-$a) ."\n" if $debug>1;
+ my $z = substr($y,0,$a) .
+ "<b>" . substr($y,$a, $c-$a) . "</b>" .
+ substr($y,$c);
+ print "strdiff: " . $z ."\n" if $debug>1;
+ print "\n" if $debug>1;
+ return $z;
+}