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 # 11-Mar-2011 Heikki: Started this
11 my $debug= $ARGV[0] || 0; # 0=none, 1=some, 2=more, 3=much
13 my $wikilink = 'http://twiki.indexdata.dk/cgi-bin/twiki/view/ID/';
15 #### Get list of hosts
16 # I could use a hard-coded list, but I would forget to maintain it.
17 # Nagios knows most of our hosts.
19 my $hostlist = `ssh nagios grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
20 or die "Could not get host list";
22 print "Got list:\n$hostlist\n" if $debug>2;
39 my $table = "<table>\n";
41 for $hline ( split("\n",$hostlist) ) {
42 next unless ( $hline =~ /\/([a-z0-9-]+)\.cfg$/ );
44 next if ($H =~ /^commands/ );
45 next if ($H =~ /^servicegroups/ );
46 print "Checking $H\n" if $debug;
48 my $apt = `ssh $H apt-get upgrade -s -o 'Debug::NoLocking=true' `;
49 # Note, do not append -qq, we want some output even when nothing to do
51 $table .= "<tr><td colspan='3'> </td></tr>\n";
52 $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
56 print "Got apts for $H: \n$apt\n" if $debug>2;
61 for $p ( split("\n",$apt) ) {
63 /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
64 my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
65 print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
66 $det .= "<tr><td> ";
69 if ( $src =~ /Security/ ) {
70 $det .= "<b>$pkg (s)</b> ";
75 } elsif ( $src =~ /Indexdata/ ) {
76 $det .= "<i><b>$pkg</b> (id) </i>";
87 if ( !$summary{$key} ) {
90 $summary{$key} .= "$H ";
91 $new = strdiff($cur,$new);
92 $cur = strdiff($new,$cur);
94 $det .= "<td>$cur</td> ";
95 $det .= "<td>$new</td> ";
98 $table .= "<tr><td colspan='3'> </td></tr>\n";
99 $table .= "<tr><td colspan='3'><a name='$H'><b><u>$H</u></b></a> \n";
101 $table .= "<b>$pkgs</b> packages to upgrade. ";
102 $table .= "<b>$secs security</b>. " if $secs;
103 $table .= " $own from indexdata " if $own;
108 my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
109 # Fix some pages that do not follow the convention.
110 # Mostly because the host names would not make proper WikiWords
111 $updlink =~ s/Bugzilla3Updates/BugzillaUpdates/;
112 $updlink =~ s/Opencontent-solrUpdates/OpenContentSolrUpdates/;
113 $table .= " <a href='$updlink' >Upd</a>";
114 $table .= "</td></tr>\n";
115 $table .= $det if $pkgs;
116 print "\n$table\n" if $debug>2;
117 last if $H =~/dart/ && $debug;
119 $table .= "</table>\n";
122 my $outfile = "/tmp/aptcheck.html";
124 or die "Could not open $outfile for writing: $!";
126 print F "<head><title>Apt upgrade status</title></head>\n";
128 print F "<H1>Apt package status</H1>\n";
131 # Summary table: one row for per host group
133 print F "<table border='1' >\n";
134 print F "<tr><td> </td>" ;
135 print F "<td><b>Hosts</b></td>\n";
136 print F "<td><b>Packages</b></td></tr>\n";
139 print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) .
140 " / " . scalar(keys(%secpkgs)) . " / $sectot </td>\n" ;
142 for $HH ( sort(keys(%sechosts)) ) {
143 print F "<a href='#$HH'><b>$HH</b></a> ";
147 for $PP ( sort(keys(%secpkgs)) ) {
148 print F "<a href='#$PP'>$PP</a> ";
154 print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) .
155 " / " . scalar(keys(%ownpkgs)) . " / $owntot </td>\n" ;
157 for $HH ( sort(keys(%ownhosts)) ) {
158 print F "<a href='#$HH'><b>$HH</b></a> ";
162 for $PP ( sort(keys(%ownpkgs)) ) {
163 print F "<a href='#$PP'>$PP</a> ";
169 print F "<tr><td>Normal<br/>" . scalar(keys(%normhosts)) .
170 " / " . scalar(keys(%normpkgs)) . " / $normtot </td>\n" ;
172 for $HH ( sort(keys(%normhosts)) ) {
173 print F "<a href='#$HH'><b>$HH</b></a> ";
177 for $PP ( sort(keys(%normpkgs)) ) {
178 print F "<a href='#$PP'>$PP</a> ";
184 print F "<tr><td>Skipped " . scalar(keys(%skiphosts)) . "</td>\n";
185 print F "<td colspan='2'>";
186 for $HH ( sort(keys(%skiphosts)) ) {
187 print F "<a href='#$HH'><b>$HH</b></a> ";
189 print F "</td></tr>\n";
192 print F "<tr><td>Ok " . scalar(keys(%okhosts)) . "</td>\n";
193 print F "<td colspan='2'>";
194 for $HH ( sort(keys(%okhosts)) ) {
195 print F "<a href='#$HH'><b>$HH</b></a> ";
197 print F "</td></tr>\n";
199 print F "</table>\n";
206 print F "<p/><b><u>Packages</u></b>\n";
208 for $P ( sort(keys(%summary)) ) {
210 $PN = "<b>$P (s)</b>" if ($secpkgs{$P});
211 $PN = "$P (id)" if ($ownpkgs{$P});
212 print F "<tr><td><a name='$P'/>$PN</td>\n";
214 for $HH ( split(' ',$summary{$P} )) {
215 print F "<a href=#$HH>$HH</a> ";
220 print F "</table>\n";
222 print F "<p/>Produced " . `date`.
223 " on " . `hostname` . " by " . `whoami` .
225 print F "</body></html>\n";
228 or die "Could not close $outfile: $!";
230 system "scp -q $outfile nagios:/var/www/heikki/index.html";
234 # Helper to take two strings and highligt that part of the second
235 # that is different from the first.
239 print "strdiff: '$x' '$y' \n" if $debug>1;
241 return "$x <b>??</b>";
244 while ( $a < length($y) &&
245 substr($x,$a,1) eq substr($y,$a,1) ) {
248 if ( $a == length($y) ) {
252 while ( $b < length($y)-$a &&
253 substr($x,-$b,1) eq substr($y, -$b,1) ) {
256 my $c = length($y) - $b +1;
257 print "strdiff: a=$a " . substr($y,0,$a) ."\n" if $debug>1;
258 print "strdiff: b=$b " . "\n" if $debug>1;
259 print "strdiff: c=$c " . substr($y,$c) ."\n" if $debug>1;
260 print "strdiff: " . substr($y,$a, $c-$a) ."\n" if $debug>1;
261 my $z = substr($y,0,$a) .
262 "<b>" . substr($y,$a, $c-$a) . "</b>" .
264 print "strdiff: " . $z ."\n" if $debug>1;
265 print "\n" if $debug>1;