More visible upd links.
[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 # 21-May-2012 Heikki: Added a date since when a package has been pending
23 # 31-May-2012 Heikki: Pointing to the new wiki
24 # 01-Jan-2013 Heikki: Get hosts from nagios-us as well.
25 #
26 # TODO: Assumes that we release our restricted packages for all versions
27 # and architectures at the same time. Gets only the highest version from
28 # all, and reports anything less than this. Good enough for now.
29 #
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 $wikilink = 'https://twiki.indexdata.com/twiki/bin/view/ID/';
37 my $restrictedpackages = "ssh -q kebab cat /home/ftp/pub/debian/dists/*/restricted/*/Packages";
38 my $updlink="<i>-u</i>"; # to display after a name, liking to the upd page
39
40 #### Host comments
41 my %hostcomments = (
42       "ariel"    => "<i>Niels Erik</i> does the manual upgrades",
43       "bellone"  => "<i>Niels Erik</i> does the manual upgrades",
44       "cfrepous" => "<i>Wolfram</i> does the manual upgrades",
45       "leopard"  => "<i>Wolfram</i> does the manual upgrades",
46       "lsd"      => "<i>Heikki</i> takes care of all upgrades",
47       );
48       
49
50 #### Get list of hosts
51 # I could use a hard-coded list, but I would forget to maintain it.
52 # Nagios knows most of our hosts. It even knows which are worth 
53 # checking, they have a command to check apts!
54 print "Getting hostlist from nagios\n" if $debug;
55 my $hostlist1 = `ssh nagios grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
56   or die "Could not get host list from nagios (dk)";
57
58 print "Getting hostlist from nagios-us\n" if $debug;
59 my $hostlist2 = `ssh nagios-us grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
60   or die "Could not get host list from nagios (dk)";
61
62 my $hostlist = $hostlist1 . $hostlist2;
63 print "Got list:\n$hostlist\n" if $debug>2;
64
65 ###### Get list of packages that can be manually maintained
66 print "getting restricted package versions\n" if $debug;
67 my %restrpkgs;
68 my $restplines = `$restrictedpackages`
69   or die "Could not get the list of restricted packages " .
70          "from $restrictedpackages: $! ";
71 print "Got package list: \n$restplines\n" if $debug>2;
72 my $pname;
73 my $pver;
74 for my $pline ( split("\n",$restplines) ) {
75     chomp($pline);
76     $pname = $1 if $pline =~ /^Package:\s+(\S*)\s*$/;
77     $pver = $1 if $pline =~ /^Version:\s+(\S*)\s*$/;
78     print "$pline: p=$pname v=$pver\n" if $debug>2;
79     if ( $pname && $pver ) {
80         print "\nPackage $pname version $pver \n" if $debug>2;
81         if ( ! $restrpkgs{$pname} ) {
82             $restrpkgs{$pname} = $pver;
83             print "found $pname, first version $pver\n" if $debug>1;
84         } else {
85             my $bver = $restrpkgs{$pname};
86             `dpkg --compare-versions "$bver" lt "$pver" 2>/dev/null `;
87             if ( ! $? ) {
88                 print "found $pname, better version $pver (better than $bver)\n"
89                     if $debug>1;
90                 $restrpkgs{$pname} = $pver;
91             } else {
92                 print "found $pname, but version $pver is no better than $bver\n"
93                     if $debug>2;
94             }
95         }
96         $pname = ""; # clear for the next one.
97         $pver = "";
98     }
99 }
100
101 print "got " . scalar(keys(%restrpkgs)) . " restricted packages\n" if $debug;
102 if ( $debug >1 ) {
103     for $pname ( sort (keys(%restrpkgs)) ) {
104         print "  $pname " . $restrpkgs{$pname} . "\n";
105     }
106 }
107
108 # Statistics
109 my %summary;
110 my ( %sechosts, %secpkgs );
111 my ( %ownhosts, %ownpkgs );
112 my ( %manhosts, %manpkgs );
113 my ( %normhosts, %normpkgs );
114 my %okhosts;
115 my %skiphosts;
116 my %allhosts;
117 my $sectot = 0;
118 my $owntot = 0;
119 my $mantot = 0;
120 my $normtot = 0;
121 my %updlinks;
122 my %debversions;
123
124 # Pending modification dates
125 my %olddates;  # Read in from the file
126 my %newdates;  # To be written in the new version of the file
127 my $datefilename = "aptcheck.data";
128 my $dateoldfilename = "aptcheck.old";
129 my $thisdate = "*"; # indicates really old stuff
130 my $warndate; # Older than this will be boldfaced
131
132 if ( -f $datefilename ) {
133   print "Reading dates from $datefilename\n" if $debug;
134   open F, $datefilename or die "Could not open date file $datefilename: $!";
135   while (<F>) {
136     chop();
137     my ($pkg, $date) = split;
138     next unless $pkg;  # skip empty lines
139     $olddates{$pkg} = $date;
140     print "Date for '$pkg' is '$date' \n" if $debug;
141   }
142   close F;
143   $thisdate = `date +%F`;
144   chomp($thisdate);
145   $warndate = `date +%F -d "30 days ago"` ; ;
146   chomp($warndate);
147   print "Dates: now: '$thisdate' warn: '$warndate'\n" if $debug;
148 } else {
149   print "No datefile $datefilename found, starting from scratch\n";
150 }
151
152
153 my $table = "<table>\n";
154
155 #for my $hline ( split("\n",$hostlist) ) {
156 for my $hline ( sort( split("\n",$hostlist) ) ) {
157     next unless ( $hline =~ /\/([a-z0-9-]+)\.cfg$/ );
158     my $H = $1;
159     next if ($H =~ /^commands/ );
160     next if ($H =~ /^servicegroups/ );
161     print "Checking $H\n" if $debug;
162     $allhosts{$H}=1;
163     my $cmd0 = "cat /etc/debian_version";
164     my $cmd1 = "apt-cache -q policy " . join(" ",sort(keys(%restrpkgs)));
165     my $cmd2 = "apt-get upgrade -s -o 'Debug::NoLocking=true' ";
166     # Note, do not append -qq, we want some output even when nothing to do
167     print "ssh -q $H \"$cmd0; $cmd1 ; $cmd2 \" 2>/dev/null \n" if ($debug>1);
168     my $apt = `ssh -q $H "$cmd0; $cmd1 ; $cmd2 " 2>/dev/null`;
169     if ( !$apt ) {
170         $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
171         $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
172         $skiphosts{$H}=1;
173         next;
174     }
175     print "Got apts for $H: \n$apt\n" if $debug>2;
176     my $det = ""; # detail lines
177     my $pkgs = 0;
178     my $secs = 0;
179     my $own = 0;
180     my $man = 0;
181     my $restrname = "";
182     my $restrinst = "";
183     my $restrcand = "";
184     my $debver = 0;
185     for my $p ( split("\n",$apt) ) {
186         if ( !$debver ) {  # first line
187           $debver = 1;
188           $p =~ s/(5[0-9.]+)/$1 LENNY !!!/;
189           $p =~ s/(6[0-9.]+)/$1 squeeze/;
190           $p =~ s/(7[0-9.]+)/$1 wheezy/;
191           $p = "&nbsp;Debian $p";
192           $debversions{$H} = $p;
193           print "Deb version for $H is $p\n" if ($debug>1);
194           next;
195         }
196         # parse apt-cache output
197         $restrname = $1 if $p =~ /^(\S+):$/;
198         $restrinst = $1 if $p =~ /^\s+Installed:\s+(\S+)$/;
199         $restrcand = $1 if $p =~ /^\s+Candidate:\s+(\S+)$/;
200         if ( $p =~ /^\s+Version table:/ ) { # have all for that package
201             my $bver = $restrpkgs{$restrname};
202             if ( ( $restrinst eq $restrcand ) &&
203                  ( $restrinst ne $bver ) ) { 
204                 # if different, it is a regular apt upgrade, and will be seen
205                 # later. AND we want to have a different version in our repo
206                 `dpkg --compare-versions "$bver" lt "$restrinst"  2>/dev/null`;
207                 if ( $? ) { # It was not a downgrade 
208                             # manual packages may be ahead of the repo!
209                     $mantot++;
210                     $man++;
211                     $pkgs++;
212                     $manhosts{$H} = 1;
213                     $manpkgs{$restrname} = 1;
214                     $det .= "<tr>";
215                     $det .= "<td>&nbsp;&nbsp;$restrname <b>(M)</b></td>";
216                     $det .= "<td>". strdiff($bver,$restrinst)."</td>";
217                     $det .= "<td>". strdiff($restrinst,$bver)."</td>";
218                     my $datekey = "$H:$restrname";
219                     if ( $olddates{$datekey} ) {
220                         $newdates{$datekey} = $olddates{$datekey};
221                     } else {
222                         $newdates{$datekey} = $thisdate;
223                     }
224                     my $dispdate = $newdates{$datekey};
225                     # if ( $dispdate lt $warndate ) {
226                     if ( 0 ) { # manual packages don't need to be highlighted
227                         $dispdate = "<b>$dispdate !</b>";
228                     }
229                     $det .= "<td>" . $dispdate . "</td>";
230                     $det .= "</tr>\n";
231                     my $key = "$restrname";
232                     if ( !$summary{$key} ) {
233                         $summary{$key} = "";
234                     }
235                     $summary{$key} .= "$H ";
236                 }
237             }
238             $restrname = ""; # clear for next round
239             $restrinst = "";
240             $restrcand = "";
241         }
242         next unless $p =~
243             /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
244         my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
245         print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
246         $det .= "<tr><td>&nbsp;&nbsp;";
247         $pkgs++;
248         my $key = $pkg;
249         if ( $src =~ /Security/ ) {
250             $det .= "<b>$pkg (s)</b> ";
251             $sechosts{$H} = 1;
252             $secpkgs{$pkg} = 1;
253             $secs++;
254             $sectot++;
255         } elsif ( $src =~ /Indexdata/ ) {
256             $det .= "<i><b>$pkg</b> (id) </i>";
257             $ownhosts{$H}=1;
258             $ownpkgs{$pkg}=1;
259             $own++;
260             $owntot++;
261         } else {
262             $det .= "$pkg ";
263             $normhosts{$H}=1;
264             $normpkgs{$pkg}=1;
265             $normtot++;
266         }
267         if ( !$summary{$key} ) {
268             $summary{$key} = "";
269         }
270         $summary{$key} .= "$H ";
271         $new = strdiff($cur,$new);
272         $cur = strdiff($new,$cur);
273         $det .= "</td> ";
274         $det .= "<td>$cur</td> ";
275         $det .= "<td>$new</td> ";
276         my $datekey = "$H:$pkg";
277         if ( $olddates{$datekey} ) {
278             $newdates{$datekey} = $olddates{$datekey};
279         } else {
280             $newdates{$datekey} = $thisdate;
281         }
282         my $dispdate = $newdates{$datekey};
283         if ( ( $dispdate lt $warndate ) && ( $src =~ /Security/) ) {
284             $dispdate = "<b>$dispdate !</b>";
285         }
286         $det .= "<td>" . $dispdate . "</td>";
287         $det .= "</tr>\n";
288
289     }
290     $table .= "<tr><td colspan='4'>&nbsp;</td></tr>\n";
291     $table .= "<tr><td colspan='4'><a name='$H'><b><u>$H</u></b></a> &nbsp;\n";
292     if ( $pkgs ) {
293         $table .= "<b>$pkgs</b> packages to upgrade. ";
294         $table .= "<b>$secs security</b>. " if $secs;
295         $table .= " $own from indexdata. " if $own;
296         $table .= " $man manual. " if $man;
297     } else {
298         $table .= "ok";
299         $okhosts{$H} = 1;
300     }
301     my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
302     # Fix some pages that do not follow the convention.
303     # Mostly because the host names would not make proper WikiWords
304     $updlink =~ s/Bugzilla3Updates/BugzillaUpdates/; 
305     $updlink =~ s/Opencontent-solrUpdates/OpenContentSolrUpdates/; 
306     $updlinks{$H} = $updlink;
307     $table .= "&nbsp;<a href='$updlink' >Upd</a>";
308     $table .= "&nbsp;" . $debversions{$H};
309     $table .= "</td></tr>\n";
310     $table .= "<tr><td>$hostcomments{$H}</td></tr>\n"
311         if ( $hostcomments{$H} );
312     $table .= $det if $pkgs;
313     print "\n$table\n" if $debug>2;
314     last if $H =~/diane/ && $debug;
315 }
316 $table .= "</table>\n";
317
318 # Save the date file
319 if ( ! $debug ) {
320     `mv -f $datefilename $dateoldfilename`;
321     open F, ">$datefilename" or die "Could not open date file $datefilename for writing";
322     for  my $k (sort(keys(%newdates)) ) {
323         print F "$k " . $newdates{$k}. "\n";
324         print "date for '$k' '" . $newdates{$k}. "'\n" if $debug;
325     }
326     close F
327       or die "Could not close date file $datefilename: $!";
328 } else {
329     print "Not updating the date file, this is a debug run\n";
330 }
331
332 # Page header
333 my $outfile = "/tmp/aptcheck.html";
334 open F, ">$outfile"
335     or die "Could not open $outfile for writing: $!";
336 print F "<html>\n";
337 print F "<head><title>Apt upgrade status</title></head>\n";
338 print F "<body>\n";
339 print F "<H1>Apt package status</H1>\n";
340 print F "<b>" .  ( $sectot + $owntot + $mantot + $normtot ) . 
341         "</b> packages pending (<b>$sectot</b> critical) \n";
342
343 print F "<H2>Debug run, many hosts missing!</H2>\n"
344    if $debug;
345
346
347 # Summary table: one row for per host group
348 print F "<p/>\n";
349 print F "<table border='1' >\n";
350 print F "<tr><td>&nbsp;</td>" ;
351 print F "<td><b>Hosts</b></td>\n";
352 print F "<td><b>Packages</b></td></tr>\n";
353
354 if ( $sectot ) {
355     print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) . 
356         "&nbsp;/&nbsp;" .  scalar(keys(%secpkgs)) . "&nbsp;/&nbsp;$sectot </td>\n" ;
357     print F "<td>";
358     for my $HH ( sort(keys(%sechosts)) ) {
359         my $upd = $updlinks{$HH} || "#" ;
360         print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>$updlink</a> ";
361     }
362     print F "</td>";
363     print F "<td>";
364     for my $PP ( sort(keys(%secpkgs)) ) {
365         print F "<a href='#$PP'>$PP</a> ";
366     }
367     print F "</td>";
368     print F "</tr>\n";
369 }
370 if ( $owntot ) {
371     print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) . 
372         "&nbsp;/&nbsp;" .  scalar(keys(%ownpkgs)) . "&nbsp;/&nbsp;$owntot </td>\n" ;
373     print F "<td>";
374     for my $HH ( sort(keys(%ownhosts)) ) {
375         my $upd = $updlinks{$HH} || "#" ;
376         print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>$updlink</a> ";
377         #print F "<a href='#$HH'><b>$HH</b></a> ";
378     }
379     print F "</td>";
380     print F "<td>";
381     for my $PP ( sort(keys(%ownpkgs)) ) {
382         print F "<a href='#$PP'>$PP</a> ";
383     }
384     print F "</td>";
385     print F "</tr>\n";
386 }
387 if ( $mantot ) {
388     print F "<tr><td><b>Manual</b><br/>" . scalar(keys(%manhosts)) . 
389         "&nbsp;/&nbsp;" .  scalar(keys(%manpkgs)) . "&nbsp;/&nbsp;$mantot </td>\n" ;
390     print F "<td>";
391     for my $HH ( sort(keys(%manhosts)) ) {
392         my $upd = $updlinks{$HH} || "#" ;
393         print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>$updlink</a> ";
394         #print F "<a href='#$HH'><b>$HH</b></a> ";
395     }
396     print F "</td>";
397     print F "<td>";
398     for my $PP ( sort(keys(%manpkgs)) ) {
399         print F "<a href='#$PP'>$PP</a> ";
400     }
401     print F "</td>";
402     print F "</tr>\n";
403 }
404 if ( $normtot ) {
405     print F "<tr><td>Normal<br/>" . scalar(keys(%normhosts)) . 
406         "&nbsp;/&nbsp;" .  scalar(keys(%normpkgs)) . "&nbsp;/&nbsp;$normtot </td>\n" ;
407     print F "<td>";
408     for my $HH ( sort(keys(%normhosts)) ) {
409         my $upd = $updlinks{$HH} || "#" ;
410         print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>$updlink</a> ";
411         #print F "<a href='#$HH'><b>$HH</b></a> ";
412     }
413     print F "</td>";
414     print F "<td>";
415     for my $PP ( sort(keys(%normpkgs)) ) {
416         print F "<a href='#$PP'>$PP</a> ";
417     }
418     print F "</td>";
419     print F "</tr>\n";
420 }
421 if ( %skiphosts ) {
422     print F "<tr><td>Skipped " . scalar(keys(%skiphosts)) . "</td>\n";
423     print F "<td colspan='2'>";
424     for my $HH ( sort(keys(%skiphosts)) ) {
425         my $upd = $updlinks{$HH} ||
426                   $wikilink . ucfirst($HH) . "Updates" . $year;
427         print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>$updlink</a> ";
428         #print F "<a href='#$HH'><b>$HH</b></a> ";
429     }
430     print F "</td></tr>\n";
431 }
432 if ( 1 ) {
433     print F "<tr><td>Ok <br/>" . scalar(keys(%okhosts)) .
434            " of " . scalar(keys(%allhosts)) . "</td>\n";
435     print F "<td colspan='2'>";
436     for my $HH ( sort(keys(%okhosts)) ) {
437         my $upd = $updlinks{$HH} || "#" ;
438         print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>$updlink</a> ";
439         #print F "<a href='#$HH'><b>$HH</b></a> ";
440     }
441     if ( !%okhosts ) {
442         print F "<b>None at all!</b>";
443     }
444     print F "</td></tr>\n";
445 }
446 print F "</table>\n";
447
448 print F "<p/><b>" .  ( $sectot + $owntot + $mantot + $normtot ) . 
449         "</b> packages pending (<b>$sectot</b> critical) \n";
450
451 # Graph 
452 #my $secs = 60*60*24 * 7 * 2; # 2 weeks in secods
453 #my $secs = "1m"; # one month, let nagios do the math
454 my $secs = "45d"; 
455 print F "<p/>" .
456         "<a href='http://nagios.indexdata.com/cgi-bin/nagios3/graphs.cgi?" .
457         "host=nagios&service=Apt%20Summary'>\n".
458         "<img src='http://nagios.indexdata.com/" .
459               "cgi-bin/nagios3/rrd2-system.cgi?" .
460               "host=nagios&service=Apt%20Summary&" .
461               "start=-$secs&" .
462               "width=800&height=100&type=AVERAGE' /> ".
463         "</a>" .
464         "<br/>\n";
465
466 # The host table
467 print F $table;
468
469 # Package table
470 print F "<p/><b><u>Packages</u></b>\n";
471 print F "<table>\n";
472 for my $P ( sort(keys(%summary)) ) {
473     my $PN = $P;
474     $PN = "<b>$P&nbsp;(s)</b>" if ($secpkgs{$P});
475     $PN = "<i>$P&nbsp;(id)</i>" if ($ownpkgs{$P});
476     $PN = "$P&nbsp;<b>(M)</b>" if ($manpkgs{$P});
477     print F "<tr><td><a name='$P'/>$PN</td>\n";
478     print F "<td>";
479     for my $HH ( split(' ',$summary{$P} )) {
480         print F "<a href=#$HH>$HH</a> ";
481     }
482     print F "</td>\n";
483
484 }
485 print F "</table>\n";
486
487 print F "<p/>Produced " . `date`.
488         " on " . `hostname` . " by " . `whoami` .
489         "<br/>\n";
490 print F "</body></html>\n";
491
492 close(F)
493     or die "Could not close $outfile: $!";
494
495 system "scp -q $outfile nagios:/var/www/heikki/index.html";
496
497 exit(0);
498
499 # Helper to take two strings and highligt that part of the second
500 # that is different from the first. 
501 sub strdiff {
502     my $x = shift;
503     my $y = shift;
504     print "strdiff: '$x' '$y' \n" if $debug>2;
505     if ( $x eq $y ) {
506         return "$x <b>??</b>";
507     }
508     my $a = 0;
509     while ( $a < length($y) &&
510         substr($x,$a,1) eq substr($y,$a,1) ) {
511         $a++;
512     }
513     if ( $a == length($y) ) {
514         return "$y";
515     }
516     my $b = 1;
517     while ( $b < length($y)-$a &&
518         substr($x,-$b,1) eq substr($y, -$b,1) ) {
519         $b++;
520     }
521     my $c = length($y) - $b +1;
522     print "strdiff:   a=$a " . substr($y,0,$a) ."\n" if $debug>2;
523     print "strdiff:   b=$b " . "\n" if $debug>2;
524     print "strdiff:   c=$c " . substr($y,$c) ."\n" if $debug>2;
525     print "strdiff:        " . substr($y,$a, $c-$a) ."\n" if $debug>2;
526     my $z =  substr($y,0,$a) .
527              "<b>" . substr($y,$a, $c-$a) . "</b>" .
528              substr($y,$c);
529     print "strdiff:        " . $z ."\n" if $debug>2;
530     print "\n" if $debug>2;
531     return $z;
532 }