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
21 # 15-Aug-2011 Heikki: Adding a total in the headline, for nagiosgrapher
23 # TODO: Assumes that we release our restricted packages for all versions
24 # and architectures at the same time. Gets only the highest version from
25 # all, and reports anything less than this. Good enough for now.
29 my $debug= $ARGV[0] || 0; # 0=none, 1=some, 2=more, 3=much
31 my $wikilink = 'http://twiki.indexdata.dk/cgi-bin/twiki/view/ID/';
32 my $restrictedpackages = "ssh -q kebab cat /home/ftp/pub/debian/dists/*/restricted/*/Packages";
36 "ariel" => "<i>Niels Erik</i> does the manual upgrades",
37 "bellone" => "<i>Niels Erik</i> does the manual upgrades",
38 "cfrepous" => "<i>Wolfram</i> does the manual upgrades",
39 "leopard" => "<i>Wolfram</i> does the manual upgrades",
40 "lsd" => "<i>Heikki</i> takes care of all upgrades",
44 #### Get list of hosts
45 # I could use a hard-coded list, but I would forget to maintain it.
46 # Nagios knows most of our hosts. It even knows which are worth
47 # checking, they have a command to check apts!
49 my $hostlist = `ssh nagios grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
50 or die "Could not get host list";
52 print "Got list:\n$hostlist\n" if $debug>2;
54 ###### Get list of packages that can be manually maintained
55 print "getting restricted package versions\n" if $debug;
57 my $restplines = `$restrictedpackages`
58 or die "Could not get the list of restricted packages " .
59 "from $restrictedpackages: $! ";
60 print "Got package list: \n$restplines\n" if $debug>2;
63 for my $pline ( split("\n",$restplines) ) {
65 $pname = $1 if $pline =~ /^Package:\s+(\S*)\s*$/;
66 $pver = $1 if $pline =~ /^Version:\s+(\S*)\s*$/;
67 print "$pline: p=$pname v=$pver\n" if $debug>2;
68 if ( $pname && $pver ) {
69 print "\nPackage $pname version $pver \n" if $debug>2;
70 if ( ! $restrpkgs{$pname} ) {
71 $restrpkgs{$pname} = $pver;
72 print "found $pname, first version $pver\n" if $debug>1;
74 my $bver = $restrpkgs{$pname};
75 `dpkg --compare-versions $bver lt $pver`;
77 print "found $pname, better version $pver (better than $bver)\n"
79 $restrpkgs{$pname} = $pver;
81 print "found $pname, but version $pver is no better than $bver\n"
85 $pname = ""; # clear for the next one.
91 print "got " . scalar(keys(%restrpkgs)) . " restricted packages\n";
92 for $pname ( sort (keys(%restrpkgs)) ) {
93 print " $pname " . $restrpkgs{$pname} . "\n";
99 my ( %sechosts, %secpkgs );
100 my ( %ownhosts, %ownpkgs );
101 my ( %manhosts, %manpkgs );
102 my ( %normhosts, %normpkgs );
111 my $table = "<table>\n";
113 for my $hline ( split("\n",$hostlist) ) {
114 next unless ( $hline =~ /\/([a-z0-9-]+)\.cfg$/ );
116 next if ($H =~ /^commands/ );
117 next if ($H =~ /^servicegroups/ );
118 print "Checking $H\n" if $debug;
120 my $cmd1 = "apt-cache -q policy " . join(" ",sort(keys(%restrpkgs)));
121 my $cmd2 = "apt-get upgrade -s -o 'Debug::NoLocking=true' ";
122 # Note, do not append -qq, we want some output even when nothing to do
123 my $apt = `ssh -q $H "$cmd1 ; $cmd2 " 2>/dev/null`;
125 $table .= "<tr><td colspan='3'> </td></tr>\n";
126 $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
130 print "Got apts for $H: \n$apt\n" if $debug>2;
131 my $det = ""; # detail lines
139 for my $p ( split("\n",$apt) ) {
140 # parse apt-cache output
141 $restrname = $1 if $p =~ /^(\S+):$/;
142 $restrinst = $1 if $p =~ /^\s+Installed:\s+(\S+)$/;
143 $restrcand = $1 if $p =~ /^\s+Candidate:\s+(\S+)$/;
144 if ( $p =~ /^\s+Version table:/ ) { # have all for that package
145 my $bver = $restrpkgs{$restrname};
146 if ( ( $restrinst eq $restrcand ) &&
147 ( $restrinst ne $bver ) ) {
148 # if different, it is a regular apt upgrade, and will be seen
149 # later. AND we want to have a different version in our repo
150 `dpkg --compare-versions $bver lt $restrinst`;
151 if ( $? ) { # It was not a downgrade
152 # manual packages may be ahead of the repo!
157 $manpkgs{$restrname} = 1;
159 $det .= "<td> $restrname <b>(M)</b></td>";
160 $det .= "<td>". strdiff($bver,$restrinst)."</td>";
161 $det .= "<td>". strdiff($restrinst,$bver)."</td>";
163 my $key = "$restrname";
164 if ( !$summary{$key} ) {
167 $summary{$key} .= "$H ";
170 $restrname = ""; # clear for next round
175 /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
176 my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
177 print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
178 $det .= "<tr><td> ";
181 if ( $src =~ /Security/ ) {
182 $det .= "<b>$pkg (s)</b> ";
187 } elsif ( $src =~ /Indexdata/ ) {
188 $det .= "<i><b>$pkg</b> (id) </i>";
199 if ( !$summary{$key} ) {
202 $summary{$key} .= "$H ";
203 $new = strdiff($cur,$new);
204 $cur = strdiff($new,$cur);
206 $det .= "<td>$cur</td> ";
207 $det .= "<td>$new</td> ";
210 $table .= "<tr><td colspan='3'> </td></tr>\n";
211 $table .= "<tr><td colspan='3'><a name='$H'><b><u>$H</u></b></a> \n";
213 $table .= "<b>$pkgs</b> packages to upgrade. ";
214 $table .= "<b>$secs security</b>. " if $secs;
215 $table .= " $own from indexdata. " if $own;
216 $table .= " $man manual. " if $man;
221 my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
222 # Fix some pages that do not follow the convention.
223 # Mostly because the host names would not make proper WikiWords
224 $updlink =~ s/Bugzilla3Updates/BugzillaUpdates/;
225 $updlink =~ s/Opencontent-solrUpdates/OpenContentSolrUpdates/;
226 $table .= " <a href='$updlink' >Upd</a>";
227 $table .= "</td></tr>\n";
228 $table .= "<tr><td>$hostcomments{$H}</td></tr>\n"
229 if ( $hostcomments{$H} );
230 $table .= $det if $pkgs;
231 print "\n$table\n" if $debug>2;
232 last if $H =~/dart/ && $debug;
234 $table .= "</table>\n";
237 my $outfile = "/tmp/aptcheck.html";
239 or die "Could not open $outfile for writing: $!";
241 print F "<head><title>Apt upgrade status</title></head>\n";
243 print F "<H1>Apt package status</H1>\n";
244 print F "<b>" . ( $sectot + $owntot + $mantot + $normtot ) .
245 "</b> packages pending, including <b>$sectot</b> security \n";
247 print F "<H2>Debug run, many hosts missing!</H2>\n"
251 # Summary table: one row for per host group
253 print F "<table border='1' >\n";
254 print F "<tr><td> </td>" ;
255 print F "<td><b>Hosts</b></td>\n";
256 print F "<td><b>Packages</b></td></tr>\n";
259 print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) .
260 " / " . scalar(keys(%secpkgs)) . " / $sectot </td>\n" ;
262 for my $HH ( sort(keys(%sechosts)) ) {
263 print F "<a href='#$HH'><b>$HH</b></a> ";
267 for my $PP ( sort(keys(%secpkgs)) ) {
268 print F "<a href='#$PP'>$PP</a> ";
274 print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) .
275 " / " . scalar(keys(%ownpkgs)) . " / $owntot </td>\n" ;
277 for my $HH ( sort(keys(%ownhosts)) ) {
278 print F "<a href='#$HH'><b>$HH</b></a> ";
282 for my $PP ( sort(keys(%ownpkgs)) ) {
283 print F "<a href='#$PP'>$PP</a> ";
289 print F "<tr><td><b>Manual</b><br/>" . scalar(keys(%manhosts)) .
290 " / " . scalar(keys(%manpkgs)) . " / $mantot </td>\n" ;
292 for my $HH ( sort(keys(%manhosts)) ) {
293 print F "<a href='#$HH'><b>$HH</b></a> ";
297 for my $PP ( sort(keys(%manpkgs)) ) {
298 print F "<a href='#$PP'>$PP</a> ";
304 print F "<tr><td>Normal<br/>" . scalar(keys(%normhosts)) .
305 " / " . scalar(keys(%normpkgs)) . " / $normtot </td>\n" ;
307 for my $HH ( sort(keys(%normhosts)) ) {
308 print F "<a href='#$HH'><b>$HH</b></a> ";
312 for my $PP ( sort(keys(%normpkgs)) ) {
313 print F "<a href='#$PP'>$PP</a> ";
319 print F "<tr><td>Skipped " . scalar(keys(%skiphosts)) . "</td>\n";
320 print F "<td colspan='2'>";
321 for my $HH ( sort(keys(%skiphosts)) ) {
322 print F "<a href='#$HH'><b>$HH</b></a> ";
324 print F "</td></tr>\n";
327 print F "<tr><td>Ok " . scalar(keys(%okhosts)) . "</td>\n";
328 print F "<td colspan='2'>";
329 for my $HH ( sort(keys(%okhosts)) ) {
330 print F "<a href='#$HH'><b>$HH</b></a> ";
332 print F "</td></tr>\n";
334 print F "</table>\n";
341 print F "<p/><b><u>Packages</u></b>\n";
343 for my $P ( sort(keys(%summary)) ) {
345 $PN = "<b>$P (s)</b>" if ($secpkgs{$P});
346 $PN = "<i>$P (id)</i>" if ($ownpkgs{$P});
347 $PN = "$P <b>(M)</b>" if ($manpkgs{$P});
348 print F "<tr><td><a name='$P'/>$PN</td>\n";
350 for my $HH ( split(' ',$summary{$P} )) {
351 print F "<a href=#$HH>$HH</a> ";
356 print F "</table>\n";
358 print F "<p/>Produced " . `date`.
359 " on " . `hostname` . " by " . `whoami` .
361 print F "</body></html>\n";
364 or die "Could not close $outfile: $!";
366 system "scp -q $outfile nagios:/var/www/heikki/index.html";
370 # Helper to take two strings and highligt that part of the second
371 # that is different from the first.
375 print "strdiff: '$x' '$y' \n" if $debug>2;
377 return "$x <b>??</b>";
380 while ( $a < length($y) &&
381 substr($x,$a,1) eq substr($y,$a,1) ) {
384 if ( $a == length($y) ) {
388 while ( $b < length($y)-$a &&
389 substr($x,-$b,1) eq substr($y, -$b,1) ) {
392 my $c = length($y) - $b +1;
393 print "strdiff: a=$a " . substr($y,0,$a) ."\n" if $debug>2;
394 print "strdiff: b=$b " . "\n" if $debug>2;
395 print "strdiff: c=$c " . substr($y,$c) ."\n" if $debug>2;
396 print "strdiff: " . substr($y,$a, $c-$a) ."\n" if $debug>2;
397 my $z = substr($y,0,$a) .
398 "<b>" . substr($y,$a, $c-$a) . "</b>" .
400 print "strdiff: " . $z ."\n" if $debug>2;
401 print "\n" if $debug>2;