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;
35 my $table = "<table>\n";
37 for $hline ( split("\n",$hostlist) ) {
38 next unless ( $hline =~ /\/([a-z0-9]+)\.cfg$/ );
40 next if ($H =~ /^commands/ );
41 next if ($H =~ /^servicegroups/ );
42 print "Checking $H\n" if $debug;
43 my $apt = `ssh $H apt-get upgrade -s -o 'Debug::NoLocking=true' `;
44 # Note, do not append -qq, we want some output even when nothing to do
46 $table .= "<tr><td colspan='3'> </td></tr>\n";
47 $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
51 print "Got apts for $H: \n$apt\n" if $debug>2;
56 for $p ( split("\n",$apt) ) {
58 /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
59 my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
60 print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
61 $det .= "<tr><td> ";
64 if ( $src =~ /Security/ ) {
65 $det .= "<b>$pkg</b>";
70 } elsif ( $src =~ /Indexdata/ ) {
71 $det .= "<b><i>$pkg</i></b>";
81 if ( !$summary{$key} ) {
84 $new = strdiff($cur,$new);
85 $cur = strdiff($new,$cur);
86 $summary{$key} .= "$H ";
88 $det .= "<td>$cur</td> ";
89 $det .= "<td>$new</td> ";
92 $table .= "<tr><td colspan='3'> </td></tr>\n";
93 $table .= "<tr><td colspan='3'><a name='$H'><b><u>$H</u></b></a> \n";
95 $table .= "<b>$pkgs</b> packages to upgrade. ";
96 $table .= "<b>$secs security</b>. " if $secs;
97 $table .= " $own from indexdata " if $own;
101 my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
102 $table .= " <a href='$updlink' >Upd</a>";
103 $table .= "</td></tr>\n";
104 $table .= $det if $pkgs;
105 print "\n$table\n" if $debug>2;
106 last if $H =~/dart/ && $debug;
108 $table .= "</table>\n";
111 my $outfile = "/tmp/aptcheck.html";
113 or die "Could not open $outfile for writing: $!";
115 print F "<head><title>Apt upgrade status</title></head>\n";
117 print F "<H1>Apt package status</H1>\n";
121 print F "<table border='1' >\n";
122 print F "<tr><td> </td>" ;
123 print F "<td><b>Security:</b> " . scalar(keys(%sechosts)) .
124 " / " . scalar(keys(%secpkgs)) . " </td>\n" ;
125 print F "<td>Indexdata: " . scalar(keys(%ownhosts)) .
126 " / " . scalar(keys(%ownpkgs)) . " </td>\n" ;
127 print F "<td>Normal: " . scalar(keys(%normhosts)) .
128 " / " . scalar(keys(%normpkgs)) . " </td>" . "</tr>\n";
129 print F "<tr><td>Hosts</td>\n";
133 for $HH ( sort(keys(%sechosts)) ) {
134 print F "<a href='#$HH'><b>$HH</b></a> ";
136 print F " </td>\n";
137 #print F "<td><b>" . join(" ",sort(keys(%sechosts))) . "</b> </td>";
138 #print F "<td><b>" . join(" ",sort(keys(%ownhosts))) . "</b> </td>";
140 for $HH ( sort(keys(%ownhosts)) ) {
141 print F "<a href='#$HH'><b>$HH</b></a> ";
143 print F " </td>\n";
144 #print F "<td><b>" . join(" ",sort(keys(%normhosts))) . "</b> </td></tr>\n";
146 for $HH ( sort(keys(%normhosts)) ) {
147 print F "<a href='#$HH'><b>$HH</b></a> ";
149 print F " </td>\n";
151 print F "<tr><td>Packages</td>\n";
152 print F "<td>" . join(" ",sort(keys(%secpkgs))) . " </td>";
153 print F "<td>" . join(" ",sort(keys(%ownpkgs))) . " </td>";
154 print F "<td>" . join(" ",sort(keys(%normpkgs))) . " </td></tr>\n";
155 print F "</table>\n";
159 print F "<p/>Produced " . `date`.
160 " on " . `hostname` . " by " . `whoami` .
162 print F "</body></html>\n";
165 or die "Could not close $outfile: $!";
167 system "scp -q $outfile nagios:/var/www/heikki/index.html";
169 # Helper to take two strings and highligt that part of the second
170 # that is different from the first.
174 print "strdiff: '$x' '$y' \n" if $debug>1;
176 while ( $a < length($y) &&
177 substr($x,$a,1) eq substr($y,$a,1) ) {
180 if ( $a == length($y) ) {
184 while ( $b < length($y)-$a &&
185 substr($x,-$b,1) eq substr($y, -$b,1) ) {
188 my $c = length($y) - $b +1;
189 print "strdiff: a=$a " . substr($y,0,$a) ."\n" if $debug>1;
190 print "strdiff: b=$b " . "\n" if $debug>1;
191 print "strdiff: c=$c " . substr($y,$c) ."\n" if $debug>1;
192 print "strdiff: " . substr($y,$a, $c-$a) ."\n" if $debug>1;
193 my $z = substr($y,0,$a) .
194 "<b>" . substr($y,$a, $c-$a) . "</b>" .
196 print "strdiff: " . $z ."\n" if $debug>1;
197 print "\n" if $debug>1;