Minor
[git-tools-moved-to-github.git] / aptcheck / aptcheck.pl
1 #!/usr/bin/perl -w
2 #
3 # Check what packages are needed to get upgraded on all machines
4 #
5 # Depends heavily on having ssh key authentication set up to all
6 # boxes. That's why I run it on my own workstation.
7 #
8 # Regular debian upgrades are detected by running 
9 #  apt-get upgrade -s
10 # on every machine, and parsing the output.
11
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.
18 #
19 # 11-Mar-2011 Heikki: Started this
20 # 22-Mar-2011 Heikki: Adding manually maintained packages
21 #
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.
25
26 #### Init
27 use strict;
28 my $debug= $ARGV[0] || 0; # 0=none, 1=some, 2=more, 3=much
29 my $year =`date +%Y`;
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";
32
33 #### Host comments
34 my %hostcomments = (
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",
39       );
40       
41
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!
46
47 my $hostlist = `ssh nagios grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
48   or die "Could not get host list";
49
50 print "Got list:\n$hostlist\n" if $debug>2;
51
52 ###### Get list of packages that can be manually maintained
53 print "getting restricted package versions\n" if $debug;
54 my %restrpkgs;
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;
59 my $pname;
60 my $pver;
61 for my $pline ( split("\n",$restplines) ) {
62     chomp($pline);
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;
71         } else {
72             my $bver = $restrpkgs{$pname};
73             `dpkg --compare-versions $bver lt $pver`;
74             if ( ! $? ) {
75                 print "found $pname, better version $pver (better than $bver)\n"
76                     if $debug>1;
77                 $restrpkgs{$pname} = $pver;
78             } else {
79                 print "found $pname, but version $pver is no better than $bver\n"
80                     if $debug>2;
81             }
82         }
83         $pname = ""; # clear for the next one.
84         $pver = "";
85     }
86 }
87
88 if ( $debug >1 ) {
89     print "got " . scalar(keys(%restrpkgs)) . " restricted packages\n";
90     for $pname ( sort (keys(%restrpkgs)) ) {
91         print "  $pname " . $restrpkgs{$pname} . "\n";
92     }
93 }
94
95 # Statistics
96 my %summary;
97 my ( %sechosts, %secpkgs );
98 my ( %ownhosts, %ownpkgs );
99 my ( %manhosts, %manpkgs );
100 my ( %normhosts, %normpkgs );
101 my %okhosts;
102 my %skiphosts;
103 my %allhosts;
104 my $sectot = 0;
105 my $owntot = 0;
106 my $mantot = 0;
107 my $normtot = 0;
108
109 my $table = "<table>\n";
110
111 for my $hline ( split("\n",$hostlist) ) {
112     next unless ( $hline =~ /\/([a-z0-9-]+)\.cfg$/ );
113     my $H = $1;
114     next if ($H =~ /^commands/ );
115     next if ($H =~ /^servicegroups/ );
116     print "Checking $H\n" if $debug;
117     $allhosts{$H}=1;
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`;
122     if ( !$apt ) {
123         $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
124         $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
125         $skiphosts{$H}=1;
126         next;
127     }
128     print "Got apts for $H: \n$apt\n" if $debug>2;
129     my $det = ""; # detail lines
130     my $pkgs = 0;
131     my $secs = 0;
132     my $own = 0;
133     my $man = 0;
134     my $restrname = "";
135     my $restrinst = "";
136     my $restrcand = "";
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!
151                     $mantot++;
152                     $man++;
153                     $pkgs++;
154                     $manhosts{$H} = 1;
155                     $manpkgs{$restrname} = 1;
156                     $det .= "<tr>";
157                     $det .= "<td>&nbsp;&nbsp;$restrname <b>(M)</b></td>";
158                     $det .= "<td>". strdiff($bver,$restrinst)."</td>";
159                     $det .= "<td>". strdiff($restrinst,$bver)."</td>";
160                     $det .= "</tr>\n";
161                     my $key = "$restrname";
162                     if ( !$summary{$key} ) {
163                         $summary{$key} = "";
164                     }
165                     $summary{$key} .= "$H ";
166                 }
167             }
168             $restrname = ""; # clear for next round
169             $restrinst = "";
170             $restrcand = "";
171         }
172         next unless $p =~
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>&nbsp;&nbsp;";
177         $pkgs++;
178         my $key = $pkg;
179         if ( $src =~ /Security/ ) {
180             $det .= "<b>$pkg (s)</b> ";
181             $sechosts{$H} = 1;
182             $secpkgs{$pkg} = 1;
183             $secs++;
184             $sectot++;
185         } elsif ( $src =~ /Indexdata/ ) {
186             $det .= "<i><b>$pkg</b> (id) </i>";
187             $ownhosts{$H}=1;
188             $ownpkgs{$pkg}=1;
189             $own++;
190             $owntot++;
191         } else {
192             $det .= "$pkg ";
193             $normhosts{$H}=1;
194             $normpkgs{$pkg}=1;
195             $normtot++;
196         }
197         if ( !$summary{$key} ) {
198             $summary{$key} = "";
199         }
200         $summary{$key} .= "$H ";
201         $new = strdiff($cur,$new);
202         $cur = strdiff($new,$cur);
203         $det .= "</td> ";
204         $det .= "<td>$cur</td> ";
205         $det .= "<td>$new</td> ";
206         $det .= "</tr>\n";
207     }
208     $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
209     $table .= "<tr><td colspan='3'><a name='$H'><b><u>$H</u></b></a> &nbsp;\n";
210     if ( $pkgs ) {
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;
215     } else {
216         $table .= "ok";
217         $okhosts{$H} = 1;
218     }
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 .= "&nbsp;<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;
231 }
232 $table .= "</table>\n";
233
234 # Page header
235 my $outfile = "/tmp/aptcheck.html";
236 open F, ">$outfile"
237     or die "Could not open $outfile for writing: $!";
238 print F "<html>\n";
239 print F "<head><title>Apt upgrade status</title></head>\n";
240 print F "<body>\n";
241 print F "<H1>Apt package status</H1>\n";
242 print F "<H2>Debug run, many hosts missing!</H2>\n"
243    if $debug;
244
245
246 # Summary table: one row for per host group
247 print F "<p/>\n";
248 print F "<table border='1' >\n";
249 print F "<tr><td>&nbsp;</td>" ;
250 print F "<td><b>Hosts</b></td>\n";
251 print F "<td><b>Packages</b></td></tr>\n";
252
253 if ( $sectot ) {
254     print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) . 
255         "&nbsp;/&nbsp;" .  scalar(keys(%secpkgs)) . "&nbsp;/&nbsp;$sectot </td>\n" ;
256     print F "<td>";
257     for my $HH ( sort(keys(%sechosts)) ) {
258         print F "<a href='#$HH'><b>$HH</b></a> ";
259     }
260     print F "</td>";
261     print F "<td>";
262     for my $PP ( sort(keys(%secpkgs)) ) {
263         print F "<a href='#$PP'>$PP</a> ";
264     }
265     print F "</td>";
266     print F "</tr>\n";
267 }
268 if ( $owntot ) {
269     print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) . 
270         "&nbsp;/&nbsp;" .  scalar(keys(%ownpkgs)) . "&nbsp;/&nbsp;$owntot </td>\n" ;
271     print F "<td>";
272     for my $HH ( sort(keys(%ownhosts)) ) {
273         print F "<a href='#$HH'><b>$HH</b></a> ";
274     }
275     print F "</td>";
276     print F "<td>";
277     for my $PP ( sort(keys(%ownpkgs)) ) {
278         print F "<a href='#$PP'>$PP</a> ";
279     }
280     print F "</td>";
281     print F "</tr>\n";
282 }
283 if ( $mantot ) {
284     print F "<tr><td><b>Manual</b><br/>" . scalar(keys(%manhosts)) . 
285         "&nbsp;/&nbsp;" .  scalar(keys(%manpkgs)) . "&nbsp;/&nbsp;$mantot </td>\n" ;
286     print F "<td>";
287     for my $HH ( sort(keys(%manhosts)) ) {
288         print F "<a href='#$HH'><b>$HH</b></a> ";
289     }
290     print F "</td>";
291     print F "<td>";
292     for my $PP ( sort(keys(%manpkgs)) ) {
293         print F "<a href='#$PP'>$PP</a> ";
294     }
295     print F "</td>";
296     print F "</tr>\n";
297 }
298 if ( $normtot ) {
299     print F "<tr><td>Normal<br/>" . scalar(keys(%normhosts)) . 
300         "&nbsp;/&nbsp;" .  scalar(keys(%normpkgs)) . "&nbsp;/&nbsp;$normtot </td>\n" ;
301     print F "<td>";
302     for my $HH ( sort(keys(%normhosts)) ) {
303         print F "<a href='#$HH'><b>$HH</b></a> ";
304     }
305     print F "</td>";
306     print F "<td>";
307     for my $PP ( sort(keys(%normpkgs)) ) {
308         print F "<a href='#$PP'>$PP</a> ";
309     }
310     print F "</td>";
311     print F "</tr>\n";
312 }
313 if ( %skiphosts ) {
314     print F "<tr><td>Skipped " . scalar(keys(%skiphosts)) . "</td>\n";
315     print F "<td colspan='2'>";
316     for my $HH ( sort(keys(%skiphosts)) ) {
317         print F "<a href='#$HH'><b>$HH</b></a> ";
318     }
319     print F "</td></tr>\n";
320 }
321 if ( %okhosts ) {
322     print F "<tr><td>Ok " . scalar(keys(%okhosts)) . "</td>\n";
323     print F "<td colspan='2'>";
324     for my $HH ( sort(keys(%okhosts)) ) {
325         print F "<a href='#$HH'><b>$HH</b></a> ";
326     }
327     print F "</td></tr>\n";
328 }
329 print F "</table>\n";
330
331
332 # The host table
333 print F $table;
334
335 # Package table
336 print F "<p/><b><u>Packages</u></b>\n";
337 print F "<table>\n";
338 for my $P ( sort(keys(%summary)) ) {
339     my $PN = $P;
340     $PN = "<b>$P&nbsp;(s)</b>" if ($secpkgs{$P});
341     $PN = "<i>$P&nbsp;(id)</i>" if ($ownpkgs{$P});
342     $PN = "$P&nbsp;<b>(M)</b>" if ($manpkgs{$P});
343     print F "<tr><td><a name='$P'/>$PN</td>\n";
344     print F "<td>";
345     for my $HH ( split(' ',$summary{$P} )) {
346         print F "<a href=#$HH>$HH</a> ";
347     }
348     print F "</td>\n";
349
350 }
351 print F "</table>\n";
352
353 print F "<p/>Produced " . `date`.
354         " on " . `hostname` . " by " . `whoami` .
355         "<br/>\n";
356 print F "</body></html>\n";
357
358 close(F)
359     or die "Could not close $outfile: $!";
360
361 system "scp -q $outfile nagios:/var/www/heikki/index.html";
362
363 exit(0);
364
365 # Helper to take two strings and highligt that part of the second
366 # that is different from the first. 
367 sub strdiff {
368     my $x = shift;
369     my $y = shift;
370     print "strdiff: '$x' '$y' \n" if $debug>2;
371     if ( $x eq $y ) {
372         return "$x <b>??</b>";
373     }
374     my $a = 0;
375     while ( $a < length($y) &&
376         substr($x,$a,1) eq substr($y,$a,1) ) {
377         $a++;
378     }
379     if ( $a == length($y) ) {
380         return "$y";
381     }
382     my $b = 1;
383     while ( $b < length($y)-$a &&
384         substr($x,-$b,1) eq substr($y, -$b,1) ) {
385         $b++;
386     }
387     my $c = length($y) - $b +1;
388     print "strdiff:   a=$a " . substr($y,0,$a) ."\n" if $debug>2;
389     print "strdiff:   b=$b " . "\n" if $debug>2;
390     print "strdiff:   c=$c " . substr($y,$c) ."\n" if $debug>2;
391     print "strdiff:        " . substr($y,$a, $c-$a) ."\n" if $debug>2;
392     my $z =  substr($y,0,$a) .
393              "<b>" . substr($y,$a, $c-$a) . "</b>" .
394              substr($y,$c);
395     print "strdiff:        " . $z ."\n" if $debug>2;
396     print "\n" if $debug>2;
397     return $z;
398 }