#/usr/local/bin/perl
###################################################
#
# Standard RCS Header information (see co(1))
#
# $Author: schemers $
#
# $Date: 1992/10/08 21:48:02 $
#
# $Revision: 1.1 $
#
# $Locker: schemers $
#
# $Source: /networking/src/gopher/gateways/g2snmp,v $
#
# $State: Exp $
#
# $Log: g2snmp,v $
# Revision 1.1  1992/10/08  21:48:02  schemers
# Initial revision
#
#--------------------------------------------------
# Copyright (c) 1992 Board of Trustees
#            Leland Stanford Jr. University
###################################################
# /*
#  * Redistribution and use in source and binary forms are permitted
#  * provided that the above copyright notice and this paragraph are
#  * duplicated in all such forms and that any documentation,
#  * advertising materials, and other materials related to such
#  * distribution and use acknowledge that the software was developed
#  * by Stanford University.  The name of the University may not be used 
#  * to endorse or promote products derived from this software without 
#  * specific prior written permission.
#  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
#  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
#  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#  */
# 
#----------------------------------------------------------------------
# variables you should change:

$snmp_table = "/usr/local/bin/snmp-table";

#----------------------------------------------------------------------


sub snmp_main {
 local($_) = @_;

 if (/^$/) {
    &Greply("7SNMP Agent\t$Ggw menu\t$Ghost\t$Gport");
    &Greply(".");
    exit(0);
 }

if (/^menu\s+(.*)/i)         {  &do_menu($1);  } 
elsif (/^at\s+(.*)/i)        {  &dump_at($1); }
elsif (/^system\s+(.*)/i)    {  &dump_system($1); }
elsif (/^interface\s+(.*)/i) {  &dump_interface($1); }
elsif (/^ip\s+(.*)/i) {  &dump_ip($1); }
elsif (/^icmp\s+(.*)/i) {  &dump_icmp($1); }
elsif (/^tcp\s+(.*)/i) {  &dump_tcp($1); }
elsif (/^udp\s+(.*)/i) {  &dump_udp($1); }
else {
  &Gabort("Unknown command!");
  exit;
}

}

sub do_menu {
    local($agent)=@_;
    &Greply("0Address Translation Group\t$Ggw at $agent\t$Ghost\t$Gport");
    &Greply("0Interface Group\t$Ggw interface $agent\t$Ghost\t$Gport");
    &Greply("0ICMP Group\t$Ggw icmp $agent\t$Ghost\t$Gport");
    &Greply("0IP Group\t$Ggw ip $agent\t$Ghost\t$Gport");
    &Greply("0System Group\t$Ggw system $agent\t$Ghost\t$Gport");
    &Greply("0TCP Group\t$Ggw tcp $agent\t$Ghost\t$Gport");
    &Greply("0UDP Group\t$Ggw udp $agent\t$Ghost\t$Gport");
    &Greply(".");
    exit(0);
}


chop($date=`date`);

%ifType2a = (
        1, 'other',
        2, 'regular1822',
	3, 'hdh1822',
	4, 'ddn-x25',
	5, 'rfc877-x25',
	6, 'ethernet-csmacd',
	7, 'iso88023-csmacd',
	8, 'iso88024-tokenBus',
	9, 'iso88025-tokenRing',
	10,' iso88026-man',
	11,' starLan',
	12,' proteon-10Mbit',
	13,' proteon-80Mbit',
	14,' hyperchannel',
	15,' fddi',
	16,' lapb',
	17,' sdlc',
	18,' ds1',
	19,' e1',
	20,' basicISDN',
	21,' primaryISDN',
	22,' propPointToPointSerial',
	23,' ppp',
	24,'softwareLoopback',
	25,' eon,',
	26,' ethernet-3Mbit',
	27,' nsip',
	28,' slip',
	29,' ultra',
	30,' ds3',
	31,' sip',
	32,' frame-relay'
);

%ifAdminStatus2a = (1,'up',2,'down',3,'testing');
%ifOperStatus2a = %ifAdminStatus2a ;

%ifRouteType2a=(1,'other',2,'invalid',3,'direct',4,'remote');

%ipNetToMediaType2a=(1,'other',2,'invalid',3,'dynamic',4,'static');

%tcpConnState2a=(1,'closed',2,'listen',3,'synSent',4,'synReceived',
          5,'established',6,'finWait1',7,'finWait2',8,'closeWait',
          9,'lastAck',10,'closing',11,'timeWait');

