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 # 11-Mar-2011 Heikki: Started this
11 my $debug= $ARGV[0] || 0; # 0=none, 1=some, 2=more, 3=much
13 my $wikilink = 'http://twiki.indexdata.dk/cgi-bin/twiki/view/ID/';
15 #### Get list of hosts
16 # I could use a hard-coded list, but I would forget to maintain it.
17 # Nagios knows most of our hosts.
19 my $hostlist = `ssh nagios grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
20 or die "Could not get host list";
22 print "Got list:\n$hostlist\n" if $debug>2;
23 my $table = "<table>\n";
27 for $hline ( split("\n",$hostlist) ) {
28 next unless ( $hline =~ /\/([a-z0-9]+)\.cfg$/ );
30 next if ($H =~ /^commands/ );
31 next if ($H =~ /^servicegroups/ );
32 print "Checking $H\n" if $debug;
33 my $apt = `ssh $H apt-get upgrade -s -o 'Debug::NoLocking=true' `;
34 # Note, do not append -qq, we want some output even when nothing to do
36 $table .= "<tr><td colspan='3'> </td></tr>\n";
37 $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
40 print "Got apts for $H: \n$apt\n" if $debug>2;
45 for $p ( split("\n",$apt) ) {
47 /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
48 my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
49 print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
50 $det .= "<tr><td> ";
53 if ( $src =~ /Security/ ) {
54 $det .= "<b>$pkg</b>";
59 } elsif ( $src =~ /Indexdata/ ) {
60 $det .= "<b><i>$pkg</i></b>";
66 if ( !$summary{$key} ) {
69 $new = strdiff($cur,$new);
70 $cur = strdiff($new,$cur);
71 $summary{$key} .= "$H ";
73 $det .= "<td>$cur</td> ";
74 $det .= "<td>$new</td> ";
77 $table .= "<tr><td colspan='3'> </td></tr>\n";
78 $table .= "<tr><td colspan='3'><b><u>$H</u></b> \n";
80 $table .= "<b>$pkgs</b> packages to upgrade. ";
81 $table .= "<b>$secs security</b>. " if $secs;
82 $table .= " $own from indexdata " if $own;
86 my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
87 $table .= " <a href='$updlink' >Upd</a>";
88 $table .= "</td></tr>\n";
89 $table .= $det if $pkgs;
90 print "\n$table\n" if $debug>2;
91 last if $H =~/dart/ && $debug;
93 $table .= "</table>\n";
96 my $outfile = "/tmp/aptcheck.html";
98 or die "Could not open $outfile for writing: $!";
100 print F "<head><title>Apt upgrade status</title></head>\n";
102 print F "<H1>Apt package status</H1>\n";
105 print F "<h2>Security updates for " .
106 scalar(keys(%sechosts)) ." hosts: </h2>\n";
107 print F "<b>" . join(" ", sort(keys(%sechosts)))."</b><br/> ".
108 join(" ", sort(keys(%secpkgs))). "<p/>\n";
113 print F "<p/>Produced " . `date`.
114 " on " . `hostname` . " by " . `whoami` .
116 print F "</body></html>\n";
119 or die "Could not close $outfile: $!";
121 system "scp -q $outfile nagios:/var/www/heikki/index.html";
123 # Helper to take two strings and highligt that part of the second
124 # that is different from the first.
128 print "strdiff: '$x' '$y' \n" if $debug>1;
130 while ( $a < length($y) &&
131 substr($x,$a,1) eq substr($y,$a,1) ) {
134 if ( $a == length($y) ) {
138 while ( $b < length($y)-$a &&
139 substr($x,-$b,1) eq substr($y, -$b,1) ) {
142 my $c = length($y) - $b +1;
143 print "strdiff: a=$a " . substr($y,0,$a) ."\n" if $debug>1;
144 print "strdiff: b=$b " . "\n" if $debug>1;
145 print "strdiff: c=$c " . substr($y,$c) ."\n" if $debug>1;
146 print "strdiff: " . substr($y,$a, $c-$a) ."\n" if $debug>1;
147 my $z = substr($y,0,$a) .
148 "<b>" . substr($y,$a, $c-$a) . "</b>" .
150 print "strdiff: " . $z ."\n" if $debug>1;
151 print "\n" if $debug>1;