#!/s/std/bin/perl

# By Ballard, Oct 14, 1998

use Socket;

printf("%-17.17s %-33.33s %7.7s %9.9s %10.10s\n", "Name", "Machine", "Running", "IdleJobs", "HostsTotal");

open (CSW, "condor_status -pool condor.cs.wisc.edu -world -l -const 'HostsTotal > 0'|") || die;

$name = "UNDEF!";
$machine = "UNDEF!";
$running = "UNDEF!";
$idlejobs = "UNDEF!";
$hoststotal = "UNDEF!";
$collip = "UNDEF!";
$totalrunning = 0;
$totalidle = 0;
$totalhosts = 0;
$totalpools = 0;

while($adline = <CSW>) {
	$|=1;
	chop($adline);
	if ($adline !~ /=/) {
		if ($nameok) {
			if ($machine !~ /\..+\./) { # is it a FQN?
				#$collip =~ s/([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)//;
				$collip =~ s/([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)//;
				$a=$1;$b=$2;$c=$3;$d=$4;
				$collip = pack('C4',$1, $2, $3, $4);
				$machine = gethostbyaddr($collip, AF_INET);
				if (!defined($machine)) {
					$machine = "$a.$b.$c.$d";
				}
			}
			$machine =~ tr/[A-Z]/[a-z]/;
			printf("%-17.17s %-33.33s %5.5s %9.9s %10.10s\n",
				$name,$machine,$running,$idlejobs,$hoststotal);
		}
		$nameok = 0;
		$name = "UNDEF!";
		$machine = "UNDEF!";
		$running = "UNDEF!";
		$idlejobs = "UNDEF!";
		$hoststotal = "UNDEF!";
		$collip = "UNDEF!";
	} elsif ($adline =~ /^Name/i) {
		$nameok = 1;
		$adline =~ s/^Name.*=.*"([^"]+)".*$/\1/i;
		$name = $adline;
		$totalpools++;
	} elsif ($adline =~ /^Machine/i) {
		$adline =~ s/^Machine.*=.*"([^"]+)".*$/\1/i;
		$machine = $adline;
	} elsif ($adline =~ /^CollectorIpAddr/i) {
		$adline =~ s/^CollectorIpAddr.*=.*"<([^:]+):[^"]*".*$/\1/i;
		$collip = $adline;
	} elsif ($adline =~ /^RunningJobs/i) {
		$adline =~ s/^RunningJobs.*=\w*(.*)$/\1/i;
		$running = $adline;
		if ($running < 0 || $running > 10000000) {
			$running = "INVALID!";
		} else {
			$totalrunning += $running;
		}
	} elsif ($adline =~ /^IdleJobs/i) {
		$adline =~ s/^IdleJobs.*=\w*(.*)$/\1/i;
		$idlejobs = $adline;
		if ($idlejobs < 0 || $idlejobs > 10000000) {
			$idlejobs = "INVALID!";
		} else {
			$totalidle += $idlejobs;
		}
	} elsif ($adline =~ /^HostsTotal/i) {
		$adline =~ s/^HostsTotal.*=\w*(.*)$/\1/i;
		$hoststotal = $adline;
		if ($hoststotal < 0 || $hoststotal > 10000000) {
			$hoststotal = "INVALID!";
		} else {
			$totalhosts += $hoststotal;
		}
	} else {
		
#		print "$adline\n";
	}
}
close CSW;
printf("%-17.17s %-33.33s %5.5s %9.9s %10.10s\n",
	"Totals:","$totalpools pools.",$totalrunning,$totalidle,$totalhosts);

