#!/usr/bin/perl
#
# run getstats to create reports from httpd log files
#  virtual host records are included in the same logs
#  secure server logs are handled by separate scripts

unshift(@INC, '/opt/ACISweb/lib');
require 'webconfig.pm';
&webconfig'getconfig('', \%stype,
	\@sname, \%lname, \%alias, \%droot, \%hostlist, \$poolhosts);

if ($#ARGV == 0) {
  $date=$ARGV[0];
} else {
  &usage;
}

$| = 1;					# unbuffered output
$GBIN='/opt/ACISweb/bin/getstats';
$GETSTATS="$GBIN/getstats";
die "can't find $GETSTATS\n" unless -x $GETSTATS;

if    (-x '/opt/local/bin/gzip')     {$GZIP='/opt/local/bin/gzip';}
elsif (-x '/usr/local/bin/gzip')     {$GZIP='/usr/local/bin/gzip';}
else                                 {$GZIP='';}

$rpt='/www/data/httpd/reports';		# regular reports
$hom='/www/data/httpd/homepag';		# personal homepages
$log='/www/data/httpd/log';		# log files are here
$cmd="/tmp/getstats-cmd-www-$date";	# command file
$msg="/tmp/getstats-err-www-$date";	# parse error messages go here

die "can't create $cmd: $!\n" unless open(CMD, ">$cmd");

print CMD "
set date $date
set msgfile $msg
set defaulthost www.columbia.edu

";
foreach $whost (@sname) {
  # for each virtual host create subdir for dir tree reports
  #  and subsubdir for domain reports
  next unless ($whost eq 'www') || ($stype{$whost} eq 'virtual');
  unless (-d "$rpt/$whost") {
    die "can't create $rpt/$whost: $!\n" unless
			mkdir("$rpt/$whost", 0755);
  }
  unless (-d "$rpt/$whost/dom") {
    die "can't create $rpt/$whost/dom: $!\n" unless
			mkdir("$rpt/$whost/dom", 0755);
  }
  print CMD "set domroot $lname{$whost} $rpt/$whost/dom\n";

  # only create extract files if the subdirectory exists
  print CMD "set xtroot  $lname{$whost} $log/xt/$whost\n"
			if -d "$log/xt/$whost";
}

print CMD "
# how many levels to keep in memory
set dtrlevels 5
set domlevels 2
set countbytes yes
read std $log/fu/osiyou/httpd.access.$date.gz
read std $log/fu/yasu/httpd.access.$date.gz

# print reports for the main server
set servername \"http://www.columbia.edu/\"
set icon \"http://www.columbia.edu/httpd/getstats/getstats.gif\"

set ignorepublic yes
set sortkey nreq
set ignoreimages no
write dirtree 5 $rpt/www/$date.dtrreq5.html
write dirtree 4 $rpt/www/$date.dtrreq4.html
write dirtree 3 $rpt/www/$date.dtrreq3.html
write dirtree 2 $rpt/www/$date.dtrreq2.html
write dirtree 1 $rpt/www/$date.dtrreq1.html

set sortkey alpha
set ignoreimages no
write dirtree 5 $rpt/www/$date.dtralp5.html
write dirtree 4 $rpt/www/$date.dtralp4.html
write dirtree 3 $rpt/www/$date.dtralp3.html
write dirtree 2 $rpt/www/$date.dtralp2.html
write dirtree 1 $rpt/www/$date.dtralp1.html

set sortkey nreq
set ignoreimages yes
write dirtree 5 $rpt/www/$date.htrreq5.html
write dirtree 4 $rpt/www/$date.htrreq4.html
write dirtree 3 $rpt/www/$date.htrreq3.html
write dirtree 2 $rpt/www/$date.htrreq2.html
write dirtree 1 $rpt/www/$date.htrreq1.html

set sortkey alpha
set ignoreimages yes
write dirtree 5 $rpt/www/$date.htralp5.html
write dirtree 4 $rpt/www/$date.htralp4.html
write dirtree 3 $rpt/www/$date.htralp3.html
write dirtree 2 $rpt/www/$date.htralp2.html
write dirtree 1 $rpt/www/$date.htralp1.html

# report on public_html directories
set ignorepublic no
set sortkey nreq
set ignoreimages no
write dirtree 4 $hom/$date.dtrreq4.html
write dirtree 3 $hom/$date.dtrreq3.html
write dirtree 2 $hom/$date.dtrreq2.html

# other reports: domain, redirects, errors
# note: 2-level domain reports are created automatically
#  by the set domroot command
write domain 1 $rpt/www/dom/$date.domreq1.html
write redir  5 $rpt/www/$date.redir.html
write error  5 $rpt/www/$date.error.list
";

foreach $whost (@sname) {
  next if $whost eq 'www';
  next unless $stype{$whost} eq 'virtual';
  local($icon)='http://www.columbia.edu/httpd/getstats/getstats.gif';
  $icon='http://www.columbia.edu/kermit/k95green.gif' if $whost eq 'kermit';
  $icon='http://www.alice.columbia.edu/alice.gif'
			if ($whost eq 'alice') || ($whost eq 'goaskalice');
  print CMD "
set servername \"http://$lname{$whost}/\"
set icon \"$icon\"
set ignorepublic yes
set sortkey nreq
set ignoreimages yes
write dirtree 4 $rpt/$whost/$date.htrreq4.html
write dirtree 3 $rpt/$whost/$date.htrreq3.html
write dirtree 2 $rpt/$whost/$date.htrreq2.html
write dirtree 1 $rpt/$whost/$date.htrreq1.html
write domain  1 $rpt/$whost/dom/$date.domreq1.html
write redir   5 $rpt/$whost/$date.redir.html
write error   5 $rpt/$whost/$date.error.list
";
}

close (CMD);
print "getstats will create reports for $date\n";
&execute($GETSTATS, '-c', $cmd);

system("chgrp www $rpt/*/$date*");
system("chmod a+r $rpt/*/$date*");
system("chgrp www $hom/$date*");
system("chmod o-r $hom/$date*");
unlink("$msg.gz") if -r "$msg.gz";	# remove old compressed file
if ($GZIP) {system("$GZIP $msg");}
else       {print "can't find gzip, $msg not compressed\n";}

exit;


############################################################################
sub usage {
  die "usage: run-gs-www.pl <date>

<date> is an 8 digit date of the form yyyymmdd, e.g. 19980221
";
}



sub execute {			# run a program and check the return code
    local(@args) = @_;
    local($rc) = 0xffff & system @args;
    if ($rc > 0x80) {
	$rc >>= 8;
	printf "%s\n  failed with return code $rc\n", "@args";
	exit(3);
    } elsif ($rc) {
	if ($rc & 0x80) {
	    $rc &= ~0x80;
	    print "coredump written\n";
	}
	printf "%s\n  aborted with signal $rc\n", "@args";
	exit(3);
    }
}
