3 # Check what packages are needed to get upgraded on all machines
5 # Depends heavily on having ssh key authentication set up to all
6 # boxes. That's why I run it on my own workstation.
8 # Regular debian upgrades are detected by running
10 # on every machine, and parsing the output.
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.
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.
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.
33 my $debug= $ARGV[0] || 0; # 0=none, 1=some, 2=more, 3=much
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";
41 "ariel" => "<i>Niels Erik</i> does the manual upgrades",
42 "bellone" => "<i>Niels Erik</i> does the manual upgrades",
43 "cfrepous" => "<i>Wolfram</i> does the manual upgrades",
44 "leopard" => "<i>Wolfram</i> does the manual upgrades",
45 "lsd" => "<i>Heikki</i> takes care of all upgrades",
49 #### Get list of hosts
50 # I could use a hard-coded list, but I would forget to maintain it.
51 # Nagios knows most of our hosts. It even knows which are worth
52 # checking, they have a command to check apts!
53 print "Getting hostlist from nagios\n" if $debug;
54 my $hostlist1 = `ssh nagios grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
55 or die "Could not get host list from nagios (dk)";
57 print "Getting hostlist from nagios-us\n" if $debug;
58 my $hostlist2 = `ssh nagios-us grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
59 or die "Could not get host list from nagios (dk)";
61 my $hostlist = $hostlist1 . $hostlist2;
62 print "Got list:\n$hostlist\n" if $debug>2;
64 ###### Get list of packages that can be manually maintained
65 print "getting restricted package versions\n" if $debug;
67 my $restplines = `$restrictedpackages`
68 or die "Could not get the list of restricted packages " .
69 "from $restrictedpackages: $! ";
70 print "Got package list: \n$restplines\n" if $debug>2;
73 for my $pline ( split("\n",$restplines) ) {
75 $pname = $1 if $pline =~ /^Package:\s+(\S*)\s*$/;
76 $pver = $1 if $pline =~ /^Version:\s+(\S*)\s*$/;
77 print "$pline: p=$pname v=$pver\n" if $debug>2;
78 if ( $pname && $pver ) {
79 print "\nPackage $pname version $pver \n" if $debug>2;
80 if ( ! $restrpkgs{$pname} ) {
81 $restrpkgs{$pname} = $pver;
82 print "found $pname, first version $pver\n" if $debug>1;
84 my $bver = $restrpkgs{$pname};
85 `dpkg --compare-versions "$bver" lt "$pver" 2>/dev/null `;
87 print "found $pname, better version $pver (better than $bver)\n"
89 $restrpkgs{$pname} = $pver;
91 print "found $pname, but version $pver is no better than $bver\n"
95 $pname = ""; # clear for the next one.
100 print "got " . scalar(keys(%restrpkgs)) . " restricted packages\n" if $debug;
102 for $pname ( sort (keys(%restrpkgs)) ) {
103 print " $pname " . $restrpkgs{$pname} . "\n";
109 my ( %sechosts, %secpkgs );
110 my ( %ownhosts, %ownpkgs );
111 my ( %manhosts, %manpkgs );
112 my ( %normhosts, %normpkgs );
123 # Pending modification dates
124 my %olddates; # Read in from the file
125 my %newdates; # To be written in the new version of the file
126 my $datefilename = "aptcheck.data";
127 my $dateoldfilename = "aptcheck.old";
128 my $thisdate = "*"; # indicates really old stuff
129 my $warndate; # Older than this will be boldfaced
131 if ( -f $datefilename ) {
132 print "Reading dates from $datefilename\n" if $debug;
133 open F, $datefilename or die "Could not open date file $datefilename: $!";
136 my ($pkg, $date) = split;
137 next unless $pkg; # skip empty lines
138 $olddates{$pkg} = $date;
139 print "Date for '$pkg' is '$date' \n" if $debug;
142 $thisdate = `date +%F`;
144 $warndate = `date +%F -d "30 days ago"` ; ;
146 print "Dates: now: '$thisdate' warn: '$warndate'\n" if $debug;
148 print "No datefile $datefilename found, starting from scratch\n";
152 my $table = "<table>\n";
154 #for my $hline ( split("\n",$hostlist) ) {
155 for my $hline ( sort( split("\n",$hostlist) ) ) {
156 next unless ( $hline =~ /\/([a-z0-9-]+)\.cfg$/ );
158 next if ($H =~ /^commands/ );
159 next if ($H =~ /^servicegroups/ );
160 print "Checking $H\n" if $debug;
162 my $cmd0 = "cat /etc/debian_version";
163 my $cmd1 = "apt-cache -q policy " . join(" ",sort(keys(%restrpkgs)));
164 my $cmd2 = "apt-get upgrade -s -o 'Debug::NoLocking=true' ";
165 # Note, do not append -qq, we want some output even when nothing to do
166 print "ssh -q $H \"$cmd0; $cmd1 ; $cmd2 \" 2>/dev/null \n" if ($debug>1);
167 my $apt = `ssh -q $H "$cmd0; $cmd1 ; $cmd2 " 2>/dev/null`;
169 $table .= "<tr><td colspan='3'> </td></tr>\n";
170 $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
174 print "Got apts for $H: \n$apt\n" if $debug>2;
175 my $det = ""; # detail lines
184 for my $p ( split("\n",$apt) ) {
185 if ( !$debver ) { # first line
187 $p =~ s/(5[0-9.]+)/$1 LENNY !!!/;
188 $p =~ s/(6[0-9.]+)/$1 squeeze/;
189 $p =~ s/(7[0-9.]+)/$1 wheezy/;
190 $p = " Debian $p";
191 $debversions{$H} = $p;
192 print "Deb version for $H is $p\n" if ($debug>1);
195 # parse apt-cache output
196 $restrname = $1 if $p =~ /^(\S+):$/;
197 $restrinst = $1 if $p =~ /^\s+Installed:\s+(\S+)$/;
198 $restrcand = $1 if $p =~ /^\s+Candidate:\s+(\S+)$/;
199 if ( $p =~ /^\s+Version table:/ ) { # have all for that package
200 my $bver = $restrpkgs{$restrname};
201 if ( ( $restrinst eq $restrcand ) &&
202 ( $restrinst ne $bver ) ) {
203 # if different, it is a regular apt upgrade, and will be seen
204 # later. AND we want to have a different version in our repo
205 `dpkg --compare-versions "$bver" lt "$restrinst" 2>/dev/null`;
206 if ( $? ) { # It was not a downgrade
207 # manual packages may be ahead of the repo!
212 $manpkgs{$restrname} = 1;
214 $det .= "<td> $restrname <b>(M)</b></td>";
215 $det .= "<td>". strdiff($bver,$restrinst)."</td>";
216 $det .= "<td>". strdiff($restrinst,$bver)."</td>";
217 my $datekey = "$H:$restrname";
218 if ( $olddates{$datekey} ) {
219 $newdates{$datekey} = $olddates{$datekey};
221 $newdates{$datekey} = $thisdate;
223 my $dispdate = $newdates{$datekey};
224 # if ( $dispdate lt $warndate ) {
225 if ( 0 ) { # manual packages don't need to be highlighted
226 $dispdate = "<b>$dispdate !</b>";
228 $det .= "<td>" . $dispdate . "</td>";
230 my $key = "$restrname";
231 if ( !$summary{$key} ) {
234 $summary{$key} .= "$H ";
237 $restrname = ""; # clear for next round
242 /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
243 my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
244 print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
245 $det .= "<tr><td> ";
248 if ( $src =~ /Security/ ) {
249 $det .= "<b>$pkg (s)</b> ";
254 } elsif ( $src =~ /Indexdata/ ) {
255 $det .= "<i><b>$pkg</b> (id) </i>";
266 if ( !$summary{$key} ) {
269 $summary{$key} .= "$H ";
270 $new = strdiff($cur,$new);
271 $cur = strdiff($new,$cur);
273 $det .= "<td>$cur</td> ";
274 $det .= "<td>$new</td> ";
275 my $datekey = "$H:$pkg";
276 if ( $olddates{$datekey} ) {
277 $newdates{$datekey} = $olddates{$datekey};
279 $newdates{$datekey} = $thisdate;
281 my $dispdate = $newdates{$datekey};
282 if ( ( $dispdate lt $warndate ) && ( $src =~ /Security/) ) {
283 $dispdate = "<b>$dispdate !</b>";
285 $det .= "<td>" . $dispdate . "</td>";
289 $table .= "<tr><td colspan='4'> </td></tr>\n";
290 $table .= "<tr><td colspan='4'><a name='$H'><b><u>$H</u></b></a> \n";
292 $table .= "<b>$pkgs</b> packages to upgrade. ";
293 $table .= "<b>$secs security</b>. " if $secs;
294 $table .= " $own from indexdata. " if $own;
295 $table .= " $man manual. " if $man;
300 my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
301 # Fix some pages that do not follow the convention.
302 # Mostly because the host names would not make proper WikiWords
303 $updlink =~ s/Bugzilla3Updates/BugzillaUpdates/;
304 $updlink =~ s/Opencontent-solrUpdates/OpenContentSolrUpdates/;
305 $updlinks{$H} = $updlink;
306 $table .= " <a href='$updlink' >Upd</a>";
307 $table .= " " . $debversions{$H};
308 $table .= "</td></tr>\n";
309 $table .= "<tr><td>$hostcomments{$H}</td></tr>\n"
310 if ( $hostcomments{$H} );
311 $table .= $det if $pkgs;
312 print "\n$table\n" if $debug>2;
313 last if $H =~/diane/ && $debug;
315 $table .= "</table>\n";
319 `mv -f $datefilename $dateoldfilename`;
320 open F, ">$datefilename" or die "Could not open date file $datefilename for writing";
321 for my $k (sort(keys(%newdates)) ) {
322 print F "$k " . $newdates{$k}. "\n";
323 print "date for '$k' '" . $newdates{$k}. "'\n" if $debug;
326 or die "Could not close date file $datefilename: $!";
328 print "Not updating the date file, this is a debug run\n";
332 my $outfile = "/tmp/aptcheck.html";
334 or die "Could not open $outfile for writing: $!";
336 print F "<head><title>Apt upgrade status</title></head>\n";
338 print F "<H1>Apt package status</H1>\n";
339 print F "<b>" . ( $sectot + $owntot + $mantot + $normtot ) .
340 "</b> packages pending (<b>$sectot</b> critical) \n";
342 print F "<H2>Debug run, many hosts missing!</H2>\n"
346 # Summary table: one row for per host group
348 print F "<table border='1' >\n";
349 print F "<tr><td> </td>" ;
350 print F "<td><b>Hosts</b></td>\n";
351 print F "<td><b>Packages</b></td></tr>\n";
354 print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) .
355 " / " . scalar(keys(%secpkgs)) . " / $sectot </td>\n" ;
357 for my $HH ( sort(keys(%sechosts)) ) {
358 my $upd = $updlinks{$HH} || "#" ;
359 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
363 for my $PP ( sort(keys(%secpkgs)) ) {
364 print F "<a href='#$PP'>$PP</a> ";
370 print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) .
371 " / " . scalar(keys(%ownpkgs)) . " / $owntot </td>\n" ;
373 for my $HH ( sort(keys(%ownhosts)) ) {
374 my $upd = $updlinks{$HH} || "#" ;
375 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
376 #print F "<a href='#$HH'><b>$HH</b></a> ";
380 for my $PP ( sort(keys(%ownpkgs)) ) {
381 print F "<a href='#$PP'>$PP</a> ";
387 print F "<tr><td><b>Manual</b><br/>" . scalar(keys(%manhosts)) .
388 " / " . scalar(keys(%manpkgs)) . " / $mantot </td>\n" ;
390 for my $HH ( sort(keys(%manhosts)) ) {
391 my $upd = $updlinks{$HH} || "#" ;
392 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
393 #print F "<a href='#$HH'><b>$HH</b></a> ";
397 for my $PP ( sort(keys(%manpkgs)) ) {
398 print F "<a href='#$PP'>$PP</a> ";
404 print F "<tr><td>Normal<br/>" . scalar(keys(%normhosts)) .
405 " / " . scalar(keys(%normpkgs)) . " / $normtot </td>\n" ;
407 for my $HH ( sort(keys(%normhosts)) ) {
408 my $upd = $updlinks{$HH} || "#" ;
409 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
410 #print F "<a href='#$HH'><b>$HH</b></a> ";
414 for my $PP ( sort(keys(%normpkgs)) ) {
415 print F "<a href='#$PP'>$PP</a> ";
421 print F "<tr><td>Skipped " . scalar(keys(%skiphosts)) . "</td>\n";
422 print F "<td colspan='2'>";
423 for my $HH ( sort(keys(%skiphosts)) ) {
424 my $upd = $updlinks{$HH} ||
425 $wikilink . ucfirst($HH) . "Updates" . $year;
426 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
427 #print F "<a href='#$HH'><b>$HH</b></a> ";
429 print F "</td></tr>\n";
433 print F "<tr><td>Ok " . scalar(keys(%okhosts)) . "</td>\n";
434 print F "<td colspan='2'>";
435 for my $HH ( sort(keys(%okhosts)) ) {
436 my $upd = $updlinks{$HH} || "#" ;
437 print F "<a href='#$HH'><b>$HH</b></a><a href='$upd'>,</a> ";
438 #print F "<a href='#$HH'><b>$HH</b></a> ";
441 print F "<b>None at all!</b>";
443 print F "</td></tr>\n";
445 print F "</table>\n";
448 #my $secs = 60*60*24 * 7 * 2; # 2 weeks in secods
449 #my $secs = "1m"; # one month, let nagios do the math
452 "<a href='http://nagios.indexdata.com/cgi-bin/nagios3/graphs.cgi?" .
453 "host=nagios&service=Apt%20Summary'>\n".
454 "<img src='http://nagios.indexdata.com/" .
455 "cgi-bin/nagios3/rrd2-system.cgi?" .
456 "host=nagios&service=Apt%20Summary&" .
458 "width=800&height=100&type=AVERAGE' /> ".
466 print F "<p/><b><u>Packages</u></b>\n";
468 for my $P ( sort(keys(%summary)) ) {
470 $PN = "<b>$P (s)</b>" if ($secpkgs{$P});
471 $PN = "<i>$P (id)</i>" if ($ownpkgs{$P});
472 $PN = "$P <b>(M)</b>" if ($manpkgs{$P});
473 print F "<tr><td><a name='$P'/>$PN</td>\n";
475 for my $HH ( split(' ',$summary{$P} )) {
476 print F "<a href=#$HH>$HH</a> ";
481 print F "</table>\n";
483 print F "<p/>Produced " . `date`.
484 " on " . `hostname` . " by " . `whoami` .
486 print F "</body></html>\n";
489 or die "Could not close $outfile: $!";
491 system "scp -q $outfile nagios:/var/www/heikki/index.html";
495 # Helper to take two strings and highligt that part of the second
496 # that is different from the first.
500 print "strdiff: '$x' '$y' \n" if $debug>2;
502 return "$x <b>??</b>";
505 while ( $a < length($y) &&
506 substr($x,$a,1) eq substr($y,$a,1) ) {
509 if ( $a == length($y) ) {
513 while ( $b < length($y)-$a &&
514 substr($x,-$b,1) eq substr($y, -$b,1) ) {
517 my $c = length($y) - $b +1;
518 print "strdiff: a=$a " . substr($y,0,$a) ."\n" if $debug>2;
519 print "strdiff: b=$b " . "\n" if $debug>2;
520 print "strdiff: c=$c " . substr($y,$c) ."\n" if $debug>2;
521 print "strdiff: " . substr($y,$a, $c-$a) ."\n" if $debug>2;
522 my $z = substr($y,0,$a) .
523 "<b>" . substr($y,$a, $c-$a) . "</b>" .
525 print "strdiff: " . $z ."\n" if $debug>2;
526 print "\n" if $debug>2;