#!/usr/bin/perl --

###############################################################################
#                                                                             #
# Filename: bakSNMPAsm                                                        #
#                                                                             #
# Description:                                                                #
#                                                                             #
###############################################################################
###############################################################################
#                                                                             #
#  Copyright  2000 - 2001 Intel Corporation.                                 #
#  Intel Corporation All Rights Reserved.                                     #
#                                                                             #
#  The source code contained or described herein and all documents related to #
#  the source code ("Material") are owned by Intel Corporation or its         #
#  suppliers or licensors.  Title to the Material remains with Intel          #
#  Corporation or its suppliers and licensors.  The Material contains trade   #
#  secrets and proprietary and confidential information of Intel or its       #
#  suppliers and licensors.  The Material is protected by worldwide copyright #
#  and trade secret laws and treaty provisions.  No part of the Material may  #
#  be used, copied, reproduced, modified, published, uploaded, posted,        #
#  transmitted, distributed, or disclosed in any way without Intel's prior    #
#  express written permission.                                                #
#                                                                             #
#  No license under any patent, copyright, trade secret or other intellectual #
#  property right is granted to or conferred upon you by disclosure or        #
#  delivery of the Materials, either expressly, by implication, inducement,   #
#  estoppel or otherwise. Any license under such intellectual property rights #
#  must be express and approved by Intel in writing.                          #
#                                                                             #
###############################################################################

#-----------------------------------------------------------------------------
#	Definitions
$CAT="/bin/cat";
$GREP="/bin/grep";
$DF='/bin/df';
$PROC_MOUNTS="/proc/mounts";

if( -d "/proc/sys/dev/sensors/ds1780-i2c-0-2d")
{
	$PROC_DIR                        = "/proc/sys/dev/sensors/ds1780-i2c-0-2d/";
}else{
	$PROC_DIR                        = "/proc/sys/dev/sensors/adm9240-i2c-0-2d/";
}
$ASMBIN_DIR						 = "/usr/local/asm/bin/";

# For values read from PF status.
$PF_STATS				= 	"/var/tmp/pfstats.asm";
$PF_NORMAL 				= 0;
$PF_LOWER 				= 1;
$PF_TRIGGERED 			= 1;
$PF_LOWER_CRITICAL 		= 2;
$PF_UPPER 				= 3;
$PF_UPPER_CRITICAL 		= 4;


$STATUS_TEMP                     = $PROC_DIR."temp";
$STATUS_VOLT_PLUS12              = $PROC_DIR."12V";
$STATUS_VOLT_MINUS12             = $PROC_DIR."Vccp2";
$STATUS_VOLT_PLUS5               = $PROC_DIR."5V";
$STATUS_VOLT_PLUS3POINT3         = $PROC_DIR."3.3V";
$STATUS_VOLT_CPUIO               = $PROC_DIR."2.5V";
$STATUS_VOLT_CPUCORE             = $PROC_DIR."Vccp1";
$STATUS_CHASSIS                  = $PROC_DIR."alarms";
$STATUS_FAN1					 = $PROC_DIR."fan1";
$STATUS_FAN2					 = $PROC_DIR."fan2";

$FANMUX							 = $ASMBIN_DIR."sysFanMUX";
$ASM_CONF						 = "/etc/asm/asm.conf";

#-----------------------------------------------------------------------------
#	Structures

#	Commands available for the associated ASM OID.
$commands{".1.3.6.1.4.1.343.2.10.3.3.1.0"}	= "get_manufacturer";
$commands{".1.3.6.1.4.1.343.2.10.3.3.2.0"}	= "get_version";
$commands{".1.3.6.1.4.1.343.2.10.3.3.3.0"}	= "get_device";
$commands{".1.3.6.1.4.1.343.2.10.3.3.4.0"}	= "get_thresholds";
$commands{".1.3.6.1.4.1.343.2.10.3.3.5.0"}	= "get_thresholds";
$commands{".1.3.6.1.4.1.343.2.10.3.3.6.0"}	= "get_thresholds";
$commands{".1.3.6.1.4.1.343.2.10.3.3.7.0"}	= "get_thresholds";
$commands{".1.3.6.1.4.1.343.2.10.3.3.8.0"}	= "get_thresholds";
$commands{".1.3.6.1.4.1.343.2.10.3.3.9.0"}	= "get_thresholds";
$commands{".1.3.6.1.4.1.343.2.10.3.3.10.0"}	= "get_thresholds";
$commands{".1.3.6.1.4.1.343.2.10.3.3.11.0"}	= "get_chassis";

#	Thresholds have the same general structure. This is the asm.conf
#	and driver data in the format of:
#	<non_critical_pair>:<critical_pair>:<driver>
$data{".1.3.6.1.4.1.343.2.10.3.3.4.0"} = "TEMPERATURE_THRESHOLDS:TEMPERATURE_CRITICAL_THRESHOLDS:$STATUS_TEMP";
$data{".1.3.6.1.4.1.343.2.10.3.3.5.0"} = "V0_THRESHOLDS:V0_CRITICAL_THRESHOLDS:$STATUS_VOLT_PLUS12";
$data{".1.3.6.1.4.1.343.2.10.3.3.6.0"} = "V1_THRESHOLDS:V1_CRITICAL_THRESHOLDS:$STATUS_VOLT_MINUS12";
$data{".1.3.6.1.4.1.343.2.10.3.3.7.0"} = "V2_THRESHOLDS:V2_CRITICAL_THRESHOLDS:$STATUS_VOLT_PLUS5";
$data{".1.3.6.1.4.1.343.2.10.3.3.8.0"} = "V3_THRESHOLDS:V3_CRITICAL_THRESHOLDS:$STATUS_VOLT_PLUS3POINT3";
$data{".1.3.6.1.4.1.343.2.10.3.3.9.0"} = "V4_THRESHOLDS:V4_CRITICAL_THRESHOLDS:$STATUS_VOLT_CPUIO";
$data{".1.3.6.1.4.1.343.2.10.3.3.10.0"} = "V5_THRESHOLDS:V5_CRITICAL_THRESHOLDS:$STATUS_VOLT_CPUCORE";

