#!/usr/bin/perl -w
# -*- perl -*-

#  Sun Temperature

#  Connect to the specified remotehost and run the 'prtdiag' program.
#  Parse the output for the remotehost's temperature information and 
#  return that information to Cricket.
#
#  The system ambient, or control card temperature is returned first, 
#  and then the each cpu/board, in numeric order.
#
#  Note: This script will return 17 values for all Ultra-Enterprise systems
#  (Control board plus sixteen cpu boards.) to accomidate Ultra Enterprise 
#  6000's and 6500's. 
#
#  Note: This script depends on 'rsh' to acquire the prtdiag data. 
#  Alternative methods of acquiring the output of prtdiag (such as 'ssh')
#  should be investigated.

#  Author: Matthew Stier
#  Email: Matthew.Stier@fnc.fujitsu.com


BEGIN {
	$gInstallRoot = (($0 =~ m:^(.*/):)[0] || "./") . "..";
}

use lib "$gInstallRoot/lib";
use strict;

# Test for the number of arguments
if ($#ARGV+1 != 1) {
	print STDERR "usage: $0 hostname\n";
	exit 1;
}

# Get the hostname
my($remotehost) = $ARGV[0];

# Get the system type
my($uname) = `/usr/bin/rsh $remotehost /usr/bin/uname -i`;
chomp $uname;

# Get the prtdiag data
my($prtdiag) = "/usr/platform/" . $uname . "/sbin/prtdiag";
my(@return) = `/usr/bin/rsh $remotehost $prtdiag -v`;

# Return the data, based upon the CPU model

# Ultra-4 motherboard, used on Sun Enterprise 450's
if ( $uname eq "SUNW,Ultra-4" ) {
my($ambient)	= 0;
my(@cpu)	= (0,0,0,0);
my($temp)	= 0;
foreach (@return) {
	$temp = 1 if (/^System Temperature/);
	$temp = 0 if (/^$/);
	$ambient = $1 if (/^AMBIENT\s+(\d+)/);
	$cpu[$1] = $2 if (/^CPU (\d+)\s+(\d+)/);
}

print $ambient . "\n";
foreach (@cpu) {
	print $_ . "\n";
}
}


# Sun Ultra-Enterprised, used on 4000's through 6500's.
if ( $uname eq "SUNW,Ultra-Enterprise" ) {
my($clock)	= 0;
my(@board)	= (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
my($temp)	= 0;
foreach (@return) {
	$temp = 1 if (/^System Temperature/);
	$temp = 0 if (/^$/);
	# Verified on UE4500
	$clock = $1 if (/^CLK\s+\S+\s+(\d+)/);
	$board[$1] = $2 if (/^ (\d+)\s+\S+\s+(\d+)/);
        # Verified on UE6000
	$clock = $1 if (/^Control Board:\s+(\d+)/);
	$board[$1] = $2 if (/^Board (\d+):\s+(\d+)/);
}

print $clock . "\n";
foreach (@board) {
	print $_ . "\n";
}
}