%ipForwarding2a=(1,'gateway',2,'host');

%tcpRtoAlgorithm2a = (1,'other',2,'constant',3,'rsre',4,'vanj');

sub sysServices2a {
  local($x) = @_;
  local($result);

  $result .= "physical, " if ($x & 1);
  $result .= "data-link, " if ($x & 2);
  $result .= "internet, " if ($x & 4);   
  $result .= "end-to-end, " if ($x & 8); 
  $result .= "application, " if ($x & 64);

  $result =~ s/, $//;
  return $result;
}

sub timeTicks2a {
  local($ticks)= @_;
  local($d,$h,$m,$s,$result);
  
  $ticks /= 100;

  $d = int($ticks / (60 * 60 * 24));
  $ticks %= (60*60*24);

  $h = int($ticks / (60 * 60));
  $ticks %= (60*60);
 
  $m = int($ticks/60);
  $s = $ticks % 60;

  $result = sprintf("%d:%02d:%02d",$h,$m,$s);
  if ($d==1) {
        $result = sprintf("%d day ",$d) . $result;
  } elsif ($d>1) {
        $result = sprintf("%d days ",$d) . $result;
  }
  return $result;

}

sub hexString2a {
   local($string) = @_;
   local($result,$n);
     
   foreach (split(/:/,$string)) {
       $n = oct("0x$_");
       last if !$n;
       $result .= sprintf("%c",$n);
   }
   $result;
}

sub dump_system {

  local($host) = @_;
  local(*int);

  &get_table($host,"system",*int);

    print "\n";
    print "Host: $host\nDate: $date\n";
    print "\n";
    print " "*30,"System Table\n";
    print "System Description\n  ", $int{"sysDescr[0]"},"\n\n";
    print "UpTime       ", &timeTicks2a($int{"sysUpTime[0]"}),"\n";
    print "ObjectID     ", $int{"sysObjectID[0]"},"\n";
    $sysName =  $int{"sysName[0]"};
    $sysContact = $int{"sysContact[0]"};
    $sysLocation = $int{"sysLocation[0]"};
    $sysServices = $int{"sysServices[0]"};
    if ($sysName || $sysContact || $sysLocation || $sysServices) {
    print "Name         $sysName\n";
    print "Contact      $sysContact\n";
    print "Location     $sysLocation\n";
    print "Services     ",&sysServices2a($sysServices),"\n";
    }
    print "\n";
}

sub dump_interface {
  local($host) = @_;
  local(*int);

  &get_table($host,"interfaces",*int);

  $number = $int{"ifNumber[0]"};
    print "\n";
    print "Host: $host\nDate: $date\n";
    print "\n";
    print " "*30,"Interface Table\n";
    print "\n";

    print "Number of interfaces = $number\n";

  for ($i=1; $i <= $number; $i++) {
    print "\n";
   print "-"*40,"Interface $i\n";
    $ifIndex = $int{"ifIndex[$i]"};
    $ifDescr = &hexString2a($int{"ifDescr[$i]"});
    $ifType  = $ifType2a{$int{"ifType[$i]"}};
    $ifMtu   = $int{"ifMtu[$i]"};
    $ifSpeed = $int{"ifSpeed[$i]"};
    $ifPhysAddress = $int{"ifPhysAddress[$i]"};
    $ifAdminStatus = $ifAdminStatus2a{$int{"ifAdminStatus[$i]"}};
    $ifOperStatus  = $ifOperStatus2a{$int{"ifOperStatus[$i]"}};
    $ifLastChange  = &timeTicks2a($int{"ifLastChange[$i]"});
    $ifInOctets = $int{"ifInOctets[$i]"}+0;
    $ifInUcastPkts = $int{"ifInUcastPkts[$i]"}+0;
    $ifInNUcastPkts = $int{"ifInNUcastPkts[$i]"}+0;
    $ifInDiscards =  $int{"ifInDiscards[$i]"}+0;
    $ifInErrors = $int{"ifInErrors[$i]"}+0;
    $ifInUnknownProtos = $int{"ifInUnknownProtos[$i]"}+0;
    $ifOutOctets = $int{"ifOutOctets[$i]"}+0;
    $ifOutUcastPkts = $int{"ifOutUcastPkts[$i]"}+0;
    $ifOutNUcastPkts =$int{"ifOutNUcastPkts[$i]"}+0;
    $ifOutDiscards = $int{"ifOutDiscards[$i]"}+0;
    $ifOutErrors = $int{"ifOutErrors[$i]"}+0;
    $ifOutQlen =  $int{"ifOutQlen[$i]"}+0;
print "\n";
if ($ifDescr) {  
printf("Description     %s (%d)\n",$ifDescr,$ifIndex); 
}
else          {  
printf("Description     (%d)\n",$ifIndex); 
}
printf("Address         $ifPhysAddress\n");
printf("Type            %s\n",$ifType);
printf("Mtu             %s\n",$ifMtu);
printf("Speed           %s (%s)\n",$ifSpeed,int($ifSpeed/1000000)." MBS");
printf("Status          %s",$ifOperStatus);
if ($ifAdminStatus ne $ifOperStatus) { 
      printf("(desired state: $ifAdminStatus)"); 

}
if ($int{"ifLastChange[$i]"}) { print "(last config change: $ifLastChange)";}
printf("\n");
printf("Unknwn Proto    %s\n",$ifInUnknownProtos);
printf("Output Q len    %s\n",$ifOutQlen);


printf("Input\n");
printf("  Octects       %d\n",$ifInOctects);
printf("  unicast       %d\n",$ifInUcastPkts);
printf("  broad/multi   %d\n",$ifInNUcastPkts);
printf("  discards      %d\n",$ifInDiscards);
printf("  errors        %d\n",$ifInErrors);
printf("Output\n");
printf("  Octects       %d\n",$ifOutOctets);
printf("  unicast       %d\n",$ifOutUcastPkts);
printf("  broad/multi   %d\n",$ifOutNUcastPkts);
printf("  discards      %d\n",$ifOutDiscards);
printf("  errors        %d\n",$ifOutErrors);


  }
}

