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