# $ZEUSHOME/web/bin/statd.cron.pl
#
#######  Performs statd log processing and archiving  #######
# 
# zeus.statd calls this script nightly at 00:30
# 
# Requirements: uname, tar and compress must lie in PATH
#
# zeus.statd logs at 5-minute intervals to the files:
#
#   $ZEUSHOME/web/log/statd/YYYYMMDD.log
#
# File format is:
#   hhmm:
#   <hhead>\t<vserv>\t<nHits>\t<nBytes>\n
#   <hhead>\t<vserv>\t<nHits>\t<nBytes>\n
#
# hhmm is the time (at 5 minute intervals), <nHits>, <nBytes>
# are cumulative figures for the 5-minute interval.
#
#   $ZEUSHOME/web/log/statd/YYYYMMDD.summary
#
# File format is:
#   <hhead>\t<vserv>\t<nHits>\t<nBytes>\n
#   <hhead>\t<vserv>\t<nHits>\t<nBytes>\n
#
# The figures being cumulative for the entire day (or part 
# of day)
#
# statd.cron.pl deletes or archives (tar & compress) old data 
# to another folder.
#
# You can extend this script to perform other routine 
# processing, eg., transfer of data to a billing system etc.

# ------------------------------------------------------------

# Archive data if older than <X> days; <X> > 0;
# <X> = 0 for never archive
# Override with --archive_days=<X>
$archive_if_older_than = 7;

# Archive file location.  Files are silently deleted if == ""
# Override with --archive_dir=<directory>
$archive_to = "";

foreach (@ARGV) {
   if (/^--(.*?)=(.*)$/) {
      ($key, $val) = ($1, $2);
      if ($key eq "archive_days") { $archive_if_older_than = $val; }
      if ($key eq "archive_dir")  { $archive_to = $val; }
   }
}

# Tar to stdout using $my_tar
$my_tar = "tar -cf -";

# Compress stdin to stdout using $my_compress
$my_compress = "compress";

# Archive file suffix
$archive_file_suffix = ".tar.Z";

# Hostname
$hostname = `uname -n`;

# ------------------------------------------------------------

($me = $0) =~ s/^.*\///;
$logdir = $ENV{'ZEUSHOME'}."/web/log/statd";

opendir DIR, $logdir or die "$me: Can't read $logdir: $!";
@days = sort { $b cmp $a } map { s/\.log$//;$_ } grep /^[0-9]{8}\.log$/, 
        readdir DIR;
closedir DIR;

$today = time;

if ($archive_if_older_than > 0) {
   $thresh = localtime_ymd($today-(24*60*60)*$archive_if_older_than);
   foreach $day (@days) { archive($day) if $day lt $thresh; }
}

exit 0;

# ------------------------------------------------------------

sub archive() {
   my ($day) = @_;

   if ($archive_to) {
      print "$me: Archiving $day.\n";
   
      if (-f "$archive_to/$hostname$day$archive_file_suffix") {
         print "$me: archive already exists - skipping!\n";
         return;
      }

      $ret = system ("cd $logdir; $my_tar $day.* | $my_compress > $archive_to/$hostname$day$archive_file_suffix");
      if ($ret != 0) {
         print "$me: Archive to $archive_to failed.\n";
         return;
      }
   } else {
      print "$me: Removing  $day.\n";
   }
   unlink <$logdir/$day.*>;
}

# ------------------------------------------------------------

# strftime( "%Y%m%d", ... ), e.g. "19990701"
sub localtime_ymd {
    my (@tval) = localtime( $_[0] );
    return sprintf "%d%02d%02d", 1900+$tval[5], $tval[4]+1, $tval[3];
}
