Blame contrib/switchmaker/switchmaker

Packit 667938
#!/usr/local/bin/perl
Packit 667938
#######################################################
Packit 667938
# switchmaker for MRTG
Packit 667938
#######################################################
Packit 667938
#   Author: Markley Dykeman - 04/27/99
Packit 667938
#  Purpose: This program makes an MRTG configuration 
Packit 667938
#           file for Cisco 5500 switches.
Packit 667938
#    Usage: switchmaker <fqdm> <snmpRO>
Packit 667938
# Requires: SNMP-Util-1.6 and SNMP-1.8
Packit 667938
#######################################################
Packit 667938
Packit 667938
my $testing = 0;
Packit 667938
Packit 667938
# Libraries
Packit 667938
use SNMP::Util;
Packit 667938
use Socket;
Packit 667938
use strict;
Packit 667938
Packit 667938
# Environment
Packit 667938
$ENV{'MAX_LOG_LEVEL'} = 'status';
Packit 667938
Packit 667938
# Usage display
Packit 667938
if (@ARGV < 1){
Packit 667938
   print "Usage: \n";
Packit 667938
   print "      switchmaker <IP> <community string>\n";
Packit 667938
   print "\n";
Packit 667938
   print "       IP = IP address or Switch name\n";
Packit 667938
   print "       comm = defaults to hostname\n";
Packit 667938
   exit;
Packit 667938
}
Packit 667938
Packit 667938
# Arrays
Packit 667938
my(%iftype)=(	'1'=>'other',
Packit 667938
		'2'=>'cddi',
Packit 667938
		'3'=>'fddi',
Packit 667938
		'4'=>'tppmd',
Packit 667938
		'5'=>'mlt3',
Packit 667938
		'6'=>'sddi',
Packit 667938
		'7'=>'smf',
Packit 667938
		'8'=>'e10BaseT',
Packit 667938
		'9'=>'e10BaseF',
Packit 667938
		'10'=>'scf',
Packit 667938
		'11'=>'e100BaseTX',
Packit 667938
		'12'=>'e100BaseT4',
Packit 667938
		'13'=>'e100BaseF',
Packit 667938
		'14'=>'atmOc3mmf',
Packit 667938
		'15'=>'atmOc3smf',
Packit 667938
		'16'=>'atmOc3utp',
Packit 667938
		'17'=>'e100BaseFsm',
Packit 667938
		'18'=>'e10a100BaseTX',
Packit 667938
		'19'=>'mii',
Packit 667938
		'20'=>'vlanRouter',
Packit 667938
		'22'=>'tokenring',
Packit 667938
		'23'=>'atmOc12mmf',
Packit 667938
		'24'=>'atmOc12smf',
Packit 667938
		'25'=>'atmDs3',
Packit 667938
		'26'=>'tokenringMmf',
Packit 667938
		'27'=>'e1000BaseLX'
Packit 667938
		);
Packit 667938
Packit 667938
# Declare working variables
Packit 667938
my ($sysDescr, $sysName, $sysIpAddr, %portGrp, $portIfIndex, $interfacecnt, @result, @oid_list, $iphost);
Packit 667938
my (@oid_cutoff, $i);  
Packit 667938
Packit 667938
# Get command line arguments
Packit 667938
my $IP = $ARGV[0];
Packit 667938
my $Comm_string = $ARGV[1];
Packit 667938
Packit 667938
# Open connection to device
Packit 667938
my $snmp = new SNMP::Util(-device    => $IP,
Packit 667938
                       -community => $Comm_string);
Packit 667938
Packit 667938
########################
Packit 667938
# Get system information
Packit 667938
########################
Packit 667938
Packit 667938
@result = $snmp->next( 'ne', '1.3.6.1.2.1.1.1'); #sysDescr
Packit 667938
$sysDescr = $result[1];
Packit 667938
Packit 667938
@result = $snmp->next( 'ne', '1.3.6.1.2.1.1.5'); #sysName
Packit 667938
$sysName = $result[1];
Packit 667938
Packit 667938
@result = $snmp->next( 'ne', '1.3.6.1.4.1.9.5.1.1.2'); #sysIpAddr
Packit 667938
$sysIpAddr = $result[1];
Packit 667938
Packit 667938
$iphost = gethostbyaddr(pack('C4',split(/\./,$sysIpAddr)), AF_INET); #iphost
Packit 667938
if (!defined $iphost || ($iphost eq '')){
Packit 667938
   $iphost = 'Unknown DNS name';
Packit 667938
}
Packit 667938
Packit 667938
#########################
Packit 667938
# Walk through ports
Packit 667938
#########################
Packit 667938
@oid_list = (	'1.3.6.1.4.1.9.5.1.4.1.1.11', #portIfIndex
Packit 667938
		'1.3.6.1.4.1.9.5.1.4.1.1.5',  #portType
Packit 667938
		'1.3.6.1.4.1.9.5.1.4.1.1.10', #portDuplex
Packit 667938
		'1.3.6.1.4.1.9.5.1.4.1.1.1',  #portModuleIndex
Packit 667938
		'1.3.6.1.4.1.9.5.1.4.1.1.2',  #portIndex
Packit 667938
		'1.3.6.1.4.1.9.5.1.4.1.1.4',  #portName
Packit 667938
		);
