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