Also total package instances
[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 $sectot = 0;
35 my $owntot = 0;
36 my $normtot = 0;
37
38 my $table = "<table>\n";
39
40 for $hline ( split("\n",$hostlist) ) {
41     next unless ( $hline =~ /\/([a-z0-9]+)\.cfg$/ );
42     my $H = $1;
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
48     if ( !$apt ) {
49         $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
50         $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
51         $skiphosts{$H}=1;
52         next;
53     }
54     print "Got apts for $H: \n$apt\n" if $debug>2;
55     my $det = "";
56     my $pkgs = 0;
57     my $secs = 0;
58     my $own = 0;
59     for $p ( split("\n",$apt) ) {
60         next unless $p =~
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>&nbsp;&nbsp;";
65         $pkgs++;
66         my $key = $pkg;
67         if ( $src =~ /Security/ ) {
68             $det .= "<b>$pkg</b>";
69             $key = "<b>$pkg</b>";
70             $sechosts{$H}=1;
71             $secpkgs{$pkg}=1;
72             $secs++;
73             $sectot++;
74         } elsif ( $src =~ /Indexdata/ ) {
75             $det .= "<b><i>$pkg</i></b>";
76             $key = "<i>$pkg</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         $new = strdiff($cur,$new);
91         $cur = strdiff($new,$cur);
92         $summary{$key} .= "$H ";
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     }
107     my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
108     $table .= "&nbsp;<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;
113 }
114 $table .= "</table>\n";
115
116 # Produce page
117 my $outfile = "/tmp/aptcheck.html";
118 open F, ">$outfile"
119     or die "Could not open $outfile for writing: $!";
120 print F "<html>\n";
121 print F "<head><title>Apt upgrade status</title></head>\n";
122 print F "<body>\n";
123 print F "<H1>Apt package status</H1>\n";
124
125
126 # Summary table
127 print F "<table border='1' >\n";
128 print F "<tr><td>&nbsp;</td>" ;
129 print F "<td><b>Security:</b> " . scalar(keys(%sechosts)) . 
130         " / " .  scalar(keys(%secpkgs)) . " / $sectot </td>\n" ;
131 print F "<td>Indexdata: " . scalar(keys(%ownhosts)) . 
132         " / " .  scalar(keys(%ownpkgs)) . " / $owntot </td>\n" ;
133 print F "<td>Normal: " . scalar(keys(%normhosts)) . 
134         " / " .  scalar(keys(%normpkgs)) . "  / $normtot </td>" . "</tr>\n";
135 print F "<tr><td>Hosts</td>\n";
136
137
138 print F "<td>";
139 for $HH ( sort(keys(%sechosts)) ) {
140     print F "<a href='#$HH'><b>$HH</b></a> ";
141 }
142 print F "&nbsp;</td>\n";
143 #print F "<td><b>" . join(" ",sort(keys(%sechosts))) . "</b>&nbsp;</td>";
144 #print F "<td><b>" . join(" ",sort(keys(%ownhosts))) . "</b>&nbsp;</td>";
145 print F "<td>";
146 for $HH ( sort(keys(%ownhosts)) ) {
147     print F "<a href='#$HH'><b>$HH</b></a> ";
148 }
149 print F "&nbsp;</td>\n";
150 #print F "<td><b>" . join(" ",sort(keys(%normhosts))) . "</b>&nbsp;</td></tr>\n";
151 print F "<td>";
152 for $HH ( sort(keys(%normhosts)) ) {
153     print F "<a href='#$HH'><b>$HH</b></a> ";
154 }
155 print F "&nbsp;</td>\n";
156
157 print F "<tr><td>Packages</td>\n";
158 print F "<td>" . join(" ",sort(keys(%secpkgs))) . "&nbsp;</td>";
159 print F "<td>" . join(" ",sort(keys(%ownpkgs))) . "&nbsp;</td>";
160 print F "<td>" . join(" ",sort(keys(%normpkgs))) . "&nbsp;</td></tr>\n";
161 print F "</table>\n";
162
163 print F $table;
164
165 print F "<p/>Produced " . `date`.
166         " on " . `hostname` . " by " . `whoami` .
167         "<br/>\n";
168 print F "</body></html>\n";
169
170 close(F)
171     or die "Could not close $outfile: $!";
172
173 system "scp -q $outfile nagios:/var/www/heikki/index.html";
174
175 # Helper to take two strings and highligt that part of the second
176 # that is different from the first. 
177 sub strdiff {
178     my $x = shift;
179     my $y = shift;
180     print "strdiff: '$x' '$y' \n" if $debug>1;
181     my $a = 0;
182     while ( $a < length($y) &&
183         substr($x,$a,1) eq substr($y,$a,1) ) {
184         $a++;
185     }
186     if ( $a == length($y) ) {
187         return "$y ???";
188     }
189     my $b = 1;
190     while ( $b < length($y)-$a &&
191         substr($x,-$b,1) eq substr($y, -$b,1) ) {
192         $b++;
193     }
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>" .
201              substr($y,$c);
202     print "strdiff:        " . $z ."\n" if $debug>1;
203     print "\n" if $debug>1;
204     return $z;
205 }