#!/usr/bin/perl 


use Getopt::Std;
	       
# Port? This is the standard port#
my $port = 161;
my $ReverseTXRX;

my %opts; 
getopt("-ic", \%opts);
my $host = $opts{'i'} || Usage();
my $community = $opts{'c'} || Usage();
my $Percentage = 1;
my $MAX_RSSI = 40.0;

#################################################################
#---------------------- 4/22/2003 Earl -------------------------#
#################################################################

use Net::SNMP;
# use strict;

# Setup OIDs 
my $sysDescr =                   '1.3.6.1.4.1.410.1.1.1.1.0'; 
my $sysReset =                   '1.3.6.1.4.1.410.1.1.1.2.0'; 
my $sysTrapSwitch =              '1.3.6.1.4.1.410.1.1.1.3.0'; 
my $sysLoadDefaults =            '1.3.6.1.4.1.410.1.1.1.4.0'; 
my $sysDevInfo =                 '1.3.6.1.4.1.410.1.1.1.5.0';
my $sysUpload =                  '1.3.6.1.4.1.410.1.1.1.6.0';

my $operIPAddress =              '1.3.6.1.4.1.410.1.1.2.1.0';
my $operIPMask =                 '1.3.6.1.4.1.410.1.1.2.2.0';
my $operEthernetAddress =        '1.3.6.1.4.1.410.1.1.2.3.0';
my $operGateway =                '1.3.6.1.4.1.410.1.1.2.4.0';
my $operDHCP =                   '1.3.6.1.4.1.410.1.1.2.5.0';
my $operPrimaryPort =            '1.3.6.1.4.1.410.1.1.2.6.0';
my $operConfigurationPort =      '1.3.6.1.4.1.410.1.1.2.7.0';

my $operIPFilter          =      '1.3.6.1.4.1.410.1.1.3.1.0';
my $operForwardBcast      =      '1.3.6.1.4.1.410.1.1.3.2.0';
my $operSendBackBcast     =      '1.3.6.1.4.1.410.1.1.3.3.0';
my $operSendBackUnicast   =      '1.3.6.1.4.1.410.1.1.3.4.0';

my $bridgeOperationalMode =      '1.3.6.1.4.1.410.1.1.4.1.0';
my $bridgeRemoteBridgeBSSID =    '1.3.6.1.4.1.410.1.1.4.2.0';


my $EthRxStatistics =            '1.3.6.1.4.1.410.1.1.7.1.0'; 
my $EthTxStatistics =            '1.3.6.1.4.1.410.1.1.7.2.0'; 

my $TestModeOnOff =              '1.3.6.1.4.1.410.1.1.8.1.0'; 
my $TestModeCommandStructure  =  '1.3.6.1.4.1.410.1.1.8.2.0'; 
my $TestModeRadioConfiguration = '1.3.6.1.4.1.410.1.1.8.8.0'; 
my $TestModeStatistics     =     '1.3.6.1.4.1.410.1.1.8.9.0'; 

my $operChannelID =              '1.3.6.1.4.1.410.1.2.1.1.0';
my $operESSIDLength =            '1.3.6.1.4.1.410.1.2.1.2.0';
my $operESSID =                  '1.3.6.1.4.1.410.1.2.1.3.0';
my $operPreambleType =           '1.3.6.1.4.1.410.1.2.1.6.0';
my $operAuthenticationType =     '1.3.6.1.4.1.410.1.2.1.7.0';
 
my $wirelessStatistics =         '1.3.6.1.4.1.410.1.2.3.1.0';

my $ClientsNum =                 '1.3.6.1.4.1.410.1.2.5.1.0'; 
my $AssociatedSTAsInfo =         '1.3.6.1.4.1.410.1.2.5.2.0';

my $AuthorizedMacEnable =        '1.3.6.1.4.1.410.1.2.6.1.0';
my $AuthorizedMac =              '1.3.6.1.4.1.410.1.2.6.2.0';
my $AuthRadiusIP =               '1.3.6.1.4.1.410.1.2.6.3.0';
  
