Changeset 271


Ignore:
Timestamp:
Sep 15, 2017, 10:38:11 PM (7 years ago)
Author:
g7moreau
Message:
  • Rename port_num -> port_id and port_index -> port_ix
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r270 r271  
    527527         }
    528528
    529       my $swport_num = $result->{$research1};
    530       my $swport_hr = snmp_get_switchport_num2hr($session, $swport_num);
     529      my $swport_id = $result->{$research1};
     530      my $swport_hr = snmp_get_switchport_id2hr($session, $swport_id);
    531531
    532532      $session->close;
     
    547547      $ret{'switch_hostname'}    = $sw->{'hostname'};
    548548      $ret{'switch_description'} = $sw->{'description'};
    549       $ret{'switch_port_id'}     = $swport_num;
     549      $ret{'switch_port_id'}     = $swport_id;
    550550      $ret{'switch_port_hr'}     = $swport_hr; # human readable
    551551
     
    583583
    584584      if (defined $result and $result->{$research1} ne 'noSuchInstance') {
    585          my $swport_num = $result->{$research1};
    586          my $swport_hr = snmp_get_switchport_num2hr($session, $swport_num);
     585         my $swport_id = $result->{$research1};
     586         my $swport_hr = snmp_get_switchport_id2hr($session, $swport_id);
    587587
    588588         SWITCH_PORT_IGNORE:
     
    597597         $ret->{$sw->{'hostname'}}{'hostname'}    = $sw->{'hostname'};
    598598         $ret->{$sw->{'hostname'}}{'description'} = $sw->{'description'};
    599          $ret->{$sw->{'hostname'}}{'port'}        = $swport_num;
     599         $ret->{$sw->{'hostname'}}{'port'}        = $swport_id;
    600600         $ret->{$sw->{'hostname'}}{'port_hr'}     = $swport_hr;
    601601         }
     
    716716
    717717#---------------------------------------------------------------
    718 sub snmp_get_switchport_num2hr {
    719    my ($snmp_session, $swport_num) = @_;
    720 
    721    # On H3C, port number and port index are not the same
     718sub snmp_get_switchport_id2hr {
     719   my ($snmp_session, $swport_id) = @_;
     720
     721   # On H3C, port id (port_id) and port index (port_ix) are not the same
    722722   # Double SNMP request to get the name
    723    # First get the index, second get the name
    724 
    725    my $research_index = $OID_NUMBER{'ifIndex'} .'.'. $swport_num;
    726    my $result_index = $snmp_session->get_request(
    727       -varbindlist => [$research_index]
     723   # First get the index, second get the name (port_hr)
     724
     725   my $research_ix = $OID_NUMBER{'ifIndex'} .'.'. $swport_id;
     726   my $result_ix = $snmp_session->get_request(
     727      -varbindlist => [$research_ix]
    728728      );
    729729
    730    my $swport_index = $swport_num;
    731    $swport_index = $result_index->{$research_index} if defined $result_index;
    732 
    733    return snmp_get_switchport_index2hr($snmp_session, $swport_index);
    734    }
    735 
    736 #---------------------------------------------------------------
    737 sub snmp_get_switchport_index2hr {
    738    my ($snmp_session, $swport_index) = @_;
    739 
    740    my $research_hr = $OID_NUMBER{'ifName'} .'.'. $swport_index;
     730   my $swport_ix = $swport_id;
     731   $swport_ix = $result_ix->{$research_ix} if defined $result_ix;
     732
     733   return snmp_get_switchport_ix2hr($snmp_session, $swport_ix);
     734   }
     735
     736#---------------------------------------------------------------
     737sub snmp_get_switchport_ix2hr {
     738   my ($snmp_session, $swport_ix) = @_;
     739
     740   my $research_hr = $OID_NUMBER{'ifName'} .'.'. $swport_ix;
    741741   my $result_hr = $snmp_session->get_request(
    742742      -varbindlist => [$research_hr]
    743743      );
    744    my $swport_hr = $swport_index;
     744   my $swport_hr = $swport_ix;
    745745   $swport_hr = normalize_port_human_readable($result_hr->{$research_hr}) if defined $result_hr;
    746746
     
    757757         # IEEE8023-LAG-MIB::dot3adAggPortAttachedAggID.28 = INTEGER: 337
    758758         my $port_aggregator_index = $snmp_session->var_bind_list->{$oid_current};
    759          my ($current_port_index) = reverse split /\./, $oid_current; # last number
     759         my ($current_port_ix) = reverse split /\./, $oid_current; # last number
    760760
    761761         # prepare next loop item
     
    763763
    764764         next LOOP_ON_OID_PORT if $port_aggregator_index == 0;
    765          next LOOP_ON_OID_PORT if not $port_aggregator_index == $swport_index;
    766 
    767          my $current_port_name = snmp_get_switchport_index2hr($snmp_session, $current_port_index);
     765         next LOOP_ON_OID_PORT if not $port_aggregator_index == $swport_ix;
     766
     767         my $current_port_name = snmp_get_switchport_ix2hr($snmp_session, $current_port_ix);
    768768         $swport_hr .= "$SEP_AGGREGATOR_PORT$current_port_name";
    769769         }
     
    774774#---------------------------------------------------------------
    775775# Reverse search port number
    776 sub snmp_get_switchport_hr2num {
     776sub snmp_get_switchport_hr2id {
    777777   my ($snmp_session, $swport_hr, $verbose) = @_;
    778778
     
    781781   ($swport_hr) = split /$SEP_AGGREGATOR_PORT/, $swport_hr;
    782782
    783    my $swport_num = $swport_hr;
     783   my $swport_id = $swport_hr;
    784784   # direct return if already numeric (next loop is expensive) / old or simple switch
    785    return $swport_num if $swport_num =~ m/^\d+$/;
    786 
    787    my $research_index = $OID_NUMBER{'ifIndex'}; # base OID
    788    my @args = ( -varbindlist =>  [$research_index]);
     785   return $swport_id if $swport_id =~ m/^\d+$/;
     786
     787   my $research_ix = $OID_NUMBER{'ifIndex'}; # base OID
     788   my @args = ( -varbindlist =>  [$research_ix]);
    789789   LOOP_ON_OID_PORT:
    790790   while ( defined $snmp_session->get_next_request(@args) ) {
    791791      my ($oid_current) = $snmp_session->var_bind_names;
    792       last LOOP_ON_OID_PORT if  not Net::SNMP::oid_base_match($research_index, $oid_current);
     792      last LOOP_ON_OID_PORT if  not Net::SNMP::oid_base_match($research_ix, $oid_current);
    793793     
    794794      my $port_ifIndex = $snmp_session->var_bind_list->{$oid_current};
    795       my ($port_index) = reverse split /\./, $oid_current; # last number
     795      my ($port_ix) = reverse split /\./, $oid_current; # last number
    796796      printf "PORT1: %s => %s\n", $oid_current, $port_ifIndex if $verbose;
    797797
     
    806806      printf "PORT2: $oid_ifName => $current_port_hr\n" if $verbose;
    807807      if ($current_port_hr eq $swport_hr) {
    808          print "PORT3: $current_port_hr <-> $port_index\n" if $verbose;
     808         print "PORT3: $current_port_hr <-> $port_ix\n" if $verbose;
    809809         
    810810         # return port number ifIndex need by OID portUpDown
    811          $swport_num = $port_ifIndex; # other possible value could be $port_index
     811         $swport_id = $port_ifIndex; # other possible value could be $port_ix
    812812         last LOOP_ON_OID_PORT;
    813813         }
    814814      }
    815    return $swport_num;
     815   return $swport_id;
    816816   }
    817817
     
    23292329
    23302330   # Retrieve numeric port value
    2331    my $port_num = snmp_get_switchport_hr2num($session, normalize_port_human_readable($port), $verbose ? 'yes' : '');
    2332    die "Error : Port $port does not exist on switch $switch_name\n" if not $port_num =~ m/^\d+$/;
    2333 
    2334    my $search_portstatus = $OID_NUMBER{'portUpDown'} .'.'. $port_num;
     2331   my $port_id = snmp_get_switchport_hr2id($session, normalize_port_human_readable($port), $verbose ? 'yes' : '');
     2332   die "Error : Port $port does not exist on switch $switch_name\n" if not $port_id =~ m/^\d+$/;
     2333
     2334   my $search_portstatus = $OID_NUMBER{'portUpDown'} .'.'. $port_id;
    23352335   print "Info: switch $switch_name port $port SNMP OID $search_portstatus\n" if $verbose;
    23362336
     
    23742374
    23752375   # Retrieve numeric port value
    2376    my $port_num = snmp_get_switchport_hr2num($session, normalize_port_human_readable($port), $verbose ? 'yes' : '');
    2377    die "Error : Port $port does not exist on switch $switch_name\n" if not $port_num =~ m/^\d+$/;
    2378 
    2379    my $search_portstatus = $OID_NUMBER{'portUpDown'} .'.'. $port_num;
     2376   my $port_id = snmp_get_switchport_hr2id($session, normalize_port_human_readable($port), $verbose ? 'yes' : '');
     2377   die "Error : Port $port does not exist on switch $switch_name\n" if not $port_id =~ m/^\d+$/;
     2378
     2379   my $search_portstatus = $OID_NUMBER{'portUpDown'} .'.'. $port_id;
    23802380   print "Info: switch $switch_name port $port SNMP OID $search_portstatus\n" if $verbose;
    23812381
     
    24172417
    24182418   # Retrieve numeric port value
    2419    my $port_num = snmp_get_switchport_hr2num($session, normalize_port_human_readable($port), $verbose ? 'yes' : '');
    2420    die "Error : Port $port does not exist on switch $switch_name\n" if not $port_num =~ m/^\d+$/;
    2421 
    2422    my $search_portstatus = $OID_NUMBER{'portUpDown'} .'.'. $port_num;
    2423    print "Info: switch $switch_name port $port ($port_num) SNMP OID $search_portstatus\n" if $verbose;
     2419   my $port_id = snmp_get_switchport_hr2id($session, normalize_port_human_readable($port), $verbose ? 'yes' : '');
     2420   die "Error : Port $port does not exist on switch $switch_name\n" if not $port_id =~ m/^\d+$/;
     2421
     2422   my $search_portstatus = $OID_NUMBER{'portUpDown'} .'.'. $port_id;
     2423   print "Info: switch $switch_name port $port ($port_id) SNMP OID $search_portstatus\n" if $verbose;
    24242424
    24252425   my $result = $session->get_request(
     
    24842484
    24852485      if (defined $result and $result->{$research1} ne 'noSuchInstance') {
    2486          my $swport_num = $result->{$research1};
    2487          my $swport_hr = snmp_get_switchport_num2hr($session, $swport_num);
     2486         my $swport_id = $result->{$research1};
     2487         my $swport_hr = snmp_get_switchport_id2hr($session, $swport_id);
    24882488         print "Klask find MAC $mac_address on switch $sw_name port $swport_hr\n";
    24892489         }
Note: See TracChangeset for help on using the changeset viewer.