From: Heikki Levanto Date: Tue, 15 Mar 2011 12:57:11 +0000 (+0100) Subject: Added aptcheck stuff X-Git-Url: http://jira.indexdata.com/?a=commitdiff_plain;h=fb0a04d2b6adb5906df39518729087f8c4d0254f;p=git-tools-moved-to-github.git Added aptcheck stuff --- diff --git a/aptcheck/README b/aptcheck/README new file mode 100644 index 0000000..5fad11c --- /dev/null +++ b/aptcheck/README @@ -0,0 +1,6 @@ +Heikki's apt-upgrade check. + +Too small project to have its own git, so it lives here. + +See comments in the .pl file + diff --git a/aptcheck/aptcheck.pl b/aptcheck/aptcheck.pl new file mode 100755 index 0000000..23dab9d --- /dev/null +++ b/aptcheck/aptcheck.pl @@ -0,0 +1,153 @@ +#!/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 = "\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 .= "\n"; + $table .= " "; + $det .= " "; + $det .= " "; + $det .= "\n"; + } + $table .= "\n"; + $table .= "\n"; + $table .= $det if $pkgs; + print "\n$table\n" if $debug>2; + last if $H =~/dart/ && $debug; +} +$table .= "
 
$H (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 .= "
  "; + $pkgs++; + my $key = $pkg; + if ( $src =~ /Security/ ) { + $det .= "$pkg"; + $key = "$pkg"; + $sechosts{$H}=1; + $secpkgs{$pkg}=1; + $secs++; + } elsif ( $src =~ /Indexdata/ ) { + $det .= "$pkg"; + $key = "$pkg"; + $own++; + } else { + $det .= "$pkg"; + } + if ( !$summary{$key} ) { + $summary{$key} = ""; + } + $new = strdiff($cur,$new); + $cur = strdiff($new,$cur); + $summary{$key} .= "$H "; + $det .= "$cur$new
 
$H  \n"; + if ( $pkgs ) { + $table .= "$pkgs packages to upgrade. "; + $table .= "$secs security. " if $secs; + $table .= " $own from indexdata " if $own; + } else { + $table .= "ok"; + } + my $updlink = $wikilink . ucfirst($H) . "Updates" . $year; + $table .= " Upd"; + $table .= "
\n"; + +# Produce page +my $outfile = "/tmp/aptcheck.html"; +open F, ">$outfile" + or die "Could not open $outfile for writing: $!"; +print F "\n"; +print F "Apt upgrade status\n"; +print F "\n"; +print F "

Apt package status

\n"; + +if ( %sechosts ) { + print F "

Security updates for " . + scalar(keys(%sechosts)) ." hosts:

\n"; + print F "" . join(" ", sort(keys(%sechosts)))."
". + join(" ", sort(keys(%secpkgs))). "

\n"; +} + +print F $table; + +print F "

Produced " . `date`. + " on " . `hostname` . " by " . `whoami` . + "
\n"; +print F "\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) . + "" . substr($y,$a, $c-$a) . "" . + substr($y,$c); + print "strdiff: " . $z ."\n" if $debug>1; + print "\n" if $debug>1; + return $z; +}