my $WirelessKnownAPs =           '1.3.6.1.4.1.410.1.2.7.1.0'; 

my $NetworkSettings =            '1.3.6.1.4.1.410.1.2.8.1.0'; 


# Start the SNMP session
my ($session, $error) = Net::SNMP->session(
	    -hostname  => shift || $host,
             -community => shift || $community,
             -port      => shift || $port
          );


   $NOR   = snmpQuery($session,$sysDescr);
   $Data  = snmpQuery($session,$WirelessKnownAPs);
   $Data1 = snmpQuery($session,$wirelessStatistics);
   $Data2 = snmpQuery($session,$bridgeRemoteBridgeBSSID);
   $Data3 = snmpQuery($session,$ClientsNum);
   $Data4 = snmpQuery($session,$TestModeCommandStructure);
   $Data5 = snmpQuery($session,$bridgeOperationalMode);
   $Data6 = snmpQuery($session,$operChannelID);
   $Data7 = snmpQuery($session,$TestModeRadioConfiguration);
   $Data10 =snmpQuery($session,$EthRxStatistics); 
   $Data11 =snmpQuery($session,$EthTxStatistics); 
   $Data12 =snmpQuery($session,$operESSID); 
   $Data13 =snmpQuery($session,$operAuthenticationType); 
   $Data14 =snmpQuery($session,$operPreambleType); 
   $Data15 =snmpQuery($session,$AuthorizedMacEnable); 
   $Data17 =snmpQuery($session,$AuthRadiusIP); 
   $Data16 =snmpQuery($session,$AuthorizedMac); 
   $Data18 =snmpQuery($session,$NetworkSettings); 
   $Data19 =snmpQuery($session,$sysDevInfo); 

$mo = hex($Data5);
 
 if ($mo == 1) {$Data5 = "Point to Multi-Point";
  }
 if ($mo == 2) {$Data5 = "AP";
  }
 if ($mo == 3) {$Data5 = "AP Client";
  }
 if ($mo == 4) {$Data5 = "Point to Point";
  }
 if ($mo == 5) {$Data5 = "Repeater";
  }
 
$auth = hex($Data13);
 
 if ($auth == 1) {$Data13 = "Open System";
  }
 if ($auth == 2) {$Data13 = "Shared Key";
  }
 if ($auth == 3) {$Data13 = "Both Types";
  }
 
$preamble = hex($Data14);
 
 if ($preamble == 1) {$Data14 = "Short";
  }
 if ($preamble == 2) {$Data14 = "Long";
  }

$MacEn = hex($Data15);
 
 if ($MacEn == 1) {$Data15 = "Enabled";
  }
 if ($MacEn == 2) {$Data15 = "Disabled";
  }

    print " \n";
 
    print "Data for IP       ",$host;
    print " \n";
  
    my ($session, $error) = Net::SNMP->session(
	    -hostname  => shift || $host,
             -community => shift || $community,
             -port      => shift || $port
          );
          
   $Data19 =snmpQuery($session,$sysDevInfo); 
   $Data20 =snmpQuery($session,$operIPAddress); 
   $Data21 =snmpQuery($session,$operIPMask); 
   $Data22 =snmpQuery($session,$operEthernetAddress); 
   $Data23 =snmpQuery($session,$operGateway); 
   $Data24 =snmpQuery($session,$operDHCP); 
   $Data25 =snmpQuery($session,$operPrimaryPort); 
   $Data26 =snmpQuery($session,$operConfigurationPort); 
   $Data27 =snmpQuery($session,$operIPFilter); 
   $Data28 =snmpQuery($session,$operForwardBcast); 
   $Data29 =snmpQuery($session,$operSendBackBcast); 
   $Data30 =snmpQuery($session,$operSendBackUnicast); 
   $Data31 =snmpQuery($session,$bridgeOperationalMode); 
   $Data32 =snmpQuery($session,$bridgeRemoteBridgeBSSID); 
   
   print "MAC Address       ";
   print substr($Data19,10,12);
   print " \n";
 
