Cross-ref table by package name
[git-tools-moved-to-github.git] / aptcheck / aptcheck.pl
1 #!/usr/bin/perl -w
2 #
3 # Check what packages are needed to get upgraded on all machines
4 #
5 # Depends heavily on having ssh key authentication set up to all
6 # boxes. That's why I run it on my own workstation.
7 #
8 # 11-Mar-2011 Heikki: Started this
9
10 #### Init
11 my $debug= $ARGV[0] || 0; # 0=none, 1=some, 2=more, 3=much
12 my $year =`date +%Y`;
13 my $wikilink = 'http://twiki.indexdata.dk/cgi-bin/twiki/view/ID/';
14
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.
18
19 my $hostlist = `ssh nagios grep -l Apt /etc/nagios3/indexdata-conf.d/*.cfg`
20   or die "Could not get host list";
21
22 print "Got list:\n$hostlist\n" if $debug>2;
23
24 # Statistics
25 my %summary;
26 my %sechosts;
27 my %secpkgs;
28 my %ownhosts;
29 my %ownpkgs;
30 my %normhosts;
31 my %normpkgs;
32 my %okhosts;
33 my %skiphosts;
34 my %allhosts;
35 my $sectot = 0;
36 my $owntot = 0;
37 my $normtot = 0;
38
39 my $table = "<table>\n";
40
41 for $hline ( split("\n",$hostlist) ) {
42     next unless ( $hline =~ /\/([a-z0-9-]+)\.cfg$/ );
43     my $H = $1;
44     next if ($H =~ /^commands/ );
45     next if ($H =~ /^servicegroups/ );
46     print "Checking $H\n" if $debug;
47     $allhosts{$H}=1;
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
50     if ( !$apt ) {
51         $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
52         $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
53         $skiphosts{$H}=1;
54         next;
55     }
56     print "Got apts for $H: \n$apt\n" if $debug>2;
57     my $det = "";
58     my $pkgs = 0;
59     my $secs = 0;
60     my $own = 0;
61     for $p ( split("\n",$apt) ) {
62         next unless $p =~
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>&nbsp;&nbsp;";
67         $pkgs++;
68         my $key = $pkg;
69         if ( $src =~ /Security/ ) {
70             $det .= "<b>$pkg (s)</b> ";
71             $sechosts{$H}=1;
72             $secpkgs{$pkg}=1;
73             $secs++;
74             $sectot++;
75         } elsif ( $src =~ /Indexdata/ ) {
76             $det .= "<i><b>$pkg</b> (id) </i>";
77             $ownhosts{$H}=1;
78             $ownpkgs{$pkg}=1;
79             $own++;
80             $owntot++;
81         } else {
82             $det .= "$pkg ";
83             $normhosts{$H}=1;
84             $normpkgs{$pkg}=1;
85             $normtot++;
86         }
87         if ( !$summary{$key} ) {
88             $summary{$key} = "";
89         }
90         $summary{$key} .= "$H ";
91         $new = strdiff($cur,$new);
92         $cur = strdiff($new,$cur);
93         $det .= "</td> ";
94         $det .= "<td>$cur</td> ";
95         $det .= "<td>$new</td> ";
96         $det .= "</tr>\n";
97     }
98     $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
99     $table .= "<tr><td colspan='3'><a name='$H'><b><u>$H</u></b></a> &nbsp;\n";
100     if ( $pkgs ) {
101         $table .= "<b>$pkgs</b> packages to upgrade. ";
102         $table .= "<b>$secs security</b>. " if $secs;
103         $table .= " $own from indexdata " if $own;
104     } else {
105         $table .= "ok";
106         $okhosts{$H} = 1;
107     }
108     my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
109     $table .= "&nbsp;<a href='$updlink' >Upd</a>";
110     $table .= "</td></tr>\n";
111     $table .= $det if $pkgs;
112     print "\n$table\n" if $debug>2;
113     last if $H =~/dart/ && $debug;
114 }
115 $table .= "</table>\n";
116
117 # Page header
118 my $outfile = "/tmp/aptcheck.html";
119 open F, ">$outfile"
120     or die "Could not open $outfile for writing: $!";
121 print F "<html>\n";
122 print F "<head><title>Apt upgrade status</title></head>\n";
123 print F "<body>\n";
124 print F "<H1>Apt package status</H1>\n";
125
126
127 # Summary table: one row for per host group
128 print F "<p/>\n";
129 print F "<table border='1' >\n";
130 print F "<tr><td>&nbsp;</td>" ;
131 print F "<td><b>Hosts</b></td>\n";
132 print F "<td><b>Packages</b></td></tr>\n";
133
134 if ( $sectot ) {
135     print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) . 
136         " / " .  scalar(keys(%secpkgs)) . " / $sectot </td>\n" ;
137     print F "<td>";
138     for $HH ( sort(keys(%sechosts)) ) {
139         print F "<a href='#$HH'><b>$HH</b></a> ";
140     }
141     print F "</td>";
142     print F "<td>";
143     for $PP ( sort(keys(%secpkgs)) ) {
144         print F "<a href='#$PP'>$PP</a> ";
145     }
146     print F "</td>";
147     print F "</tr>\n";
148 }
149 if ( $owntot ) {
150     print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) . 
151         " / " .  scalar(keys(%ownpkgs)) . " / $owntot </td>\n" ;
152     print F "<td>";
153     for $HH ( sort(keys(%ownhosts)) ) {
154         print F "<a href='#$HH'><b>$HH</b></a> ";
155     }
156     print F "</td>";
157     print F "<td>";
158     for $PP ( sort(keys(%ownpkgs)) ) {
159         print F "<a href='#$PP'>$PP</a> ";
160     }
161     print F "</td>";
162     #print F "<td>" . join(" ",sort(keys(%ownpkgs))) . "&nbsp;</td>";
163     print F "</tr>\n";
164 }
165 if ( $normtot ) {
166     print F "<tr><td>Indexdata<br/>" . scalar(keys(%normhosts)) . 
167         " / " .  scalar(keys(%normpkgs)) . " / $normtot </td>\n" ;
168     print F "<td>";
169     for $HH ( sort(keys(%normhosts)) ) {
170         print F "<a href='#$HH'><b>$HH</b></a> ";
171     }
172     print F "<td>" . join(" ",sort(keys(%normpkgs))) . "&nbsp;</td>";
173     print F "</tr>\n";
174 }
175 if ( %skiphosts ) {
176     print F "<tr><td>Skipped: " . scalar(keys(%skiphosts)) . "</td>\n";
177     print F "<td colspan='2'>";
178     for $HH ( sort(keys(%skiphosts)) ) {
179         print F "<a href='#$HH'><b>$HH</b></a> ";
180     }
181     print F "</td></tr>\n";
182 }
183 if ( %okhosts ) {
184     print F "<tr><td>Ok: " . scalar(keys(%okhosts)) . "</td>\n";
185     print F "<td colspan='2'>";
186     for $HH ( sort(keys(%okhosts)) ) {
187         print F "<a href='#$HH'><b>$HH</b></a> ";
188     }
189     print F "</td></tr>\n";
190 }
191 print F "</table>\n";
192
193
194 # The host table
195 print F $table;
196
197 # Package table
198 print F "<p/><b><u>Packages</u></b>\n";
199 print F "<table>\n";
200 for $P ( sort(keys(%summary)) ) {
201     my $PN = $P;
202     $PN = "<b>$P (s)</b>" if ($secpkgs{$P});
203     $PN = "$P (id)" if ($ownpkgs{$P});
204     print F "<tr><td><a name='$P'/>$PN</td>\n";
205     print F "<td>";
206     for $HH ( split(' ',$summary{$P} )) {
207         print F "<a href=#$HH>$HH</a> ";
208     }
209     print F "</td>\n";
210
211 }
212 print F "</table>\n";
213
214 print F "<p/>Produced " . `date`.
215         " on " . `hostname` . " by " . `whoami` .
216         "<br/>\n";
217 print F "</body></html>\n";
218
219 close(F)
220     or die "Could not close $outfile: $!";
221
222 system "scp -q $outfile nagios:/var/www/heikki/index.html";
223
224 exit(0);
225
226 # Helper to take two strings and highligt that part of the second
227 # that is different from the first. 
228 sub strdiff {
229     my $x = shift;
230     my $y = shift;
231     print "strdiff: '$x' '$y' \n" if $debug>1;
232     my $a = 0;
233     while ( $a < length($y) &&
234         substr($x,$a,1) eq substr($y,$a,1) ) {
235         $a++;
236     }
237     if ( $a == length($y) ) {
238         return "$y ???";
239     }
240     my $b = 1;
241     while ( $b < length($y)-$a &&
242         substr($x,-$b,1) eq substr($y, -$b,1) ) {
243         $b++;
244     }
245     my $c = length($y) - $b +1;
246     print "strdiff:   a=$a " . substr($y,0,$a) ."\n" if $debug>1;
247     print "strdiff:   b=$b " . "\n" if $debug>1;
248     print "strdiff:   c=$c " . substr($y,$c) ."\n" if $debug>1;
249     print "strdiff:        " . substr($y,$a, $c-$a) ."\n" if $debug>1;
250     my $z =  substr($y,0,$a) .
251              "<b>" . substr($y,$a, $c-$a) . "</b>" .
252              substr($y,$c);
253     print "strdiff:        " . $z ."\n" if $debug>1;
254     print "\n" if $debug>1;
255     return $z;
256 }