sub dump_at {

  local($host) = @_;
  local(*at);

  &get_table($host,"at",*at);

    print "\n";
    print "Host: $host\nDate: $date\n";
    print "\n";
    print "       Address Translation Group\n";
    print "\n";

    printf("%8s %17s       %s\n","Interface","Physical Addr","IP Addr");

  foreach (keys %at) {

      if (/^atIfIndex\[([^]]*)/) {
         $_ = $index = $1;
         ($if,$ip) = /(\d+)\.\d+\.(.*)/;
         printf("   %2d       %s    %s\n",$if,$at{"atPhysAddress[$index]"},$ip);
      }
  }
    print "\n";
}


sub dump_ip {

  local($host) = @_;
  local(*ip);

  &get_table($host,"ip",*ip);

    print "\n";
    print "Host: $host\nDate: $date\n";
    print "\n";
    print " "*10,"IP Group\n";

  print "\n";
  print "Forwarding          ",$ipForwarding2a{$ip{"ipForwarding[0]"}},"\n";
  print "Default TTL         ",$ip{"ipDefaultTTL[0]"}+0,"\n";
  print "Input\n";
  print "  Datagrams         ",$ip{"ipInReceives[0]"}+0,"\n";
  print "  Hardware Err      ",$ip{"ipInHdrErrors[0]"}+0,"\n";
  print "  Address Err       ",$ip{"ipInAddrErrors[0]"}+0,"\n";
  print "  Unknown Proto     ",$ip{"ipInUnknownProtos[0]"}+0,"\n";
  print "  Discards          ",$ip{"ipInDiscards[0]"}+0,"\n";
  print "  Delivers          ",$ip{"ipInDelivers[0]"}+0,"\n";
  print "Output\n";
  print "  Datagrams         ",$ip{"ipOutReceives[0]"}+0,"\n";
  print "  Discards          ",$ip{"ipOutDiscards[0]"}+0,"\n";
  print "  No Routes         ",$ip{"ipOutNoRoutes[0]"}+0,"\n";
  print "Reassembly\n";
  print "  Timeout           ",$ip{"ipReasmTimeout[0]"}+0,"\n";
  print "  Requests          ",$ip{"ipReasmReqds[0]"}+0,"\n";
  print "  OK                ",$ip{"ipReasmOKs[0]"}+0,"\n";
  print "  Fail              ",$ip{"ipReasmFails[0]"}+0,"\n";
  print "Frags\n";
  print "  OK                ",$ip{"ipFragOKs[0]"}+0,"\n";
  print "  Fail              ",$ip{"ipFragFails[0]"}+0,"\n";
  print "  Created           ",$ip{"ipFragCreates[0]"}+0,"\n";
  print "Forwarded Datagrams ",$ip{"ipForwDatagrams[0]"}+0,"\n";

  $done=0;
  foreach (keys %ip) {

      if (/^ipAdEntAddr\[([^]]*)/) {
    if (!$done) {
    print "\n";
         print " "*20,"IP Interface Address Table\n";
         printf("%s  %-15s %-15s %-15s  %s\n",
         "Interface","IP address","Net Mask","Broadcast","Reasm Max (mib II)");
         $done=1;
    }
         $_ = $1;
         printf("  %2d       %-15s %-15s %-15s  %s\n",
		$ip{"ipAdEntIfIndex[$_]"},
                $_,
		$ip{"ipAdEntNetMask[$_]"},
		$ip{"ipAdEntBcastAddr[$_]"},
		$ip{"ipAdEntReasmMaxSize[$_]"});
      }
  }

  $done=0;
  foreach (keys %ip) {

      if (/^ipRouteDest\[([^]]*)/) {
    if (!$done) {
    print "\n";
         print " "*25,"Routing Table\n";

         printf("%-15s %7s  %-15s %-7s  %s\n","Destination","Interface","Next Hop","Type","Mask (mib II)");
         $done=1;
    }
         $_ = $1;
         printf("%-15s    %2d      %-15s %-7s  %s\n",
                $_,
		$ip{"ipRouteIfIndex[$_]"},
		$ip{"ipRouteNextHop[$_]"},
		$ifRouteType2a{$ip{"ipRouteType[$_]"}},
		$ip{"ipRouteMask[$_]"});
      }
  }

$done=0;
  foreach (keys %ip) {
      if (/^ipNetToMediaIfIndex\[([^]]*)/) {
if (!$done) {
         print "\n";
         print " "*15,"Net to Media Table (ARP cache)\n";
         printf(" %s   %-17s    %-15s  %s\n",
           "Interface","Physical Address","IP Address","Type");
   $done=1;
}
         $index = $_ = $1;
         ($if,$ip) = /(\d+)\.(.*)/;
         printf("   %2d        %-17s    %-15s  %s\n",
                $if,
		$ip{"ipNetToMediaPhysAddress[$index]"},
                $ip,
		$ipNetToMediaType2a{$ip{"ipNetToMediaType[$index]"}});
      }
  }
    print "\n";

}


