Old edits
[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 my %updlinks;
115
116 my $table = "<table>\n";
117
118 for my $hline ( split("\n",$hostlist) ) {
119     next unless ( $hline =~ /\/([a-z0-9-]+)\.cfg$/ );
120     my $H = $1;
121     next if ($H =~ /^commands/ );
122     next if ($H =~ /^servicegroups/ );
123     print "Checking $H\n" if $debug;
124     $allhosts{$H}=1;
125     my $cmd1 = "apt-cache -q policy " . join(" ",sort(keys(%restrpkgs)));
126     my $cmd2 = "apt-get upgrade -s -o 'Debug::NoLocking=true' ";
127     # Note, do not append -qq, we want some output even when nothing to do
128     print "ssh -q $H \"$cmd1 ; $cmd2 \" 2>/dev/null" if ($debug>1);
129     my $apt = `ssh -q $H "$cmd1 ; $cmd2 " 2>/dev/null`;
130     if ( !$apt ) {
131         $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
132         $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
133         $skiphosts{$H}=1;
134         next;
135     }
136     print "Got apts for $H: \n$apt\n" if $debug>2;
137     my $det = ""; # detail lines
138     my $pkgs = 0;
139     my $secs = 0;
140     my $own = 0;
141     my $man = 0;
142     my $restrname = "";
143     my $restrinst = "";
144     my $restrcand = "";
145     for my $p ( split("\n",$apt) ) {
146         # parse apt-cache output
147         $restrname = $1 if $p =~ /^(\S+):$/;
148         $restrinst = $1 if $p =~ /^\s+Installed:\s+(\S+)$/;
149         $restrcand = $1 if $p =~ /^\s+Candidate:\s+(\S+)$/;
150         if ( $p =~ /^\s+Version table:/ ) { # have all for that package
151             my $bver = $restrpkgs{$restrname};
152             if ( ( $restrinst eq $restrcand ) &&
153                  ( $restrinst ne $bver ) ) { 
154                 # if different, it is a regular apt upgrade, and will be seen
155                 # later. AND we want to have a different version in our repo
156                 `dpkg --compare-versions "$bver" lt "$restrinst" `;
157                 if ( $? ) { # It was not a downgrade 
158                             # manual packages may be ahead of the repo!
159                     $mantot++;
160                     $man++;
161                     $pkgs++;
162                     $manhosts{$H} = 1;
163                     $manpkgs{$restrname} = 1;
164                     $det .= "<tr>";
165                     $det .= "<td>&nbsp;&nbsp;$restrname <b>(M)</b></td>";
166                     $det .= "<td>". strdiff($bver,$restrinst)."</td>";
167                     $det .= "<td>". strdiff($restrinst,$bver)."</td>";
168                     $det .= "</tr>\n";
169                     my $key = "$restrname";
170                     if ( !$summary{$key} ) {
171                         $summary{$key} = "";
172                     }
173                     $summary{$key} .= "$H ";
174                 }
175             }
176             $restrname = ""; # clear for next round
177             $restrinst = "";
178             $restrcand = "";
179         }
180         next unless $p =~
181             /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
182         my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
183         print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
184         $det .= "<tr><td>&nbsp;&nbsp;";
185         $pkgs++;
186         my $key = $pkg;
187         if ( $src =~ /Security/ ) {
188             $det .= "<b>$pkg (s)</b> ";
189             $sechosts{$H} = 1;
190             $secpkgs{$pkg} = 1;
191             $secs++;
192             $sectot++;
193         } elsif ( $src =~ /Indexdata/ ) {
194             $det .= "<i><b>$pkg</b> (id) </i>";
195             $ownhosts{$H}=1;
196             $ownpkgs{$pkg}=1;
197             $own++;
198             $owntot++;
199         } else {
200             $det .= "$pkg ";
201             $normhosts{$H}=1;
202             $normpkgs{$pkg}=1;
203             $normtot++;
204         }
205         if ( !$summary{$key} ) {
206             $summary{$key} = "";
207         }
208         $summary{$key} .= "$H ";
209         $new = strdiff($cur,$new);
210         $cur = strdiff($new,$cur);
211         $det .= "</td> ";
212         $det .= "<td>$cur</td> ";
213         $det .= "<td>$new</td> ";
214         $det .= "</tr>\n";
215     }
216     $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
217     $table .= "<tr><td colspan='3'><a name='$H'><b><u>$H</u></b></a> &nbsp;\n";
218     if ( $pkgs ) {
219         $table .= "<b>$pkgs</b> packages to upgrade. ";
220         $table .= "<b>$secs security</b>. " if $secs;
221         $table .= " $own from indexdata. " if $own;
222         $table .= " $man manual. " if $man;
223     } else {
224         $table .= "ok";
225         $okhosts{$H} = 1;
226     }
227     my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
228     # Fix some pages that do not follow the convention.
229     # Mostly because the host names would not make proper WikiWords
230     $updlink =~ s/Bugzilla3Updates/BugzillaUpdates/; 
231     $updlink =~ s/Opencontent-solrUpdates/OpenContentSolrUpdates/; 
232     $updlinks{$H} = $updlink;
233     $table .= "&nbsp;<a href='$updlink' >Upd</a>";
234     $table .= "</td></tr>\n";
235     $table .= "<tr><td>$hostcomments{$H}</td></tr>\n"
236         if ( $hostcomments{$H} );
237     $table .= $det if $pkgs;
238     print "\n$table\n" if $debug>2;
239     last if $H =~/diane/ && $debug;
240 }
241 $table .= "</table>\n";
242
243 # Page header
244 my $outfile = "/tmp/aptcheck.html";
245 open F, ">$outfile"
246     or die "Could not open $outfile for writing: $!";
247 print F "<html>\n";
248 print F "<head><title>Apt upgrade status</title></head>\n";
249 print F "<body>\n";
250 print F "<H1>Apt package status</H1>\n";
251 print F "<b>" .  ( $sectot + $owntot + $mantot + $normtot ) . 
252         "</b> packages pending (<b>$sectot</b> critical) \n";
253
254 print F "<H2>Debug run, many hosts missing!</H2>\n"
255    if $debug;
256
257
258 # Summary table: one row for per host group
259 print F "<p/>\n";
260 print F "<table border='1' >\n";
261 print F "<tr><td>&nbsp;</td>" ;
262 print F "<td><b>Hosts</b></td>\n";
263 print F "<td><b>Packages</b></td></tr>\n";
264
265 if ( $sectot ) {
266     print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) . 
267         "&nbsp;/&nbsp;" .  scalar(keys(%secpkgs)) . "&nbsp;/&nbsp;$sectot </td>\n" ;
268     print F "<td>";
269     for my $HH ( sort(keys(%sechosts)) ) {
270         my $upd = $updlinks{$HH} || "#" ;
271         print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
272     }
273     print F "</td>";
274     print F "<td>";
275     for my $PP ( sort(keys(%secpkgs)) ) {
276         print F "<a href='#$PP'>$PP</a> ";
277     }
278     print F "</td>";
279     print F "</tr>\n";
280 }
281 if ( $owntot ) {
282     print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) . 
283         "&nbsp;/&nbsp;" .  scalar(keys(%ownpkgs)) . "&nbsp;/&nbsp;$owntot </td>\n" ;
284     print F "<td>";
285     for my $HH ( sort(keys(%ownhosts)) ) {
286         my $upd = $updlinks{$HH} || "#" ;
287         print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
288         #print F "<a href='#$HH'><b>$HH</b></a> ";
289     }
290     print F "</td>";
291     print F "<td>";
292     for my $PP ( sort(keys(%ownpkgs)) ) {
293         print F "<a href='#$PP'>$PP</a> ";
294     }
295     print F "</td>";
296     print F "</tr>\n";
297 }
298 if ( $mantot ) {
299     print F "<tr><td><b>Manual</b><br/>" . scalar(keys(%manhosts)) . 
300         "&nbsp;/&nbsp;" .  scalar(keys(%manpkgs)) . "&nbsp;/&nbsp;$mantot </td>\n" ;
301     print F "<td>";
302     for my $HH ( sort(keys(%manhosts)) ) {
303         my $upd = $updlinks{$HH} || "#" ;
304         print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
305         #print F "<a href='#$HH'><b>$HH</b></a> ";
306     }
307     print F "</td>";
308     print F "<td>";
309     for my $PP ( sort(keys(%manpkgs)) ) {
310         print F "<a href='#$PP'>$PP</a> ";
311     }
312     print F "</td>";
313     print F "</tr>\n";
314 }
315 if ( $normtot ) {
316     print F "<tr><td>Normal<br/>" . scalar(keys(%normhosts)) . 
317         "&nbsp;/&nbsp;" .  scalar(keys(%normpkgs)) . "&nbsp;/&nbsp;$normtot </td>\n" ;
318     print F "<td>";
319     for my $HH ( sort(keys(%normhosts)) ) {
320         my $upd = $updlinks{$HH} || "#" ;
321         print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
322         #print F "<a href='#$HH'><b>$HH</b></a> ";
323     }
324     print F "</td>";
325     print F "<td>";
326     for my $PP ( sort(keys(%normpkgs)) ) {
327         print F "<a href='#$PP'>$PP</a> ";
328     }
329     print F "</td>";
330     print F "</tr>\n";
331 }
332 if ( %skiphosts ) {
333     print F "<tr><td>Skipped " . scalar(keys(%skiphosts)) . "</td>\n";
334     print F "<td colspan='2'>";
335     for my $HH ( sort(keys(%skiphosts)) ) {
336         my $upd = $updlinks{$HH} ||
337                   $wikilink . ucfirst($HH) . "Updates" . $year;
338         print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
339         #print F "<a href='#$HH'><b>$HH</b></a> ";
340     }
341     print F "</td></tr>\n";
342 }
343 #if ( %okhosts ) {
344 if ( 1 ) {
345     print F "<tr><td>Ok " . scalar(keys(%okhosts)) . "</td>\n";
346     print F "<td colspan='2'>";
347     for my $HH ( sort(keys(%okhosts)) ) {
348         my $upd = $updlinks{$HH} || "#" ;
349         print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
350         #print F "<a href='#$HH'><b>$HH</b></a> ";
351     }
352     if ( !%okhosts ) {
353         print F "<b>None at all!</b>";
354     }
355     print F "</td></tr>\n";
356 }
357 print F "</table>\n";
358
359 # Graph 
360 #my $secs = 60*60*24 * 7 * 2; # 2 weeks in secods
361 #my $secs = "1m"; # one month, let nagios do the math
362 my $secs = "45d"; 
363 print F "<p/>" .
364         "<a href='http://nagios.indexdata.com/cgi-bin/nagios3/graphs.cgi?" .
365         "host=nagios&service=Apt%20Summary'>\n".
366         "<img src='http://nagios.indexdata.com/" .
367               "cgi-bin/nagios3/rrd2-system.cgi?" .
368               "host=nagios&service=Apt%20Summary&" .
369               "start=-$secs&" .
370               "width=800&height=100&type=AVERAGE' /> ".
371         "</a>" .
372         "<br/>\n";
373
374 # The host table
375 print F $table;
376
377 # Package table
378 print F "<p/><b><u>Packages</u></b>\n";
379 print F "<table>\n";
380 for my $P ( sort(keys(%summary)) ) {
381     my $PN = $P;
382     $PN = "<b>$P&nbsp;(s)</b>" if ($secpkgs{$P});
383     $PN = "<i>$P&nbsp;(id)</i>" if ($ownpkgs{$P});
384     $PN = "$P&nbsp;<b>(M)</b>" if ($manpkgs{$P});
385     print F "<tr><td><a name='$P'/>$PN</td>\n";
386     print F "<td>";
387     for my $HH ( split(' ',$summary{$P} )) {
388         print F "<a href=#$HH>$HH</a> ";
389     }
390     print F "</td>\n";
391
392 }
393 print F "</table>\n";
394
395 print F "<p/>Produced " . `date`.
396         " on " . `hostname` . " by " . `whoami` .
397         "<br/>\n";
398 print F "</body></html>\n";
399
400 close(F)
401     or die "Could not close $outfile: $!";
402
403 system "scp -q $outfile nagios:/var/www/heikki/index.html";
404
405 exit(0);
406
407 # Helper to take two strings and highligt that part of the second
408 # that is different from the first. 
409 sub strdiff {
410     my $x = shift;
411     my $y = shift;
412     print "strdiff: '$x' '$y' \n" if $debug>2;
413     if ( $x eq $y ) {
414         return "$x <b>??</b>";
415     }
416     my $a = 0;
417     while ( $a < length($y) &&
418         substr($x,$a,1) eq substr($y,$a,1) ) {
419         $a++;
420     }
421     if ( $a == length($y) ) {
422         return "$y";
423     }
424     my $b = 1;
425     while ( $b < length($y)-$a &&
426         substr($x,-$b,1) eq substr($y, -$b,1) ) {
427         $b++;
428     }
429     my $c = length($y) - $b +1;
430     print "strdiff:   a=$a " . substr($y,0,$a) ."\n" if $debug>2;
431     print "strdiff:   b=$b " . "\n" if $debug>2;
432     print "strdiff:   c=$c " . substr($y,$c) ."\n" if $debug>2;
433     print "strdiff:        " . substr($y,$a, $c-$a) ."\n" if $debug>2;
434     my $z =  substr($y,0,$a) .
435              "<b>" . substr($y,$a, $c-$a) . "</b>" .
436              substr($y,$c);
437     print "strdiff:        " . $z ."\n" if $debug>2;
438     print "\n" if $debug>2;
439     return $z;
440 }