Changeset 271
- Timestamp:
- Sep 15, 2017, 10:38:11 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/klask
r270 r271 527 527 } 528 528 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); 531 531 532 532 $session->close; … … 547 547 $ret{'switch_hostname'} = $sw->{'hostname'}; 548 548 $ret{'switch_description'} = $sw->{'description'}; 549 $ret{'switch_port_id'} = $swport_ num;549 $ret{'switch_port_id'} = $swport_id; 550 550 $ret{'switch_port_hr'} = $swport_hr; # human readable 551 551 … … 583 583 584 584 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); 587 587 588 588 SWITCH_PORT_IGNORE: … … 597 597 $ret->{$sw->{'hostname'}}{'hostname'} = $sw->{'hostname'}; 598 598 $ret->{$sw->{'hostname'}}{'description'} = $sw->{'description'}; 599 $ret->{$sw->{'hostname'}}{'port'} = $swport_ num;599 $ret->{$sw->{'hostname'}}{'port'} = $swport_id; 600 600 $ret->{$sw->{'hostname'}}{'port_hr'} = $swport_hr; 601 601 } … … 716 716 717 717 #--------------------------------------------------------------- 718 sub snmp_get_switchport_ num2hr {719 my ($snmp_session, $swport_ num) = @_;720 721 # On H3C, port number and port indexare not the same718 sub 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 722 722 # Double SNMP request to get the name 723 # First get the index, second get the name 724 725 my $research_i ndex = $OID_NUMBER{'ifIndex'} .'.'. $swport_num;726 my $result_i ndex = $snmp_session->get_request(727 -varbindlist => [$research_i ndex]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] 728 728 ); 729 729 730 my $swport_i ndex = $swport_num;731 $swport_i ndex = $result_index->{$research_index} if defined $result_index;732 733 return snmp_get_switchport_i ndex2hr($snmp_session, $swport_index);734 } 735 736 #--------------------------------------------------------------- 737 sub snmp_get_switchport_i ndex2hr {738 my ($snmp_session, $swport_i ndex) = @_;739 740 my $research_hr = $OID_NUMBER{'ifName'} .'.'. $swport_i ndex;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 #--------------------------------------------------------------- 737 sub snmp_get_switchport_ix2hr { 738 my ($snmp_session, $swport_ix) = @_; 739 740 my $research_hr = $OID_NUMBER{'ifName'} .'.'. $swport_ix; 741 741 my $result_hr = $snmp_session->get_request( 742 742 -varbindlist => [$research_hr] 743 743 ); 744 my $swport_hr = $swport_i ndex;744 my $swport_hr = $swport_ix; 745 745 $swport_hr = normalize_port_human_readable($result_hr->{$research_hr}) if defined $result_hr; 746 746 … … 757 757 # IEEE8023-LAG-MIB::dot3adAggPortAttachedAggID.28 = INTEGER: 337 758 758 my $port_aggregator_index = $snmp_session->var_bind_list->{$oid_current}; 759 my ($current_port_i ndex) = reverse split /\./, $oid_current; # last number759 my ($current_port_ix) = reverse split /\./, $oid_current; # last number 760 760 761 761 # prepare next loop item … … 763 763 764 764 next LOOP_ON_OID_PORT if $port_aggregator_index == 0; 765 next LOOP_ON_OID_PORT if not $port_aggregator_index == $swport_i ndex;766 767 my $current_port_name = snmp_get_switchport_i ndex2hr($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); 768 768 $swport_hr .= "$SEP_AGGREGATOR_PORT$current_port_name"; 769 769 } … … 774 774 #--------------------------------------------------------------- 775 775 # Reverse search port number 776 sub snmp_get_switchport_hr2 num{776 sub snmp_get_switchport_hr2id { 777 777 my ($snmp_session, $swport_hr, $verbose) = @_; 778 778 … … 781 781 ($swport_hr) = split /$SEP_AGGREGATOR_PORT/, $swport_hr; 782 782 783 my $swport_ num= $swport_hr;783 my $swport_id = $swport_hr; 784 784 # 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_i ndex = $OID_NUMBER{'ifIndex'}; # base OID788 my @args = ( -varbindlist => [$research_i ndex]);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]); 789 789 LOOP_ON_OID_PORT: 790 790 while ( defined $snmp_session->get_next_request(@args) ) { 791 791 my ($oid_current) = $snmp_session->var_bind_names; 792 last LOOP_ON_OID_PORT if not Net::SNMP::oid_base_match($research_i ndex, $oid_current);792 last LOOP_ON_OID_PORT if not Net::SNMP::oid_base_match($research_ix, $oid_current); 793 793 794 794 my $port_ifIndex = $snmp_session->var_bind_list->{$oid_current}; 795 my ($port_i ndex) = reverse split /\./, $oid_current; # last number795 my ($port_ix) = reverse split /\./, $oid_current; # last number 796 796 printf "PORT1: %s => %s\n", $oid_current, $port_ifIndex if $verbose; 797 797 … … 806 806 printf "PORT2: $oid_ifName => $current_port_hr\n" if $verbose; 807 807 if ($current_port_hr eq $swport_hr) { 808 print "PORT3: $current_port_hr <-> $port_i ndex\n" if $verbose;808 print "PORT3: $current_port_hr <-> $port_ix\n" if $verbose; 809 809 810 810 # return port number ifIndex need by OID portUpDown 811 $swport_ num = $port_ifIndex; # other possible value could be $port_index811 $swport_id = $port_ifIndex; # other possible value could be $port_ix 812 812 last LOOP_ON_OID_PORT; 813 813 } 814 814 } 815 return $swport_ num;815 return $swport_id; 816 816 } 817 817 … … 2329 2329 2330 2330 # 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; 2335 2335 print "Info: switch $switch_name port $port SNMP OID $search_portstatus\n" if $verbose; 2336 2336 … … 2374 2374 2375 2375 # 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; 2380 2380 print "Info: switch $switch_name port $port SNMP OID $search_portstatus\n" if $verbose; 2381 2381 … … 2417 2417 2418 2418 # 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; 2424 2424 2425 2425 my $result = $session->get_request( … … 2484 2484 2485 2485 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); 2488 2488 print "Klask find MAC $mac_address on switch $sw_name port $swport_hr\n"; 2489 2489 }
Note: See TracChangeset
for help on using the changeset viewer.