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</b>";
76 } elsif ( $src =~ /Indexdata/ ) {
77 $det .= "<b><i>$pkg</i></b>";
89 if ( !$summary{$key} ) {
92 $new = strdiff($cur,$new);
93 $cur = strdiff($new,$cur);
94 $summary{$key} .= "$H ";
96 $det .= "<td>$cur</td> ";
97 $det .= "<td>$new</td> ";
100 $table .= "<tr><td colspan='3'> </td></tr>\n";
101 $table .= "<tr><td colspan='3'><a name='$H'><b><u>$H</u></b></a> \n";
103 $table .= "<b>$pkgs</b> packages to upgrade. ";
104 $table .= "<b>$secs security</b>. " if $secs;
105 $table .= " $own from indexdata " if $own;
110 my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
111 $table .= " <a href='$updlink' >Upd</a>";
112 $table .= "</td></tr>\n";
113 $table .= $det if $pkgs;
114 print "\n$table\n" if $debug>2;
115 last if $H =~/dart/ && $debug;
117 $table .= "</table>\n";
120 my $outfile = "/tmp/aptcheck.html";
122 or die "Could not open $outfile for writing: $!";
124 print F "<head><title>Apt upgrade status</title></head>\n";
126 print F "<H1>Apt package status</H1>\n";
129 # Summary table - NEW: one row for per host group
131 print F "<table border='1' >\n";
132 print F "<tr><td> </td>" ;
133 print F "<td><b>Hosts</b></td>\n";
134 print F "<td><b>Packages</b></td></tr>\n";
137 print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) .
138 " / " . scalar(keys(%secpkgs)) . " / $sectot </td>\n" ;
140 for $HH ( sort(keys(%sechosts)) ) {
141 print F "<a href='#$HH'><b>$HH</b></a> ";
143 print F "<td>" . join(" ",sort(keys(%secpkgs))) . " </td>";
147 print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) .
148 " / " . scalar(keys(%ownpkgs)) . " / $owntot </td>\n" ;
150 for $HH ( sort(keys(%ownhosts)) ) {
151 print F "<a href='#$HH'><b>$HH</b></a> ";
153 print F "<td>" . join(" ",sort(keys(%ownpkgs))) . " </td>";
157 print F "<tr><td>Indexdata<br/>" . scalar(keys(%normhosts)) .
158 " / " . scalar(keys(%normpkgs)) . " / $normtot </td>\n" ;
160 for $HH ( sort(keys(%normhosts)) ) {
161 print F "<a href='#$HH'><b>$HH</b></a> ";
163 print F "<td>" . join(" ",sort(keys(%normpkgs))) . " </td>";
167 print F "<tr><td>Skipped: " . scalar(keys(%skiphosts)) . "</td>\n";
168 print F "<td colspan='2'>";
169 for $HH ( sort(keys(%skiphosts)) ) {
170 print F "<a href='#$HH'><b>$HH</b></a> ";
172 print F "</td></tr>\n";
175 print F "<tr><td>Ok: " . scalar(keys(%okhosts)) . "</td>\n";
176 print F "<td colspan='2'>";
177 for $HH ( sort(keys(%okhosts)) ) {
178 print F "<a href='#$HH'><b>$HH</b></a> ";
180 print F "</td></tr>\n";
182 print F "</table>\n";
188 print F "<p/>Produced " . `date`.
189 " on " . `hostname` . " by " . `whoami` .
191 print F "</body></html>\n";
194 or die "Could not close $outfile: $!";
196 system "scp -q $outfile nagios:/var/www/heikki/index.html";
200 # Helper to take two strings and highligt that part of the second
201 # that is different from the first.
205 print "strdiff: '$x' '$y' \n" if $debug>1;
207 while ( $a < length($y) &&
208 substr($x,$a,1) eq substr($y,$a,1) ) {
211 if ( $a == length($y) ) {
215 while ( $b < length($y)-$a &&
216 substr($x,-$b,1) eq substr($y, -$b,1) ) {
219 my $c = length($y) - $b +1;
220 print "strdiff: a=$a " . substr($y,0,$a) ."\n" if $debug>1;
221 print "strdiff: b=$b " . "\n" if $debug>1;
222 print "strdiff: c=$c " . substr($y,$c) ."\n" if $debug>1;
223 print "strdiff: " . substr($y,$a, $c-$a) ."\n" if $debug>1;
224 my $z = substr($y,0,$a) .
225 "<b>" . substr($y,$a, $c-$a) . "</b>" .
227 print "strdiff: " . $z ."\n" if $debug>1;
228 print "\n" if $debug>1;