Packit 667938
@oid_cutoff = 	'1.3.6.1.4.1.9.5.1.4.1.1.11';
Packit 667938
Packit 667938
# Walk the MIBs...
Packit 667938
my ($more) = 1;
Packit 667938
while ($more) {
Packit 667938
   @result = $snmp->next(	-format => 'one',
Packit 667938
				-oids => \@oid_list,
Packit 667938
                    		);
Packit 667938
Packit 667938
   # Are we done walking?
Packit 667938
   if (@result[0] !~ /@oid_cutoff/) {
Packit 667938
     $more = 0;
Packit 667938
   }
Packit 667938
Packit 667938
   # if not, process the data
Packit 667938
   if ($more) {
Packit 667938
      $portIfIndex = $result[2];
Packit 667938
      $interfacecnt = $result[2];
Packit 667938
      $portGrp{$portIfIndex}{'portIfIndex'} = $result[2];
Packit 667938
      $portGrp{$portIfIndex}{'portType'} = $result[5];
Packit 667938
      $portGrp{$portIfIndex}{'portDuplex'} = $result[8];
Packit 667938
      $portGrp{$portIfIndex}{'portModuleIndex'} = $result[11];
Packit 667938
      $portGrp{$portIfIndex}{'portIndex'} = $result[14];
Packit 667938
      $portGrp{$portIfIndex}{'portName'} = $result[17];
Packit 667938
   }
Packit 667938
Packit 667938
   # Update OID for snmp->next
Packit 667938
   @oid_list = ($result[0],$result[3],$result[6],$result[9],$result[12],$result[15]);
Packit 667938
Packit 667938
}
Packit 667938
Packit 667938
##############################
Packit 667938
# Walk through MIB2 interfaces 
Packit 667938
##############################
Packit 667938
@oid_list = (	'1.3.6.1.2.1.2.2.1.1', #ifIndex
Packit 667938
		'1.3.6.1.2.1.2.2.1.2', #ifDescr
Packit 667938
		'1.3.6.1.2.1.2.2.1.3', #ifType
Packit 667938
		'1.3.6.1.2.1.2.2.1.5', #ifSpeed
Packit 667938
		'1.3.6.1.2.1.2.2.1.7', #ifAdminStatus
Packit 667938
		'1.3.6.1.2.1.2.2.1.8', #ifOperStatus
Packit 667938
		);
