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

SNMP编程

开发平台:

C/C++

  1. #! /usr/bin/perl
  2. # -*- mode: Perl -*-
  3. ##################################################################
  4. # MRTG 2.13.2  -- Config file creator
  5. ##################################################################
  6. # Created by Tobias Oetiker <oetiker@ee.ethz.ch>
  7. # this produces an mrtg config file for one router or more routers
  8. # by pulling info off the router via snmp
  9. ##################################################################
  10. # Distributed under the GNU copyleft
  11. # Copyright 2000 by Tobias Oetiker
  12. ##################################################################
  13. # DEBUG TARGETS
  14. # base - basic program flow
  15. # snpo - SNMP Polling
  16. # snpd - SNMP Detail
  17. #@main::DEBUG=qw(base snpo coca);
  18. @main::DEBUG=qw();
  19. require 5.005;
  20. use strict;
  21. BEGIN {
  22.     # Automatic OS detection ... do NOT touch
  23.     if ( $^O =~ /^(?:(ms)?(dos|win(32|nt)?))/i ) {
  24.         $main::OS = 'NT';
  25.         $main::SL = '\';
  26.         $main::PS = ';';
  27.     } elsif ( $^O =~ /^NetWare$/i ) {
  28.         $main::OS = 'NW';
  29.         $main::SL = '/';
  30.         $main::PS = ';';
  31.     } elsif ( $^O =~ /^VMS$/i ) {
  32.         $main::OS = 'VMS';
  33.         $main::SL = '.';
  34.         $main::PS = ':';
  35.     } else {
  36.         $main::OS = 'UNIX';
  37.         $main::SL = '/';
  38.         $main::PS = ':';
  39.     }
  40. }
  41. use FindBin;
  42. use lib "${FindBin::Bin}";
  43. use lib "${FindBin::Bin}${main::SL}..${main::SL}lib${main::SL}mrtg2";
  44. use MRTG_lib "2.100015";
  45. use Getopt::Long;
  46. use Pod::Usage;
  47. use Socket;
  48. sub main {
  49.     my %opt;
  50.     my %routers;
  51.     my %confcache;
  52.     my $ipv4only;
  53.     my %v3opt;
  54.     $opt{fullcmd} =
  55.             "$0 ".(join " ",
  56.             map {$_ =~ /[ []*{};><&]/ ? "'$_'" : $_ } @ARGV);
  57.     $opt{community}="public";
  58.     $opt{interfaces}=1;
  59.     options(%opt,%routers);
  60.     # Check for IPv6 libraries if IPv6 is enabled.
  61.     # If the check fails, IPv6 is disabled.
  62.     $ipv4only = 1;
  63.     if ($opt{'enable-ipv6'} == 1) {
  64.         if ((eval {require Socket6;}) && (eval {require IO::Socket::INET6;})) {
  65.             debug ('base',"IPv6 libraries found, IPv6 enabled.");
  66.             $ipv4only = 0;
  67.         } else {
  68.             warn "WARNING: IPv6 libraries not found, IPv6 disabled.n";
  69.             $opt{'enable-ipv6'} = 0;
  70.         }
  71.     }
  72.     if ($opt{'use-16bit'} == 1) {
  73.         warn "WARNING: Using 16bit RequestIDsn";
  74.         $SNMP_Session::default_use_16bit_request_ids=1;
  75.     }
  76.     # Check for SNMP V3
  77.     #
  78.      if (exists($opt{username}) or lc($opt{enablesnmpv3}) eq "yes") {
  79.        if  (eval {require Net_SNMP_util;})   {
  80.             import Net_SNMP_util;
  81.             debug('base', "SNMP V3 libraries found, SNMP V3 enabled.");
  82.     $opt{enablesnmpv3} = "yes";
  83.        push @{$opt{global}}, "enablesnmpv3: yes";
  84.         } else {
  85.             warn "WARNING: SNMP V3 libraries not found, SNMP V3 disabled. Falling back to V2c.n";
  86.             require SNMP_util;
  87.             import SNMP_util;
  88.     $opt{enablesnmpv3} = "revert";
  89.         }
  90. }
  91. else {
  92.         require SNMP_util;
  93.         import SNMP_util;
  94. $opt{enablesnmpv3} = "no";
  95. }
  96.     init();
  97.     foreach my $router
  98.     (sort
  99.      {($routers{$a}{noofrouter}) <=> ($routers{$b}{noofrouter})}
  100.      keys %routers)
  101.     {
  102. my @snmpopt = split(/:/,$routers{$router}{'snmp-options'});
  103. if ($snmpopt[5] == 3) {
  104. if ($opt{enablesnmpv3} eq "revert") {
  105. $snmpopt[5] = 2;
  106. warn "reverting to snmpV2c for router $routern";
  107. $routers{$router}{'snmp-options'} = join(":",@snmpopt);
  108. } else {
  109. die "SNMP V3 requires a --username parameter as part of the User Security Model for router $routers{$router}{routerkey}" if $opt{enablesnmpv3} eq "no";
  110. %v3opt = parsev3(%opt);
  111. }
  112. } else {
  113. debug('base',"snmpv3 available, but using v1/v2c for $routers{$router}{routerkey}") if $opt{enablesnmpv3} eq "yes";
  114. }
  115.         # pod2usage(-verbose=>1,-message=>"ERROR: Could not Parse $routern")
  116.         #     unless  $router =~ /.*@.*/;
  117.         debug('base',"Get Device Info on $router");
  118.         $routers{$router}{ipv4only} = $ipv4only;
  119.         if ( my $devinfo = DeviceInfo($router,%routers,%v3opt) ) {
  120.             $routers{$router}{deviceinfo} = $devinfo;
  121.         } else {
  122.             warn "WARNING: Skipping $router as no info could be retrievednn";
  123.             sleep 5;
  124.             next;
  125.         }
  126.         if ($opt{interfaces}) {
  127.             debug('base',"Populating confcache");
  128.             populateconfcache(%confcache,$router,$routers{$router}{ipv4only},1,%v3opt);
  129.             debug('base',"Get Interface Info");
  130.             InterfaceInfo(%confcache,%routers,$router,%opt,%v3opt);
  131.         }
  132.     }
  133.     GenConf(%opt,%routers,%confcache,%v3opt);
  134. } # end main
  135. main;
  136. exit 0;
  137. sub InterfaceInfo($$$$$) {
  138.     my $confcache = shift;
  139.     my $routers = shift;
  140.     my $router = shift;
  141.     my $opt = shift;
  142.     my $v3opt = shift;
  143.     my @Variables = qw (ifIndex ifType ifAdminStatus ifOperStatus ifMtu);
  144.     my $snmphost = v4onlyifnecessary($router, $routers->{$router}{ipv4only});
  145.     if ($routers->{$router}{deviceinfo}{Vendor} eq 'cisco' &&
  146.         $routers->{$router}{deviceinfo}{sysDescr} =~ m/Versions+(dd.d+)/) {
  147.         push @Variables,  ($1 > 11.0) ? "ifAlias" : "CiscolocIfDescr";
  148.         if ($1 > 11.2) {push @Variables, "vmVlan";};
  149.     } elsif ( $routers->{$router}{deviceinfo}{Vendor} =~ /(?:hp|juniper|foundry|dellLan|force10|extremenetworks)/) {
  150.         push @Variables, "ifAlias";
  151.     }
  152.     my $descr = $routers->{$router}{deviceinfo}{sysDescr};
  153.     if ($routers->{$router}{deviceinfo}{Vendor} eq 'cisco' &&
  154.         $descr =~ m/CatalystsOperatingsSystem|CiscosSystemssWS-C2900/ ) {
  155.         push @Variables,  "CiscoCatalystPortName";
  156.         push @Variables, "vmVlan";
  157.     }
  158.     foreach my $var (@Variables) {
  159.         debug('base',"Walking $var");
  160.         foreach my $tuple (snmpwalk($snmphost,$v3opt, $var)){
  161.             my($if,$value) = split /:/, $tuple, 2;
  162.             $value =~ s/[- ]+$//; # no trailing space
  163.             $routers->{$router}{$if}{$var} = $value;
  164.     debug('snpd',"  $router -> $if -> $var = $value");
  165.         }
  166.     }
  167.     # interface speed var depends on snmp version
  168.     my $snmp_version = ($router =~ /:/)? (split(':', $router, 6))[5] : 1;
  169.     if ( $snmp_version =~ /[23]/ ) {
  170.         debug('base',"Walking ifSpeed");
  171.         my @ifSpeed = snmpwalk($snmphost, $v3opt,'ifSpeed');
  172. debug('snpd',"@ifSpeed = @ifSpeedn");
  173.         debug('base',"Walking ifHighSpeed");
  174.         my @ifHighSpeed = snmpwalk($snmphost,$v3opt, 'ifHighSpeed');
  175.         for ( my $i=0; $i<=$#ifHighSpeed; $i++ ) {
  176.             my ($if,$value) = split /:/, $ifSpeed[$i], 2;
  177. # the mib entry on ifSpeed says
  178. #            "An estimate of the interface's current bandwidth in bits
  179. #            per second.  For interfaces which do not vary in bandwidth
  180. #            or for those where no accurate estimation can be made, this
  181. #            object should contain the nominal bandwidth.  If the
  182. #            bandwidth of the interface is greater than the maximum value
  183. #            reportable by this object then this object should report its
  184. #            maximum value (4,294,967,295) and ifHighSpeed must be used
  185. #            to report the interace's speed.  For a sub-layer which has
  186. #            no concept of bandwidth, this object should be zero."
  187.             if ( (not defined $value) || ($value == 2**32-1)) {
  188.                 ($if, $value) = split /:/, $ifHighSpeed[$i], 2;
  189.         $value = $value * 1000000;  # highSpeed = contador * 10^6
  190.                 #debug('base',"Speed: $if - $value");
  191.             }
  192.             $routers->{$router}{$if}{'ifSpeed'} = $value;
  193.         }
  194.     } else {
  195.         debug('base',"Walking ifSpeed");
  196.         foreach my $tuple (snmpwalk($snmphost,$v3opt, 'ifSpeed')){
  197.             my($if,$value) = split /:/, $tuple, 2;
  198.             $routers->{$router}{$if}{'ifSpeed'} = $value;
  199.     debug('snpd',"  $router -> $if -> ifSpeed = $value");
  200.         }
  201.     }
  202.     # magic speed determination for portmaster IFs
  203.     if ($routers->{$router}{deviceinfo}{Vendor} eq 'portmaster') {
  204.         # We can only approximate speeds
  205.         #
  206.         # so we think that ppp can do 76800 bit/s, and slip 38400.
  207.         # (actualy, slip is a bit faster, but usualy users with newer modems
  208.         # use ppp). Alternatively, you can set async speed to 115200 or
  209.         # 230400 (the maximum speed supported by portmaster).
  210.         #
  211.         # But if the interface is ptpW (sync), max speed is 128000
  212.         # change it to your needs. On various Portmasters there are
  213.         # various numbers of sync interfaces, so modify it.
  214.         #
  215.         #  The most commonly used PM-2ER has only one sync.
  216.         #
  217.         #  Paul Makeev (mac@redline.ru)
  218.         #
  219.         foreach my $if (keys %{$routers->{$router}}) {
  220.             next unless $if =~ /^d+$/;
  221.             my $ift = $routers->{$router}{$if}{ifType};
  222.             my $ifd = $routers->{$router}{$if}{Descr};
  223.             if ($ift ==  23) {
  224.                 if ($ifd eq 'ptpW1') {
  225.                     $routers->{$router}{$if}{ifSpeed} = 128000;
  226.                 } else {
  227.                     $routers->{$router}{$if}{ifSpeed} = 76800;
  228.                 }
  229.             } elsif ($ift == 28) {
  230.                 $routers->{$router}{$if}{ifSpeed} = 38400;
  231.             } elsif ($ift == 6) {
  232.                 $routers->{$router}{$if}{ifSpeed} = 10000000;
  233.             }
  234.         }
  235.     }
  236.     # match confcache info into tree
  237.     my $cachekey = cleanhostkey $router;
  238.     foreach my $method (keys %{$$confcache{$cachekey}}) {
  239.         foreach my $key (keys %{$$confcache{$cachekey}{$method}}) {
  240.             my $if = readfromcache($confcache,$router,$method,$key);
  241.             next unless $if =~ /^d+$/;
  242.             $routers->{$router}{$if}{$method} = $key;
  243.             for ($method) {
  244.                 #fix special chars in ifdescr
  245.                 # no need for this we fix if references later on
  246.                 # /^Descr|Name$/ && do {
  247.                 #     $routers->{$router}{$if}{"R$method"} = $routers->{$router}{$if}{$method};
  248.                 #     $routers->{$router}{$if}{$method} =~ s/(:)/\$1/g;
  249.                 #     next;
  250.                 # };
  251.                 #find hostname of IF
  252.                 !$$opt{noreversedns} && /^Ip$/ and do {
  253.                     my $name =
  254.                     gethostbyaddr(
  255.                     pack('C4',
  256.                          split(/./,
  257.                          $routers->{$router}{$if}{$method})),
  258.                          AF_INET);
  259.                     $routers->{$router}{$if}{DNSName} = ($name or "");
  260.                     next;
  261.                 };
  262.             }
  263.         }
  264.     }
  265. } # end InterfaceInfo
  266. sub GenConf ($$$$) {
  267.     my $opt = shift;
  268.     my $routers = shift;
  269.     my $confcache = shift;
  270.     my $v3opt = shift;
  271.     my $conf = "# Created by n# $$opt{fullcmd}nn";
  272.     # print global options
  273.     $conf .= <<ECHO;
  274. ### Global Config Options
  275. #  for UNIX
  276. # WorkDir: /home/http/mrtg
  277. #  or for NT
  278. # WorkDir: c:\mrtgdata
  279. ### Global Defaults
  280. #  to get bits instead of bytes and graphs growing to the right
  281. # Options[_]: growright, bits
  282. ECHO
  283.     # Add EnableIPv6 option to configuration file
  284.     if ($$opt{'enable-ipv6'} == 1) {
  285.         $conf .= "EnableIPv6: yesn";
  286.     } else {
  287.         $conf .= "EnableIPv6: non";
  288.     }
  289.     foreach my $router
  290.       (sort {($$routers{$a}{noofrouter}) <=> ($$routers{$b}{noofrouter})}
  291.        keys %$routers ) {
  292.         my $router_ref = $$routers{$router};
  293.         my $router_opt = $$router_ref{opt};
  294.         my $router_dev = $$router_ref{deviceinfo};
  295.         # Did any global options appear on the command line
  296.         # before this router?  If so, include them into the
  297.         # configuration file.
  298.         if (defined $$router_opt{global}) {
  299.             foreach my $key (@{$$router_opt{global}}) {
  300.                 $conf .= "$keyn";
  301.             }
  302.         }
  303.         # If IPv6 is enabled, add IPv4Only target directive for targets
  304.         # that do not support SNMP over IPv6.
  305.         my $ipv4only_directive;
  306.         my $router_ipv4only = ($$opt{'enable-ipv6'} == 1) && $$router_ref{ipv4only};
  307.         my $syscontact = $$router_dev{sysContact};
  308.         my $html_syscontact = html_escape($syscontact);
  309.         my $syslocation = $$router_dev{sysLocation};
  310.         my $html_syslocation = html_escape($syslocation);
  311.         my $sysname = $$router_dev{sysName};
  312.         my $sysdescr = $$router_dev{sysDescr};
  313.         my $comment_sysdescr = $sysdescr;
  314.         # make sure embeded newlines do not harm us here
  315.         $comment_sysdescr =~ s/[nr]+/n#          /g;
  316.         my $community = $$router_ref{community};
  317.         $community  =~ s/([@ ])/\$1/g;
  318.         my $router_connect = "$community@$$router_ref{routername}$$router_ref{snmpopt_current}";
  319. my @v3options;
  320. foreach my $v3op (keys %$v3opt) {
  321. push @v3options, $v3op."=>'".$$v3opt{$v3op}."'";
  322. }
  323. my $v3options = join(",",@v3options) if $$router_ref{'snmp-options'} =~ /(?=:.*?){4}:3/ ;
  324.         my $html_sysdescr = html_escape($sysdescr);
  325.         my $router_name =
  326.             ($$router_ref{routername}
  327.              . (($$router_ref{'dns-domain'})?'.':'')
  328.              . $$router_ref{'dns-domain'});
  329.         # James Overbeck 2001/09/20
  330.         # Moved $directory_name definition from within the interface
  331.         # foreach loop to here. In its previous location, $directory_name
  332.         # was not accessible to host templates. $directory_name is not
  333.         # changed per-interface so it might as well be here instead of
  334.         # where it was.
  335.         my $directory_name = "";
  336.         if (defined $$router_opt{subdirs}) {
  337.             $directory_name = $$router_opt{subdirs};
  338.             $directory_name =~ s/HOSTNAME/$router_name/g;
  339.             $directory_name =~ s/SNMPNAME/$$router_dev{sysName}/g;
  340.         }
  341.         my $target_lines = "";
  342.         my $problem_lines = "";
  343.         my $head_lines = "
  344. ######################################################################
  345. # System: $sysname
  346. # Description: $comment_sysdescr
  347. # Contact: $syscontact
  348. # Location: $syslocation
  349. ######################################################################
  350. ";
  351.         my $separator_lines = "nn";
  352.         # Host specific config lines generation code starts HERE
  353.         if(defined $$router_opt{'host-template'}) {
  354.             # First test if the file exists and is readable, die if not.
  355.             die "File $$router_opt{'host-template'} didn't exist.n"
  356.             unless (-e $$router_opt{'host-template'}
  357.                 and -r $$router_opt{'host-template'});
  358.             # Open the file (or die).
  359.             open IF_TEMPLATE, $$router_opt{'host-template'}
  360.                 or die "File $$router_opt{'host-template'} couldn't be opened.n";
  361.             my @template_lines = readline *IF_TEMPLATE;
  362.             $@ = undef;
  363.             eval (join "", @template_lines);
  364.             die  "ERROR Evaluation of the contents in the file nn".
  365.                  "$$router_opt{'host-template'}ngave the error nn"$@"nnExiting cfgmakern" if $@;
  366.         }
  367.         $conf .= ($head_lines
  368.               . $problem_lines
  369.               . $target_lines
  370.               . $separator_lines);
  371.         # Host specific config lines generation code ends HERE
  372.         if ($$router_opt{'interfaces'}) {
  373.             foreach my $ifindex (sort {int($a) <=> int($b)} keys %$router_ref) {
  374.                 next unless $ifindex =~ /^d+$/;
  375.                 my $i = $$router_ref{$ifindex};
  376.                 # Now define a number of variables used for this interface.
  377.                 # Some variables are just used internally by cfgmaker to
  378.                 # process the interface, others are provided for usage in
  379.                 # host and interface templates and for interface filters.
  380.                 my $if_index = $ifindex;
  381.                 my $if_eth = $$i{Eth};
  382.                 # does it make sense to look at the interface ?
  383.                 my @prob;
  384.                 my $default_ifstate = 1; # State assumed up.
  385.                 my $default_iftype = 1;  # iftype assumed ok.
  386.                 my $if_ok = 1;           #
  387.                 my $if_admin = ($$i{ifAdminStatus} == 1);
  388.                 my $if_oper = ($$i{ifOperStatus} == 1);
  389.                 my $if_type = $$i{ifType};
  390.                 my $if_is_ethernet = 0 < scalar(grep {$_ == $if_type;}
  391.                                     (6,7,26,62,69,117));
  392.                 my $if_is_isdn = (0 < scalar (grep {$_ == $if_type;}
  393.                                     (20,21,63,75,76,77)));
  394.                 my $if_is_dialup = $if_is_isdn ||
  395.                                 (0 < scalar (grep {$_ == $if_type;}
  396.                                     (23,81,82,108)));
  397.                 my $if_is_atm = (0 < scalar(grep {$_ == $if_type;}
  398.                                     (37,49,107,105,106,114,134)));
  399.                 my $if_is_wan = 0 < scalar(grep {$_ == $if_type;}
  400.                                     (22,30,32,39,44,46));
  401.                 my $if_is_lan = $if_is_ethernet ||
  402.                                 (0 < scalar (grep {$_ == $if_type;}
  403.                                     (8,9,11,15,26,55,59,60,115)));
  404.                 my $if_is_dsl = (0 < scalar(grep {$_ == $if_type;}
  405.                                     (94,95,96,97)));
  406.                 my $if_is_loopback = $if_type == 24;
  407.                 my $if_is_ciscovlan =
  408.                     ($$router_dev{Vendor} eq 'cisco'
  409.                     and $$i{Descr} =~ /^V(LAN|lan)d+$/);
  410.                 my $if_ip = $$i{Ip};
  411.                 my $if_snmp_descr = $$i{Descr};
  412.                 $if_snmp_descr =~ s/n$//; # no you don't want to know who does this
  413.                                            # ok ... dell 3524
  414.                 $if_snmp_descr =~ s/ /-/g;
  415.                 my $if_type_num = $$i{ifType};
  416.                 my $if_snmp_name = $$i{Name};
  417.                 my $if_snmp_alias = $$i{ifAlias};
  418.                 my $if_cisco_descr = $$i{CiscolocIfDescr};
  419.                 my $if_dns_name = $$i{DNSName};
  420.                 my $if_vlan_id = $$i{vmVlan};
  421. my $if_MTU = $$i{ifMtu};
  422.                 # For Nokia IPSO, find non-ethernet interfaces with IP addresses
  423.                 # and add missing MAC address and Port Speed information to
  424.                 # to the LOGICAL and LOGICAL+VLAN interfaces.
  425.                 if ( $$router_dev{Vendor} eq 'nokiaipsofw' ) {
  426.                     if ($$i{ifType} ne "6" &&
  427.                         $$router_dev{sysDescr} =~ / IPSO / &&
  428.                         $$i{Ip} =~ /^d+/ &&
  429.                        ($$i{Eth} == undef ||
  430.                         $$i{ifSpeed} < 10 ||
  431.                         $$i{ifSpeed} == undef )
  432.                        ) {
  433.                         my $logical_if_name = $$i{Name};
  434.                         # Split the LOGICAL interface name in attempt
  435.                         # to match with base PHYSICAL interface detail.
  436.                         my ($logical_if_HEAD, $logical_if_TAIL) =
  437.                             $logical_if_name =~ /^(.*)(cd+)$/;
  438.                         foreach my $ifindexTMP (sort {int($a) <=> int($b)}
  439.                             keys %$router_ref) {
  440.                             next unless $ifindexTMP =~ /^d+$/;
  441.                             my $physical_if_name = $$router_ref{$ifindexTMP};
  442.                             if ($$physical_if_name{ifType} == 6 &&
  443.                                 $logical_if_HEAD eq $$physical_if_name{Name} ) {
  444.                                 $$i{Eth} = $$physical_if_name{Eth}
  445.                                 if ( $$i{Eth} == undef );
  446.                                 $$i{ifSpeed} = $$physical_if_name{ifSpeed}
  447.                                 if ( $$i{ifSpeed} == undef || $$i{ifSpeed} < 10 );
  448.                             }
  449.                         }
  450.                     }
  451.                 }
  452.                 # First investigate the state of the interface.
  453.                 if (not defined $$router_opt{'no-down'}) {
  454.                     if ($$i{ifAdminStatus} == 2) {
  455.                         push @prob, "it is administratively DOWN";
  456.                         $default_ifstate = 0;
  457.                     } elsif ($$i{ifAdminStatus} == 3) {
  458.                         push @prob, "it is in administrative TEST mode";
  459.                         $default_ifstate = 0;
  460.                     }
  461.                     if (not defined $$router_opt{'show-op-down'}) {
  462.                        if ($$i{ifOperStatus} == 2) {
  463.                           push @prob, "it is operationally DOWN";
  464.                           $default_ifstate = 0;
  465.                        } elsif ($$i{ifOperStatus} == 3) {
  466.                            push @prob, "it is in operational TEST mode";
  467.                           $default_ifstate = 0;
  468.                       }
  469.                     }
  470.                  }
  471.                 # Investigate the type of the interface.
  472.                 if ($$router_dev{Vendor} eq 'cisco' && $$i{ifType} == 18) { # by fwo@obsidian.co.za
  473.                     push @prob, "it is a DS1 controllers";
  474.                     $default_iftype = 0;
  475.                 } elsif ($$router_dev{Vendor} eq 'cisco' && $$i{ifType} == 19) { # by fwo@obsidian.co.za
  476.                     push @prob, "it is a E1 controllers";
  477.                     $default_iftype = 0;
  478.                 } elsif ($$i{ifType} == 24) {
  479.                     push @prob, "it is a Software Loopback interface" ;
  480.                     $default_iftype = 0;
  481.                 } elsif ($$router_dev{Vendor} eq 'cisco' && $$i{ifType} == 30) { # by blube@floridadigital.net
  482.                     push @prob, "it is a DS3 controller";
  483.                     $default_iftype = 0;
  484.                 } elsif ($$router_dev{Vendor} eq 'cisco' && $$i{ifType} == 102) { # by dan.mcdonald@austinenergy.com
  485.                     push @prob, "it is a Voice controller";
  486.                     $default_iftype = 0;
  487.                 } elsif ($$router_dev{Vendor} eq 'cisco' && $$i{ifType} == 103) { # by dan.mcdonald@austinenergy.com
  488.                     push @prob, "it is a Voice dial peer";
  489.                     $default_iftype = 0;
  490.                 } elsif ($$i{ifType} == 162) {
  491.                     push @prob, "it is a CEF Sub-interface"; # John Begley <maslow@mediaone.net>
  492.                 } elsif ($$router_dev{Vendor} eq 'cisco'
  493.                   and $$i{Descr} eq 'Null0') {
  494.                     push @prob, "it is a cisco Null0 interface";
  495.                     $default_iftype = 0;
  496.                 }
  497.                 my $default = $default_iftype && $default_ifstate;
  498.                 # Do some futher investigation if the interface makes
  499.                 # sense to collect on
  500. # I debated whether to insert the zero-speed check before
  501. # or after the "will always be zero" sections below.
  502. # I settled on before since I'll assume the user knows
  503. # what speed the zero-speed interfaces should be better
  504. # than the simple logic below.
  505. if ($$i{ifSpeed} == 0 && $$router_opt{'zero-speed'}) {
  506.     # Set all interfaces with zero speed to the value specified
  507.     # by the --zero-speed= command line option.
  508.     # Be sure the value specified is a valid integer.
  509.     # It seems like this could be done once when
  510.     # $$router_opt is set, but I didn't see any example
  511.     # of input validation in that part of cfgmaker,
  512.     # so it gets done here, more times than are
  513.     # really necessary.  ;-)
  514.     unless ($$router_opt{'zero-speed'} =~ /^d+$/) {
  515.          die "ERROR: zero-speed specified with non-integer speed: $$router_opt{'zero-speed'}";
  516.     }
  517.     $$i{ifSpeed} = $$router_opt{'zero-speed'};
  518. }
  519.                 if ($$i{ifSpeed} == 0 && $$router_dev{Vendor} eq 'foundry' && $$i{ifType} == 194) {
  520.                     # foundry ATM subinterfaces always report 0 speed, make it 155Mbps instead.
  521.                     $$i{ifSpeed} =  155000000;
  522.                 } elsif ($$i{ifSpeed} == 0 && $$router_dev{Vendor} eq 'foundry' && $$i{ifType} == 135) {
  523.                     # Foundry virtual Ethernet interfaces always report 0 speed, make it 100Mbps instead.
  524.                     $$i{ifSpeed} = 12500000;
  525.                 } elsif ($$i{ifSpeed} == 0 && $$router_dev{Vendor} eq 'cisco' && $$i{sysDescr} == /FWSM-Firewall / )  {
  526.                     # Cisco PIX Firewall Switch Modules have effective backplane speed of 600 Megs
  527.     $$i{ifSpeed} = 600000000;
  528.                 } elsif ($$i{ifSpeed} == 0 && $$router_dev{Vendor} eq '3com' && $$i{Descr} =~ /RMON VLAN (d+)/ ) {
  529.                     $$i{ifSpeed} = 100000000;
  530.                     $if_vlan_id = $1;
  531.                 } elsif ($$i{ifSpeed} == 0) {
  532.                     push @prob, "has a speed of $$i{ifSpeed} which makes no sense";
  533.                     $if_ok = 0;
  534.                 }
  535.                 my $message;
  536.                 if ($message = IsCounterBroken($ifindex, $router_ref,$v3opt)) {
  537.                     # set snmpopt_current to working snmp options
  538.                     push @prob, "got '$message' from interface when trying to query";
  539.                     $if_ok = 0;
  540.                 }
  541.                 my $community = $$router_ref{community};
  542.                 $community  =~ s/([@ ])/\$1/g;
  543.                 my $router_connect = "$community@$$router_ref{routername}$$router_ref{snmpopt_current}";
  544.    
  545. my $v3options = join(",",@v3options) if $$router_ref{snmpopt_current} =~ /(?=:.*?){4}:3/ ;
  546.                 # determine interface reference
  547.                 my $if_ref;
  548.                 if (defined $$router_opt{ifref}) {
  549.                     for ($$router_opt{ifref}) {
  550.                         /^ip$/ && do { $if_ref = "/".$$i{Ip} if $$i{Ip}  ; last};
  551.                         /^eth$/ && do { $if_ref = "!".$$i{Eth} if $$i{Eth}; last};
  552.                         /^descr$/ && do {$if_ref = "\".$$i{Descr} if $$i{Descr}; last};
  553.                         /^name$/ && do {$if_ref = "#".$$i{Name} if $$i{Name}; last};
  554.                         /^type$/ && do {$if_ref = "%".$$i{Type} if $$i{Type}; last};
  555.                         /^nr$/ && do {$if_ref = $ifindex; last};
  556.                         die "ERROR: Invalid value for --ifref: '$$router_opt{ifref}'n";
  557.                     }
  558.                     if (not defined $if_ref) {
  559.                         push @prob, "--ifref=$$router_opt{ifref} is not unique for this interface";
  560.                         $if_ref = $ifindex;
  561.                         $if_ok = 0;
  562.                     }
  563.                 } else {
  564.                     $if_ref = $ifindex;
  565.                 }
  566.                 # generate Target name
  567.                 my $trim_if_ref = $if_ref;
  568.                 $trim_if_ref =~ s/[#!/\:s@%]+/_/g;
  569.                 $trim_if_ref =~ s/^_*(.+?)_*$/$1/;
  570.                 my $target_name = "${router_name}_$trim_if_ref";
  571.                 my $if_title_desc = $if_ref;
  572.                 $if_title_desc =~ s/^[^d]//;
  573.                 my $if_speed = int($$i{ifSpeed} / 8);
  574.                 my $if_speed_str = fmi($if_speed,$$router_ref{flags});
  575.                 my $if_type_desc = IfType($$i{ifType});
  576.                 my $html_if_type_desc = html_escape($if_type_desc);
  577.                 my $desc_prefix = 'Traffic Analysis for ';
  578.                 my $port_dot = $$i{Name};
  579.                 $port_dot =~ s///./g;
  580.                 my $if_port_name = $$router_ref{$port_dot}{CiscoCatalystPortName};
  581.                 if (defined $$router_opt{ifdesc}) {
  582.                     $desc_prefix = '';
  583.                     for ($$router_opt{ifdesc}) {
  584.                         /^ip$/ && do { $if_title_desc = $$i{Ip} if $$i{Ip}  ; last};
  585.                         /^eth$/ && do { $if_title_desc = $$i{Eth} if $$i{Eth}; last};
  586.                         /^descr$/ && do {$if_title_desc = $$i{Descr} if $$i{Descr}; last};
  587.                         /^alias$/ && do {$if_title_desc =
  588.                                      "$$i{Descr} $$i{ifAlias} $$i{CiscolocIfDescr}";
  589.                                     last};
  590.                         /^name$/ && do {$if_title_desc = "#".$$i{Name} if $$i{Name}; last};
  591.                         /^catname$/ && do {$if_title_desc = "$if_port_name"; last};
  592.                         /^type$/ && do {$if_title_desc = "%".$$i{Type} if $$i{Type}; last};
  593.                         /^nr$/ && do {$if_title_desc = "Interface $ifindex"; last};
  594.                         /^$/ && do {$if_title_desc = $if_type_desc;
  595.                                 $if_title_desc =~ s/^$/$$i{Descr}/;
  596.                                 last};
  597.                         die "ERROR: Invalid value for --ifdesc: '$$router_opt{ifdesc}'n";
  598.                     }
  599.                 }
  600.                 # Now setup a large number of variables needed for the
  601.                 # generation of the configuration lines.
  602.                 $if_title_desc =~ s/\([:@\/# ])/$1/g; # unescape
  603.                 $if_title_desc = $if_snmp_name if not $if_title_desc;
  604.                 my $html_if_title_desc = html_escape($if_title_desc);
  605.                 my $html_desc_prefix = html_escape($desc_prefix);
  606.                 my $html_if_snmp_descr = html_escape($if_snmp_descr);
  607.                 my $html_if_snmp_name = html_escape($if_snmp_name);
  608.                 my $html_if_snmp_alias = html_escape($if_snmp_alias);
  609.                 my $html_if_cisco_descr = html_escape($if_cisco_descr);
  610.                 my $if_description = "$if_snmp_descr $if_snmp_alias $if_cisco_descr";
  611.                 my $html_if_description = html_escape($if_description);
  612.                 my $if_title = "$desc_prefix$if_title_desc -- $sysname";
  613.                 my $html_if_title = html_escape($if_title);
  614.                 my $head_lines = "### Interface $ifindex >> Descr: '$if_snmp_descr' |".
  615.                                  " Name: '$if_snmp_name' | Ip: '$if_ip' | Eth: '$$i{Eth}' ###n";
  616.                 my $target_lines = "";
  617.                 my $separator_lines = "nn";
  618.                 # escape the if reference
  619.                 $if_ref =~ s/([& :])/\$1/g;
  620.                 my $default_target_directive = "Target[$target_name]: $if_ref:$router_connect";
  621. $default_target_directive .= "nSnmpOptions[$target_name]: $v3options"  if $$router_ref{snmpopt_current} =~ /(?=:.*?){4}:3/ ;
  622.                 my $default_setenv_directive = "SetEnv[$target_name]: MRTG_INT_IP="$if_ip" MRTG_INT_DESCR="$if_snmp_descr"";
  623.                 my $default_directory_directive = ($directory_name ? "Directory[$target_name]: $directory_name" : "");
  624.                 my $default_maxbytes_directive = "MaxBytes[$target_name]: $if_speed";
  625.                 $ipv4only_directive = $router_ipv4only ? "IPv4Only[$target_name]: yes" : "";
  626.                 my $default_title_directive = "Title[$target_name]: $html_desc_prefix$html_if_title_desc -- $sysname";
  627.                 my $default_pagetop_directive =
  628.                 "PageTop[$target_name]: <h1>$html_desc_prefix$html_if_title_desc -- $sysname</h1>
  629. <div id="sysdetails">
  630. <table>
  631. <tr>
  632. <td>System:</td>
  633. <td>$sysname in $html_syslocation</td>
  634. </tr>
  635. <tr>
  636. <td>Maintainer:</td>
  637. <td>$html_syscontact</td>
  638. </tr>
  639. <tr>
  640. <td>Description:</td>
  641. <td>$html_if_description</td>
  642. </tr>
  643. <tr>
  644. <td>ifType:</td>
  645. <td>$html_if_type_desc ($if_type_num)</td>
  646. </tr>
  647. <tr>
  648. <td>ifName:</td>
  649. <td>$html_if_snmp_name</td>
  650. </tr>";
  651.                 $default_pagetop_directive .= "
  652. <tr>
  653. <td>Port Name:</td>
  654. <td>$if_port_name</td>
  655. </tr>n" if defined $if_port_name;
  656.                 $default_pagetop_directive .= "
  657. <tr>
  658. <td>Max Speed:</td>
  659. <td>$if_speed_str</td>
  660. </tr>n";
  661.                 $default_pagetop_directive .= "
  662. <tr>
  663. <td>Ip:</td>
  664. <td>$if_ip ($if_dns_name)</td>
  665. </tr>n" if $if_ip;
  666.                 $default_pagetop_directive .= "
  667. </table>
  668. </div>";
  669.                 my $default_target_lines =
  670.                     ("n"
  671.                     . $default_target_directive . "n"
  672.                     . $default_setenv_directive . "n"
  673.                     . ($default_directory_directive
  674.                     ? ($default_directory_directive . "n")
  675.                     : "")
  676.                     . $default_maxbytes_directive . "n"
  677.                     . ($ipv4only_directive
  678.                     ? ($ipv4only_directive . "n")
  679.                     : "")
  680.                     . $default_title_directive . "n"
  681.                     . $default_pagetop_directive . "n");
  682.                 # If --if-filter is provided, evalutat that.  If it
  683.                 # returns true, clear @prob.  If it returns false,
  684.                 # instead add a complaint to @prob.
  685.                 if (defined $$router_opt{'if-filter'}) {
  686.                     $@ = undef;
  687.                     if (eval($$router_opt{'if-filter'})) {
  688.                         @prob = ();
  689.                     } else {
  690.                         push @prob, "filter specified by --if-filter rejected the interface";
  691.                         $if_ok = 0;
  692.                     }
  693.                     die "ERROR: with if-filter $$router_opt{'if-filter'}: $@" if $@;
  694.                 }
  695.                 # issue problem report
  696.                 my $problem_lines = "";
  697.                 if (@prob) {
  698.                     $problem_lines .= "### The following interface is commented out because:n";
  699.                     map {$problem_lines .= "### * $_n"} @prob;
  700.                     $if_ok = 0;
  701.                 }
  702.                 # The target config generation code starts HERE.
  703.                 if (defined $$router_opt{'if-template'}) {
  704.                     # First test if the file exists and is readable,
  705.                     # die if not.
  706.                     die "File $$router_opt{'if-template'} didn't exist.n" unless (-e $$router_opt{'if-template'}
  707.                         and -r $$router_opt{'if-template'});
  708.                     # Open the file (or die).
  709.                     open IF_TEMPLATE, $$router_opt{'if-template'}
  710.                         or die "File $$router_opt{'if-template'} couldn't be opened.n";
  711.                     my @template_lines = readline *IF_TEMPLATE;
  712.                     $@ = undef;
  713.                     eval (join "", @template_lines);
  714.                     die "Evaluation of the contents in the file nn$$router_opt{'if-template'}n".
  715.                         "gave the error nn"$@"nnExiting cfgmakern" if $@;
  716.                 } else {
  717.                     $target_lines = $default_target_lines;
  718.                 }
  719.                 if ($target_lines && not $if_ok) { # comment out the target lines if needed
  720.                     $target_lines =~ s/^/# /gm;
  721.                 }
  722.                 $conf .= ($head_lines
  723.                     . $problem_lines
  724.                     . $target_lines
  725.                     . $separator_lines);
  726.             }
  727.             # Target generation code ends HERE.
  728.         }
  729.     }
  730.     # print any global options which might have
  731.     # appeared on the command line after the last
  732.     # router.
  733.     if (defined $$opt{global}) {
  734.         foreach my $key (@{$$opt{global}}) {
  735.             $conf .= "$keyn";
  736.         }
  737.     }
  738.     if ($$opt{output}) {
  739.         debug ('base', "Writing $$opt{output}");
  740.         open X, ">$$opt{output}" or die "ERROR: creating $$opt{output}: $!n";
  741.         print X $conf;
  742.         close X;
  743.     } else {
  744.         print $conf;
  745.     }
  746. } # end GenConf
  747. sub IsCounterBroken ($$$) {
  748.     my $if = shift;
  749.     my $router_ref = shift;
  750.     my $v3opt = shift;
  751.     my $router = $$router_ref{routerkey};
  752.     my $old_state = $SNMP_Session::suppress_warnings;
  753.     $$router_ref{snmpopt_current} = $$router_ref{'snmp-options'};
  754.     $SNMP_Session::suppress_warnings = 3;
  755.     my $ipv4only = $$router_ref{ipv4only};
  756.     my $snmphost = v4onlyifnecessary($router, $ipv4only);
  757.     if ($router =~ /:d*:d*:d*:d*:[23]$/) {
  758.         # anybody knows why /:(d*:){4}2$/ does not work ?
  759.         my $speed = (snmpget($snmphost, $v3opt, 'ifHighSpeed.'.$if))[0];
  760.         debug('base',"snmpget $snmphost for ifHighSpeed.$if -> $speed Mb/s");
  761.         $SNMP_Session::errmsg = undef;
  762.         my $counter = (snmpget($snmphost,$v3opt, 'ifHCInOctets.'.$if))[0];
  763.         debug('base',"snmpget $snmphost for ifHCInOctets.$if -> $counter");
  764.         if(not $speed or $counter eq "" or $SNMP_Session::errmsg){
  765.             $SNMP_Session::errmsg = undef;
  766.             $$router_ref{snmpopt_current} =~ s/:+[23]$//;
  767.             debug('base',"check for HighspeedCounters failed ... Dropping back to V1");
  768.         } else {
  769.            $SNMP_Session::suppress_warnings = $old_state;
  770.            return 0;
  771.         }
  772.     }
  773.     $router = "$$router_ref{community}@$$router_ref{routername}$$router_ref{'snmp-options'}";
  774.     $snmphost = v4onlyifnecessary($router, $ipv4only);
  775.     if ( $$router_ref{snmpopt_current} !~ /:[23]/) {
  776.          snmpget($snmphost, 'ifInOctets.'.$if);
  777.          if (defined $SNMP_Session::errmsg) {
  778.              my $error = $SNMP_Session::errmsg;
  779.              $SNMP_Session::errmsg = undef;
  780.              $error =~ s/n/n###     /g;
  781.              $SNMP_Session::suppress_warnings = $old_state;
  782.              return $error;
  783.          }
  784.     }
  785.     $SNMP_Session::suppress_warnings = $old_state;
  786.     return 0;
  787. } # end IsCounterBroken
  788. # DeviceInfo does fallback between IPv6 and IPv4: if an IPv6 snmpwalk returns
  789. # undef values (= an error) and the target is a hostname, then it repeats the
  790. # query using IPv4 in case the target does not support SNMP over IPv6.
  791. # If DeviceInfo falls back to IPv4, it sets the ipv4only field for the target
  792. # in the routers hash.
  793. sub DeviceInfo ($$$) {
  794.     my $router=shift;
  795.     my $routers=shift;
  796.     my $v3opt=shift;
  797.     my @variables = qw(sysDescr sysContact
  798.                sysName sysLocation
  799.                sysObjectID);
  800.     my %DevInfo;
  801.     my $ipv4only = $$routers{$router}{ipv4only};
  802.     my @variables = snmpwalk(v4onlyifnecessary($router, $ipv4only),$v3opt,'1.3.6.1.2.1.1'); # walk system
  803.     if (!(defined $variables[0])) {
  804.         # Do we need to fall back to IPv4?
  805.         my ($commmunity, $host) = ($1, $2) if ($router =~ /^(.*)@([^@]+)$/);
  806.         if ( ( ! $ipv4only ) && ( $host !=~ /^[(.*)]/) ) {
  807.             # Not using IPv4, not an IPv6 address, so a hostname
  808.             debug ('base',"No response using IPv6 for $router, trying again using IPv4");
  809.             $$routers{$router}{ipv4only} = 1;
  810.             @variables = snmpwalk(v4onlyifnecessary($router, 1),$v3opt, '1.3.6.1.2.1.1');
  811.         }
  812.     }
  813.     if ( defined $variables[0] ) {
  814.         my (%DevInfo, %revOIDS);
  815. if ($$routers{$router}{enablesnmpv3} eq "yes") {
  816.          %revOIDS = reverse %Net_SNMP_util::OIDS;
  817.  }
  818.  else {
  819. %revOIDS = reverse %SNMP_util::OIDS;
  820. }
  821.         foreach my $variable ( @variables ) {
  822.             my ($oid, $value) = split ( ':', $variable, 2);
  823.             $DevInfo{ $revOIDS{'1.3.6.1.2.1.1.'.$oid} } = $value;
  824.         }
  825.         # vendor identification
  826.         my %vendorIDs = (
  827.             # Add your vendor here
  828.             # sysObjectID                Vendora
  829.             '1.3.6.1.4.1.43.' =>        '3com',
  830.             '1.3.6.1.4.1.11.' =>        'hp',
  831.             '1.3.6.1.4.1.9.' =>         'cisco',
  832.             '1.3.6.1.4.1.674.10895.' => 'dellLan',
  833.             '1.3.6.1.4.1.1916.' =>      'extremenetworks',
  834.             '1.3.6.1.4.1.1991.' =>      'foundry',
  835.             '1.3.6.1.4.1.6027.' =>      'force10',
  836.             '1.3.6.1.4.1.2636.' =>      'juniper',
  837.             '1.3.6.1.4.1.94.' =>        'nokiaipsofw',
  838.             '1.3.6.1.4.1.307.' =>       'portmaster'
  839.             );
  840.         foreach (keys %vendorIDs) {
  841.             $DevInfo{Vendor} = $vendorIDs{$_} if ($DevInfo{sysObjectID} =~ /Q$_E/);
  842.         }
  843.         debug('base',"Vendor Id: $DevInfo{Vendor}");
  844.         return %DevInfo;
  845.     } else {
  846.         # we just die because the snmp module has already complained
  847.         return undef;
  848.     }
  849. } # end DeviceInfo
  850. sub fmi ($$) {
  851.     my $number = shift;
  852.     my $flags = shift;
  853.     my(@short);
  854.     if ($$flags{bits} eq "set"){
  855.         $number*=8;
  856.         @short = ("bits/s","kbits/s","Mbits/s","Gbits/s");
  857.     } else {
  858.         @short = ("Bytes/s","kBytes/s","MBytes/s","GBytes/s");
  859.     }
  860.     my $digits=length("".$number);
  861.     my $divm=0;
  862.     while ($digits-$divm*3 > 4) { $divm++; }
  863.     my $divnum = $number/10**($divm*3);
  864.     return sprintf("%1.1f %s",$divnum,$short[$divm]);
  865. } # end fmi
  866. sub IfType ($) {
  867.   return {'1'=>'Other',
  868.       '2'=>'regular1822',
  869.       '3'=>'hdh1822',
  870.       '4'=>'ddnX25',
  871.       '5'=>'rfc877x25',
  872.       '6'=>'ethernetCsmacd',
  873.       '7'=>'iso88023Csmacd',
  874.       '8'=>'iso88024TokenBus',
  875.       '9'=>'iso88025TokenRing',
  876.       '10'=>'iso88026Man',
  877.       '11'=>'starLan',
  878.       '12'=>'proteon10Mbit',
  879.       '13'=>'proteon80Mbit',
  880.       '14'=>'hyperchannel',
  881.       '15'=>'fddi',
  882.       '16'=>'lapb',
  883.       '17'=>'sdlc',
  884.       '18'=>'ds1',
  885.       '19'=>'e1',
  886.       '20'=>'basicISDN',
  887.       '21'=>'primaryISDN',
  888.       '22'=>'propPointToPointSerial',
  889.       '23'=>'ppp',
  890.       '24'=>'softwareLoopback',
  891.       '25'=>'eon',
  892.       '26'=>'ethernet-3Mbit',
  893.       '27'=>'nsip',
  894.       '28'=>'slip',
  895.       '29'=>'ultra',
  896.       '30'=>'ds3',
  897.       '31'=>'sip',
  898.       '32'=>'frame-relay',
  899.       '33'=>'rs232',
  900.       '34'=>'para',
  901.       '35'=>'arcnet',
  902.       '36'=>'arcnetPlus',
  903.       '37'=>'atm',
  904.       '38'=>'miox25',
  905.       '39'=>'sonet',
  906.       '40'=>'x25ple',
  907.       '41'=>'iso88022llc',
  908.       '42'=>'localTalk',
  909.       '43'=>'smdsDxi',
  910.       '44'=>'frameRelayService',
  911.       '45'=>'v35',
  912.       '46'=>'hssi',
  913.       '47'=>'hippi',
  914.       '48'=>'modem',
  915.       '49'=>'aal5',
  916.       '50'=>'sonetPath',
  917.       '51'=>'sonetVT',
  918.       '52'=>'smdsIcip',
  919.       '53'=>'propVirtual',
  920.       '54'=>'propMultiplexor',
  921.       '55'=>'100BaseVG',
  922.       '56'=>'Fibre Channel',
  923.       '57'=>'HIPPI Interface',
  924.       '58'=>'Obsolete for FrameRelay',
  925.       '59'=>'ATM Emulation of 802.3 LAN',
  926.       '60'=>'ATM Emulation of 802.5 LAN',
  927.       '61'=>'ATM Emulation of a Circuit',
  928.       '62'=>'FastEthernet (100BaseT)',
  929.       '63'=>'ISDN & X.25',
  930.       '64'=>'CCITT V.11/X.21',
  931.       '65'=>'CCITT V.36',
  932.       '66'=>'CCITT G703 at 64Kbps',
  933.       '67'=>'Obsolete G702 see DS1-MIB',
  934.       '68'=>'SNA QLLC',
  935.       '69'=>'Full Duplex Fast Ethernet (100BaseFX)',
  936.       '70'=>'Channel',
  937.       '71'=>'Radio Spread Spectrum (802.11)',
  938.       '72'=>'IBM System 360/370 OEMI Channel',
  939.       '73'=>'IBM Enterprise Systems Connection',
  940.       '74'=>'Data Link Switching',
  941.       '75'=>'ISDN S/T Interface',
  942.       '76'=>'ISDN U Interface',
  943.       '77'=>'Link Access Protocol D (LAPD)',
  944.       '78'=>'IP Switching Opjects',
  945.       '79'=>'Remote Source Route Bridging',
  946.       '80'=>'ATM Logical Port',
  947.       '81'=>'AT&T DS0 Point (64 Kbps)',
  948.       '82'=>'AT&T Group of DS0 on a single DS1',
  949.       '83'=>'BiSync Protocol (BSC)',
  950.       '84'=>'Asynchronous Protocol',
  951.       '85'=>'Combat Net Radio',
  952.       '86'=>'ISO 802.5r DTR',
  953.       '87'=>'Ext Pos Loc Report Sys',
  954.       '88'=>'Apple Talk Remote Access Protocol',
  955.       '89'=>'Proprietary Connectionless Protocol',
  956.       '90'=>'CCITT-ITU X.29 PAD Protocol',
  957.       '91'=>'CCITT-ITU X.3 PAD Facility',
  958.       '92'=>'MultiProtocol Connection over Frame/Relay',
  959.       '93'=>'CCITT-ITU X213',
  960.       '94'=>'Asymetric Digitial Subscriber Loop (ADSL)',
  961.       '95'=>'Rate-Adapt Digital Subscriber Loop (RDSL)',
  962.       '96'=>'Symetric Digitial Subscriber Loop (SDSL)',
  963.       '97'=>'Very High Speed Digitial Subscriber Loop (HDSL)',
  964.       '98'=>'ISO 802.5 CRFP',
  965.       '99'=>'Myricom Myrinet',
  966.       '100'=>'Voice recEive and transMit (voiceEM)',
  967.       '101'=>'Voice Foreign eXchange Office (voiceFXO)',
  968.       '102'=>'Voice Foreign eXchange Station (voiceFXS)',
  969.       '103'=>'Voice Encapulation',
  970.       '104'=>'Voice Over IP Encapulation',
  971.       '105'=>'ATM DXI',
  972.       '106'=>'ATM FUNI',
  973.       '107'=>'ATM IMA',
  974.       '108'=>'PPP Multilink Bundle',
  975.       '109'=>'IBM IP over CDLC',
  976.       '110'=>'IBM Common Link Access to Workstation',
  977.       '111'=>'IBM Stack to Stack',
  978.       '112'=>'IBM Virtual IP Address (VIPA)',
  979.       '113'=>'IBM Multi-Protocol Channel Support',
  980.       '114'=>'IBM IP over ATM',
  981.       '115'=>'ISO 802.5j Fiber Token Ring',
  982.       '116'=>'IBM Twinaxial Data Link Control (TDLC)',
  983.       '117'=>'Gigabit Ethernet',
  984.       '118'=>'Higher Data Link Control (HDLC)',
  985.       '119'=>'Link Access Protocol F (LAPF)',
  986.       '120'=>'CCITT V.37',
  987.       '121'=>'CCITT X.25 Multi-Link Protocol',
  988.       '122'=>'CCITT X.25 Hunt Group',
  989.       '123'=>'Transp HDLC',
  990.       '124'=>'Interleave Channel',
  991.       '125'=>'Fast Channel',
  992.       '126'=>'IP (for APPN HPR in IP Networks)',
  993.       '127'=>'CATV MAC Layer',
  994.       '128'=>'CATV Downstream Interface',
  995.       '129'=>'CATV Upstream Interface',
  996.       '130'=>'Avalon Parallel Processor',
  997.       '131'=>'Encapsulation Interface',
  998.       '132'=>'Coffee Pot',
  999.       '133'=>'Circuit Emulation Service',
  1000.       '134'=>'ATM Sub Interface',
  1001.       '135'=>'Layer 2 Virtual LAN using 802.1Q',
  1002.       '136'=>'Layer 3 Virtual LAN using IP',
  1003.       '137'=>'Layer 3 Virtual LAN using IPX',
  1004.       '138'=>'IP Over Power Lines',
  1005.       '139'=>'Multi-Media Mail over IP',
  1006.       '140'=>'Dynamic synchronous Transfer Mode (DTM)',
  1007.       '141'=>'Data Communications Network',
  1008.       '142'=>'IP Forwarding Interface',
  1009.           '162'=>'Cisco Express Forwarding Interface',
  1010.       }->{(shift)};
  1011. } # end IfType
  1012. sub options ($$) {
  1013.     my $opt = shift;
  1014.     my $routers = shift;
  1015.     my $noofrouter = 0; # How many routers we've seen on cmdline.
  1016.     # The $flags hash stores what we've seen in Options[_],
  1017.     # Options[^] and Options[$] so far.
  1018.     # A cmdline arg like --global 'Options[_]: bits' will insert
  1019.     # the element $$flags{default}{bits}="set".
  1020.     # Similarly --global 'Options[$]:' will delete all elements
  1021.     # in $$flags{append}
  1022.     #
  1023.     # This was originally created to manipulate the "bits" flag
  1024.     # so fmi should know when to use "bits" or "bytes".  It might
  1025.     # be overkill to use such a comples solution but it makes life
  1026.     # easier if cfgmaker in the future has to be extended to be
  1027.     # aware of other Options[] settings like gauge, growright etc.
  1028.     my %flags;
  1029.     {
  1030.         my $def = {};
  1031.         my $pre = {};
  1032.         my $app = {};
  1033.         %flags = (default => $def,
  1034.                   prepend => $pre,
  1035.                   append  => $app);
  1036.     }
  1037.     my $addrouter_ornf = addrouter($opt,
  1038.                    $routers,
  1039.                    $noofrouter,
  1040.                    %flags);
  1041.     Getopt::Long::Configure("permute");
  1042.     GetOptions( $opt,
  1043.         'help|?',
  1044.         'man',
  1045.         'subdirs=s',
  1046.         'no-down',
  1047.         'show-op-down',
  1048.         'noreversedns',
  1049.         'ifref=s',
  1050.         'ifdesc=s',
  1051.         'if-filter=s',
  1052.         'if-template=s',
  1053.         'interfaces!',
  1054.         'host-template=s',
  1055.         'community=s',
  1056. 'username=s',
  1057. 'authkey=s',
  1058. 'authpassword=s',
  1059. 'authprotocol=s',
  1060. 'contextengineid=s',
  1061. 'contextname=s',
  1062. 'privkey=s',
  1063. 'privpassword=s',
  1064. 'privprotocol=s',
  1065.         'snmp-options=s',
  1066.         'dns-domain=s',
  1067.         'version',
  1068.         'output=s',
  1069.         'global=s@',
  1070.         'enable-ipv6',
  1071.         'enable-snmpv3',
  1072.         'use-16bit',
  1073. 'zero-speed=s',
  1074.         '<>', $addrouter_ornf) or pod2usage(2);
  1075.     die("cfgmaker for mrtg-2.13.2n") if $$opt{version};
  1076.     pod2usage(-exitval => 0, -verbose => 2) if $$opt{man};
  1077.     pod2usage(-verbose => 1) if not keys %$routers;
  1078. } # end options
  1079. # The callback routine used by GetOptions to process "non-option
  1080. # strings" (routers) among the arguments is given only ONE argument.
  1081. # However, I want it to be able to specify both the %options hash
  1082. # (for read access) and the %routers hash (for modifying) as well
  1083. # as the router's name.  This makes for three arguments.
  1084. #
  1085. # The solution is to use a closure.  addrouter takes a opt hash, a
  1086. # routers hash, an index to the current number of routers and a flags
  1087. # hash and then returns a function which "remembers" these
  1088. # values (the closure) but also takes an argument (the router name).
  1089. sub addrouter() {
  1090.     my $opt = shift;
  1091.     my $routers = shift;
  1092.     my $noofrouter = shift;
  1093.     my $flags = shift;
  1094.     return sub {
  1095.         my $rawname = shift;
  1096.         $$noofrouter++;  # First increase the number of routers seen.
  1097.         my ($community,$routername,$routerkey,$snmpopt,$dnsdomain,$tmpname,@tmpsnmp);
  1098.         # Now make sure that the router is defined with the
  1099.         # proper community, domainname and SNMP options.
  1100.         # Dissect the rawname to find out what it contains.
  1101.         # First check for community:
  1102.         if ($rawname =~ /^(.+)@([^@]+)$/) {
  1103.             # Community was given explicitly!
  1104.             $community = $1;
  1105.             $tmpname = $2
  1106.         } else {
  1107.             $community = $$opt{community};
  1108.             $tmpname = $rawname;
  1109.         }
  1110.         # Now separate the router name from the options. We
  1111.         # can't just split on the : character because a numeric
  1112.         # IPv6 address contains a variable number of :'s
  1113.         if( ($tmpname =~ /^([.*]):(.*)$/) || ($tmpname =~ /^([.*])$/) ){
  1114.             # Numeric IPv6 address between []
  1115.             ($routername, $snmpopt) = ($1, $2);
  1116.         } else {
  1117.             # Hostname or numeric IPv4 address
  1118.             ($routername, $snmpopt) = split(':', $tmpname, 2);
  1119.         }
  1120.         @tmpsnmp = split(':', $snmpopt);
  1121.         $routername =~ s/.$//; # if the hostname ends in a '.' remove it
  1122.                                 # it seems to cause trouble in some other
  1123.                                 # parts of mrtg
  1124.         # Now setup the SNMP options.
  1125.         if (not defined $$opt{'snmp-options'}) {
  1126.             $snmpopt = ':' . (join ':', @tmpsnmp);  # No merge needed.
  1127.         } else {
  1128.             my ($t,$o,@s);
  1129.             my @optsnmp = split ':',$$opt{'snmp-options'};
  1130.             # Trim first element as the SNMP options start
  1131.             # with a colon and thus the first element is a
  1132.             # dummy "" string not corresponding to any SNMP option
  1133.             # (or rather, corresponding to a router, if there had
  1134.             # been one...)
  1135.             shift @optsnmp;
  1136.             while ((scalar @tmpsnmp > 0)
  1137.                or (scalar @optsnmp > 0)) {
  1138.                 $t = shift @tmpsnmp;
  1139.                 $o = shift @optsnmp;
  1140.                 if(not defined $t) {$t = "";}
  1141.                 if(not defined $o) {$o = "";}
  1142.                 if($t ne "")
  1143.                 {
  1144.                     push @s, $t;
  1145.                 } else {
  1146.                     push @s, $o;
  1147.                 }
  1148.             }
  1149.             $snmpopt = ':' . (join ':', @s);
  1150.         }
  1151.         my $newopt={};  # Perhaps unecessary initialization but...
  1152.         foreach my $o (keys %$opt) {
  1153.             my $ovalue = $$opt{$o};
  1154.             $$newopt{$o} = $ovalue
  1155.             unless
  1156.                 ($o =~ /^fullcmd$/ or
  1157.                  $o =~ /^community$/ or
  1158.                  $o =~ /^snmp-options$/ or
  1159.                  $o =~ /^global$/ or
  1160.                  $o =~ /^output$/
  1161.                  );
  1162.             # Ok, copy the --globals array from $$opt so we know
  1163.             # that which global(s) to print out into the config.
  1164.             push @{$$newopt{$o}}, @{$$opt{$o}} if ($o =~ /^global$/);
  1165.             # Go through these --global statements one by one.
  1166.             # If anyone of them contains Options[] for any of the
  1167.             # targets [_], [^] or [_], process those statements
  1168.             # tenderly and populate the $$flags{}{} hashes accordingly.
  1169.             for my $g (@{$$opt{"global"}}) {
  1170.                 my ($t,$fs);
  1171.                 $g =~ /^options[([_^$])]:s*(.*)$/i;
  1172.                 $t = $1;
  1173.                 $fs = $2;
  1174.                 $t =~ s/_/default/;
  1175.                 $t =~ s/^/prepend/;
  1176.                 $t =~ s/$/append/;
  1177.                 # If a line like "options[X]:" is found clear
  1178.                 # all flags for that category and then go to next
  1179.                 # --global 'Options[..' line if any.
  1180.                 if ($fs =~ /^s*$/) {
  1181.                     $$flags{$t} = {};
  1182.                     next;
  1183.                 } else {
  1184.                     for my $f (split /s*,s*/,$fs) {
  1185.                         $$flags{$t}{$f} = "set";
  1186.                     }
  1187.                 }
  1188.             }
  1189.             $$opt{$o} = [] if ($o =~ /^global$/);
  1190.         }
  1191.         # Now let this router get it's own copy of
  1192.         # the "currently effective" flags.
  1193.         # Note, Options[_] should only be considered
  1194.         # if Options[^] and Options[$] both are absent.
  1195.         my $newflags = {};
  1196.         if((0 == keys %{$$flags{prepend}})
  1197.             and (0== keys %{$$flags{append}})) {
  1198.             for my $f (keys %{$$flags{default}}) {
  1199.                 $$newflags{$f}="set";
  1200.             }
  1201.         } else {
  1202.             for my $f (keys %{$$flags{prepend}},
  1203.                    keys %{$$flags{append}}) {
  1204.                 $$newflags{$f}="set";
  1205.             }
  1206.         }
  1207.         if(defined $$opt{'dns-domain'}) {
  1208.             $dnsdomain=$$opt{'dns-domain'};
  1209.         } else {
  1210.             $dnsdomain="";
  1211.         }
  1212.         $routerkey =
  1213.             "${community}@${routername}"
  1214.             . (($dnsdomain eq "")?"":".")
  1215.                 . "${dnsdomain}${snmpopt}";
  1216.         $$routers{$routerkey}= {
  1217.             # rawname is the unprocessed string from the
  1218.             # command line.
  1219.             rawname     => $rawname,
  1220.             # opt is the commandline options which are
  1221.             # in effect for THIS particular router.
  1222.             opt         => $newopt,
  1223.             # noofrouter is the unique number for the
  1224.             # router.  The first router on the command
  1225.             # line is given number 1, the second number 2
  1226.             # and so on.
  1227.             noofrouter  => $$noofrouter,
  1228.             # flags contains which --global 'Options[^_$]: flags'
  1229.             # are effective for THIS particular router.
  1230.             flags       => $newflags,
  1231.             # community is the SNMP community used for the router
  1232.             community   => $community,
  1233.             # snmpopt is the SNMP options on the form
  1234.             # [port[:timeout[:retries[:backoff[:version]]]]]
  1235.             # The empty string simply means that no
  1236.             # specific SNMP options has been given.
  1237.             'snmp-options' => $snmpopt,
  1238.             # dns-domain is a domain which should be added
  1239.             # to the routers hostname.
  1240.             # e.g if dns-domain is place.xyz and host is router
  1241.             # the host "router.place.xyz" will be polled.
  1242.             # If host is "router.dept" the poll will be against
  1243.             # "router.dept.place.xyz".
  1244.             'dns-domain' => $dnsdomain,
  1245.             # routername is the routers name as given on the
  1246.             # command line but with SNMP community (if given)
  1247.             # and SNMP options (if given) stripped.
  1248.             #
  1249.             # (Yes, routername COULD be on the form
  1250.             # "host.domain" or "host.subdomain.domain")
  1251.             routername  => $routername,
  1252.             # routerkey is the same as the has key used for the
  1253.             # router, which is the router name with everything
  1254.             # taken into account: community, dns-domain and
  1255.             # snmp-options.  This is the value used when doing
  1256.             # SNMP communication with the router.
  1257.             routerkey => $routerkey,
  1258.             };
  1259.     }
  1260. } # end addrouter
  1261. sub html_escape ($) {
  1262.     my $s = shift;
  1263.     $s =~ s/&/&amp;/g;
  1264.     $s =~  s/</&lt;/g;
  1265.     $s =~  s/>/&gt;/g;
  1266.     $s =~ s/[nr]+([^nr])/<BR>n $1/g;
  1267.     return $s;
  1268. } # end html_escape
  1269. sub parsev3 ($) {
  1270.     my $opt = shift;
  1271.     my %v3opt;
  1272. if (!exists ($$opt{username})) {
  1273.      die "SMNP V3 requires a --username paramter as part of the User Security Model";
  1274. } else {
  1275. $v3opt{username} = $$opt{username};
  1276. }
  1277. $v3opt{contextname} = $$opt{contextname} if exists($$opt{contextname});
  1278. if (exists ($$opt{authkey})) {
  1279. die "Can't use both an --authkey and --authpassword in the User Security Model" if exists($$opt{authpassword});
  1280. $v3opt{authkey} = $$opt{authkey};
  1281. }
  1282. if (exists ($$opt{authpassword})) {
  1283. die "Use of --authpassword requires --contextengineid" if !exists($$opt{contextengineid});
  1284. $v3opt{authpassword} = $$opt{authpassword};
  1285. }
  1286. if (exists ($$opt{authprotocol})) {
  1287. die "Only sha and md5 are defined for --authprotocol" if $$opt{authprotocol} !~ /^(md5|sha)$/i;
  1288. die "--authprotocol can only be used with --authpassword or --authkey" if ! exists($$opt{authpassword}) and ! exists($$opt{authkey});
  1289. ($v3opt{authprotocol}) = (lc($$opt{authprotocol}) =~ /^(md5|sha)$/);
  1290. }
  1291. if (exists ($$opt{privkey})) {
  1292. die "Can't use both an --privkey and --privpassword in the User Security Model" if exists($$opt{privpassword});
  1293. die "Can't have privacy parameters without authentication in the User security Model" if ! exists($$opt{authpassword}) and ! exists($$opt{authkey});
  1294. $v3opt{privkey} = $$opt{privkey};
  1295. }
  1296. if (exists ($$opt{privpassword})) {
  1297. die "Use of --privpassword requires --contextengineid" if !exists($$opt{contextengineid});
  1298. die "Can't have privacy parameters without authentication in the User security Model" if ! exists($$opt{authpassword}) and ! exists($$opt{authkey});
  1299. $v3opt{privpassword} = $$opt{privpassword};
  1300. }
  1301. if (exists ($$opt{privprotocol})) {
  1302. die "Only des, 3desede, aescfb128, aescffb192 and aescfb256 are defined for --privprotocol" if $$opt{privprotocol} !~ /^(3*des(ede)*|aescfb(128|192|256))$/;
  1303. die "--privprotocol can only be used with --privpassword or --privkey" if ! exists($$opt{privpassword}) and ! exists($$opt{privkey});
  1304. $v3opt{privprotocol} = lc($$opt{privprotocol});
  1305. }
  1306. return %v3opt;
  1307. }
  1308. sub init () {
  1309.   snmpmapOID('sysObjectID' => '1.3.6.1.2.1.1.2.0',
  1310.              'CiscolocIfDescr' => '1.3.6.1.4.1.9.2.2.1.1.28',
  1311.              'CiscoCatalystPortName' => '1.3.6.1.4.1.9.5.1.4.1.1.4',
  1312.              'vmVlan' => '1.3.6.1.4.1.9.9.68.1.2.2.1.2',
  1313.              'ifAlias' => '1.3.6.1.2.1.31.1.1.1.18');
  1314. } # end init
  1315. __END__
  1316. =pod
  1317. =head1 NAME
  1318. cfgmaker - Creates mrtg.cfg files (for mrtg-2.13.2)
  1319. =head1 SYNOPSIS
  1320. cfgmaker [options] [community@]router [[options] [community@]router ...]
  1321. =head1 OPTIONS
  1322.  --ifref=nr    interface references by Interface Number (default)
  1323.  --ifref=ip                     ... by Ip Address
  1324.  --ifref=eth                        ... by Ethernet Number
  1325.  --ifref=descr                      ... by Interface Description
  1326.  --ifref=name                       ... by Interface Name
  1327.  --ifref=type                       ... by Interface Type
  1328.  --ifdesc=nr       interface description uses Interface Number (default)
  1329.  --ifdesc=ip                        ... uses Ip Address
  1330.  --ifdesc=eth                       ... uses Ethernet Number
  1331.  --ifdesc=descr                     ... uses Interface Description
  1332.  --ifdesc=name                      ... uses Interface Name
  1333.  --ifdesc=catname                   ... uses CatOS Interface Name
  1334.  --ifdesc=alias                     ... uses Interface Alias
  1335.  --ifdesc=type                      ... uses Interface Type
  1336.  --if-filter=f     Test every interface against filter f to decide wether
  1337.                    or not to include that interface into the collection.
  1338.                    Currently f is being evaluated as a Perl expression
  1339.                    and it's truth value is used to reject or accept the
  1340.                    interface.
  1341.                    (Experimental, under development, might change)
  1342.  --if-template=templatefile
  1343.                    Replace the normal target entries for the interfaces
  1344.                    with an entry as specified by the contents in the file
  1345.                    templatefile.  The file is supposed to contain Perl
  1346.                    code to be executed to generate the lines for the
  1347.                    target in the configuration file.
  1348.                    (Experimental, under development, might change)
  1349.  --host-template=templatefile
  1350.                    In addition to creating targets for a host's interfaces
  1351.                    do also create targets for the host itself as specified
  1352.                    by the contents in the file templatefile.  The file is
  1353.                    supposed to contain Perl code to be executed to generate
  1354.                    the lines for the host related targets (such as CPU,
  1355.                    ping response time measurements etc.) in the config-
  1356.                    uration file.
  1357.                    (Experimental, under development, might change)
  1358.  --global "x: a"   add global config entries
  1359.  --no-down         do not look at admin or opr status of interfaces
  1360.  --show-op-down    show interfaces which are operatively down
  1361.  --zero-speed=spd  use this speed in bits-per-second as the interface
  1362.                    speed for all interfaces that return a speed of 0
  1363.                    via ifSpeed/ifHighSpeed.  100Mbps = 100000000
  1364.  --subdirs=format  give each router its own subdirectory, naming each per
  1365.                    "format", in which HOSTNAME and SNMPNAME will be
  1366.                    replaced by the values of those items -- for instance,
  1367.                    --subdirs=HOSTNAME or --subdirs="HOSTNAME (SNMPNAME)"
  1368.  --noreversedns    do not reverse lookup ip numbers
  1369.  --community=cmty  Set the default community string to "cmty" instead of
  1370.                    "public".
  1371.  --enable-ipv6     Enable IPv6 support, if the required libraries are
  1372.                    present. Numeric IPv6 addresses must be enclosed
  1373.                    in square brackets, e.g. public@[2001:760:4::1]:161
  1374.  --use-16bit       Use 16bit SNMP request IDs to query all routers.
  1375.  --snmp-options=:[<port>][:[<tmout>][:[<retr>][:[<backoff>][:<ver>]]]]
  1376.                    Specify default SNMP options to be appended to all
  1377.                    routers following.  Individual fields can be empty.
  1378.                    Routers following might override some or all of the
  1379.            options given to --snmp-options.
  1380.  --dns-domain=domain
  1381.            Specifies a domain to append to the name of all
  1382.            routers following.
  1383.  --nointerfaces    Don't do generate any configuration lines for interfaces,
  1384.                    skip the step of gathering interface information and
  1385.                    don't run any interface template code.
  1386.  --interfaces      Generate configuration lines for interfaces (this is the
  1387.                    default).  The main purpose of this option is to negate
  1388.                    an --nointerfaces appearing earlier on the command line.
  1389.  --help            brief help message
  1390.  --man             full documentation
  1391.  --version         print the version of cfgmaker
  1392.  --output=file     output filename default is STDOUT
  1393. =head1 DESCRIPTION
  1394. B<Cfgmaker> creates MRTG configuration files based on information
  1395. pulled from a router or another SNMP manageable device.
  1396. [I<community>B<@>]I<router>
  1397. I<Community> is the community name of the device you want to create a
  1398. configuration for. If not specified, it defaults to 'B<public>'; you might
  1399. want to try this first if you do not know the community name of a
  1400. device. If you are using the wrong community name you will get no
  1401. response from the device.
  1402. I<Router> is the DNS name or the IP number of an SNMP-managable device.
  1403. Following the name you can specify 6 further options separated by
  1404. colons.  The full syntax looks like this:
  1405. B<router>[:[B<prt>][:[B<tmout>][:[B<retr>][:[B<backoff>][:B<vers>]]]]]
  1406. Of special interest may be the last parameter, B<vers>.  If you set this to
  1407. '2' then your device will be queried with SNMP version 2 requests. This
  1408. allows to poll the 64 bit traffic counters in the device and will thus work
  1409. much better with fast interfaces (no more counter overrun).  Note that the
  1410. order in which the routers are specified on the command line do matter as
  1411. the same order is used when the configuration file is generated.  The first
  1412. specified router has it's configuration lines genrated first, followed by
  1413. the lines belonging to the next router and so on.
  1414. Note that the first line of the generated cfg file will contain all the
  1415. commandline options you used for generating it. This is to allow for the
  1416. easy 'regeneration' in case you want to add newhosts or make some other
  1417. global change.
  1418. =head2 Configuration
  1419. Except for the B<--output> and B<--global> options, all options affect
  1420. only the routers following them on the command line.  If an option
  1421. specified earlier on the command line reappears later on the command
  1422. line with another value, the new value overrides the old value as far as
  1423. remaining routers are concerned.  This way options might be tailored for
  1424. groups of routers or for individual routers.
  1425. See B<--output> and B<--global> for how their behaviour is affected by
  1426. where or how many times they appear on the command line.
  1427. See the B<Examples> below on how to set an option differently for
  1428. multiple routers.
  1429. =over
  1430. =item B<--help>
  1431. Print a brief help message and exit.
  1432. =item B<--man>
  1433. Prints the manual page and exits.
  1434. =item B<--version>
  1435. Print the version of cfgmaker.  This should match the version of MRTG
  1436. for which config files are being created.
  1437. =item B<--ifref> B<nr>|B<ip>|B<eth>|B<descr>|B<name>
  1438. Select the interface identification method.  Default is B<nr> which
  1439. identifies the router interfaces by their number.  Unfortunately the
  1440. interface numbering scheme in an SNMP tree can change. Some routers
  1441. change their numbering when new interfaces are added, others change
  1442. thier numbering every full moon just for fun.
  1443. To work around this sad problem MRTG can identify interfaces by 4
  1444. other properties. None of these works for all interfaces, but you
  1445. should be able to find one which does fine for you. Note that
  1446. especially ethernet addrsses can be problematic as some routers have
  1447. the same ethernet address on most of their interface cards.
  1448. Select B<ip> to identify the interface by its IP number. Use B<eth> to
  1449. use the ethernet address for identification. Use B<descr> to use
  1450. the Interface description. Or use B<name> to use the Interface name.
  1451. If your chosen method does not allow unique interface identification on
  1452. the device you are querying, B<cfgmaker> will tell you about it.
  1453. =item B<--ifdesc> B<nr>|B<ip>|B<eth>|B<descr>|B<name>|B<type>|B<alias>
  1454. Select what to use as the description of the interface.  The description
  1455. appears in the C<Title[]> property for the target as well as the text header
  1456. in the HTML code defined in the target's C<PageTop[]>.  Default is to use
  1457. B<nr> which is just the interface number which isn't always useful
  1458. to the viewer of the graphs.
  1459. There are 6 other properties which could be used.  Use B<ip> if you want
  1460. to use the interface's IP-address.  Use B<eth> if you want to use the
  1461. interface's ethernet address.  If you want a better description, you can
  1462. use either B<descr>, B<name> or B<alias>.  Exactly what each of these do
  1463. varies between different equipment so you might need to experiment.  For
  1464. instance, for a serial interface on a Cisco router running IOS using B<name>
  1465. might result in C<"S0"> being the interface description , B<descr> might result
  1466. in C<"Serial0"> and B<alias> might result in C<"Link to HQ"> (provided that is
  1467. what is used as the interface's C<description> in the router's configuration).
  1468. Finally, if you want to describe the interface by it's Btype
  1469. (i.e C<"ethernetCSMA">, C<"propPointtoPoint"> etc) you can use B<type>.
  1470. =item B<--if-filter> 'B<filter-expression>'
  1471. First of all, this is under some developement and is experimental.
  1472. Use this if you want to have better control over what interfaces gets
  1473. included into the configuration.  The B<filter-expression> is evaluated
  1474. as a piece of Perl code and is expected
  1475. to return a truth value.  If true, include the interface and if false,
  1476. exclude the interface.
  1477. For a further discussion on how these filters work, see the section
  1478. L<Details on Filters> below.
  1479. =item B<--if-template> B<template-file>
  1480. First of all, this is under some development and is experimental.
  1481. Use this if you want to control what the line for each target should look
  1482. like in the configuration file.  The contents of the file B<template-file>
  1483. will be evaluated as a Perl program which generates the lines using certain
  1484. variables for input and output.
  1485. For a further discussion on how these templates work, see the section
  1486. L<Details on Temaplates> below.
  1487. =item B<--host-template> B<template-file>
  1488. First of all, this is under some development and is experimental.
  1489. Use this if you want to have some extra targets related to the host itself
  1490. such as CPU utilization, ping response time to the host, number of busy
  1491. modems etc.  The contents of the file B<template-file> will be evaluated
  1492. once per host as a Perl program which generates the lines using certain
  1493. variables for input and output.
  1494. For a further discussion on how these templates work, see the section
  1495. L<Details on Templates> below.
  1496. =item B<--community> B<community-string>
  1497. Use this to set the community for the routers following on the command
  1498. line to B<community-string>.  Individual routers might overrride this
  1499. community string by using the syntax B<community>B<@>B<router>.
  1500. =item B<--enable-ipv6>
  1501. This option enables IPv6 support. It requires the appropriate perl
  1502. modules; if they are not found then IPv6 is disabled (see the ipv6
  1503. documentation).
  1504. cfgmaker will use IPv6 or IPv4 depending on the target. If the target
  1505. is a numeric address, the protocol depends on the type of address. If the
  1506. target is a hostname, cfgmaker will try to resolve the name first to an
  1507. IPv6 address then to an IPv4 address.
  1508. IPv6 numeric addresses must be specified between square braces.
  1509. For example:
  1510.  cfgmaker --enable-ipv6 [2001:760:4::1]:165:::2
  1511. If the target has both an IPv6 address and an IPv4 address with the same
  1512. hostname, cfgmaker first queries the target using IPv6 and falls back to
  1513. IPv4 if it fails. This is useful for targets which don't support SNMP
  1514. over IPv6.
  1515. =item B<--use-16bit>
  1516. This option forces the use of 16bit SNMP request IDs.  Some broken SNMP
  1517. agents do not accept 32bit request IDs.  Try to avoid this option as much
  1518. as possible, complain to your agent vendor instead.
  1519. =item B<--snmp-options>  :[B<port>][:[B<timeout>][:[B<retries>][:[B<backoff>][:B<version>]]]]
  1520. Use this to set the default SNMP options for all routers following on the
  1521. command line.  Individual values might be omitted as well as trailing
  1522. colons.  Note that routers might override individual (or all) values
  1523. specified by B<--snmp-options> by using the syntax
  1524. B<router>[:[B<port>][:[B<timeout>][:[B<retries>][:[B<backoff>][:B<version>]]]]]
  1525. =item B<--global> B<">I<bla: abc>B<">
  1526. Use this to add global options to the generated config file.
  1527. You can call B<--global> several times to add multiple options.
  1528. The line will appear in the configuration just before the config for
  1529. the next router appearing on the command line.
  1530.  --global "workdir: /home/mrtg"
  1531. If you want some default Options you might want to put
  1532.  --global "options[_]: growright,bits"
  1533. Specifying B<--global> after the last router on the command line will
  1534. create a line in the configuration file which will appear after all the
  1535. routers.
  1536. =item B<--noreversedns>
  1537. Do not try to reverse lookup IP numbers ... a must for DNS free environments.
  1538. =item B<--no-down>
  1539. Normally cfgmaker will not include interfaces which are marked
  1540. anything but administratively and operationally UP. With this
  1541. switch you get them all.
  1542. =item B<--show-op-down>
  1543. Include interfaces which are operatively down.
  1544. =item B<--zero-speed> I<speed>
  1545. Assign this speed in bits-per-second to all interfaces which return 0
  1546. for ifSpeed and ifHighSpeed.  Some switches, notably Foundry equipment,
  1547. return a speed of zero for some interfaces.  For example, to have
  1548. all interfaces reporting zero set to 100Mbps, use
  1549. --zero-speed=100000000.
  1550. =item B<--subdirs> I<format>
  1551. Give each router its own subdirectory for the HTML and graphics (or
  1552. .rrd) files.  The directory name is the given I<format> string with a
  1553. couple of pattern replacements.  The string "HOSTNAME" will be
  1554. replaced by the hostname of the router (however you specified it on
  1555. the B<cfgmaker> commandline -- it may be an actual hostname or just an
  1556. IP address), and "SNMPNAME" will be replaced with the device's idea of
  1557. its own name (the same name that appears on the right side of the
  1558. "Title" lines).  For instance, a call like:
  1559.  cfgmaker --subdirs=HOSTNAME__SNMPNAME public@10.10.0.18
  1560. would result in the generation of lines looking something like:
  1561.  Directory[10.10.0.18_1]: 10.10.0.18__fp2200-bothrip-1.3
  1562. =item B<--output> I<file>
  1563. Write the output from B<cfgmaker> into the file I<file>. The default
  1564. is to use C<STDOUT>. B<--output> is expected to appear only once on the
  1565. command line. If used multiple times, the file specified by the last
  1566. B<--output> will be used.
  1567. =item B<--nointerfaces>
  1568. Don't generate configuration lines for interfaces.
  1569. This makes cfgmaker skip all steps related to interfaces which means
  1570. it will not do any polling of the router to retrieve interface
  1571. information which speeds up the execution of cfgmaker and it will
  1572. neither run any interface templates.
  1573. =item B<--interfaces>
  1574. This makes cfgmaker generate configuration lines for interfaces (the
  1575. default behaviour).
  1576. The main usage of this option is to negate an --nointerfaces appearing
  1577. earlier on the command line.
  1578. =back
  1579. =head2 SNMP V3 Options
  1580. B<Cfgmaker> supports SNMP V3.  There are optional parameters affecting SNMP operation.
  1581. =head3 SNMPv3 Arguments
  1582. A SNMP context is a collection of management information accessible by a SNMP
  1583. entity.  An item of management information may exist in more than one context
  1584. and a SNMP entity potentially has access to many contexts.  The combination of
  1585. a contextEngineID and a contextName unambiguously identifies a context within
  1586. an administrative domain.  In a SNMPv3 message, the contextEngineID and
  1587. contextName are included as part of the scopedPDU.  All methods that generate
  1588. a SNMP message optionally take a B<--contextengineid> and B<--contextname>
  1589. argument to configure these fields.
  1590. =over
  1591. =item Context Engine ID
  1592. The B<--contextengineid> argument expects a hexadecimal string representing
  1593. the desired contextEngineID.  The string must be 10 to 64 characters (5 to
  1594. 32 octets) long and can be prefixed with an optional "0x".  Once the
  1595. B<--contextengineid> is specified it stays with the object until it is changed
  1596. again or reset to default by passing in the undefined value.  By default, the
  1597. contextEngineID is set to match the authoritativeEngineID of the authoritative
  1598. SNMP engine.
  1599. =item Context Name
  1600. The contextName is passed as a string which must be 0 to 32 octets in length
  1601. using the B<--contextname> argument.  The contextName stays with the object
  1602. until it is changed.  The contextName defaults to an empty string which
  1603. represents the "default" context.
  1604. =back
  1605. =head3 User-based Security Model Arguments
  1606. The User-based Security Model (USM) used by SNMPv3 requires that a securityName
  1607. be specified using the B<-username> argument.  The creation of a Net::SNMP
  1608. object with the version set to SNMPv3 will fail if the B<--username> argument
  1609. is not present.  The B<-username> argument expects a string 1 to 32 octets
  1610. in length.
  1611. Different levels of security are allowed by the User-based Security Model which
  1612. address authentication and privacy concerns.  A SNMPv3 target will
  1613. derive the security level (securityLevel) based on which of the following
  1614. arguments are specified.
  1615. By default a securityLevel of 'noAuthNoPriv' is assumed.  If the B<--authkey>
  1616. or B<--authpassword> arguments are specified, the securityLevel becomes
  1617. 'authNoPriv'.  The B<--authpassword> argument expects a string which is at
  1618. least 1 octet in length.  Optionally, the B<--authkey> argument can be used so
  1619. that a plain text password does not have to be specified in a script.  The
  1620. B<--authkey> argument expects a hexadecimal string produced by localizing the
  1621. password with the authoritativeEngineID for the specific destination device.
  1622. The C<snmpkey> utility included with the Net::SNMP  distribution can be used to create
  1623. the hexadecimal string (see L<snmpkey>).
  1624. Two different hash algorithms are defined by SNMPv3 which can be used by the
  1625. Security Model for authentication.  These algorithms are HMAC-MD5-96 "MD5"
  1626. (RFC 1321) and HMAC-SHA-96 "SHA-1" (NIST FIPS PUB 180-1).   The default
  1627. algorithm used by the module is HMAC-MD5-96.  This behavior can be changed by
  1628. using the B<--authprotocol> argument.  This argument expects either the string
  1629. 'md5' or 'sha' to be passed to modify the hash algorithm.
  1630. By specifying the arguments B<--privkey> or B<--privpassword> the securityLevel
  1631. associated with the object becomes 'authPriv'.  According to SNMPv3, privacy
  1632. requires the use of authentication.  Therefore, if either of these two
  1633. arguments are present and the B<--authkey> or B<--authpassword> arguments are
  1634. missing, the creation of the object fails.  The B<--privkey> and
  1635. B<--privpassword> arguments expect the same input as the B<--authkey> and
  1636. B<--authpassword> arguments respectively.
  1637. The User-based Security Model described in RFC 3414 defines a single encryption
  1638. protocol to be used for privacy.  This protocol, CBC-DES "DES" (NIST FIPS PUB
  1639. 46-1), is used by default or if the string 'des' is passed to the
  1640. B<--privprotocol> argument.  By working with the Extended Security Options
  1641. Consortium http://www.snmp.com/eso/, the module also supports additional
  1642. protocols which have been defined in draft specifications.  The draft
  1643. http://www.snmp.com/eso/draft-reeder-snmpv3-usm-3desede-00.txt
  1644. defines the support of CBC-3DES-EDE "Triple-DES" (NIST FIPS 46-3) in the
  1645. User-based Security Model.  This protocol can be selected using the
  1646. B<--privprotocol> argument with the string '3desede'.  The draft
  1647. http://www.snmp.com/eso/draft-blumenthal-aes-usm-04.txt
  1648. describes the use of CFB128-AES-128/192/256 "AES" (NIST FIPS PUB 197) in the
  1649. USM. The three AES encryption protocols, differentiated by their key sizes,
  1650. can be selected by passing 'aescfb128', 'aescfb192', or 'aescfb256' to the
  1651. B<-privprotocol> argument.
  1652. =head2 Details on Filters
  1653. The purpose of the filters is to decide which interfaces to accept and
  1654. which interfaces to reject.  This decision is done for each interface by
  1655. evaluating the filter expression as a piece of Perl code and investigating
  1656. the result of the evaluation.  If true, accept the interface otherwise
  1657. reject it.
  1658. When working with filters, remember that Perl has it's own idea of what truth
  1659. and false is.  The empty string "" and the string "0" are false, all other
  1660. strings are true.  This further imples that any integer value of 0 is
  1661. false as well as any undef value.  It also implies that all references
  1662. are considered true.
  1663. As the filter is evaluated as a Perl expression, several useful constructs
  1664. in Perl are worth mentioning:
  1665. Expressions might be grouped by using parentheses "()".  Expressions might
  1666. be combined using boolean operators such as the following:
  1667. =over
  1668. =item "B<and>" (equivalent with "B<&&>")
  1669. Boolean "and" of the two expressions, is only true if both expressions are
  1670. true.  Example: I<expression1> B<and> I<expression2>
  1671. =item "B<or>" (equivalent with "B<||>")
  1672. Boolean "or" of the two expressions, is true if either or both expressions
  1673. are true.  Example: I<expression1> B<or> I<expression2>
  1674. =item "B<not>" (equivalent with "B<!>")
  1675. Boolean negation of a single expression.  Example:  B<not> I<expression> .
  1676. Yet another example: B<!>I<expression>
  1677. =back
  1678. (For more details on this I recommend a book on Perl)
  1679. =head3 Predefined Filter Variables
  1680. To facilitate, there are a number of predefined values available to use
  1681. in the filter.  Note that these variables are also available when templates
  1682. interfaces are evaluated (but not host templates).
  1683. Caveat:  All these variables' names begin with a dollar sign  ($), which
  1684. is a syntactic requirement for scalar variables in Perl.  The danger here
  1685. is that the dollar sign in many shells is an active character (often
  1686. used for shell variables exactly as in Perl variables) so it is important
  1687. to ensure that the Perl expression isn't evaluated by the command line
  1688. shell as shell code before being passed to cfgmaker as command line
  1689. arguments.  In shells like Bourne shell, ksh shell or bash shell, placing
  1690. the entire expression within single qoutes will avoid such accidental
  1691. evaluation:
  1692.  '--if-filter=($default_iftype && $if_admin)'
  1693. =over
  1694. =item B<$if_type>
  1695. This is an integer specifying the interface type as
  1696. per the SNMP standards and as reported by the polled device.  A complete list
  1697. of interface types would be impractical for this document , but there are
  1698. a number predefined varables below.  Normally, cfgmaker puts in the target's
  1699. PageTop this iftype value within paranthesis after the name of the interface
  1700. type. (e.g "propPointToPointSerial (22)").
  1701. Here's a list of some of the most common interface types by number:
  1702.    6 ethernetCsmacd
  1703.    7 iso88023Csmacd
  1704.    9 iso88025TokenRing
  1705.   15 fddi
  1706.   19 E1
  1707.   20 basicISDN
  1708.   21 primaryISDN
  1709.   22 propPointToPointSerial
  1710.   23 ppp
  1711.   24 softwareLoopback
  1712.   30 ds3
  1713.   32 frame-relay
  1714.   33 rs232
  1715.   37 atm
  1716.   39 sonet
  1717.   44 frameRelayService
  1718.   46 hssi
  1719.   49 aal5
  1720.   53 propVirtual
  1721.   62 Fast Ethernet (100BaseT)
  1722.   63 ISDN & X.25
  1723.   69 Full Duplex Fast Ethernet (100BaseFX)
  1724.   94 Asymetric Digital Subscriber Loop (ADSL)
  1725.  117 Gigabit Ethernet
  1726.  134 ATM Sub Interface
  1727. =item B<$default>
  1728. True if and only if cfgmaker normally should
  1729. accepted the interface based on the interfaces administrative and
  1730. operational state (taking the flags B<--no-down> and B<--show-op-down> into
  1731. account) and it's type (and a few other things).
  1732. =item B<$default_ifstate>
  1733. True if and only if cfgmaker would have accepted the
  1734. interface based on it's operational and administrative states (also taking
  1735. into account the presence of the flags B<--no-down> and B<--show-op-down>).
  1736. =item B<$default_iftype>
  1737. True if and only if cfgmaker would have accepted the
  1738. interface based on it's type (and a few type specific details in addition).
  1739. =item B<$if_admin>
  1740. True if and only if the interface is in an adminstrative up
  1741. state.
  1742. =item B<$if_oper>
  1743. True if and only if the interface is in an operational up
  1744. state.
  1745. =back
  1746. A number of variables are also predefined to easily decide if an interface
  1747. belong to a certain cathegory or not.  Below is all those variables listed
  1748. together with which if_type numbers each variable will be true for.  Note
  1749. that some variables refer to other variables as well.
  1750. =over
  1751. =item B<$if_is_ethernet>
  1752. True for ethernet interfaces (nr 6, 7, 26, 62, 69 and 117).
  1753. =item B<$if_is_isdn>
  1754. True for various ISDN interface types (nr 20, 21, 63, 75, 76 and 77)
  1755. =item B<$if_is_dialup>
  1756. True for dial-up interfaces such as PPP as well
  1757. as ISDN.  (nr 23, 81, 82 and 108 in addition to the numbers of
  1758. B<$if_is_isdn>).
  1759. =item B<$if_is_atm>
  1760. True for miscellaneous ATM related interface types (nr 37, 49, 107, 105,
  1761. 106, 114 and 134).
  1762. =item B<$if_is_wan>
  1763. True for WAN interfaces point to point, Frame Relay and High Speed Serial ( 22,32,44,46)
  1764. =item B<$if_is_lan>
  1765. True for LAN interfaces (8, 9, 11, 15, 26, 55, 59, 60 and 115 in addition
  1766. to the numbers of B<$if_is_ethernet>).
  1767. =item B<$if_is_dsl>
  1768. True for ADSL, RDSL, HDSL and SDSL (nr 94, 95, 96, 97)
  1769. =item B<$if_is_loopback>
  1770. True for software loopback interfaces (nr 24)
  1771. =item B<$if_is_ciscovlan>
  1772. True for Cisco VLAN interfaces (interfaces with the
  1773. word Vlan or VLAN in their ifdescs)
  1774. =item B<$if_vlan_id>
  1775. Returns the vlan id associated with a specific port
  1776. on Cisco Catalyst switches under both Catalyst OS
  1777. and IOS.  If it is not a vlan interface, will return undef.
  1778. =item B<$if_MTU>
  1779. Returns the Maximum Transfer Unit associated with a specific port.
  1780. =back
  1781. Besides that, you can also use the variables defined for templates below.
  1782. Further, all the variables available in cfgmaker is at the scripts disposal
  1783. even if the use of such features is discouraged.  More "shortcuts" in the
  1784. form of variables and functions will be made avaiable in the future instead.
  1785. =head3 Examples on Filters
  1786. The following filter will not affect which interfaces get's included or
  1787. excluded, it will make cfgmaker behave as normally.
  1788.  '--if-filter=$default'
  1789. The following filter will make cfgmaker exclude PPP (23) interfaces:
  1790.  '--if-filter=$default && $if_type!=23'
  1791. The following filter will make cfgmaker behave as usual except that it will
  1792. consider the operational state of an interface irrelevant but still reject
  1793. all interfaces which are administratively down.
  1794.  '--if-filter=$if_admin && $default_iftype'
  1795. =head2 Details on Templates
  1796. The contents of the template files are evaluated as a Perl program.  A
  1797. number or Perl variables are available for the program to read and others
  1798. are used to be written to.
  1799. As quite a few of the predefined variables has values which are are supposed
  1800. to be used in HTML code some of them have an "HTML-escaped" variant, e.g
  1801. $html_syslocation is the HTML escaped variant of $syslocation.  The HTML
  1802. escaping means that the chars "<", ">" and "&" are replaced by "&lt;",
  1803. "&gt;" and "&amp;" and that newlines embedded in the string are prepended
  1804. with "<BR>" and appended with a space character (if a newline is last in the
  1805. string it is not touched).
  1806. =head3 Writable Template Variables
  1807. These are the variables available to store the configuration lines in.
  1808. Some of them are initialized prior to the evaluation of the template but
  1809. such content normally is comments for inclusion in the final configuration
  1810. file so those variables might be reset to the empty string in the template
  1811. code to eliminate the comments.  The other way around is also possible, the
  1812. contents of these variables might be extended with further information
  1813. for various reasons such as debugging etc.
  1814. Once the template has been evaluated, the following happens:  if the
  1815. template is a interface template and the actual interface for some reason
  1816. is rejected and thus needs to be commented out, all the lines in the
  1817. variable B<$target_lines> are turned into comments by adding a hash mark
  1818. ("#") at their beginning.  Then all the variables B<$head_lines>,
  1819. B<$problem_lines> , B<$target_lines> and B<$separator_lines>
  1820. are concatenated together to form the lines to add to the configuration file.
  1821. =over
  1822. =item B<$target_lines>
  1823. This variable is the placeholder for the configuration lines created
  1824. by the template.  B<$target_lines> is predefined to be empty when
  1825. the template code is evaluated.
  1826. =item B<$head_lines>
  1827. This variable is intended to be the placeholder for the comment line
  1828. appearing just before the target in the configuration file.  It is
  1829. initialized with that comment line before the evaluation of the template
  1830. code and if the template doesn't modify B<$head_lines> during evaluation,
  1831. the comment will look like usual in the config file.
  1832. =item B<$problem_lines>
  1833. This variable is intended to be the placholder for the comment lines
  1834. describing any problems which might have been encountered when trying
  1835. to add the target into the configuration.  For host templates it's
  1836. normally not used and for those it's predefined as the empty string.
  1837. For interface templates B<$problem_lines> is predefined with
  1838. the error description comments which cfgmaker normally would use for
  1839. rejected interfaces or as the empty string for accepted interfaces.
  1840. It is possible to test against B<$problem_lines> to find out if
  1841. an interface will be included or rejected but this is not recommended.
  1842. Test against B<$if_ok> instead.
  1843. =item B<$separator_lines>
  1844. This variable is the placeholder for the string to use as the separator
  1845. between the code for individual targets.  The contents of this variable
  1846. is put after each target (so the lines will appear after the end of the
  1847. last target in the config as well).
  1848. =back
  1849. =head3 Predefined Template Variables
  1850. All the variables below are available for interface templates to use.
  1851. For host templates, only those listed under L<Host and System Variables>
  1852. are available.
  1853. For interface templates the variables listed under
  1854. L<Predefined Filter Variables> are also available.
  1855. =head3 Host and System Variables
  1856. =over
  1857. =item B<$router_name>
  1858. This is the fully qualified name for the router.  It is affected by the
  1859. following items on the command line:  the router name itself and
  1860. B<--dns-domain>.
  1861. =item B<$router_connect>
  1862. This is the reference string for the router being polled.  It is on the
  1863. form community@router possibly followed by some snmp options.  It is
  1864. affected by the following items on the command line:  the router name
  1865. itself, B<--community>, B<--snmp-options> and B<--dns-domain>.
  1866. (There's no HTML escaped variant available)
  1867. =item B<$directory_name>
  1868. This variable should contain the directory name as cfgmaker normally would
  1869. use as the value for the "Directory[]" directive.  The value is determined
  1870. by the B<--subdirs> command line option.  If B<--subdirs> isn't specified
  1871. B<$directory_name> will be the empty string.  (There's no HTML escaped
  1872. variant available)
  1873. =item B<$syscontact>
  1874. This variable is the router's SNMP sysContact value.  (HTML escaped
  1875. variant: B<$html_syscontact>)
  1876. =item B<$sysname>
  1877. This variable is the router's SNMP sysName value.  (No HTML escaped
  1878. variant available)
  1879. =item B<$syslocation>
  1880. This variable is the router's SNMP sysLocation value.  (HTML escaped
  1881. variant: B<$html_syslocation>)
  1882. =item B<$sysdescr>
  1883. This variable is the router's SNMP sysDescr value.  It is normally not used
  1884. by cfgmaker but might be useful in a template.  (HTML escaped variant:
  1885. B<$html_sysdescr>)
  1886. =back
  1887. =head3 Interface Target Related Variables
  1888. =over
  1889. =item B<$target_name>
  1890. This is what cfgmaker normally would use as the the name of the target.
  1891. The target name is what is found within the square brackets, "[]", for target
  1892. directives.  (There's no HTML escaped variant available)
  1893. =item B<$if_ref>
  1894. This the reference string for the interface.  It is expected to be used
  1895. in the "Target[xyz]" directive to distinguish what interface to use.  The
  1896. value of this variable is affected by the B<--ifref> command line option.
  1897. It is normally used together with B<$router_connect>.
  1898. (There's no HTML escaped variant available)
  1899. =item B<$if_ok>
  1900. This variable is true if the interface is going to be included into the
  1901. configuration file, otherwise false.  Don't test against other variables
  1902. such as B<$problem_lines> to find out if an interface will be rejected
  1903. or not, use this B<$if_ok> instead.
  1904. =item B<$default_target_lines>
  1905. This variable contains all the target lines which cfgmaker by default outputs
  1906. for this interface.  It's useful if you want to have the "standard target"
  1907. but want to add some extra lines to it by using a template.
  1908. =back
  1909. By default cfgmaker uses the following directives for each target it
  1910. generates: Target[], SetEnv[], MaxBytes[], Title[], PageTop[] and if
  1911. there is any directory specified also the Directory[] directive.
  1912. To facilitate the creation of templates which generates target configs
  1913. which are similar to the default one, each of the above mentioned
  1914. directive lines have a corresponding variable containing the line as
  1915. cfgmaker would have output it by default.
  1916. Note that none of these have a HTML escaped variant, text in them is
  1917. HTML escaped where needed.  Also note that they do not have any newline
  1918. at the end.
  1919. =over
  1920. =item B<$default_target_directive>
  1921. This variable contains the default string for the Target[] directive line.
  1922. =item B<$default_setenv_directive>
  1923. This variable contains the default string for the SetEnv[] directive line.
  1924. =item B<$default_directory_directive>
  1925. This variable contains the default string for the Directory[] directive line
  1926. which means it is an empty string (with no newline) if there's no directory.
  1927. =item B<$default_maxbytes_directive>
  1928. This variable contains the default string for the MaxBytes[] directive line.
  1929. =item B<$default_title_directive>
  1930. This variable contains the default string for the Title[] directive line.
  1931. =item B<$default_pagetop_directive>
  1932. This variable contains the default string for the PageTop[] directive lines.
  1933. =back
  1934. =head3 Interface Network Configuration Variables
  1935. =over
  1936. =item B<$if_ip>
  1937. This variable should contain the IP-address of the interface, if any has
  1938. been assigned to it.  (There's no HTML escaped variant available)
  1939. =item B<$ifindex>
  1940. This variable is the SNMP ifIndex for the interface which per definition
  1941. always is an integer.  (There's no HTML escaped variant available)
  1942. =item B<$if_index>
  1943. Equivalent with B<$ifindex>.
  1944. =item B<$if_eth>
  1945. Contains the ethernet address of the interface, if any.  (There's no HTML
  1946. escaped variant available)
  1947. =item B<$if_speed>
  1948. This variable is the speed in bytes/second (with prefixes).  (There's no
  1949. HTML escaped variant available)
  1950. =item B<$if_speed_str>
  1951. This variable is a cooked speed description which is either in bits or
  1952. bytes depending on wether or not the bits option is active and also with
  1953. the proper prefix for the speed (k, M, G etc).  (No HTML escaped variant
  1954. available)
  1955. =item B<$if_type_desc>
  1956. This variable is a textual description of the interface type.  (HTML
  1957. escaped variant: B<$html_if_type_desc>)
  1958. =item B<$if_type_num>
  1959. This variable the integer value corresponding to the interface type (for a
  1960. listing for the value for the more common interface types, see the section
  1961. DETAILS ON FILTERS above).  (No HTML escaped variant available)
  1962. =item B<$if_dns_name>
  1963. This is the DNS name for the interface.  (No HTML escaped variant available)
  1964. =back
  1965. =head3 Interface Name, Description and Alias Variables
  1966. It might seem confusing with both I<Name>, I<Description> and I<Alias> in
  1967. this context and to some extent it is.  I<Name> and I<Description> are
  1968. usually supported on most equipment but how they are used varies, both
  1969. between manufacturers as well as between different cathegories of equipment
  1970. from the same manufacturer.  The I<Alias> is at least supported by Cisco
  1971. IOS, and that variable contains whatever is used in the IOS statement
  1972. called "description" for the interface (not to be confused with the SNMP
  1973. variables for I<Description>).
  1974. For better control from the command line consider B<$if_title_desc> which contents
  1975. are controlled by the B<--if-descr> command line option.
  1976. =over
  1977. =item B<$if_snmp_descr>
  1978. This variable should contain the "raw" description of the interface as
  1979. determined by the SNMP polling of the router.  (HTML escaped variant:
  1980. B<$html_if_snmp_descr>)
  1981. =item B<$if_snmp_name>
  1982. The "raw" name for the interface as provided by SNMP polling.  (HTML escaped
  1983. variant: B<$html_if_snmp_name>)
  1984. =item B<$if_snmp_alias>
  1985. The "raw" ifAlias for the interface as provided by SNMP polling. (HTML
  1986. escaped variant: B<$html_if_snmp_alias>)
  1987. =item B<$if_cisco_descr>
  1988. The "raw" CiscolocIfDescr for the interface as provided by SNMP polling.
  1989. (HTML escaped variant: B<$html_if_cisco_descr>)
  1990. =item B<$if_description>
  1991. This is the "cooked" description string for the interface, taking into account
  1992. the SNMP values found for the interface's RDescr, ifAlias and
  1993. CiscolocIfDescr.  (HTML escaped variant: B<$html_if_description>)
  1994. =item B<$if_title>
  1995. The full string cfgmaker by default would have used for the Title[] directive
  1996. in the configuration as well as the content of the topmost H1 tag in the
  1997. PageTop[].  Is composed by the contents of B<$desc_prefix>,
  1998. B<$if_title_desc> and B<$sysname>.
  1999. As B<$if_title> depends on B<$if_title_desc>, it is possible to indirectly
  2000. control B<$if_title> by using the command line option B<--if-descr>.
  2001. (HTML escaped variant: B<$html_if_title>)
  2002. =item B<$if_port_name>
  2003. If the host is a Cisco Catalyst LAN switch, this variable is the name of
  2004. that port.  (No HTML escaped variant available)
  2005. =item B<$desc_prefix>
  2006. This variable is a prefix of the description of what the target is to use in
  2007. the "Title[]" directive and in the H1 section of the "PageTop[]".  Default is
  2008. "Traffic analysis for ".  (HTML escaped variant: B<$html_desc_prefix>)
  2009. =item B<$if_title_desc>
  2010. This is the description of the interface normally used by cfgmaker as part
  2011. of the variable B<$if_title>.  The latter is used as the full string in the
  2012. "Title[]" directove and the H1 section in the PageTop[].
  2013. B<$if_title_desc> is controlled by the command line option B<--if-descr>
  2014. which indirectly controls the contents of B<$if_title>
  2015. (HTML escaped variant: B<$html_if_title_desc>)
  2016. =back
  2017. =head3 Help Functions for Templates
  2018. The following functions exists to facilitate the writing of host and
  2019. interface templates.
  2020. =over
  2021. =item B<html_escape(I<string>)>
  2022. B<html_escape()> takes a string as an argument and returns a new string
  2023. where the following substitutions has been done:  the chars "<", ">" and
  2024. "&" are replaced by "&lt;", "&gt;" and "&amp;" and that newlines embedded
  2025. in the string are prepended with "<BR>" and appended with a space character
  2026. (newlines at the end of the string are not touched).
  2027. =back
  2028. =head3 Example Template Files
  2029. =head4 Template Example 1: Eliminating Rejected Targets From Appearing
  2030. This template file generates exactly the same configuration code per
  2031. interface as cfgmaker does by default, with the exception that it eliminates
  2032. all lines (comments as well as config code) for an interface if the
  2033. interface happens to be rejected.
  2034.  if(not $problem_lines)
  2035.  {
  2036.    $target_lines .= <<ECHO;
  2037.  Target[$target_name]: $if_ref:$router_connect
  2038.  SetEnv[$target_name]: MRTG_INT_IP="$if_ip" MRTG_INT_DESCR="$if_snmp_descr"
  2039.  ECHO
  2040.    if ($directory_name) {
  2041.        $target_lines .= "Directory[$target_name]: $directory_namen";
  2042.    }
  2043.    $target_lines .= <<ECHO;
  2044.  MaxBytes[$target_name]: $if_speed
  2045.  Title[$target_name]: $html_desc_prefix$html_if_title_desc -- $sysname
  2046.  PageTop[$target_name]: <h1>$html_desc_prefix$html_if_title_desc -- $sysname</h1>
  2047. <div id="sysdetails">
  2048. <table>
  2049. <tr>
  2050. <td>System:</td>
  2051. <td>$sysname in $html_syslocation</td>
  2052. </tr>
  2053. <tr>
  2054. <td>Maintainer:</td>
  2055. <td>$html_syscontact</td>
  2056. </tr>
  2057. <tr>
  2058. <td>Description:</td>
  2059. <td>$html_if_description</td>
  2060. </tr>
  2061. <tr>
  2062. <td>ifType:</td>
  2063. <td>$html_if_type_desc ($if_type_num)</td>
  2064. </tr>
  2065. <tr>
  2066. <td>ifName:</td>
  2067. <td>$html_if_snmp_name</td>
  2068. </tr>
  2069.  ECHO
  2070.    $target_lines .= <<ECHO if defined $if_port_name;
  2071. <tr>
  2072. <td>Port Name:</td>
  2073. <td>$if_port_name</td>
  2074. </tr>
  2075.  ECHO
  2076.    $target_lines .= <<ECHO;
  2077. <tr>
  2078. <td>Max Speed:</td>
  2079. <td>$if_speed_str</td>
  2080. </tr>
  2081.  ECHO
  2082.    $target_lines .= <<ECHO if $if_ip;
  2083. <tr>
  2084. <td>Ip:</td>
  2085. <td>$if_ip ($if_dns_name)</td>
  2086. </tr>
  2087.  ECHO
  2088.    $target_lines .= <<ECHO;
  2089. </table>
  2090. </div>
  2091.  ECHO
  2092.  } else {
  2093.    $head_lines="";
  2094.    $problem_lines="";
  2095.    $target_lines="";
  2096.    $separator_lines="";
  2097.  }
  2098. =head3 Template Example 2: Simplier Version of Example 1
  2099. Example 1 was partly intended to demonstrate how to customize the generation
  2100. of interface targets but also to provide a hint of how the variables are
  2101. used in the "default" template which one could consider that cfgmaker
  2102. normally uses.
  2103. If you're only intrested in the easiest way of entirely eliminating those
  2104. reject interfaces, the template below would do the job as well by using
  2105. B<$default_target_lines>.
  2106.  if($if_ok) {
  2107.   $target_lines = $default_target_lines;
  2108.  } else {
  2109.    $head_lines="";
  2110.    $problem_lines="";
  2111.    $target_lines="";
  2112.    $separator_lines="";
  2113.  }
  2114. =head3 Template Example 3: Creating CPU Targets for Hosts
  2115. Below is an example of a host template.
  2116.  $head_lines .= <<ECHO;
  2117.  #---------------------------------------------------------------------
  2118.  ECHO
  2119.  my $target_name = $router_name . ".cpu";
  2120.  $target_lines .= <<ECHO;
  2121.  YLegend[$target_name]: Percentage CPU load
  2122.  ShortLegend[$target_name]: %
  2123.  Legend1[$target_name]: CPU load in %
  2124.  Legend2[$target_name]:
  2125.  Legend3[$target_name]: Max Observed CPU load
  2126.  Legend4[$target_name]:
  2127.  LegendI[$target_name]: &nbsp;CPU Load:
  2128.  LegendO[$target_name]:
  2129.  WithPeak[$target_name]: ywm
  2130.  MaxBytes[$target_name]: 100
  2131.  Options[$target_name]: growright, gauge, nopercent
  2132.  Title[$target_name]: $router_name CPU load
  2133.  Target[$target_name]: 1.3.6.1.4.1.9.2.1.58.0&1.3.6.1.4.1.9.2.1.58.0:$router_connect
  2134.  PageTop[$target_name]: <h1>$router_name CPU load</h1>
  2135. <div>
  2136. <table>
  2137. <tr>
  2138. <td>System:</td>
  2139. <td>$router_name in $html_syslocation</td>
  2140. </tr>
  2141. <tr>
  2142. <td>Maintainer:</td>
  2143. <td>$html_syscontact</td>
  2144. </tr>
  2145. <tr>
  2146. <td>Description:</td>
  2147. <td>$html_sysdescr</td>
  2148. </tr>
  2149. <tr>
  2150. <td>Resource:</td>
  2151. <td>CPU.</td>
  2152. </tr>
  2153. </table>
  2154. </div>
  2155.  ECHO
  2156. =head1 EXAMPLES
  2157. The first example creates a config file for I<router.place.xyz>:  the router
  2158. has the community name I<public>.  Interfaces get identified by their
  2159. IP number.  Two global options get added to the config file.  The
  2160. config file gets redirected to I<mrtg.conf>.  The '' signs at the end
  2161. of the line mean that this command should be written on a single line.
  2162.  cfgmaker --global "WorkDir: /home/tobi"           
  2163.           --global "Options[_]: growright,bits"    
  2164.           --ifref=ip                               
  2165.           public@router.place.xyz > mrtg.cfg
  2166. Note: if cfgmaker is not in your path, but you are in the directory where
  2167. cfgmaker is stored, you can start it with ./cfgmaker
  2168. The next example creates a config file for four devices:
  2169. I<router1.place.xyz>, I<router2.place.xyz>, I<switch1.place.xyz> and
  2170. I<switch2.place.xyz> all with the community I<public>.
  2171. The two routers will have B<--ifref> set to B<descr> whilst the two
  2172. switches will use B<--ifref> set to B<name>.  Further the routers will
  2173. use B<--ifdesc> set to B<alias> and I<switch1.place.xyz> will use
  2174. B<--ifdesc> set to B<descr> whilst I<switch2.place.xyz> use B<name> instead.
  2175. Finally, there will be two Options lines inserted in the configuration:
  2176. One will be in the beginning, whilst the other will be inserted after
  2177. the lines related to the two routers but before those lines related
  2178. to the switches.
  2179.  cfgmaker --global "WorkDir: /home/tobi"           
  2180.           --global "Options[_]: growright,bits"    
  2181.           --ifref=descr                            
  2182.           --ifdesc=alias                           
  2183.           public@router1.place.xyz                 
  2184.           public@router2.place.xyz                 
  2185.           --global "Options[_]: growright"         
  2186.           --ifref=name                             
  2187.           --ifdesc=descr                           
  2188.           public@switch1.place.xyz                 
  2189.           --ifdesc=name                            
  2190.           public@switch2.place.xyz > mrtg.cfg
  2191. The next example demonstrates how to use the B<--community>,
  2192. B<--snmp-options> and B<--dns-domain> to make the command line
  2193. simpler.  All the equipment will use the community I<hidden>, except for
  2194. the ppp-server which use community I<access>.  All equipment uses these
  2195. SNMP options: B<1s timeout>, B<1 retry> and B<SNMP version 2> (B<backoff> and
  2196. B<port> is unspecified which means they use the default values).
  2197. The exception again is the ppp-server which uses B<SNMP version 1>.
  2198. Finally, all the equipment is part of the domain I<place.xyz>, except
  2199. for the ppp-server which is part of the domain I<remote.place.xyz>.
  2200. Note that the latter is achieved simply by specifying the name
  2201. of the ppp-server to be I<ppp-server.B<remote>> .
  2202.  cfgmaker --global "WorkDir: /home/tobi"           
  2203.           --global "Options[_]: growright,bits"    
  2204.           --dns-domain=place.xyz                   
  2205.           --community=hidden                       
  2206.           --snmp-options=::1:1::2                  
  2207.           router1                                  
  2208.           router2                                  
  2209.           router3                                  
  2210.           router4                                  
  2211.           router5                                  
  2212.           switch1                                  
  2213.           switch2                                  
  2214.           switch3                                  
  2215.           switch4                                  
  2216.           switch5                                  
  2217.           switch6                                  
  2218.           switch7                                  
  2219.           access@ppp-server.remote:::::1 > mrtg.cfg
  2220. =head1 SEE ALSO
  2221. L<mrtg-reference>
  2222. =head1 AUTHOR
  2223. Tobias Oetiker E<lt>tobi@oetiker.chE<gt> and
  2224. Jakob Ilves E<lt>jakob.ilves@oracle.comE<gt>
  2225. =head1 LICENSE
  2226. GNU General Public License
  2227. =head1 COPYRIGHT
  2228. Cfgmaker is Copyright 2000 by Tobias Oetiker E<lt>tobi@oetiker.chE<gt>
  2229. =cut