#!/s/std/bin/perl -w
#############################
# Finds max accessed volumes
# Gregory Touretsky, 31/12/98
# Should run on AFS FS
# <gregory.touretsky@intel.com>

# placed here by timc@cs.wisc.edu 10jun2002
# use to quickly and easily determine which volume is getting hammered
# on an AFS fileserver.

if($#ARGV != 1 && $#ARGV != 2) {
   print "Usage: check_fs <par1> <par2> [par3]\n";
   print "  <par1> - yes - send signal to fileserver to get new data\n";
   print "           no  - work with existing FileLog\n";
   print "  <par2> - files - show usage per inode\n";
   print "           volumes - show usage per volume\n";
   print "  <par3> - date to check\n";
   exit(-2);
}

$kill=shift @ARGV || "no";

$what_to_check=shift @ARGV || "volumes";  # should be files or volumes

$filelog="/usr/afs/logs/FileLog";
$sleep_time=30;

$fs_string=`/bin/ps -ef | grep fileserver | grep -v grep`;
if(! $fs_string) {
  die "No fileserver running on this machine\n";
}

($fs_pid,$fs_started_m,$fs_started_d)=(split(" ",$fs_string))[1,4,5];

if($fs_started_m=~/:/) {$fs_started_d="";}

print "Fileserver started $fs_started_m $fs_started_d\n";

if($kill eq "yes") {
   print STDERR "Sending TSTP signal to fileserver ($fs_pid)\n";
   `kill -TSTP $fs_pid`;
   print STDERR "Kill -TSTP sent to fileserver. Sleeping $sleep_time
sec...\n";
   sleep $sleep_time;
   print STDERR "Sending HUP signal to fileserver\n";
   `kill -HUP $fs_pid`;
   print STDERR "Copying $filelog to $filelog.1\n";
   `cp $filelog ${filelog}.1`;

} else {print "Signal will not be sent, working with existing log\n";}

open FLOG, "$filelog.1" || die "Can't open $filelog.1";
@flog=<FLOG>;
close FLOG;

$time=localtime;
($month,$day)=(split(" ",$time))[1,2];
if($day<10) {$day=" ".$day;}
$date=shift @ARGV || $month." ".$day;

while(@flog) {
  $str=shift @flog;
  chomp $str;

  if($str =~ /$date .* Fid/) {
     $vol=(split(" ",$str))[8];
     if($volumes{$vol}) {$volumes{$vol}+=1;}
     else {$volumes{$vol}=1;}
  }
}

if($what_to_check eq "files") {
  foreach $vol (sort keys %volumes) {
    print "$vol $volumes{$vol}\n";
  }
}
elsif($what_to_check eq "volumes") {
  foreach $vol (sort keys %volumes) {
    $volid=$vol;$volid=~s/\..*//;
    if($volids{$volid}) {$volids{$volid}+=$volumes{$vol};} else
{$volids{$volid}=$volumes{$vol};}
  }

  foreach $volid (sort keys %volids) {
    print "$volid $volids{$volid}\n";
  }
} else {print "Illegal usage. Please, specify only files or volumes\n";
exit(-1);}
