#!/usr/local/bin/perl
#
#  Checks load of a group of hosts 
#
#  Rajitha Sumanasekera
#  rajitha@dcs.uky.edy
#  Aug 02, 1997 
#
#  Note: Uses a .SECrc file with host groups.
#
#  Sample host file:
#       =DCS
#       clarinet.dcs
#       violin.dcs
#

if (@ARGV  >= 1) {
  $ttype = $ARGV[0];
  $numhosts = $ARGV[1];
} else {
  @groups = &get_groups;  
  print("\n    Usage: up_e [group] [number of hosts]\n\n");
  print("\tWhere group is one of:\n\t    @groups\n");
  die "\n";
}

if (@ARGV == 1) {
    $numhosts = 100;
    print("Checking load of all $ttype hosts:\n");
}

if (get_host($ttype,1) eq "") {
  die "\n\tNo such host group\n\N";
}

$n = 0;
print "\n";

while ($host = get_host($ttype, ++$n)) {
  $user = "";
  if (index($host,"edu") >= 0) {
    if (index($host,"uky") < 1) {
      $user = "-l griff";
    }
  }

  chop($time_ = `rsh $host $user /bin/uptime`);
  $time_ = substr($time_, 1 + rindex($time_,":"));
  print "#$n Load on $host: $time_\n";      

  if (($n + 1) > $numhosts) {
    last;
  }
}

close(HOSTS);
print "\n";

sub get_host {
  chop($rcFile =`echo \$HOME`);
  $rcFile .= "/.SECrc";
  open(HOST, $rcFile) || die "can't find $rcFile\n";
  while (<HOST>) {
    s/\s+//;
    s/=//;
	
    if ($_ eq $_[0]) {
      last;
    }
  }

  $hostNum = 0;
  while ($host = <HOST>) {
    $host =~ s/\s+//;

    if ($host eq "") {
      next;
    }

    $hostNum++;
    if ($hostNum == $_[1]) {
      last;
    }
    $host ="";
  }

  if ($host =~ /\=/) {
      $host = "";
  }
  close(HOST);

  $host;
}


sub get_groups {
  local(@groups, $num);
  chop($rcFile =`echo \$HOME`);
  $rcFile .= "/.SECrc";
  open(HOST, $rcFile) || die "can't find $rcFile\n";
  
  while (<HOST>) {
    s/\s+//;

    if ($_ =~ /\=/) {
      s/=//;
      $groups[$num++] = $_;      
    }
  }
  @groups;
}

