#!/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;

# Statistics
my %summary;
my %sechosts;
my %secpkgs;
my %ownhosts;
my %ownpkgs;
my %normhosts;
my %normpkgs;
my %okhosts;
my %skiphosts;
my $sectot = 0;
my $owntot = 0;
my $normtot = 0;

my $table = "<table>\n";

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'>&nbsp;</td></tr>\n";
        $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
        $skiphosts{$H}=1;
        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>&nbsp;&nbsp;";
        $pkgs++;
        my $key = $pkg;
        if ( $src =~ /Security/ ) {
            $det .= "<b>$pkg</b>";
            $key = "<b>$pkg</b>";
            $sechosts{$H}=1;
            $secpkgs{$pkg}=1;
            $secs++;
            $sectot++;
        } elsif ( $src =~ /Indexdata/ ) {
            $det .= "<b><i>$pkg</i></b>";
            $key = "<i>$pkg</i>";
            $ownhosts{$H}=1;
            $ownpkgs{$pkg}=1;
            $own++;
            $owntot++;
        } else {
            $det .= "$pkg";
            $normhosts{$H}=1;
            $normpkgs{$pkg}=1;
            $normtot++;
        }
        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'>&nbsp;</td></tr>\n";
    $table .= "<tr><td colspan='3'><a name='$H'><b><u>$H</u></b></a> &nbsp;\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 .= "&nbsp;<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";


# Summary table
print F "<table border='1' >\n";
print F "<tr><td>&nbsp;</td>" ;
print F "<td><b>Security</b><br/>" . scalar(keys(%sechosts)) . 
        " / " .  scalar(keys(%secpkgs)) . " / $sectot </td>\n" ;
print F "<td>Indexdata<br/>" . scalar(keys(%ownhosts)) . 
        " / " .  scalar(keys(%ownpkgs)) . " / $owntot </td>\n" ;
print F "<td>Normal<br/>" . scalar(keys(%normhosts)) . 
        " / " .  scalar(keys(%normpkgs)) . "  / $normtot </td>" . "</tr>\n";
print F "<tr><td>Hosts</td>\n";


print F "<td>";
for $HH ( sort(keys(%sechosts)) ) {
    print F "<a href='#$HH'><b>$HH</b></a> ";
}
print F "&nbsp;</td>\n";
#print F "<td><b>" . join(" ",sort(keys(%sechosts))) . "</b>&nbsp;</td>";
#print F "<td><b>" . join(" ",sort(keys(%ownhosts))) . "</b>&nbsp;</td>";
print F "<td>";
for $HH ( sort(keys(%ownhosts)) ) {
    print F "<a href='#$HH'><b>$HH</b></a> ";
}
print F "&nbsp;</td>\n";
#print F "<td><b>" . join(" ",sort(keys(%normhosts))) . "</b>&nbsp;</td></tr>\n";
print F "<td>";
for $HH ( sort(keys(%normhosts)) ) {
    print F "<a href='#$HH'><b>$HH</b></a> ";
}
print F "&nbsp;</td>\n";

print F "<tr><td>Packages</td>\n";
print F "<td>" . join(" ",sort(keys(%secpkgs))) . "&nbsp;</td>";
print F "<td>" . join(" ",sort(keys(%ownpkgs))) . "&nbsp;</td>";
print F "<td>" . join(" ",sort(keys(%normpkgs))) . "&nbsp;</td></tr>\n";
print F "</table>\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;
}
