Added aptcheck stuff
[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 my $table = "<table>\n";
24 my %summary;
25 my %sechosts;
26 my %secpkgs;
27 for $hline ( split("\n",$hostlist) ) {
28     next unless ( $hline =~ /\/([a-z0-9]+)\.cfg$/ );
29     my $H = $1;
30     next if ($H =~ /^commands/ );
31     next if ($H =~ /^servicegroups/ );
32     print "Checking $H\n" if $debug;
33     my $apt = `ssh $H apt-get upgrade -s -o 'Debug::NoLocking=true' `;
34     # Note, do not append -qq, we want some output even when nothing to do
35     if ( !$apt ) {
36         $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
37         $table .= "<tr><td colspan='3'><b><u>$H</u></b> (skipped)\n";
38         next;
39     }
40     print "Got apts for $H: \n$apt\n" if $debug>2;
41     my $det = "";
42     my $pkgs = 0;
43     my $secs = 0;
44     my $own = 0;
45     for $p ( split("\n",$apt) ) {
46         next unless $p =~
47             /^Inst ([^ ]+) \[([^]]+)\] \(([^ ]+) ([^:]+):/;
48         my ( $pkg,$cur,$new,$src ) = ( $1,$2,$3,$4 );
49         print "$H: $pkg: $cur -> $new ($src)\n" if $debug>1;
50         $det .= "<tr><td>&nbsp;&nbsp;";
51         $pkgs++;
52         my $key = $pkg;
53         if ( $src =~ /Security/ ) {
54             $det .= "<b>$pkg</b>";
55             $key = "<b>$pkg</b>";
56             $sechosts{$H}=1;
57             $secpkgs{$pkg}=1;
58             $secs++;
59         } elsif ( $src =~ /Indexdata/ ) {
60             $det .= "<b><i>$pkg</i></b>";
61             $key = "<i>$pkg</i>";
62             $own++;
63         } else {
64             $det .= "$pkg";
65         }
66         if ( !$summary{$key} ) {
67             $summary{$key} = "";
68         }
69         $new = strdiff($cur,$new);
70         $cur = strdiff($new,$cur);
71         $summary{$key} .= "$H ";
72         $det .= "</td> ";
73         $det .= "<td>$cur</td> ";
74         $det .= "<td>$new</td> ";
75         $det .= "</tr>\n";
76     }
77     $table .= "<tr><td colspan='3'>&nbsp;</td></tr>\n";
78     $table .= "<tr><td colspan='3'><b><u>$H</u></b> &nbsp;\n";
79     if ( $pkgs ) {
80         $table .= "<b>$pkgs</b> packages to upgrade. ";
81         $table .= "<b>$secs security</b>. " if $secs;
82         $table .= " $own from indexdata " if $own;
83     } else {
84         $table .= "ok";
85     }
86     my $updlink = $wikilink . ucfirst($H) . "Updates" . $year;
87     $table .= "&nbsp;<a href='$updlink' >Upd</a>";
88     $table .= "</td></tr>\n";
89     $table .= $det if $pkgs;
90     print "\n$table\n" if $debug>2;
91     last if $H =~/dart/ && $debug;
92 }
93 $table .= "</table>\n";
94
95 # Produce page
96 my $outfile = "/tmp/aptcheck.html";
97 open F, ">$outfile"
98     or die "Could not open $outfile for writing: $!";
99 print F "<html>\n";
100 print F "<head><title>Apt upgrade status</title></head>\n";
101 print F "<body>\n";
102 print F "<H1>Apt package status</H1>\n";
103
104 if ( %sechosts ) {
105     print F "<h2>Security updates for " .
106         scalar(keys(%sechosts)) ." hosts: </h2>\n";
107     print F "<b>" . join(" ", sort(keys(%sechosts)))."</b><br/> ".
108         join(" ", sort(keys(%secpkgs))). "<p/>\n";
109 }
110
111 print F $table;
112
113 print F "<p/>Produced " . `date`.
114         " on " . `hostname` . " by " . `whoami` .
115         "<br/>\n";
116 print F "</body></html>\n";
117
118 close(F)
119     or die "Could not close $outfile: $!";
120
121 system "scp -q $outfile nagios:/var/www/heikki/index.html";
122
123 # Helper to take two strings and highligt that part of the second
124 # that is different from the first. 
125 sub strdiff {
126     my $x = shift;
127     my $y = shift;
128     print "strdiff: '$x' '$y' \n" if $debug>1;
129     my $a = 0;
130     while ( $a < length($y) &&
131         substr($x,$a,1) eq substr($y,$a,1) ) {
132         $a++;
133     }
134     if ( $a == length($y) ) {
135         return "$y ???";
136     }
137     my $b = 1;
138     while ( $b < length($y)-$a &&
139         substr($x,-$b,1) eq substr($y, -$b,1) ) {
140         $b++;
141     }
142     my $c = length($y) - $b +1;
143     print "strdiff:   a=$a " . substr($y,0,$a) ."\n" if $debug>1;
144     print "strdiff:   b=$b " . "\n" if $debug>1;
145     print "strdiff:   c=$c " . substr($y,$c) ."\n" if $debug>1;
146     print "strdiff:        " . substr($y,$a, $c-$a) ."\n" if $debug>1;
147     my $z =  substr($y,0,$a) .
148              "<b>" . substr($y,$a, $c-$a) . "</b>" .
149              substr($y,$c);
150     print "strdiff:        " . $z ."\n" if $debug>1;
151     print "\n" if $debug>1;
152     return $z;
153 }