$oid = $ARGV[1];
if( exists($commands{$oid} ) )
{
	if($commands{$oid} =~ "get_thresholds")
	{
		eval "$commands{$oid}('$oid','$data{$oid}')";
	}elsif($commands{$oid} =~ "get_pf_stats")
	{
		eval "$commands{$oid}('$oid','$sub')";
	}else{
		eval "$commands{$oid}('$oid')";
	}
}else{

	print "$oid\n";
	print "string\n";
	print "Unsupported request\n";
}

###############################################################################
# GetSMBIOSData
###############################################################################

sub GetSMBIOSData
{
	my ($Key)			= @_;
	my $SMBIOSDataRaw	= `/usr/ins/intel/bin/asm/sysSMBIOS dump`;
	my @SMBIOSData		= split(/^/m, $SMBIOSDataRaw);
	my $KeyTemp;
	my $Value			= '';
	my $Line;

	# Convert to lower case.
	$Key = lc($Key);

	# Run through the data...
	foreach $Line (@SMBIOSData)
	{
		# If the line has an equal sign...
		if ($Line =~ /=/)
		{
			# Break out the key and value.
			($KeyTemp, $Value) = split(/=/, $Line);

			# Clear out the leading/trailing whitespace.
			$KeyTemp =~ s/^\s*(.*?)\s*$/$1/;

			# If the keys match...
			if ($Key eq lc($KeyTemp))
			{
				# Clear out the leading/trailing whitespace.
				$Value =~ s/^\s*(.*?)\s*$/$1/;
				last;
			}
			else
			{
				# Clean out the value.
				$Value = '';
			}
		}
	}

	return $Value;
}

## 	Get manufacturer.
sub get_manufacturer
{
	print "$oid\n";
	print "string\n";
	print "Intel Corp.\n";
}

## 	Get version.
sub get_version
{
	print "$oid\n";
	print "string\n";
	print "Version 1.1\n";
}

## 	Get description.
sub get_device
{
	open  (handle, "/bin/uname --all |");
	@list = split(" ", <handle>);
	close handle;

	print "$oid\n";
	print "string\n";
	print "$list[1]\n";
}

## 	Get thresholds
sub get_thresholds
{
	my $oid = shift;
	my $data = shift;
	my $ProcessorFamily;

	($noncritical, $critical, $device) = split(":", $data);

	# Look out for the core CPU voltage adjustment...
	if (('V5_THRESHOLDS' eq $noncritical) &&
		('V5_CRITICAL_THRESHOLDS' eq $critical) &&
		($ProcessorFamily = uc(GetSMBIOSData('PROCESSOR_FAMILY'))))
	{
		# Clean up the data.
		$ProcessorFamily =~ s/ /_/g;
		$ProcessorFamily =~ s/^(.*)_{1}PROCESSOR(.*)/$1$2/;

		# Set the keys.
		$noncritical	= 'V5_THRESHOLDS_' . $ProcessorFamily;
		$critical		= 'V5_CRITICAL_THRESHOLDS_' . $ProcessorFamily;
	}
	
	# Get thresholds from asm.conf.
	open( handle, "< $ASM_CONF");
	while(<handle>)
	{
		($command, $value) = split("\=");
		chomp($value);
		if($command =~ $noncritical)
		{
			($nonCriticalLow, $nonCriticalHigh) = split(",", $value);
		}elsif($command =~ $critical)
		{
			($criticalLow, $criticalHigh) = split(",", $value);
		}
	}
	close(handle);

	# Get current value.
	open( handle, "< $device");
	@list = split(" ", <handle>);
	close(handle);
	$current = $list[2];
	if($device =~ $STATUS_VOLT_MINUS12)
	{
		# Adjust for -12 voltage.
		$current = ((($current * (192 / 270)) * (-1200 / 192)) + 23.12) * -1
	}
	
	# Output as follows:
	# 1	normal
	# 2	upper-critical
	# 3	upper-warning
    # 4	lower-critical
	# 5	lower-warning
	if($current <= $criticalLow)
	{
		$output = 4;
	}elsif($current <= $nonCriticalLow)
	{
		$output = 5;
	}elsif($current >= $criticalHigh)
	{
		$output = 2;
	}elsif($current >= $nonCriticalHigh)
	{
		$output = 3;
	}else{
		$output = 1;
	}
	
	print "$oid\n";
	print "integer\n";
	print "$output\n";
}

## 	Get chassis status
sub get_chassis
{
	my $oid = shift;

	open( handle, "< $STATUS_CHASSIS");
	$status =  (<handle> & (1 << 4) ) ? 1 : 0;
	close(handle);

	print "$oid\n";
	print "integer\n";
	print "$status\n";
}

sub ReadSensorParameterArray
{
      my ($look,$lines) = @_;
      my ($len,$line,$x,$param,$howMany);

      $howMany = 0;
      if (open(FILE,"<$ASM_CONF") == 0)
      {
            return 0;
      }else{
            ## save all lines matching the $look, in the referenced array
            $look = $look.'=';
            $len = length($look);
            while (defined($line = <FILE>))
            {
                  if (substr($line,0,$len) eq $look)
                  {
                        ## This will chop off everything after the =
                        ($x,$param) = split(/=/,$line,2);
                        chomp($param);
                        push(@$lines,$param);
                        $howMany++;
                  }
            }
            close(FILE);
            return($howMany);
      }
}
