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