Removed false '??' from version numbers
[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.
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                     if ( !$summary{$restrname} ) {
153                         $summary{$restrname} = "";
154                     }
155                     $summary{$restrname} .= "$H ";
156                 }
157             }
158             $restrname = ""; # clear for next round
159             $restrinst = "";
160             $restrcand = "";
161         }
162         next unless $p =~
163             /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
164         my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
165         print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
166         $det .= "<tr><td>&nbsp;&nbsp;";
167         $pkgs++;
168         my $key = $pkg;
169         if ( $src =~ /Security/ ) {
170             $det .= "<b>$pkg (s)</b> ";
171             $sechosts{$H} = 1;
172             $secpkgs{$pkg} = 1;
173             $secs++;
174             $sectot++;
175         } elsif ( $src =~ /Indexdata/ ) {
176             $det .= "<i><b>$pkg</b> (id) </i>";
177             $ownhosts{$H}=1;
178             $ownpkgs{$pkg}=1;
179             $own++;
180             $owntot++;
181         } else {
182             $det .= "$pkg ";
183             $normhosts{$H}=1;
184             $normpkgs{$pkg}=1;
185             $normtot++;
186         }
187         if ( !$summary{$key} ) {
188             $summary{$key} = "";
189         }
190         $summary{$key} .= "$H ";
191         $new = strdiff($cur,$new);
192         $cur = strdiff($new,$cur);
193         $det .= "</td> ";
194         $det .= "<td>$cur</td> ";
195         $det .= "<td>$new</td> ";
196         $det .= "</tr>\n";
197     }
198     $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
199     $table .= "<tr><td colspan='3'><a name='$H'><b><u>$H</u></b></a> &nbsp;\n";
200     if ( $pkgs ) {
201         $table .= "<b>$pkgs</b> packages to upgrade. ";
202         $table .= "<b>$secs security</b>. " if $secs;
203         $table .= " $own from indexdata. " if $own;
204         $table .= " $man manual. " if $man;
205     } else {
206         $table .= "ok";
207         $okhosts{$H} = 1;
208     }
209     my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
210     # Fix some pages that do not follow the convention.
211     # Mostly because the host names would not make proper WikiWords
212     $updlink =~ s/Bugzilla3Updates/BugzillaUpdates/; 
213     $updlink =~ s/Opencontent-solrUpdates/OpenContentSolrUpdates/; 
214     $table .= "&nbsp;<a href='$updlink' >Upd</a>";
215     $table .= "</td></tr>\n";
216     $table .= $det if $pkgs;
217     print "\n$table\n" if $debug>2;
218     last if $H =~/dart/ && $debug;
219 }
220 $table .= "</table>\n";
221
222 # Page header
223 my $outfile = "/tmp/aptcheck.html";
224 open F, ">$outfile"
225     or die "Could not open $outfile for writing: $!";
226 print F "<html>\n";
227 print F "<head><title>Apt upgrade status</title></head>\n";
228 print F "<body>\n";
229 print F "<H1>Apt package status</H1>\n";
230
231
232 # Summary table: one row for per host group
233 print F "<p/>\n";
234 print F "<table border='1' >\n";
235 print F "<tr><td>&nbsp;</td>" ;
236 print F "<td><b>Hosts</b></td>\n";
237 print F "<td><b>Packages</b></td></tr>\n";
238
239 if ( $sectot ) {
240     print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) . 
241         "&nbsp;/&nbsp;" .  scalar(keys(%secpkgs)) . "&nbsp;/&nbsp;$sectot </td>\n" ;
242     print F "<td>";
243     for my $HH ( sort(keys(%sechosts)) ) {
244         print F "<a href='#$HH'><b>$HH</b></a> ";
245     }
246     print F "</td>";
247     print F "<td>";
248     for my $PP ( sort(keys(%secpkgs)) ) {
249         print F "<a href='#$PP'>$PP</a> ";
250     }
251     print F "</td>";
252     print F "</tr>\n";
253 }
254 if ( $owntot ) {
255     print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) . 
256         "&nbsp;/&nbsp;" .  scalar(keys(%ownpkgs)) . "&nbsp;/&nbsp;$owntot </td>\n" ;
257     print F "<td>";
258     for my $HH ( sort(keys(%ownhosts)) ) {
259         print F "<a href='#$HH'><b>$HH</b></a> ";
260     }
261     print F "</td>";
262     print F "<td>";
263     for my $PP ( sort(keys(%ownpkgs)) ) {
264         print F "<a href='#$PP'>$PP</a> ";
265     }
266     print F "</td>";
267     print F "</tr>\n";
268 }
269 if ( $mantot ) {
270     print F "<tr><td><b>Manual</b><br/>" . scalar(keys(%manhosts)) . 
271         "&nbsp;/&nbsp;" .  scalar(keys(%manpkgs)) . "&nbsp;/&nbsp;$mantot </td>\n" ;
272     print F "<td>";
273     for my $HH ( sort(keys(%manhosts)) ) {
274         print F "<a href='#$HH'><b>$HH</b></a> ";
275     }
276     print F "</td>";
277     print F "<td>";
278     for my $PP ( sort(keys(%manpkgs)) ) {
279         print F "<a href='#$PP'>$PP</a> ";
280     }
281     print F "</td>";
282     print F "</tr>\n";
283 }
284 if ( $normtot ) {
285     print F "<tr><td>Normal<br/>" . scalar(keys(%normhosts)) . 
286         "&nbsp;/&nbsp;" .  scalar(keys(%normpkgs)) . "&nbsp;/&nbsp;$normtot </td>\n" ;
287     print F "<td>";
288     for my $HH ( sort(keys(%normhosts)) ) {
289         print F "<a href='#$HH'><b>$HH</b></a> ";
290     }
291     print F "</td>";
292     print F "<td>";
293     for my $PP ( sort(keys(%normpkgs)) ) {
294         print F "<a href='#$PP'>$PP</a> ";
295     }
296     print F "</td>";
297     print F "</tr>\n";
298 }
299 if ( %skiphosts ) {
300     print F "<tr><td>Skipped " . scalar(keys(%skiphosts)) . "</td>\n";
301     print F "<td colspan='2'>";
302     for my $HH ( sort(keys(%skiphosts)) ) {
303         print F "<a href='#$HH'><b>$HH</b></a> ";
304     }
305     print F "</td></tr>\n";
306 }
307 if ( %okhosts ) {
308     print F "<tr><td>Ok " . scalar(keys(%okhosts)) . "</td>\n";
309     print F "<td colspan='2'>";
310     for my $HH ( sort(keys(%okhosts)) ) {
311         print F "<a href='#$HH'><b>$HH</b></a> ";
312     }
313     print F "</td></tr>\n";
314 }
315 print F "</table>\n";
316
317
318 # The host table
319 print F $table;
320
321 # Package table
322 print F "<p/><b><u>Packages</u></b>\n";
323 print F "<table>\n";
324 for my $P ( sort(keys(%summary)) ) {
325     my $PN = $P;
326     $PN = "<b>$P (s)</b>" if ($secpkgs{$P});
327     $PN = "$P (id)" if ($ownpkgs{$P});
328     print F "<tr><td><a name='$P'/>$PN</td>\n";
329     print F "<td>";
330     for my $HH ( split(' ',$summary{$P} )) {
331         print F "<a href=#$HH>$HH</a> ";
332     }
333     print F "</td>\n";
334
335 }
336 print F "</table>\n";
337
338 print F "<p/>Produced " . `date`.
339         " on " . `hostname` . " by " . `whoami` .
340         "<br/>\n";
341 print F "</body></html>\n";
342
343 close(F)
344     or die "Could not close $outfile: $!";
345
346 system "scp -q $outfile nagios:/var/www/heikki/index.html";
347
348 exit(0);
349
350 # Helper to take two strings and highligt that part of the second
351 # that is different from the first. 
352 sub strdiff {
353     my $x = shift;
354     my $y = shift;
355     print "strdiff: '$x' '$y' \n" if $debug>2;
356     if ( $x eq $y ) {
357         return "$x <b>??</b>";
358     }
359     my $a = 0;
360     while ( $a < length($y) &&
361         substr($x,$a,1) eq substr($y,$a,1) ) {
362         $a++;
363     }
364     if ( $a == length($y) ) {
365         return "$y";
366     }
367     my $b = 1;
368     while ( $b < length($y)-$a &&
369         substr($x,-$b,1) eq substr($y, -$b,1) ) {
370         $b++;
371     }
372     my $c = length($y) - $b +1;
373     print "strdiff:   a=$a " . substr($y,0,$a) ."\n" if $debug>2;
374     print "strdiff:   b=$b " . "\n" if $debug>2;
375     print "strdiff:   c=$c " . substr($y,$c) ."\n" if $debug>2;
376     print "strdiff:        " . substr($y,$a, $c-$a) ."\n" if $debug>2;
377     my $z =  substr($y,0,$a) .
378              "<b>" . substr($y,$a, $c-$a) . "</b>" .
379              substr($y,$c);
380     print "strdiff:        " . $z ."\n" if $debug>2;
381     print "\n" if $debug>2;
382     return $z;
383 }