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;
38 my $table = "<table>\n";
40 for $hline ( split("\n",$hostlist) ) {
41 next unless ( $hline =~ /\/([a-z0-9-]+)\.cfg$/ );
43 next if ($H =~ /^commands/ );
44 next if ($H =~ /^servicegroups/ );
45 print "Checking $H\n" if $debug;
46 my $apt = `ssh $H apt-get upgrade -s -o 'Debug::NoLocking=true' `;
47 # Note, do not append -qq, we want some output even when nothing to do
49 $table .= "<tr><td colspan='3'> </td></tr>\n";
50 $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
54 print "Got apts for $H: \n$apt\n" if $debug>2;
59 for $p ( split("\n",$apt) ) {
61 /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
62 my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
63 print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
64 $det .= "<tr><td> ";
67 if ( $src =~ /Security/ ) {
68 $det .= "<b>$pkg</b>";
74 } elsif ( $src =~ /Indexdata/ ) {
75 $det .= "<b><i>$pkg</i></b>";
87 if ( !$summary{$key} ) {
90 $new = strdiff($cur,$new);
91 $cur = strdiff($new,$cur);
92 $summary{$key} .= "$H ";
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;
107 my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
108 $table .= " <a href='$updlink' >Upd</a>";
109 $table .= "</td></tr>\n";
110 $table .= $det if $pkgs;
111 print "\n$table\n" if $debug>2;
112 last if $H =~/dart/ && $debug;
114 $table .= "</table>\n";
117 my $outfile = "/tmp/aptcheck.html";
119 or die "Could not open $outfile for writing: $!";
121 print F "<head><title>Apt upgrade status</title></head>\n";
123 print F "<H1>Apt package status</H1>\n";
127 print F "<table border='1' >\n";
128 print F "<tr><td> </td>" ;
129 print F "<td><b>Security</b><br/>" . scalar(keys(%sechosts)) .
130 " / " . scalar(keys(%secpkgs)) . " / $sectot </td>\n" ;
131 print F "<td>Indexdata<br/>" . scalar(keys(%ownhosts)) .
132 " / " . scalar(keys(%ownpkgs)) . " / $owntot </td>\n" ;
133 print F "<td>Normal<br/>" . scalar(keys(%normhosts)) .
134 " / " . scalar(keys(%normpkgs)) . " / $normtot </td>" . "</tr>\n";
135 print F "<tr><td>Hosts</td>\n";
139 for $HH ( sort(keys(%sechosts)) ) {
140 print F "<a href='#$HH'><b>$HH</b></a> ";
142 print F " </td>\n";
143 #print F "<td><b>" . join(" ",sort(keys(%sechosts))) . "</b> </td>";
144 #print F "<td><b>" . join(" ",sort(keys(%ownhosts))) . "</b> </td>";
146 for $HH ( sort(keys(%ownhosts)) ) {
147 print F "<a href='#$HH'><b>$HH</b></a> ";
149 print F " </td>\n";
150 #print F "<td><b>" . join(" ",sort(keys(%normhosts))) . "</b> </td></tr>\n";
152 for $HH ( sort(keys(%normhosts)) ) {
153 print F "<a href='#$HH'><b>$HH</b></a> ";
155 print F " </td>\n";
157 print F "<tr><td>Packages</td>\n";
158 print F "<td>" . join(" ",sort(keys(%secpkgs))) . " </td>";
159 print F "<td>" . join(" ",sort(keys(%ownpkgs))) . " </td>";
160 print F "<td>" . join(" ",sort(keys(%normpkgs))) . " </td></tr>\n";
161 print F "</table>\n";
165 print F "<p/>Produced " . `date`.
166 " on " . `hostname` . " by " . `whoami` .
168 print F "</body></html>\n";
171 or die "Could not close $outfile: $!";
173 system "scp -q $outfile nagios:/var/www/heikki/index.html";
175 # Helper to take two strings and highligt that part of the second
176 # that is different from the first.
180 print "strdiff: '$x' '$y' \n" if $debug>1;
182 while ( $a < length($y) &&
183 substr($x,$a,1) eq substr($y,$a,1) ) {
186 if ( $a == length($y) ) {
190 while ( $b < length($y)-$a &&
191 substr($x,-$b,1) eq substr($y, -$b,1) ) {
194 my $c = length($y) - $b +1;
195 print "strdiff: a=$a " . substr($y,0,$a) ."\n" if $debug>1;
196 print "strdiff: b=$b " . "\n" if $debug>1;
197 print "strdiff: c=$c " . substr($y,$c) ."\n" if $debug>1;
198 print "strdiff: " . substr($y,$a, $c-$a) ."\n" if $debug>1;
199 my $z = substr($y,0,$a) .
200 "<b>" . substr($y,$a, $c-$a) . "</b>" .
202 print "strdiff: " . $z ."\n" if $debug>1;
203 print "\n" if $debug>1;