sub dump_tcp {

  local($host) = @_;
  local(*tcp);

  &get_table($host,"tcp",*tcp);

    print "\n";
    print "Host: $host\nDate: $date\n";
    print "\n";
    print "       TCP Group\n";
    print "\n";
print "Retrans Algorithm    ",$tcpRtoAlgorithm2a{$tcp{"tcpRtoAlgorithm[0]"}},"\n";
print "Retrans Min Timeout  ",$tcp{"tcpRtoMin[0]"},"\n";
print "Retrans Max Timeout  ",$tcp{"tcpRtoMax[0]"},"\n";
print "Max Connections      ",$tcp{"tcpMaxConn[0]"},"\n";
print "Active Opens         ",$tcp{"tcpActiveOpens[0]"},"\n";
print "Passive Opens        ",$tcp{"tcpPassiveOpens[0]"},"\n";
print "Attempt Fails        ",$tcp{"tcpAttemptFails[0]"},"\n";
print "Connection Resets    ",$tcp{"tcpEstabResets[0]"},"\n";
print "Current Connections  ",$tcp{"tcpCurrEstab[0]"},"\n";
print "Input Segments       ",$tcp{"tcpInSegs[0]"},"\n";
print "Output Segments      ",$tcp{"tcpOutSegs[0]"},"\n";
print "Retrans Segments     ",$tcp{"tcpRetransSegs[0]"},"\n";
print "Input Errors         ",$tcp{"tcpInErrs[0]"},"\n";
print "Output Resets        ",$tcp{"tcpOutRsts[0]"},"\n";

    print "\n";
print " "*20,"TCP Connection Table\n";
print " "*10,"Local "," "*20,"Remote\n";
         printf("%-15s %5s      %-15s %5s    %s\n",
               "Address","Port","Address","Port","State");
  foreach (keys %tcp) {

      if (/^tcpConnState\[([^]]*)/) {
         $_ = $index = $1;
         ($lip,$lp,$rip,$rp) = 
              /(\d+\.\d+\.\d+\.\d+)\.(\d+)\.(\d+\.\d+\.\d+\.\d+)\.(\d+)/;
              $tcpConnState = $tcpConnState2a{$tcp{"tcpConnState[$index]"}};
         printf("%-15s %5d      %-15s %5d    %s\n",
                    $lip,$lp,$rip,$p,$tcpConnState);
      }
  }
    print "\n";
}


