cfgmakerov
上传用户:shbosideng
上传日期:2013-05-04
资源大小:1555k
文件大小:15k
源码类别:

SNMP编程

开发平台:

C/C++

  1. #!/usr/local/bin/perl5
  2. # -*- mode: Perl -*-
  3. ##################################################################
  4. # Config file creator
  5. ##################################################################
  6. # Created by Laurie Gellatly <gellatly@one.net.au>
  7. # based on mrtg cfgmaker by Tobias Oetiker <oetiker@ee.ethz.ch>
  8. # this produces a config file for one router, by pulling info
  9. # off the router via snmp and then writing files ready for
  10. # snmpCollect to use (via xnmcollect) and later ovlg calls
  11. #################################################################
  12. #
  13. # Distributed under the GNU copyleft
  14. #
  15. # $Id: cfgmakerov,v 1.1.1.1 2002/02/26 10:16:36 oetiker Exp $
  16. #
  17. use SNMP_Session "0.58";
  18. use BER "0.57";
  19. use Socket;
  20. use strict;
  21. use vars '$DEBUG';
  22. my $DEBUG = 0;
  23. my $webdir = "/var/opt/ns-fasttrack/docs/mrtg/";
  24. %snmpget::OIDS = (  'sysDescr' => '1.3.6.1.2.1.1.1.0',
  25.     'sysContact' => '1.3.6.1.2.1.1.4.0',
  26.     'sysName' => '1.3.6.1.2.1.1.5.0',
  27.     'sysLocation' => '1.3.6.1.2.1.1.6.0',
  28.     'sysUptime' => '1.3.6.1.2.1.1.3.0',
  29.     'ifNumber' =>  '1.3.6.1.2.1.2.1.0',
  30.     ###################################
  31.     # add the ifNumber ....
  32.     'ifDescr' => '1.3.6.1.2.1.2.2.1.2',
  33.     'ifType' => '1.3.6.1.2.1.2.2.1.3',
  34.     'ifIndex' => '1.3.6.1.2.1.2.2.1.1',
  35.     'ifSpeed' => '1.3.6.1.2.1.2.2.1.5', 
  36.     'ifOperStatus' => '1.3.6.1.2.1.2.2.1.8',             
  37.     'ifAdminStatus' => '1.3.6.1.2.1.2.2.1.7',            
  38.     # up 1, down 2, testing 3
  39.     'ipAdEntAddr' => '1.3.6.1.2.1.4.20.1.1',
  40.     'ipAdEntIfIndex' => '1.3.6.1.2.1.4.20.1.2',
  41.     'sysObjectID' => '1.3.6.1.2.1.1.2.0',
  42.     'CiscolocIfDescr' => '1.3.6.1.4.1.9.2.2.1.1.28',
  43.     'ifAlias' => '1.3.6.1.2.1.31.1.1.1.18',
  44.     'ifName' => '1.3.6.1.2.1.31.1.1.1.1'
  45.  );
  46. sub main {
  47.   my(%ifType_d)=('1'=>'Other',
  48.  '2'=>'regular1822',
  49.  '3'=>'hdh1822',
  50.  '4'=>'ddnX25',
  51.  '5'=>'rfc877x25',
  52.  '6'=>'ethernetCsmacd',
  53.  '7'=>'iso88023Csmacd',
  54.  '8'=>'iso88024TokenBus',
  55.  '9'=>'iso88025TokenRing',
  56.  '10'=>'iso88026Man',
  57.  '11'=>'starLan',
  58.  '12'=>'proteon10Mbit',
  59.  '13'=>'proteon80Mbit',
  60.  '14'=>'hyperchannel',
  61.  '15'=>'fddi',
  62.  '16'=>'lapb',
  63.  '17'=>'sdlc',
  64.  '18'=>'ds1',
  65.  '19'=>'e1',
  66.  '20'=>'basicISDN',
  67.  '21'=>'primaryISDN',
  68.  '22'=>'propPointToPointSerial',
  69.  '23'=>'ppp',
  70.  '24'=>'softwareLoopback',
  71.  '25'=>'eon',
  72.  '26'=>'ethernet-3Mbit',
  73.  '27'=>'nsip',
  74.  '28'=>'slip',
  75.  '29'=>'ultra',
  76.  '30'=>'ds3',
  77.  '31'=>'sip',
  78.  '32'=>'frame-relay',
  79.  '33'=>'rs232',
  80.  '34'=>'para',
  81.  '35'=>'arcnet',
  82.  '36'=>'arcnetPlus',
  83.  '37'=>'atm',
  84.  '38'=>'miox25',
  85.  '39'=>'sonet',
  86.  '40'=>'x25ple',
  87.  '41'=>'iso88022llc',
  88.  '42'=>'localTalk',
  89.  '43'=>'smdsDxi',
  90.  '44'=>'frameRelayService',
  91.  '45'=>'v35',
  92.  '46'=>'hssi',
  93.  '47'=>'hippi',
  94.  '48'=>'modem',
  95.  '49'=>'aal5',
  96.  '50'=>'sonetPath',
  97.  '51'=>'sonetVT',
  98.  '52'=>'smdsIcip',
  99.  '53'=>'propVirtual',
  100.  '54'=>'propMultiplexor',
  101.  '55'=>'100BaseVG'
  102.  );
  103.   my($vendor)=0;
  104.   my($router) = $ARGV[0];
  105.   die <<USAGE  unless $router;
  106. USAGE: cfgmakerov  'router'
  107. EXAMPLE:  cfgmakerov ezwf7.ethz.ch >>mrtg.cfg
  108. USAGE
  109.   
  110.   
  111.   my($sysDescr,$sysContact,$sysName,$sysLocation,$ifNumber,$sysObjectID) =
  112.     snmpget($router,
  113.     'sysDescr','sysContact','sysName',  'sysLocation', 'ifNumber', 'sysObjectID');
  114.  ### The next three lines can be deleted once Someone gives this box a name  
  115.    if ($router eq '10.26.254.1') {
  116.       $sysName = 'nrgiga';
  117.    }
  118.    if ($sysName eq "") {
  119.       $sysName = $router;
  120.    }
  121.    my($sysNameL) = $sysName;
  122.    $sysNameL =~ tr/[A-Z]/[a-z]/;
  123.    $sysDescr =~ s/r/<BR>/g;  # Change returns to <BR>
  124.    my($ciscobox) = ($sysObjectID =~ /cisco/);
  125.    my($portmaster) = ($sysObjectID =~ /livingston/);
  126.    $vendor = ($ciscobox || $portmaster) ;
  127.    my($ifInErrors) = '1.3.6.1.2.1.2.2.1.14';
  128.    my($ifOutErrors) = '1.3.6.1.2.1.2.2.1.20';
  129. ### Create the directory if it does not exist
  130.    mkdir("$webdir$sysNameL",0777);
  131.    
  132. print <<ECHO;
  133. WorkDir: $webdir$sysNameL
  134. IconDir: ../
  135. ######################################################################
  136. # Description: $sysDescr
  137. #     Contact: $sysContact
  138. # System Name: $sysName
  139. #    Location: $sysLocation
  140. #.....................................................................
  141. ECHO
  142. if ( $ciscobox ) {
  143.    print <<ECHO;
  144. # Router CPU load %
  145. Target[$sysNameL.cpu]: `perl ovcvtfile $router 0 2` 
  146. MaxBytes[$sysNameL.cpu]: 100
  147. Title[$sysNameL.cpu]: CPU LOAD
  148. PageTop[$sysNameL.cpu]: <H1>CPU Load %</H1>
  149. ShortLegend[$sysNameL.cpu]: %
  150. XSize[$sysNameL.cpu]: 380
  151. YSize[$sysNameL.cpu]: 100
  152. YLegend[$sysNameL.cpu]: CPU Utilization
  153. Legend1[$sysNameL.cpu]: CPU Utilization in % (Load)
  154. Legend2[$sysNameL.cpu]: CPU Utilization in % (Load)
  155. Legend3[$sysNameL.cpu]:
  156. Legend4[$sysNameL.cpu]:
  157. LegendI[$sysNameL.cpu]:
  158. LegendO[$sysNameL.cpu]: &nbsp;Usage:
  159. Options[$sysNameL.cpu]: growright, gauge
  160. #---------------------------------------------------------------
  161. ECHO
  162. }
  163.   my @ipadent = snmpgettable($router, 'ipAdEntAddr');
  164.   print STDERR "Got Addressesn" if $DEBUG;
  165.   my @ipadentif = snmpgettable($router, 'ipAdEntIfIndex');
  166.   print STDERR "Got IfTablen" if $DEBUG;
  167.   my(%ipaddr, %iphost,$index);
  168.   while (scalar @ipadentif){
  169.     $index = shift @ipadentif;
  170.     $ipaddr{$index} = shift @ipadent;
  171.     $iphost{$index} = 
  172.       gethostbyaddr(pack('C4',split(/./,$ipaddr{$index})), AF_INET);
  173.     if ($iphost{$index} eq ''){
  174.  $iphost{$index} = $ipaddr{$index};
  175.     }   
  176.     
  177.   }
  178.   my(@ifdescr) = snmpgettable($router, 'ifDescr');
  179.   print STDERR "Got IfDescrn" if $DEBUG;
  180.   my(@iftype) = snmpgettable($router, 'ifType');
  181.   print STDERR "Got IfTypen" if $DEBUG;
  182.   my(@ifspeed) = snmpgettable($router, 'ifSpeed');
  183.   print STDERR "Got IfSpeedn" if $DEBUG;
  184.   my(@ifadminstatus) = snmpgettable($router, 'ifAdminStatus');
  185.   print STDERR "Got IfStatusn" if $DEBUG;
  186.   my(@ifoperstatus) = snmpgettable($router, 'ifOperStatus');
  187.   print STDERR "Got IfOperStatusn" if $DEBUG;
  188.   my(@ifindex) = snmpgettable($router, 'ifIndex');
  189.   print STDERR "Got IfIndexn" if $DEBUG;
  190.   my(@ifname) = snmpgettable($router, 'ifName');
  191.   print STDERR "Got IfNamen" if $DEBUG;
  192.   my(%sifdesc,%siftype,%sifspeed,%sifadminstatus,%sifoperstatus,%sciscodescr,%sifname);
  193.   ### May need the cisco IOS version number so we know which oid to use
  194.   ###   to get the cisco description.
  195.   ###
  196.   ### - mjd 2/5/98 (Mike Diehn) (mdiehn@mindspring.net)
  197.   ###
  198.   my ($cisco_ver, $cisco_descr_oid, @ciscodescr);
  199.   if ( $ciscobox ) {
  200.     ($cisco_ver) = ($sysDescr =~ /Versions+([d.]+)/);
  201.     $cisco_descr_oid = ($cisco_ver ge "11.3") ? "ifAlias" : "CiscolocIfDescr";
  202.   }
  203.   
  204.   while (scalar @ifindex) {
  205.   # as these arrays get filled from the bottom, 
  206.   # we need to empty them from the botom as well ...
  207.   # fifo
  208.     $index = shift @ifindex;
  209.     $sifdesc{$index} = shift @ifdescr;
  210.     $sifname{$index} = shift @ifname;
  211.     $siftype{$index} = shift @iftype;
  212.     $sifspeed{$index} = shift @ifspeed;
  213.     $sifadminstatus{$index} = shift @ifadminstatus;
  214.     $sifoperstatus{$index} = shift @ifoperstatus;
  215.     if ($portmaster) {
  216.       # Trying to extract extra info livingston
  217.       # We can only approximate speeds
  218.       # 
  219.       # so we think that ppp can do 76800 bit/s, and slip 38400.
  220.       # (actualy, slip is a bit faster, but usualy users with newer modems
  221.       # use ppp). Alternatively, you can set async speed to 115200 or
  222.       # 230400 (the maximum speed supported by portmaster).
  223.       # 
  224.       # But if the interface is ptpW (sync), max speed is 128000
  225.       # change it to your needs. On various Portmasters there are
  226.       # various numbers of sync interfaces, so modify it.
  227.       # 
  228.       #  The most commonly used PM-2ER has only one sync.
  229.       # 
  230.       #  Paul Makeev (mac@redline.ru)
  231.       # 
  232.       if ($siftype{$index} eq 'ppp') {
  233.       if ($sifdesc{$index} eq 'ptpW1') {
  234.       $sifspeed{$index} = 128000;
  235.       } else {
  236.       $sifspeed{$index} = 76800;
  237.       }
  238.       } elsif ($siftype{$index} eq 'slip') {
  239.       $sifspeed{$index} = 38400;
  240.       } elsif ($siftype{$index} eq 'ethernetCsmacd') {
  241.       $sifspeed{$index} = 10000000;
  242.       }
  243.     }
  244.     ### Move this section south so we know what type of
  245.     ###  circuit we're looking at before we retrieve
  246.     ###  the cisco interface alias.
  247.     ###
  248.     ### This whole cicso thing should be re-written, but since
  249.     ###   this script doesn't need to run quickly...
  250.     ###
  251.     ###  - mjd 2/5/98
  252.     ###
  253.     # Get the user configurable interface description entered in the config 
  254.     # if it's a cisco-box
  255.     #
  256.     if ($ciscobox) {
  257. my ($enoid, @descr);
  258. $enoid = $snmpget::OIDS{"$cisco_descr_oid"} . "." . $index;
  259. if ( $cisco_ver ge "11.2" or $siftype{$index} ne 'frame-relay' ) {
  260.   ### This is either not a frame-relay sub-interface or
  261.   ###  this router is running IOS 11.2+ and interface
  262.   ###  type won't matter. In either of these cases, it's
  263.   ###  ok to try getting the ifAlias or ciscoLocIfDesc.
  264.   ###
  265.   @descr = snmpget($router, $enoid);
  266. } else {
  267.   ### This is a frame-relay sub-interface *and* the router
  268.   ###  is running an IOS older than 11.2. Therefore, we can
  269.   ###  get neither ifAlias nor ciscoLocIfDesc. Do something
  270.   ###  useful.
  271.   ###
  272.   @descr = ("Cisco PVCs descriptions require IOS 11.2+.");
  273. } # end if else
  274. ### Put whatever I got into the array we'll use later to append the result
  275. ###   of this operation onto the results from the ifDescr fetch.
  276. ###
  277. push @ciscodescr, shift @descr;
  278.     } # end if ($ciscobox)
  279.     # especially since cisco does not return a if
  280.     # descr for each interface it has ...
  281.     ## JB 2/8/97 - sometimes IOS inserts E1 controllers in the standard-MIB
  282.     ## interface table, but not in the local interface table. This puts the
  283.     ## local interface description table out-of-sync. the following 
  284.     ## modification skips over E1 cards as interfaces.
  285.     #
  286.     ### I suspect that the mod I made above, to use the ifAlias
  287.     ###   oid if possible, may cause problems here. If it seems
  288.     ###   that your descriptions are out of sync, try commenting
  289.     ###   out the "if ( condition )" and it's closing right brace
  290.     ###   so that the "shift @ciscodescr" get executed for *all*
  291.     ###   iterations of this loop.
  292.     ###
  293.     ### - mjd 2/5/95
  294.     ###
  295.     if ($ciscobox && $siftype{$index} ne 'ds1') {
  296.   $sciscodescr{$index} = "<BR>" . (shift @ciscodescr) if @ciscodescr;
  297.     }
  298. }
  299.   foreach $index ( sort { $a <=> $b } keys %sifdesc) {
  300.     my $c;
  301.     my $speed = int($sifspeed{$index} / 8); # bits to byte
  302. # LJG Change to report in Bits
  303. #    my $speed_str=&fmi($speed);
  304.     my $speed_str=&fmi($sifspeed{$index});
  305.     my $name="$sysNameL.$index";
  306.     my $namerr="$sysNameL.$index.err";
  307.   if (($sifadminstatus{$index} ne "up")
  308. # this check added by Josh - don't query E1-stack controllers
  309.       || ($siftype{$index} eq 'ds1')
  310. || ($siftype{$index} eq 'softwareLoopback')
  311. || ($speed == 0 ) 
  312. # LJG Change to report all in Bits
  313. # || ($speed > 400 * 10**6)  #speeds of 400 MByte/s are not realistic
  314. || ($speed > 3200 * 10**6)  #speeds of 400 MByte/s are not realistic
  315. || ($sifoperstatus{$index} eq 'down')
  316. || (($sifdesc{$index}=~ /Dialer/) && ($ciscobox))) {
  317.     print <<ECHO;
  318. ########
  319. ######## This Interface is one of the following
  320. ######## - administratively not UP
  321. ######## - it is in test mode
  322. ######## - it is a softwareLoopback interface
  323. ######## - has a unrealistic speed setting
  324. ######## It is commented out for this reason.
  325. ########
  326. ECHO
  327.   $c="# ";
  328.   }else {
  329.     $c = '';
  330.   }
  331.     
  332.   print <<ECHO;
  333. ${c}
  334. ${c}Target[$name]: `perl ovcvtfile $router $index 1`
  335. ${c}MaxBytes[$name]: $speed
  336. ${c}Title[$name]: $sysName ($iphost{$index}): $sifdesc{$index} $sifname{$index}
  337. ${c}Options[$name]: growright, gauge, bits
  338. ${c}PageTop[$name]: <H1>Traffic Analysis for $sifdesc{$index}
  339. ${c} $sciscodescr{$index}</H1>
  340. ${c} <TABLE>
  341. ${c}   <TR><TD>System:</TD><TD>$sysName in $sysLocation</TD></TR>
  342. ${c}   <TR><TD>Maintainer:</TD><TD>$sysContact</TD></TR>
  343. ${c}   <TR><TD>Interface:</TD><TD>$sifdesc{$index} ($index - $sifname{$index})</TD></TR>
  344. ${c}   <TR><TD>IP:</TD><TD>$iphost{$index} ($ipaddr{$index})</TD></TR>
  345. ${c}   <TR><TD>Max Speed:</TD>
  346. ${c}       <TD>$speed_str ($siftype{$index})</TD></TR>
  347. ${c}  </TABLE>
  348. ${c}
  349. #---------------------------------------------------------------
  350. # In and Out Error Counts
  351. ${c}
  352. ${c}Target[$namerr]: `perl ovcvtfile $router $index 0`
  353. ${c}MaxBytes[$namerr]: 2147483600
  354. ${c}Title[$namerr]: $sysName ($iphost{$index}): $sifdesc{$index} $sifname{$index} I/O Errors
  355. ${c}Options[$namerr]: growright, gauge
  356. ${c}PageTop[$namerr]: <H1>I/O Error Analysis for $sifdesc{$index}
  357. ${c} $sciscodescr{$index}</H1>
  358. ${c}YLegend[$namerr]: Errors/Period
  359. ${c}Legend1[$namerr]: Inward Errors
  360. ${c}Legend2[$namerr]: Outward Errors
  361. ${c}Legend3[$namerr]: 
  362. ${c}Legend4[$namerr]: 
  363. ${c}LegendI[$namerr]: &nbsp;In :
  364. ${c}LegendO[$namerr]: &nbsp;Out :
  365. ${c}
  366. #---------------------------------------------------------------
  367. ECHO
  368.   }
  369. }
  370.   
  371. main;
  372. exit(0);
  373. sub snmpget{  
  374.   my($host,@vars) = @_;
  375.   my(@enoid, $var,$response, $bindings, $binding, $value, $inoid,$outoid,
  376.      $upoid,$oid,@retvals,$t1,$t2,@resp,$tempo);
  377.   my($hackcisco);
  378.   foreach $var (@vars) {
  379.     die "Unknown SNMP var $varn" 
  380.       unless $snmpget::OIDS{$var} || $var =~ /^d+[.d+]*.d+$/;
  381.     if ($var =~ /^d+[.d+]*.d+/) {
  382. $value = $value.$var." ";
  383. $hackcisco = 1;
  384.     } else {
  385. $value = $value.$snmpget::OIDS{$var}." ";
  386. $hackcisco = 0;
  387.     }
  388.   }
  389.   open(SNMPG, "snmpget $host $value |");
  390. #  print STDERR $value;
  391.   while ($tempo =<SNMPG>){
  392. #      print STDERR $tempo;
  393.       ($t1, $t2, $tempo ) = split /:/ , $tempo, 3;
  394.       if (!$tempo){
  395.  if ($t2){
  396.     $t1 = $t1 .":".$t2;
  397.  }
  398.  $tempo = pop(@retvals)." ".$t1 ;
  399.       }
  400.       $tempo=~s/t/ /g;
  401.       $tempo=~s/n/ /g;
  402.       $tempo=~s/^s+//;
  403.       $tempo=~s/s+$//;
  404.       push @retvals,  $tempo;
  405.   }
  406. #   print STDERR @retvals; 
  407.   close (SNMPG);    
  408.     return (@retvals);
  409. }
  410. sub snmpgettable{
  411.   my($host,$var) = @_;
  412.   my($next_oid,$enoid,$orig_oid, 
  413.      $response, $bindings, $binding, $t1, $t2,$outoid,
  414.      $upoid,$oid,@table,$tempo);
  415.   die "Unknown SNMP var $varn" 
  416.     unless $snmpget::OIDS{$var};
  417.   
  418.   $orig_oid = $snmpget::OIDS{$var}." ";
  419.   $enoid=$orig_oid;
  420.   open(SNMPG, "snmpwalk $host $orig_oid |");
  421. #  print STDERR $orig_oid;
  422.   while ($tempo =<SNMPG>){
  423. #      print STDERR $tempo;
  424.       ($t1, $t2, $tempo ) = split /:/ , $tempo ,3;
  425.       if (!$tempo){
  426.  if ($t2){
  427.     $t1 = $t1 .":".$t2;
  428.  }
  429.  $tempo = pop(@table)." ".$t1 ;
  430.       }
  431.       $tempo=~s/t/ /g;
  432.       $tempo=~s/n/ /g;
  433.       $tempo=~s/^s+//;
  434.       $tempo=~s/s+$//;
  435.       push @table, $tempo;
  436.      
  437.   }
  438.  close (SNMPG);    
  439.   return (@table);
  440. }
  441. sub fmi {
  442.   my($number) = $_[0];
  443.   my(@short);
  444. # LJG Change to report in Bits
  445. #  @short = ("Bytes/s","kBytes/s","MBytes/s","GBytes/s");
  446.   @short = ("Bits/s","kBits/s","MBits/s","GBits/s");
  447.   my $digits=length("".$number);
  448.   my $divm=0;
  449.   while ($digits-$divm*3 > 3) { $divm++; }
  450.   my $divnum = $number/10**($divm*3);
  451.   return sprintf("%1.1f %s",$divnum,$short[$divm]);
  452. }