Mark manual packages in list
[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 #### Get list of hosts
34 # I could use a hard-coded list, but I would forget to maintain it.
35 # Nagios knows most of our hosts. It even knows which are worth 
36 # checking, they have a command to check apts!
37
38 my $hostlist = `ssh nagios grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
39   or die "Could not get host list";
40
41 print "Got list:\n$hostlist\n" if $debug>2;
42
43 ###### Get list of packages that can be manually maintained
44 print "getting restricted package versions\n" if $debug;
45 my %restrpkgs;
46 my $restplines = `$restrictedpackages`
47   or die "Could not get the list of restricted packages " .
48          "from $restrictedpackages: $! ";
49 print "Got package list: \n$restplines\n" if $debug>2;
50 my $pname;
51 my $pver;
52 for my $pline ( split("\n",$restplines) ) {
53     chomp($pline);
54     $pname = $1 if $pline =~ /^Package:\s+(\S*)\s*$/;
55     $pver = $1 if $pline =~ /^Version:\s+(\S*)\s*$/;
56     print "$pline: p=$pname v=$pver\n" if $debug>2;
57     if ( $pname && $pver ) {
58         print "\nPackage $pname version $pver \n" if $debug>2;
59         if ( ! $restrpkgs{$pname} ) {
60             $restrpkgs{$pname} = $pver;
61             print "found $pname, first version $pver\n" if $debug>1;
62         } else {
63             my $bver = $restrpkgs{$pname};
64             `dpkg --compare-versions $bver lt $pver`;
65             if ( ! $? ) {
66                 print "found $pname, better version $pver (better than $bver)\n"
67                     if $debug>1;
68                 $restrpkgs{$pname} = $pver;
69             } else {
70                 print "found $pname, but version $pver is no better than $bver\n"
71                     if $debug>2;
72             }
73         }
74         $pname = ""; # clear for the next one.
75         $pver = "";
76     }
77 }
78
79 if ( $debug >1 ) {
80     print "got " . scalar(keys(%restrpkgs)) . " restricted packages\n";
81     for $pname ( sort (keys(%restrpkgs)) ) {
82         print "  $pname " . $restrpkgs{$pname} . "\n";
83     }
84 }
85
86 # Statistics
87 my %summary;
88 my ( %sechosts, %secpkgs );
89 my ( %ownhosts, %ownpkgs );
90 my ( %manhosts, %manpkgs );
91 my ( %normhosts, %normpkgs );
92 my %okhosts;
93 my %skiphosts;
94 my %allhosts;
95 my $sectot = 0;
96 my $owntot = 0;
97 my $mantot = 0;
98 my $normtot = 0;
99
100 my $table = "<table>\n";
101
102 for my $hline ( split("\n",$hostlist) ) {
103     next unless ( $hline =~ /\/([a-z0-9-]+)\.cfg$/ );
104     my $H = $1;
105     next if ($H =~ /^commands/ );
106     next if ($H =~ /^servicegroups/ );
107     print "Checking $H\n" if $debug;
108     $allhosts{$H}=1;
109     my $cmd1 = "apt-cache -q policy " . join(" ",sort(keys(%restrpkgs)));
110     my $cmd2 = "apt-get upgrade -s -o 'Debug::NoLocking=true' ";
111     # Note, do not append -qq, we want some output even when nothing to do
112     my $apt = `ssh -q $H "$cmd1 ; $cmd2 " 2>/dev/null`;
113     if ( !$apt ) {
114         $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
115         $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
116         $skiphosts{$H}=1;
117         next;
118     }
119     print "Got apts for $H: \n$apt\n" if $debug>2;
120     my $det = ""; # detail lines
121     my $pkgs = 0;
122     my $secs = 0;
123     my $own = 0;
124     my $man = 0;
125     my $restrname = "";
126     my $restrinst = "";
127     my $restrcand = "";
128     for my $p ( split("\n",$apt) ) {
129         # parse apt-cache output
130         $restrname = $1 if $p =~ /^(\S+):$/;
131         $restrinst = $1 if $p =~ /^\s+Installed:\s+(\S+)$/;
132         $restrcand = $1 if $p =~ /^\s+Candidate:\s+(\S+)$/;
133         if ( $p =~ /^\s+Version table:/ ) { # have all for that package
134             my $bver = $restrpkgs{$restrname};
135             if ( ( $restrinst eq $restrcand ) &&
136                  ( $restrinst ne $bver ) ) { 
137                 # if different, it is a regular apt upgrade, and will be seen
138                 # later. AND we want to have a different version in our repo
139                 `dpkg --compare-versions $bver lt $restrinst`;
140                 if ( $? ) { # It was not a downgrade 
141                             # manual packages may be ahead of the repo!
142                     $mantot++;
143                     $man++;
144                     $pkgs++;
145                     $manhosts{$H} = 1;
146                     $manpkgs{$restrname} = 1;
147                     $det .= "<tr>";
148                     $det .= "<td>&nbsp;&nbsp;<b>$restrname (M)</b></td>";
149                     $det .= "<td>". strdiff($bver,$restrinst)."</td>";
150                     $det .= "<td>". strdiff($restrinst,$bver)."</td>";
151                     $det .= "</tr>\n";
152                     my $key = "$restrname (M)";
153                     if ( !$summary{$key} ) {
154                         $summary{$key} = "";
155                     }
156                     $summary{$key} .= "$H ";
157                 }
158             }
159             $restrname = ""; # clear for next round
160             $restrinst = "";
161             $restrcand = "";
162         }
163         next unless $p =~
164             /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
165         my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
166         print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
167         $det .= "<tr><td>&nbsp;&nbsp;";
168         $pkgs++;
169         my $key = $pkg;
170         if ( $src =~ /Security/ ) {
171             $det .= "<b>$pkg (s)</b> ";
172             $sechosts{$H} = 1;
173             $secpkgs{$pkg} = 1;
174             $secs++;
175             $sectot++;
176         } elsif ( $src =~ /Indexdata/ ) {
177             $det .= "<i><b>$pkg</b> (id) </i>";
178             $ownhosts{$H}=1;
179             $ownpkgs{$pkg}=1;
180             $own++;
181             $owntot++;
182         } else {
183             $det .= "$pkg ";
184             $normhosts{$H}=1;
185             $normpkgs{$pkg}=1;
186             $normtot++;
187         }
188         if ( !$summary{$key} ) {
189             $summary{$key} = "";
190         }
191         $summary{$key} .= "$H ";
192         $new = strdiff($cur,$new);
193         $cur = strdiff($new,$cur);
194         $det .= "</td> ";
195         $det .= "<td>$cur</td> ";
196         $det .= "<td>$new</td> ";
197         $det .= "</tr>\n";
198     }
199     $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
200     $table .= "<tr><td colspan='3'><a name='$H'><b><u>$H</u></b></a> &nbsp;\n";
201     if ( $pkgs ) {
202         $table .= "<b>$pkgs</b> packages to upgrade. ";
203         $table .= "<b>$secs security</b>. " if $secs;
204         $table .= " $own from indexdata. " if $own;
205         $table .= " $man manual. " if $man;
206     } else {
207         $table .= "ok";
208         $okhosts{$H} = 1;
209     }
210     my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
211     # Fix some pages that do not follow the convention.
212     # Mostly because the host names would not make proper WikiWords
213     $updlink =~ s/Bugzilla3Updates/BugzillaUpdates/; 
214     $updlink =~ s/Opencontent-solrUpdates/OpenContentSolrUpdates/; 
215     $table .= "&nbsp;<a href='$updlink' >Upd</a>";
216     $table .= "</td></tr>\n";
217     $table .= $det if $pkgs;
218     print "\n$table\n" if $debug>2;
219     last if $H =~/dart/ && $debug;
220 }
221 $table .= "</table>\n";
222
223 # Page header
224 my $outfile = "/tmp/aptcheck.html";
225 open F, ">$outfile"
226     or die "Could not open $outfile for writing: $!";
227 print F "<html>\n";
228 print F "<head><title>Apt upgrade status</title></head>\n";
229 print F "<body>\n";
230 print F "<H1>Apt package status</H1>\n";
231
232
233 # Summary table: one row for per host group
234 print F "<p/>\n";
235 print F "<table border='1' >\n";
236 print F "<tr><td>&nbsp;</td>" ;
237 print F "<td><b>Hosts</b></td>\n";
238 print F "<td><b>Packages</b></td></tr>\n";
239
240 if ( $sectot ) {
241     print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) . 
242         "&nbsp;/&nbsp;" .  scalar(keys(%secpkgs)) . "&nbsp;/&nbsp;$sectot </td>\n" ;
243     print F "<td>";
244     for my $HH ( sort(keys(%sechosts)) ) {
245         print F "<a href='#$HH'><b>$HH</b></a> ";
246     }
247     print F "</td>";
248     print F "<td>";
249     for my $PP ( sort(keys(%secpkgs)) ) {
250         print F "<a href='#$PP'>$PP</a> ";
251     }
252     print F "</td>";
253     print F "</tr>\n";
254 }
255 if ( $owntot ) {
256     print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) . 
257         "&nbsp;/&nbsp;" .  scalar(keys(%ownpkgs)) . "&nbsp;/&nbsp;$owntot </td>\n" ;
258     print F "<td>";
259     for my $HH ( sort(keys(%ownhosts)) ) {
260         print F "<a href='#$HH'><b>$HH</b></a> ";
261     }
262     print F "</td>";
263     print F "<td>";
264     for my $PP ( sort(keys(%ownpkgs)) ) {
265         print F "<a href='#$PP'>$PP</a> ";
266     }
267     print F "</td>";
268     print F "</tr>\n";
269 }
270 if ( $mantot ) {
271     print F "<tr><td><b>Manual</b><br/>" . scalar(keys(%manhosts)) . 
272         "&nbsp;/&nbsp;" .  scalar(keys(%manpkgs)) . "&nbsp;/&nbsp;$mantot </td>\n" ;
273     print F "<td>";
274     for my $HH ( sort(keys(%manhosts)) ) {
275         print F "<a href='#$HH'><b>$HH</b></a> ";
276     }
277     print F "</td>";
278     print F "<td>";
279     for my $PP ( sort(keys(%manpkgs)) ) {
280         print F "<a href='#$PP'>$PP</a> ";
281     }
282     print F "</td>";
283     print F "</tr>\n";
284 }
285 if ( $normtot ) {
286     print F "<tr><td>Normal<br/>" . scalar(keys(%normhosts)) . 
287         "&nbsp;/&nbsp;" .  scalar(keys(%normpkgs)) . "&nbsp;/&nbsp;$normtot </td>\n" ;
288     print F "<td>";
289     for my $HH ( sort(keys(%normhosts)) ) {
290         print F "<a href='#$HH'><b>$HH</b></a> ";
291     }
292     print F "</td>";
293     print F "<td>";
294     for my $PP ( sort(keys(%normpkgs)) ) {
295         print F "<a href='#$PP'>$PP</a> ";
296     }
297     print F "</td>";
298     print F "</tr>\n";
299 }
300 if ( %skiphosts ) {
301     print F "<tr><td>Skipped " . scalar(keys(%skiphosts)) . "</td>\n";
302     print F "<td colspan='2'>";
303     for my $HH ( sort(keys(%skiphosts)) ) {
304         print F "<a href='#$HH'><b>$HH</b></a> ";
305     }
306     print F "</td></tr>\n";
307 }
308 if ( %okhosts ) {
309     print F "<tr><td>Ok " . scalar(keys(%okhosts)) . "</td>\n";
310     print F "<td colspan='2'>";
311     for my $HH ( sort(keys(%okhosts)) ) {
312         print F "<a href='#$HH'><b>$HH</b></a> ";
313     }
314     print F "</td></tr>\n";
315 }
316 print F "</table>\n";
317
318
319 # The host table
320 print F $table;
321
322 # Package table
323 print F "<p/><b><u>Packages</u></b>\n";
324 print F "<table>\n";
325 for my $P ( sort(keys(%summary)) ) {
326     my $PN = $P;
327     $PN = "<b>$P (s)</b>" if ($secpkgs{$P});
328     $PN = "$P (id)" if ($ownpkgs{$P});
329     print F "<tr><td><a name='$P'/>$PN</td>\n";
330     print F "<td>";
331     for my $HH ( split(' ',$summary{$P} )) {
332         print F "<a href=#$HH>$HH</a> ";
333     }
334     print F "</td>\n";
335
336 }
337 print F "</table>\n";
338
339 print F "<p/>Produced " . `date`.
340         " on " . `hostname` . " by " . `whoami` .
341         "<br/>\n";
342 print F "</body></html>\n";
343
344 close(F)
345     or die "Could not close $outfile: $!";
346
347 system "scp -q $outfile nagios:/var/www/heikki/index.html";
348
349 exit(0);
350
351 # Helper to take two strings and highligt that part of the second
352 # that is different from the first. 
353 sub strdiff {
354     my $x = shift;
355     my $y = shift;
356     print "strdiff: '$x' '$y' \n" if $debug>2;
357     if ( $x eq $y ) {
358         return "$x <b>??</b>";
359     }
360     my $a = 0;
361     while ( $a < length($y) &&
362         substr($x,$a,1) eq substr($y,$a,1) ) {
363         $a++;
364     }
365     if ( $a == length($y) ) {
366         return "$y";
367     }
368     my $b = 1;
369     while ( $b < length($y)-$a &&
370         substr($x,-$b,1) eq substr($y, -$b,1) ) {
371         $b++;
372     }
373     my $c = length($y) - $b +1;
374     print "strdiff:   a=$a " . substr($y,0,$a) ."\n" if $debug>2;
375     print "strdiff:   b=$b " . "\n" if $debug>2;
376     print "strdiff:   c=$c " . substr($y,$c) ."\n" if $debug>2;
377     print "strdiff:        " . substr($y,$a, $c-$a) ."\n" if $debug>2;
378     my $z =  substr($y,0,$a) .
379              "<b>" . substr($y,$a, $c-$a) . "</b>" .
380              substr($y,$c);
381     print "strdiff:        " . $z ."\n" if $debug>2;
382     print "\n" if $debug>2;
383     return $z;
384 }