sub dump_udp {

  local($host) = @_;
  local(*udp);

  &get_table($host,"udp",*udp);

    print "\n";
    print "Host: $host\nDate: $date\n";
    print "\n";
    print "       UDP Group\n";
    print "\n";

    print "Input Datagrams   ", $udp{"udpInDatagrams[0]"}+0,"\n";
    print "Input Errors      ", $udp{"udpInErrors[0]"}+0,"\n";
    print "Unknown Dest Port ", $udp{"udpNoPorts[0]"}+0,"\n";
    print "Output Datagrams  ", $udp{"udpOutDatagrams[0]"}+0,"\n";

  foreach (keys %udp) {

      if (/^udpLocalPort\[([^]]*)/) {
if (!$done) {
    print "\n";
print " "*10,"UDP Table\n";
         printf("%-15s %5s\n","Local Address","Local Port");
 $done=1;
}
         $_ = $index = $1;
         ($lip,$lp) =/(\d+\.\d+\.\d+\.\d+)\.(\d+)/;
         printf("%-15s    %5d\n",$lip,$lp);
      }
  }
    print "\n";
}



sub dump_icmp {

  local($host) = @_;
  local(*icmp);

  &get_table($host,"icmp",*icmp);

    print "\n";
    print "Host: $host\nDate: $date\n";
    print "\n";
    print " "*30,"ICMP Group\n";

    print "\n";
    printf("                           Input         Output\n");
    printf(" Messages           %12d   %12d\n", $icmp{"icmpInMsgs[0]"}, $icmp{"icmpOutMsgs[0]"});
    printf(" Errors             %12d   %12d\n", $icmp{"icmpInErrors[0]"}, $icmp{"icmpOutErrors[0]"});
    printf(" Dest unreach       %12d   %12d\n", $icmp{"icmpInDestUnreachs[0]"}, $icmp{"icmpOutDestUnreachs[0]"});
    printf(" Time exceeds       %12d   %12d\n", $icmp{"icmpInTimeExcds[0]"}, $icmp{"icmpInTOuteExcds[0]"});
    printf(" Parm problems      %12d   %12d\n", $icmp{"icmpInParmProbs[0]"}, $icmp{"icmpOutParmProbs[0]"});
    printf(" Source Quenchs     %12d   %12d\n", $icmp{"icmpInSrcQuenchs[0]"}, $icmp{"icmpOutSrcQuenchs[0]"});
    printf(" Redirects          %12d   %12d\n", $icmp{"icmpInRedirects[0]"}, $icmp{"icmpInRedOutects[0]"});
    printf(" Echos              %12d   %12d\n", $icmp{"icmpInEchos[0]"}, $icmp{"icmpOutEchos[0]"});
    printf(" Echo reps          %12d   %12d\n", $icmp{"icmpInEchoReps[0]"}, $icmp{"icmpOutEchoReps[0]"});
    printf(" Timestamps         %12d   %12d\n", $icmp{"icmpInTimestamps[0]"}, $icmp{"icmpInTOutestamps[0]"});
    printf(" Timestamp reps     %12d   %12d\n", $icmp{"icmpInTimestampReps[0]"}, $icmp{"icmpInTOutestampReps[0]"});
    printf(" Address masks      %12d   %12d\n", $icmp{"icmpInAddrMasks[0]"}, $icmp{"icmpOutAddrMasks[0]"});
    printf(" Address mask reps  %12d   %12d\n", $icmp{"icmpInAddrMaskReps[0]"}, $icmp{"icmpOutAddrMaskReps[0]"});



    print "\n";
}


sub get_table { 
    local($host,$t,*table) = @_;

 open(SNMP,"echo '$t\[]' | $snmp_table $host|");
 while(<SNMP>) {
    chop;
    /^(.*)=(.*)$/;
    $table{$1} = $2;
 }
}


1;
