#!/usr/bin/perl
# weekly-reports.pl
# this job runs after the web logs have been orbited and
#  copied to the central repository

if ($#ARGV > -1) {
    die "usage: weekly-reports.pl\n";
}

$| = 1;					# unbuffered output
umask(027);				# files not world readable
$LS='/usr/bin/ls';
$WC='/usr/ucb/wc';
$GBIN='/opt/ACISweb/bin/getstats';
$weekfile="/www/data/httpd/log/thisweek";
$REP='/www/data/httpd/reports';
$LOG='/www/data/httpd/log/fu';
$SEC='/www/data/httpd/log/sec';

$GZIP='/opt/local/bin/gzip';
$ZCAT='/opt/local/bin/zcat';

# web log files can be rotated daily or every six hours
#  so every week each host will have 7 files, or 28 files
$DAILYROT=7;
$SIXHROT=28;

# access this directory, then sleep, so amd has time to mount
system("/bin/ls -ld $LOG >/dev/null; /bin/sleep 60");

die "Unable to open $weekfile because $!\n"
			unless open(DATEFILE, "<$weekfile");
chop($thiswk=<DATEFILE>);	# report date in the form "Sep 13, 1997"
chop($thisrep=<DATEFILE>);	# report date in the form "19970913"
close(DATEFILE);


    # for these auxiliary web servers we concatenate the daily logs
    #  into weekly log files but don't generate reports
for $host ('gutentag', 'kalimera', 'zivijo') {
    print "$host logs missing\n"
	if &concatenate_httpd('httpd', $host, $DAILYROT);
}

for $host ('dobryden', 'kalimera', 'thalia') {
    print "$host httpsd logs missing\n"
	if &concatenate_httpd('httpsd', $host, $DAILYROT);
}




    # now process the logs from our primary web servers
$pflag=1;
for $host ('osiyou', 'yasu') {
    if (&concatenate_httpd('httpd', $host, $SIXHROT)) {
      print "$host logs missing\n";
      $pflag='';
    }
}

    # CREATE THE REPORTS FOR WWW.COLUMBIA.EDU AND VIRTUAL HOSTS
    #  and update the weeklist file, the web page, etc.
if ($pflag) {
    print "www reports will be created for $thiswk ($thisrep)\n\n";
    &execute("$GBIN/run-gs-www.pl $thisrep");

    # update the list of dates on the web page
    &execute("$GBIN/insert-date.pl $weekfile $REP/weeklist.sel");

    # update the list of hosts on the web page
    &execute("$GBIN/hostnames.pl");

    # create bad link reports and mail them to file owners
    &execute("$GBIN/bad-links-www.pl -d $thisrep -s -v");
}




print "------------------------------------------------------------------------\n";
$pflag=1;		# process the logs from our www1 secure server
for $host ('howsit') {
    if (&concatenate_httpd('httpsd', $host, $DAILYROT)) {
      print "$host logs missing\n";
      $pflag='';
    }
}
if ($pflag) {			# CREATE THE REPORTS FOR WWW1
    print "www1 reports will be created for $thiswk ($thisrep)\n\n";
    &execute("$GBIN/run-gs-www1.pl $thisrep");
}



print "------------------------------------------------------------------------\n";
$pflag=1;		# process the logs from our wwwc secure server
for $host ('alhan') {
    if (&concatenate_httpd('httpsd', $host, $DAILYROT)) {
      print "$host logs missing\n";
      $pflag='';
    }
}
if ($pflag) {			# CREATE THE REPORTS FOR WWWC
    print "wwwc reports will be created for $thiswk ($thisrep)\n\n";
    &execute("$GBIN/run-gs-wwwc.pl $thisrep");

    # copy ciao and earthscape extract files to a separate directory
    &execute("$GBIN/weekly-xtcopy.pl $thisrep");
}



############################################################


sub concatenate_httpd {
# uncompress and concatenate web logs to create weekly file.
# print warning unless we have exactly 7 or 28 log files (daily rotation,
#  or six hour rotation) for each host
# return TRUE on failure (can't create output file)

# httpd is "httpd" or "httpsd"
# host is the hostname, and directory name
# rotation is the number of files we need to concatenate

    local($httpd, $host, $rotation) = @_;
    local($pattern, $nlogfiles);
    local($accfil)="$httpd.access.$thisrep";	# output file
    $pattern='??????????';			# yyyymmddhh

    # chdir to log directory
    if ($httpd eq 'httpd') {chdir("$LOG/$host");}
    else		   {chdir("$SEC/$host");}

    chop($nlogfiles=`$LS prev/$httpd.access.$pattern.gz | $WC -l`);
    if (system("$ZCAT prev/$httpd.access.$pattern.gz | $GZIP >$accfil.gz")) {
	print "can't create $host/$accfil.gz\n";
	return 1;
    }
    print "   created $host/$accfil.gz\n";

    unless ($nlogfiles == $rotation) {
	print "expected $rotation log files for host $host\n";
	system("$LS prev/$httpd.access.$pattern.gz");
    }

					# find leftover wrk files
    $wrkfiles=`$LS prev/$httpd.access.$pattern.wrk.gz 2>&1`;
    if (($wrkfiles !~ /not found/) && ($wrkfiles !~ /No such file/)) {
	print "   wrk files to be deleted:\n$wrkfiles";
    }
    return '';
} # concatenate_httpd



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";
    } elsif ($rc) {
	if ($rc & 0x80) {
	    $rc &= ~0x80;
	    print "coredump written\n";
	}
	printf "%s\n  aborted with signal $rc\n", "@args";
    }
}
