Turned the table around, added lines
[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</b>";
71             $key = "<b>$pkg</b>";
72             $sechosts{$H}=1;
73             $secpkgs{$pkg}=1;
74             $secs++;
75             $sectot++;
76         } elsif ( $src =~ /Indexdata/ ) {
77             $det .= "<b><i>$pkg</i></b>";
78             $key = "<i>$pkg</i>";
79             $ownhosts{$H}=1;
80             $ownpkgs{$pkg}=1;
81             $own++;
82             $owntot++;
83         } else {
84             $det .= "$pkg";
85             $normhosts{$H}=1;
86             $normpkgs{$pkg}=1;
87             $normtot++;
88         }
89         if ( !$summary{$key} ) {
90             $summary{$key} = "";
91         }
92         $new = strdiff($cur,$new);
93         $cur = strdiff($new,$cur);
94         $summary{$key} .= "$H ";
95         $det .= "</td> ";
96         $det .= "<td>$cur</td> ";
97         $det .= "<td>$new</td> ";
98         $det .= "</tr>\n";
99     }
100     $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
101     $table .= "<tr><td colspan='3'><a name='$H'><b><u>$H</u></b></a> &nbsp;\n";
102     if ( $pkgs ) {
103         $table .= "<b>$pkgs</b> packages to upgrade. ";
104         $table .= "<b>$secs security</b>. " if $secs;
105         $table .= " $own from indexdata " if $own;
106     } else {
107         $table .= "ok";
108         $okhosts{$H} = 1;
109     }
110     my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
111     $table .= "&nbsp;<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;
116 }
117 $table .= "</table>\n";
118
119 # Page header
120 my $outfile = "/tmp/aptcheck.html";
121 open F, ">$outfile"
122     or die "Could not open $outfile for writing: $!";
123 print F "<html>\n";
124 print F "<head><title>Apt upgrade status</title></head>\n";
125 print F "<body>\n";
126 print F "<H1>Apt package status</H1>\n";
127
128
129 # Summary table - NEW: one row for per host group
130 print F "<p/>\n";
131 print F "<table border='1' >\n";
132 print F "<tr><td>&nbsp;</td>" ;
133 print F "<td><b>Hosts</b></td>\n";
134 print F "<td><b>Packages</b></td></tr>\n";
135
136 if ( $sectot ) {
137     print F "<tr><td><b>Security</b><br/>" . scalar(keys(%sechosts)) . 
138         " / " .  scalar(keys(%secpkgs)) . " / $sectot </td>\n" ;
139     print F "<td>";
140     for $HH ( sort(keys(%sechosts)) ) {
141         print F "<a href='#$HH'><b>$HH</b></a> ";
142     }
143     print F "<td>" . join(" ",sort(keys(%secpkgs))) . "&nbsp;</td>";
144     print F "</tr>\n";
145 }
146 if ( $owntot ) {
147     print F "<tr><td><b>Indexdata</b><br/>" . scalar(keys(%ownhosts)) . 
148         " / " .  scalar(keys(%ownpkgs)) . " / $owntot </td>\n" ;
149     print F "<td>";
150     for $HH ( sort(keys(%ownhosts)) ) {
151         print F "<a href='#$HH'><b>$HH</b></a> ";
152     }
153     print F "<td>" . join(" ",sort(keys(%ownpkgs))) . "&nbsp;</td>";
154     print F "</tr>\n";
155 }
156 if ( $normtot ) {
157     print F "<tr><td>Indexdata<br/>" . scalar(keys(%normhosts)) . 
158         " / " .  scalar(keys(%normpkgs)) . " / $normtot </td>\n" ;
159     print F "<td>";
160     for $HH ( sort(keys(%normhosts)) ) {
161         print F "<a href='#$HH'><b>$HH</b></a> ";
162     }
163     print F "<td>" . join(" ",sort(keys(%normpkgs))) . "&nbsp;</td>";
164     print F "</tr>\n";
165 }
166 if ( %skiphosts ) {
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> ";
171     }
172     print F "</td></tr>\n";
173 }
174 if ( %okhosts ) {
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> ";
179     }
180     print F "</td></tr>\n";
181 }
182 print F "</table>\n";
183
184
185 # The host table
186 print F $table;
187
188 print F "<p/>Produced " . `date`.
189         " on " . `hostname` . " by " . `whoami` .
190         "<br/>\n";
191 print F "</body></html>\n";
192
193 close(F)
194     or die "Could not close $outfile: $!";
195
196 system "scp -q $outfile nagios:/var/www/heikki/index.html";
197
198 exit(0);
199
200 # Helper to take two strings and highligt that part of the second
201 # that is different from the first. 
202 sub strdiff {
203     my $x = shift;
204     my $y = shift;
205     print "strdiff: '$x' '$y' \n" if $debug>1;
206     my $a = 0;
207     while ( $a < length($y) &&
208         substr($x,$a,1) eq substr($y,$a,1) ) {
209         $a++;
210     }
211     if ( $a == length($y) ) {
212         return "$y ???";
213     }
214     my $b = 1;
215     while ( $b < length($y)-$a &&
216         substr($x,-$b,1) eq substr($y, -$b,1) ) {
217         $b++;
218     }
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>" .
226              substr($y,$c);
227     print "strdiff:        " . $z ."\n" if $debug>1;
228     print "\n" if $debug>1;
229     return $z;
230 }