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
22 # TODO: Assumes that we release our restricted packages for all versions
23 # and architectures at the same time. Gets only the highest version from
24 # all, and reports anything less than this. Good enough for now.
28 my $debug= $ARGV[0] || 0; # 0=none, 1=some, 2=more, 3=much
30 my $wikilink = 'http://twiki.indexdata.dk/cgi-bin/twiki/view/ID/';
31 my $restrictedpackages = "ssh -q kebab cat /home/ftp/pub/debian/dists/*/restricted/*/Packages";
35 "ariel" => "<i>Niels Erik</i> does the manual upgrades",
36 "bellone" => "<i>Niels Erik</i> does the manual upgrades",
37 "cfrepous" => "<i>Wolfram</i> does the manual upgrades",
38 "leopard" => "<i>Wolfram</i> does the manual upgrades",
42 #### Get list of hosts
43 # I could use a hard-coded list, but I would forget to maintain it.
44 # Nagios knows most of our hosts. It even knows which are worth
45 # checking, they have a command to check apts!
47 my $hostlist = `ssh nagios grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
48 or die "Could not get host list";
50 print "Got list:\n$hostlist\n" if $debug>2;
52 ###### Get list of packages that can be manually maintained
53 print "getting restricted package versions\n" if $debug;
55 my $restplines = `$restrictedpackages`
56 or die "Could not get the list of restricted packages " .
57 "from $restrictedpackages: $! ";
58 print "Got package list: \n$restplines\n" if $debug>2;
61 for my $pline ( split("\n",$restplines) ) {
63 $pname = $1 if $pline =~ /^Package:\s+(\S*)\s*$/;
64 $pver = $1 if $pline =~ /^Version:\s+(\S*)\s*$/;
65 print "$pline: p=$pname v=$pver\n" if $debug>2;
66 if ( $pname && $pver ) {
67 print "\nPackage $pname version $pver \n" if $debug>2;
68 if ( ! $restrpkgs{$pname} ) {
69 $restrpkgs{$pname} = $pver;
70 print "found $pname, first version $pver\n" if $debug>1;
72 my $bver = $restrpkgs{$pname};
73 `dpkg --compare-versions $bver lt $pver`;
75 print "found $pname, better version $pver (better than $bver)\n"
77 $restrpkgs{$pname} = $pver;
79 print "found $pname, but version $pver is no better than $bver\n"
83 $pname = ""; # clear for the next one.
89 print "got " . scalar(keys(%restrpkgs)) . " restricted packages\n";
90 for $pname ( sort (keys(%restrpkgs)) ) {
91 print " $pname " . $restrpkgs{$pname} . "\n";
97 my ( %sechosts, %secpkgs );
98 my ( %ownhosts, %ownpkgs );
99 my ( %manhosts, %manpkgs );
100 my ( %normhosts, %normpkgs );
109 my $table = "<table>\n";
111 for my $hline ( split("\n",$hostlist) ) {
112 next unless ( $hline =~ /\/([a-z0-9-]+)\.cfg$/ );
114 next if ($H =~ /^commands/ );
115 next if ($H =~ /^servicegroups/ );
116 print "Checking $H\n" if $debug;
118 my $cmd1 = "apt-cache -q policy " . join(" ",sort(keys(%restrpkgs)));
119 my $cmd2 = "apt-get upgrade -s -o 'Debug::NoLocking=true' ";
120 # Note, do not append -qq, we want some output even when nothing to do
121 my $apt = `ssh -q $H "$cmd1 ; $cmd2 " 2>/dev/null`;
123 $table .= "<tr><td colspan='3'> </td></tr>\n";
124 $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
128 print "Got apts for $H: \n$apt\n" if $debug>2;
129 my $det = ""; # detail lines
137 for my $p ( split("\n",$apt) ) {
138 # parse apt-cache output
139 $restrname = $1 if $p =~ /^(\S+):$/;
140 $restrinst = $1 if $p =~ /^\s+Installed:\s+(\S+)$/;
141 $restrcand = $1 if $p =~ /^\s+Candidate:\s+(\S+)$/;
142 if ( $p =~ /^\s+Version table:/ ) { # have all for that package
143 my $bver = $restrpkgs{$restrname};
144 if ( ( $restrinst eq $restrcand ) &&
145 ( $restrinst ne $bver ) ) {
146 # if different, it is a regular apt upgrade, and will be seen
147 # later. AND we want to have a different version in our repo
148 `dpkg --compare-versions $bver lt $restrinst`;
149 if ( $? ) { # It was not a downgrade
150 # manual packages may be ahead of the repo!
155 $manpkgs{$restrname} = 1;
157 $det .= "<td> <b>$restrname (M)</b></td>";
158 $det .= "<td>". strdiff($bver,$restrinst)."</td>";
159 $det .= "<td>". strdiff($restrinst,$bver)."</td>";
161 my $key = "$restrname (M)";
162 if ( !$summary{$key} ) {
165 $summary{$key} .= "$H ";
168 $restrname = ""; # clear for next round
173 /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
174 my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
175 print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
176 $det .= "<tr><td> ";
179 if ( $src =~ /Security/ ) {
180 $det .= "<b>$pkg (s)</b> ";
185 } elsif ( $src =~ /Indexdata/ ) {
186 $det .= "<i><b>$pkg</b> (id) </i>";
197 if ( !$summary{$key} ) {
200 $summary{$key} .= "$H ";
201 $new = strdiff($cur,$new);
202 $cur = strdiff($new,$cur);
204 $det .= "<td>$cur</td> ";
205 $det .= "<td>$new</td> ";
208 $table .= "<tr><td colspan='3'> </td></tr>\n";
209 $table .= "<tr><td colspan='3'><a name='$H'><b><u>$H</u></b></a> \n";
211 $table .= "<b>$pkgs</b> packages to upgrade. ";
212 $table .= "<b>$secs security</b>. " if $secs;
213 $table .= " $own from indexdata. " if $own;
214 $table .= " $man manual. " if $man;
219 my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
220 # Fix some pages that do not follow the convention.
221 # Mostly because the host names would not make proper WikiWords
222 $updlink =~ s/Bugzilla3Updates/BugzillaUpdates/;
223 $updlink =~ s/Opencontent-solrUpdates/OpenContentSolrUpdates/;
224 $table .= " <a href='$updlink' >Upd</a>";
225 $table .= "</td></tr>\n";
226 $table .= "<tr><td>$hostcomments{$H}</td></tr>\n"
227 if ( $hostcomments{$H} );
228 $table .= $det if $pkgs;
229 print "\n$table\n" if $debug>2;
230 last if $H =~/dart/ && $debug;
232 $table .= "</table>\n";
235 my $outfile = "/tmp/aptcheck.html";
237 or die "Could not open $outfile for writing: $!";
239 print F "<head><title>Apt upgrade status</title></head>\n";
241 print F "<H1>Apt package status</H1>\n";
244 # Summary table: one row for per host group
246 print F "<table border='1' >\n";
247 print F "<tr><td> </td>" ;
248 print F "<td><b>Hosts</b></td>\n";
249 print F "<td><b>Packages</b></td></tr>\n";
252 print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) .
253 " / " . scalar(keys(%secpkgs)) . " / $sectot </td>\n" ;
255 for my $HH ( sort(keys(%sechosts)) ) {
256 print F "<a href='#$HH'><b>$HH</b></a> ";
260 for my $PP ( sort(keys(%secpkgs)) ) {
261 print F "<a href='#$PP'>$PP</a> ";
267 print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) .
268 " / " . scalar(keys(%ownpkgs)) . " / $owntot </td>\n" ;
270 for my $HH ( sort(keys(%ownhosts)) ) {
271 print F "<a href='#$HH'><b>$HH</b></a> ";
275 for my $PP ( sort(keys(%ownpkgs)) ) {
276 print F "<a href='#$PP'>$PP</a> ";
282 print F "<tr><td><b>Manual</b><br/>" . scalar(keys(%manhosts)) .
283 " / " . scalar(keys(%manpkgs)) . " / $mantot </td>\n" ;
285 for my $HH ( sort(keys(%manhosts)) ) {
286 print F "<a href='#$HH'><b>$HH</b></a> ";
290 for my $PP ( sort(keys(%manpkgs)) ) {
291 print F "<a href='#$PP'>$PP</a> ";
297 print F "<tr><td>Normal<br/>" . scalar(keys(%normhosts)) .
298 " / " . scalar(keys(%normpkgs)) . " / $normtot </td>\n" ;
300 for my $HH ( sort(keys(%normhosts)) ) {
301 print F "<a href='#$HH'><b>$HH</b></a> ";
305 for my $PP ( sort(keys(%normpkgs)) ) {
306 print F "<a href='#$PP'>$PP</a> ";
312 print F "<tr><td>Skipped " . scalar(keys(%skiphosts)) . "</td>\n";
313 print F "<td colspan='2'>";
314 for my $HH ( sort(keys(%skiphosts)) ) {
315 print F "<a href='#$HH'><b>$HH</b></a> ";
317 print F "</td></tr>\n";
320 print F "<tr><td>Ok " . scalar(keys(%okhosts)) . "</td>\n";
321 print F "<td colspan='2'>";
322 for my $HH ( sort(keys(%okhosts)) ) {
323 print F "<a href='#$HH'><b>$HH</b></a> ";
325 print F "</td></tr>\n";
327 print F "</table>\n";
334 print F "<p/><b><u>Packages</u></b>\n";
336 for my $P ( sort(keys(%summary)) ) {
338 $PN = "<b>$P (s)</b>" if ($secpkgs{$P});
339 $PN = "$P (id)" if ($ownpkgs{$P});
340 print F "<tr><td><a name='$P'/>$PN</td>\n";
342 for my $HH ( split(' ',$summary{$P} )) {
343 print F "<a href=#$HH>$HH</a> ";
348 print F "</table>\n";
350 print F "<p/>Produced " . `date`.
351 " on " . `hostname` . " by " . `whoami` .
353 print F "</body></html>\n";
356 or die "Could not close $outfile: $!";
358 system "scp -q $outfile nagios:/var/www/heikki/index.html";
362 # Helper to take two strings and highligt that part of the second
363 # that is different from the first.
367 print "strdiff: '$x' '$y' \n" if $debug>2;
369 return "$x <b>??</b>";
372 while ( $a < length($y) &&
373 substr($x,$a,1) eq substr($y,$a,1) ) {
376 if ( $a == length($y) ) {
380 while ( $b < length($y)-$a &&
381 substr($x,-$b,1) eq substr($y, -$b,1) ) {
384 my $c = length($y) - $b +1;
385 print "strdiff: a=$a " . substr($y,0,$a) ."\n" if $debug>2;
386 print "strdiff: b=$b " . "\n" if $debug>2;
387 print "strdiff: c=$c " . substr($y,$c) ."\n" if $debug>2;
388 print "strdiff: " . substr($y,$a, $c-$a) ."\n" if $debug>2;
389 my $z = substr($y,0,$a) .
390 "<b>" . substr($y,$a, $c-$a) . "</b>" .
392 print "strdiff: " . $z ."\n" if $debug>2;
393 print "\n" if $debug>2;