# Break it down into the AT76C510MIB Lables for WirelessStatitcs group   

    print " \n";

   
$ACKFailure = substr($Data1,114,8);
$Total = "0";
$conv = $ACKFailure;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "ACKFailure                  ",$Total;
    print " \n";

$CTSFailure = substr($Data1,122,8);
$Total = "0";
$conv = $CTSFailure;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "CTSFailure                  ",$Total;
    print " \n";
    
$RetryPackets = substr($Data1,130,8);
$Total = "0";
$conv = $RetryPackets;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "RetryPackets                ",$Total;
    print " \n";
    
$ReceivedDuplicate = substr($Data1,138,8);
$Total = "0";
$conv = $RetryDuplicate;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "RetryDuplicate              ",$Total;
    print " \n";
    
$FailedPackets = substr($Data1,146,8);
$Total = "0";
$conv = $FailedPackets;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "FailedPackets               ",$Total;
    print " \n";
    
$AgedPackets = substr($Data1,154,8);
$Total = "0";
$conv = $AgedPackets;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "AgedPackets                 ",$Total;
    print " \n";
    
$FCSError = substr($Data1,162,8);
$Total = "0";
$conv = $FCSError;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "FCSError                    ",$Total;
    print " \n";
    
$InvalidPLCP = substr($Data1,170,8);
$Total = "0";
$conv = $InvalidPLCP;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "InvalidPLCP                 ",$Total;
    print " \n";
    
# Break it down into the AT76C510MIB Lables for EthRX and EthTX Stats and print

$TotalBytesRX = substr($Data10,2,8);
$Total = "0";
$conv = $TotalBytesRX;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "TotalBytesRX                ",$Total;
    print " \n";
    
$TotalPacketsRX = substr($Data10,10,8);
$Total = "0";
$conv = $TotalPacketsRX;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "TotalPacketsRX              ",$Total;
    print " \n";
    
$PacketCRCErrorRX = substr($Data10,18,8);
$Total = "0";
$conv = $PacketCRCErrorRX;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "PacketCRCErrorRX            ",$Total;
    print " \n";

$TotalJabberRX = substr($Data10,106,8);
$Total = "0";
$conv = $TotalJabberRX;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "TotalJabberRX               ",$Total;
    print " \n";

$TotalBytesTX = substr($Data11,2,8);
$Total = "0";
$conv = $TotalBytesTX;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "TotalBytesTX                ",$Total;
    print " \n";
    
$TotalPacketsTX = substr($Data11,10,8);
$Total = "0";
$conv = $TotalPacketsTX;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "TotalPacketsTX              ",$Total;
    print " \n";
    
$PacketCRCErrorTX = substr($Data11,18,8);
$Total = "0";
$conv = $PacketCRCErrorTX;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "PacketCRCErrorTX            ",$Total;
    print " \n";

$TotalCollisionsTX = substr($Data11,106,8);
$Total = "0";
$conv = $TotalCollisionsTX;
$d0hb = hex(substr($conv, 0, 1));
$d0lb = hex(substr($conv, 1, 1));
$d1hb = hex(substr($conv, 2, 1));
$d1lb = hex(substr($conv, 3, 1));
$d2hb = hex(substr($conv, 4, 1));
$d2lb = hex(substr($conv, 5, 1));
$d3hb = hex(substr($conv, 6, 1));
$d3lb = hex(substr($conv, 7, 1));
$H1 = $d0lb + $d0hb * 16;
$L1 = $d1lb * 256 + ($d1hb * 4096);
$H2 = ($d2lb * 65536) + ($d2hb * (65536 * 16));
$L2 = ($d3lb * (65536 * 256)) + ($d3hb * (65536 * 4096));
$Total = ($L1) + ($H1) + ($L2) + ($H2);
    print "TotalCollisionsTX           ",$Total;
    print " \n";
    print " \n";

    print "Operational Name of Radio   ",$NOR;
    print " \n";
    print "Operational IP Address      ",($Data20);
    print " \n";
    print "Operational IP Mask         ",($Data21);
    print " \n";
    print "Operational Ether Address   ",($Data22);
    print " \n";
    print "Operational Gateway         ",($Data23);
    print " \n";