Packit 667938
@oid_cutoff =	'1.3.6.1.2.1.2.2.1.1';
Packit 667938
Packit 667938
Packit 667938
# Walk the MIBs...
Packit 667938
my ($more) = 1;
Packit 667938
while ($more) {
Packit 667938
   @result = $snmp->next(	-format => 'one',
Packit 667938
     				-oids => \@oid_list,
Packit 667938
                    		);
Packit 667938
Packit 667938
   # Are we done walking?
Packit 667938
   if (@result[0] !~ /@oid_cutoff/) {
Packit 667938
     $more = 0;
Packit 667938
   }
Packit 667938
Packit 667938
   # if not, process the data
Packit 667938
   if ($more) {
Packit 667938
      $portIfIndex = $result[2];
Packit 667938
Packit 667938
      # deal with non-workgroup ports (1 & 2
Packit 667938
      if ($portGrp{$portIfIndex}{'portIfIndex'} eq "") {
Packit 667938
         $portGrp{$portIfIndex}{'portIfIndex'} = $result[2];
Packit 667938
         if ($portIfIndex eq "1") {
Packit 667938
            $portGrp{$portIfIndex}{'portName'} =  $iphost
Packit 667938
         } else {
Packit 667938
            $portGrp{$portIfIndex}{'portName'} = $result[5];
Packit 667938
            $portGrp{$portIfIndex}{'ifDescr'} = $result[8];
Packit 667938
         }
Packit 667938
      }
Packit 667938
      $portGrp{$portIfIndex}{'ifDescr'} = $result[5] if (!($portGrp{$portIfIndex}{'ifDescr'} eq $result[8]));
Packit 667938
      $portGrp{$portIfIndex}{'ifType'} = $result[8];
Packit 667938
      $portGrp{$portIfIndex}{'ifSpeed'} = $result[11];
Packit 667938
      $portGrp{$portIfIndex}{'ifAdminStatus'} = $result[14];
Packit 667938
      $portGrp{$portIfIndex}{'ifOperStatus'} = $result[17];
Packit 667938
   }
Packit 667938
Packit 667938
   # Update OID for snmp->next
Packit 667938
   @oid_list = ($result[0],$result[3],$result[6],$result[9],$result[12],$result[15]);
Packit 667938
Packit 667938
}
Packit 667938
Packit 667938
if ($testing) {
Packit 667938
   print "Summary for $sysName ($sysIpAddr), $sysDescr\n";
Packit 667938
   for ($i = 1;$i <= $interfacecnt; $i++) {
Packit 667938
      print "$portGrp{$i}{'portIfIndex'} ";
Packit 667938
      print "$portGrp{$i}{'portType'} ";
Packit 667938
      print "$portGrp{$i}{'portDuplex'} ";
Packit 667938
      print "$portGrp{$i}{'portModuleIndex'}/";
Packit 667938
      print "$portGrp{$i}{'portIndex'} ";
Packit 667938
      print "$portGrp{$i}{'portName'}\n";
Packit 667938
      print "$portGrp{$i}{'ifDescr'} ";
Packit 667938
      print "$portGrp{$i}{'ifType'} ";
Packit 667938
      print "$portGrp{$i}{'ifSpeed'} ";
Packit 667938
      print "$portGrp{$i}{'ifAdminStatus'} ";
Packit 667938
      print "$portGrp{$i}{'ifOperStatus'}\n";
Packit 667938
   }
Packit 667938
}
Packit 667938
Packit 667938
#######################################
Packit 667938
# Start creating the configuration file
Packit 667938
#######################################
Packit 667938
print "# Add a WorkDir: /some/path line to this file\n\n";
Packit 667938
Packit 667938
for ($i = 1;$i <= $interfacecnt; $i++) {
Packit 667938
Packit 667938
   my $slash;
Packit 667938
   if ($portGrp{$i}{'portModuleIndex'} eq '') {
Packit 667938
      $slash = " ";
Packit 667938
   } else {
Packit 667938
      $slash = "/";
Packit 667938
   }
Packit 667938
Packit 667938
   my $c;
Packit 667938
   my $ifSpeed = $portGrp{$i}{'ifSpeed'};
Packit 667938
   my $speedStr = &fmi($ifSpeed);
Packit 667938
Packit 667938
   if (!($portGrp{$i}{'ifAdminStatus'} eq "up")) {
Packit 667938
      print <
Packit 667938
########
Packit 667938
######## This Interface is one of the following
Packit 667938
######## - administratively not UP
Packit 667938
######## - it is in test mode
Packit 667938
######## - it is a softwareLoopback interface
Packit 667938
######## - has a unrealistic speed setting
Packit 667938
######## It is commented out for this reason.
Packit 667938
########
Packit 667938
ECHO
Packit 667938
      $c="# ";
Packit 667938
   } else {
Packit 667938
      $c = '';
Packit 667938
   }
Packit 667938
   print <
Packit 667938
${c}
Packit 667938
${c}Target[$iphost.$i]: $i:$Comm_string\@$sysIpAddr
Packit 667938
${c}MaxBytes[$iphost.$i]: $ifSpeed
Packit 667938
${c}Title[$iphost.$i]: $portGrp{$i}{'portModuleIndex'}$slash$portGrp{$i}{'portIndex'} $portGrp{$i}{'portName'} -- $portGrp{$i}{'ifDescr'}
Packit 667938
${c}Options[$iphost.$i]: growright, bits
Packit 667938
${c}PageTop[$iphost.$i]: 

Traffic Analysis for $portGrp{$i}{'portModuleIndex'}$slash$portGrp{$i}{'portIndex'} $portGrp{$i}{'portName'}

Packit 667938
${c} 
Packit 667938
${c}   System:$iphost.$i
Packit 667938
${c}   Interface: $portGrp{$i}{'portModuleIndex'}$slash$portGrp{$i}{'portIndex'} $portGrp{$i}{'portName'} -- $portGrp{$i}{'ifDescr'} ($i)
Packit 667938
${c}   Max Speed:
Packit 667938
${c}        $speedStr ($portGrp{$i}{'portType'}) ($portGrp{$i}{'portDuplex'})
Packit 667938
${c}  
Packit 667938
${c}
Packit 667938
##---------------------------------------------------------------
Packit 667938
ECHO
Packit 667938
Packit 667938
}
Packit 667938
Packit 667938
##############################
Packit 667938
# Any errors?
Packit 667938
##############################
Packit 667938
my $error = $snmp->error;
Packit 667938
if ($error){
Packit 667938
   print "error = $error\n";
Packit 667938
}
Packit 667938
Packit 667938
sub fmi {
Packit 667938
  my($number) = $_[0];
Packit 667938
  my(@short);
Packit 667938
  $number=$number;
Packit 667938
  @short = ("b/s","kb/s","Mb/s","Gb/s");
Packit 667938
  my $digits=length("".$number);
Packit 667938
  my $divm=0;
Packit 667938
  while ($digits-$divm*3 > 4) { $divm++; }
Packit 667938
  my $divnum = $number/10**($divm*3);
Packit 667938
  return sprintf("%1.1f %s",$divnum,$short[$divm]);
Packit 667938
}