#!/usr/local/Hughes/bin/lite
/*
** 	Title	: sysinfo  
**	Purpose	: Grab some descriptive details from a box
**	Author	: David J. Hughes (bambi@Bond.edu.au)
**	Date	: 3 Nov 1993
**
**	Notes	: This script checks the MIB supported by the box and
**		  will grab whatever interesting info is available in
**		  that MIB (eg the extended system group in MIB II)
*/

modload "mod_snmp";

funct get_data(int $fd, array $vars, array $descr)
{
	$result = snmpget($fd,$vars);
	if ( # $result == 0)
	{
		echo("\nsysinfo : $ERRMSG\n");
		exit(1);
	}

	/*
	** Cycle through the data and display the results
	*/

	$index = 0;
	while($index < #$result)
	{
		$data = split($result[$index],LITE_BRK);
		printf("    %-15s : %s\n",$descr[$index], $data[1]);
		$index = $index + 1;
	}
}




$IF_TAB	= "interfaces.ifTable.ifEntry";
$CISCO_IF_TAB	= "enterprises.cisco.local.lInterfaces.lifTable.lifEntry";

$ifTypes = [	"Unknown",		"Other",
		"regular1822",		"hdh1822",
		"ddn-x25",		"rfc877-x25",
		"ethernet-csmacd",	"iso88023-csmacd",
		"iso88024-tokenBus",	"iso88025-tokenRing",
		"iso88026-man",		"starLan",
		"proteon-10Mbit",	"proteon-80Mbit",
		"hyperchannel",		"fddi",
		"lapb",			"sdlc",
		"ds1",			"e1",
		"basicISDN",		"primaryISDN",
		"propPointToPointSerial", "ppp",
		"softwareLoopback",	"eon",
		"ethernet-3Mbit",	"nsip",
		"slip",			"ultra",
		"ds3",			"sip",
		"frame-relay" ];


/*
** Check command line args
*/

if ($argc != 3)
{
	printf("\nUsage : sysinfo hostname community \n\n");
	exit(1);
}

$progname = $argv[0];
$hostname = $argv[1];
$community = $argv[2];

/*
** Fire up SNMP and get a connection to the box
*/

$fd = snmpopen($hostname,$community);
if ($fd < 0)
{
	echo("\n\nsysinfo : Unrecoverable error ($ERRMSG)!\n\n");
	exit(1);
}

/*
** Let's see what MIB this sucker's running
*/

echo("\nSystem Information for $hostname\n\n\n");
echo("General Information\n");
echo("===================\n\n");
$vars = [ "system.sysServices" ];
$result = snmpgetnext($fd,$vars);
if ( # $result == 0)
{
	echo("\n\nsysinfo : $ERRMSG\n\n");
	exit(1);
}
if ($result[0] =~ "system.sysServices")
{
	$mib = 2;
}
else
{
	$mib = 1;
}

$vars = [ "enterprises.cisco.local.lInterfaces.lifTable.lifEntry" ];
$result = snmpgetnext($fd,$vars);
if ( # $result == 0)
{
	echo("\n\nsysinfo : $ERRMSG\n\n");
	exit(1);
}
if ($result[0] =~ "enterprises.cisco.*")
{
	$cisco = "with Cisco enterprise extensions";
}
else
{
	$cisco = "";
}

printf("    %-15s : %d %s\n", "MIB Version", $mib, $cisco);

/*
** Grab some simple data from the system group
*/

if ($mib == 1)
{
	$vars = [ "system.sysdescr.0" ];
	$descr = [ "Description" ];
}
else
{
	$vars = [ "system.sysdescr.0", "system.sysObjectID.0",
		  "system.sysContact.0", "system.sysName.0",
		  "system.sysLocation.0" ];
	$descr = [ "Description", "Identification", "Contact",
		   "Name", "Location" ];
}
	
get_data($fd,$vars,$descr);


/*
** Now for the interface information.  Cycle through the table
*/

$vars[0] = $IF_TAB+".ifIndex";
$vars[1] = $IF_TAB+".ifDescr";
$vars[2] = $IF_TAB+".ifType"; 
$vars[3] = $IF_TAB+".ifSpeed";
$vars[4] = $IF_TAB+".ifPhysAddress";

/*$vars[0] = $IF_TAB+".ifIndex";
$vars[1] = $CISCO_IF_TAB+".locIfDescr";
$vars[2] = $CISCO_IF_TAB+".locIfHardType"; 
$vars[3] = $IF_TAB+".ifSpeed";
$vars[4] = $IF_TAB+".ifPhysAddress";
*/



echo("\n\nInterface Information\n");
echo("=====================\n\n");

echo("Num |    Descr     |           Type           |   Speed   |   Address\n");
echo("----+--------------+--------------------------+-----------+------------------\n");
$result = snmpgetnext($fd,$vars);
while($vars[0] =~ $IF_TAB+".ifIndex")
{
	$index = 0;

	if ( # $result == 0)
	{
		echo("\nsysinfo : $ERRMSG\n");
		exit(1);
	}

	/*
	** Split the OID and data keeping both so we can check
	** the returned OID for the end of table.
	*/

	while($index < #$result)
	{
		$data = split($result[$index],LITE_BRK);
		$vars[$index] = $data[0];
		$out[$index] = $data[1];
		$index = $index + 1;
	}

	/*
	** Print the results
	*/
	
	if($vars[0] =~ $IF_TAB+".ifIndex")
	{
		printf(" %-2s | %-12s |" , $out[0], $out[1]);
		if ((int)$out[2] <= #$ifTypes)
		{
			printf(" %-24s |",$ifTypes[(int)$out[2]]);
		}
		else
		{
			printf(" %-24s |", "Bad Type");
		}
		printf(" %-9s | %s\n",	$out[3],$out[4]);
	}
	$result = snmpgetnext($fd,$vars);
}
echo("---------------------------------------------------------------------------\n\n");