$DHCPen = hex($Data24);
 
 if ($DHCPen == 1) {$Data24 = "Enabled";
  }
 if ($DHCPen == 2) {$Data24 = "Disabled";
  }
    print "Operational DHCP            ",($Data24);
    print " \n";
    

$Filter = hex($Data27);
 
 if ($Filter == 1) {$Data27 = "Enabled";
  }
 if ($Filter == 2) {$Data27 = "Disabled";
  }
    print "Filter Non-IP Traffic       ",($Data27);
    print " \n";
    
$Bridge = hex($Data31);
 
 if ($Bridge == 1) {$Data31 = "Point 2 MiltiPoint";
  }
 if ($Bridge == 2) {$Data31 = "AP";
  }
 if ($Bridge == 3) {$Data31 = "Ap Client";
  }
 if ($Bridge == 4) {$Data31 = "Point 2 Point";
  }
 if ($Bridge == 5) {$Data31 = "Repeater";
  }
    print "Operational Mode            ",($Data31);
    print " \n";
    print "Connected to MAC            ",($Data32);
    print " \n";
     
    
# All Done. Close.  
$session->close;
exit 0;


#The actual query control, depending on the type. 
sub snmpQuery() {
    my ($session, $result,$resultval,@values,$retval,@Rest,$query,$type);
    ($session,$query,$type) = @_;
    $result = $session->get_request(
				    -varbindlist => [$query]
				    );
#
# The error check is important, more so, if the values 
# returned is not correct. This messes up the logs!
#
    if (!defined($result)) {
#
# Ideally this should be handled by the MRTG properly. 
#	printf("ERROR: %s.\n", $session->error);
	$session->close;
# Though its wrong to return '0' as exit code for an error, we'd 
# do this so that MRTG behaves.
	return;
	exit 0;
    }

    $resultval = $result->{$query};
    if ( $type == 0 ) { 
	(@Rest) = split(" ",$resultval);
	($retval,@Rest) = reverse(@Rest);
    }
    if ( $type == 1 ) {
	@values = hexString($resultval);
	($retval,@Rest) = @values;
    } 
    if ( $type == 2 ) {
	@values = hexString($resultval);
	my ( $UTPax, $BTPax, $MTPax, $TBecon, $TACK, $TRTS, $TCTS, $URPax, $BRPax, $MRPax, @Rest) = @values;
	$retval = $UTPax + $BTPax + $MTPax;	
    }
    if ( $type == 3 ) {
	@values = hexString($resultval);
	my ( $UTPax, $BTPax, $MTPax, $TBecon, $TACK, $TRTS, $TCTS, $URPax, $BRPax, $MRPax, @Rest) = @values;
	$retval = $URPax + $BRPax + $MRPax;	
    }
    if ($type == 4 ){
	$retval = $resultval;
    }
    return $retval;
}



# For the case of the Hex-String, the string needs to be broken into
# the octets, and they are read in reverse. 
sub hexString() {
    my (@vals,$dList,@Rest,$length,$i);
    ($dList,@Rest) = @_;
    $length = length($dList);

# Sanitizing by removing the 0x character first. 
    $dList = substr($dList,2,$length);
    $length = $length - 2;
# Quick and dirty extraction of values.
    $i=0;
    while ($i<$length) {
	my $val = "";
	my $k = 6;
	do {
	    my $Temp = substr($dList,$i+$k,2);
	    $val = $val.$Temp;
	    $k = $k - 2;
	} while ($k >-1);
	$i = $i+8;
	push @vals, hex($val);
    }
    return @vals;
}

