Added host comments to note who should do manuals
[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;<b>$restrname (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 (M)";
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
243
244 # Summary table: one row for per host group
245 print F "<p/>\n";
246 print F "<table border='1' >\n";
247 print F "<tr><td>&nbsp;</td>" ;
248 print F "<td><b>Hosts</b></td>\n";
249 print F "<td><b>Packages</b></td></tr>\n";
250
251 if ( $sectot ) {
252     print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) . 
253         "&nbsp;/&nbsp;" .  scalar(keys(%secpkgs)) . "&nbsp;/&nbsp;$sectot </td>\n" ;
254     print F "<td>";
255     for my $HH ( sort(keys(%sechosts)) ) {
256         print F "<a href='#$HH'><b>$HH</b></a> ";
257     }
258     print F "</td>";
259     print F "<td>";
260     for my $PP ( sort(keys(%secpkgs)) ) {
261         print F "<a href='#$PP'>$PP</a> ";
262     }
263     print F "</td>";
264     print F "</tr>\n";
265 }
266 if ( $owntot ) {
267     print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) . 
268         "&nbsp;/&nbsp;" .  scalar(keys(%ownpkgs)) . "&nbsp;/&nbsp;$owntot </td>\n" ;
269     print F "<td>";
270     for my $HH ( sort(keys(%ownhosts)) ) {
271         print F "<a href='#$HH'><b>$HH</b></a> ";
272     }
273     print F "</td>";
274     print F "<td>";
275     for my $PP ( sort(keys(%ownpkgs)) ) {
276         print F "<a href='#$PP'>$PP</a> ";
277     }
278     print F "</td>";
279     print F "</tr>\n";
280 }
281 if ( $mantot ) {
282     print F "<tr><td><b>Manual</b><br/>" . scalar(keys(%manhosts)) . 
283         "&nbsp;/&nbsp;" .  scalar(keys(%manpkgs)) . "&nbsp;/&nbsp;$mantot </td>\n" ;
284     print F "<td>";
285     for my $HH ( sort(keys(%manhosts)) ) {
286         print F "<a href='#$HH'><b>$HH</b></a> ";
287     }
288     print F "</td>";
289     print F "<td>";
290     for my $PP ( sort(keys(%manpkgs)) ) {
291         print F "<a href='#$PP'>$PP</a> ";
292     }
293     print F "</td>";
294     print F "</tr>\n";
295 }
296 if ( $normtot ) {
297     print F "<tr><td>Normal<br/>" . scalar(keys(%normhosts)) . 
298         "&nbsp;/&nbsp;" .  scalar(keys(%normpkgs)) . "&nbsp;/&nbsp;$normtot </td>\n" ;
299     print F "<td>";
300     for my $HH ( sort(keys(%normhosts)) ) {
301         print F "<a href='#$HH'><b>$HH</b></a> ";
302     }
303     print F "</td>";
304     print F "<td>";
305     for my $PP ( sort(keys(%normpkgs)) ) {
306         print F "<a href='#$PP'>$PP</a> ";
307     }
308     print F "</td>";
309     print F "</tr>\n";
310 }
311 if ( %skiphosts ) {
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> ";
316     }
317     print F "</td></tr>\n";
318 }
319 if ( %okhosts ) {
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> ";
324     }
325     print F "</td></tr>\n";
326 }
327 print F "</table>\n";
328
329
330 # The host table
331 print F $table;
332
333 # Package table
334 print F "<p/><b><u>Packages</u></b>\n";
335 print F "<table>\n";
336 for my $P ( sort(keys(%summary)) ) {
337     my $PN = $P;
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";
341     print F "<td>";
342     for my $HH ( split(' ',$summary{$P} )) {
343         print F "<a href=#$HH>$HH</a> ";
344     }
345     print F "</td>\n";
346
347 }
348 print F "</table>\n";
349
350 print F "<p/>Produced " . `date`.
351         " on " . `hostname` . " by " . `whoami` .
352         "<br/>\n";
353 print F "</body></html>\n";
354
355 close(F)
356     or die "Could not close $outfile: $!";
357
358 system "scp -q $outfile nagios:/var/www/heikki/index.html";
359
360 exit(0);
361
362 # Helper to take two strings and highligt that part of the second
363 # that is different from the first. 
364 sub strdiff {
365     my $x = shift;
366     my $y = shift;
367     print "strdiff: '$x' '$y' \n" if $debug>2;
368     if ( $x eq $y ) {
369         return "$x <b>??</b>";
370     }
371     my $a = 0;
372     while ( $a < length($y) &&
373         substr($x,$a,1) eq substr($y,$a,1) ) {
374         $a++;
375     }
376     if ( $a == length($y) ) {
377         return "$y";
378     }
379     my $b = 1;
380     while ( $b < length($y)-$a &&
381         substr($x,-$b,1) eq substr($y, -$b,1) ) {
382         $b++;
383     }
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>" .
391              substr($y,$c);
392     print "strdiff:        " . $z ."\n" if $debug>2;
393     print "\n" if $debug>2;
394     return $z;
395 }