Changeset 246


Ignore:
Timestamp:
Sep 8, 2017, 10:38:15 AM (7 years ago)
Author:
g7moreau
Message:
  • Better doc and rename some functions
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r245 r246  
    77use strict;
    88use warnings;
    9 use version; our $VERSION = qv('0.6.2');
     9use version; our $VERSION = qv('0.6.3');
    1010
    1111use Readonly;
     
    168168   sysContact      => '1.3.6.1.2.1.1.4.0',
    169169   sysLocation     => '1.3.6.1.2.1.1.6.0',
    170    searchPort1     => '1.3.6.1.2.1.17.4.3.1.2',       # BRIDGE-MIB (802.1D).
    171    searchPort2     => '1.3.6.1.2.1.17.7.1.2.2.1.2',   # Q-BRIDGE-MIB (802.1Q) add 0 if unknown vlan id
    172    vlanPortDefault => '1.3.6.1.2.1.17.7.1.4.5.1.1',   # dot1qPvid
    173    vlanStatus      => '1.3.6.1.2.1.17.7.1.4.3.1.5',   # integer 4 Create, 6 Destroy
    174    vlanName        => '1.3.6.1.2.1.17.7.1.4.3.1.1',   # string
    175    HPicfReset      => '1.3.6.1.4.1.11.2.14.11.1.4.1', # HP reboot switch
    176    ifIndex         => '1.3.6.1.2.1.17.1.4.1.2',       # dot1dBasePortIfIndex - Interface index redirection
    177    ifName          => '1.3.6.1.2.1.31.1.1.1.1',       # Interface name (give port number)
    178    portUpDown      => '1.3.6.1.2.1.2.2.1.7',          # 1.3.6.1.2.1.2.2.1.7.NoPort = 1 (up)  = 2 (down)
    179    poeState        => '1.3.6.1.2.1.105.1.1.1.3.1',    # 1.3.6.1.2.1.105.1.1.1.3.1.NoPort = 1 (poe up)  = 2 (poe down) - Cisco and Zyxel
    180    NApoeState      => '1.3.6.1.4.1.266.20.3.1.1.21',  # .NoPort = 2 (poe off)  = 8 (poe atHighPower) - Nexans
     170   searchPort1     => '1.3.6.1.2.1.17.4.3.1.2',          # BRIDGE-MIB (802.1D).
     171   searchPort2     => '1.3.6.1.2.1.17.7.1.2.2.1.2',      # Q-BRIDGE-MIB (802.1Q) add 0 if unknown vlan id
     172   vlanPortDefault => '1.3.6.1.2.1.17.7.1.4.5.1.1',      # dot1qPvid
     173   vlanStatus      => '1.3.6.1.2.1.17.7.1.4.3.1.5',      # integer 4 Create, 6 Destroy
     174   vlanName        => '1.3.6.1.2.1.17.7.1.4.3.1.1',      # string
     175   HPicfReset      => '1.3.6.1.4.1.11.2.14.11.1.4.1',    # HP reboot switch
     176   ifIndex         => '1.3.6.1.2.1.17.1.4.1.2',          # dot1dBasePortIfIndex - Interface index redirection
     177   ifName          => '1.3.6.1.2.1.31.1.1.1.1',          # Interface name (give port number)
     178   portUpDown      => '1.3.6.1.2.1.2.2.1.7',             # 1.3.6.1.2.1.2.2.1.7.NoPort = 1 (up)  = 2 (down)
     179   poeState        => '1.3.6.1.2.1.105.1.1.1.3.1',       # 1.3.6.1.2.1.105.1.1.1.3.1.NoPort = 1 (poe up)  = 2 (poe down) - Cisco and Zyxel
     180   NApoeState      => '1.3.6.1.4.1.266.20.3.1.1.21',     # .NoPort = 2 (poe off)  = 8 (poe atHighPower) - Nexans
    181181   ifAggregator    => '1.2.840.10006.300.43.1.2.1.1.12', # dot3adAggPortSelectedAggID - 0 not part of an  Aggregator - Ciso Dell HP Comware -  See https://stackoverflow.com/questions/14960157/how-to-map-portchannel-to-interfaces-via-snmp https://gist.github.com/bldewolf/6314435
    182182   );
     
    191191
    192192Readonly my $RE_FLOAT_HOSTNAME => $DEFAULT{'float-regex'} || qr{ ^float }xms;
     193
     194Readonly my $SEP_AGGREGATOR_PORT => '|';  # : is already use to join switch and port
     195
    193196
    194197################################################################
     
    341344   }
    342345
     346#---------------------------------------------------------------
    343347# Find Surname of a switch
    344348sub get_switch_model {
     
    416420
    417421#---------------------------------------------------------------
    418 # convertit l'hexa (uniquement 2 chiffres) en decimal
    419 sub digit_hex_to_dec {
     422# convert hexa (only 2 digits) to decimal
     423sub digit_hex2dec {
    420424   #00:0F:1F:43:E4:2B
    421425   my $car = '00' . uc shift;
     
    448452
    449453#---------------------------------------------------------------
    450 # convertit l'@ mac en decimal
    451 sub mac_address_hex_to_dec {
     454# convert MAC hex address to decimal
     455sub mac_address_hex2dec {
    452456   #00:0F:1F:43:E4:2B
    453457   my $mac_address = shift;
     
    456460   my $return = q{};
    457461   for (@paquets) {
    458       $return .= q{.} . digit_hex_to_dec($_);
     462      $return .= q{.} . digit_hex2dec($_);
    459463      }
    460464   return $return;
     
    462466
    463467#---------------------------------------------------------------
    464 # va rechercher le port et le switch sur lequel est la machine
     468# return the port and the switch where the computer is connected
    465469sub find_switch_port {
    466470   my $mac_address     = shift;
     
    483487      }
    484488
    485    my $research1 = $OID_NUMBER{'searchPort1'} . mac_address_hex_to_dec($mac_address);
    486    my $research2 = $OID_NUMBER{'searchPort2'} .'.'. $vlan_id . mac_address_hex_to_dec($mac_address);
     489   my $research1 = $OID_NUMBER{'searchPort1'} . mac_address_hex2dec($mac_address);
     490   my $research2 = $OID_NUMBER{'searchPort2'} .'.'. $vlan_id . mac_address_hex2dec($mac_address);
    487491
    488492   LOOP_ON_SWITCH:
     
    520524      for my $portignore (@{$sw->{portignore}}) {
    521525         next LOOP_ON_SWITCH if $swport_hr eq $portignore;
    522          my ($swport_hr_limited) = split /|/, $swport_hr; # Beginning of the swith port (Aggregator)
     526         my ($swport_hr_limited) = split /$SEP_AGGREGATOR_PORT/, $swport_hr; # Beginning of the swith port (Aggregator)
    523527         next LOOP_ON_SWITCH if $swport_hr_limited eq $portignore;
    524528         }
     
    535539
    536540#---------------------------------------------------------------
    537 # va rechercher les port et les switch sur lequel est la machine
     541# search all the port on all the switches where the computer is detected
    538542sub find_all_switch_port {
    539543   my $mac_address = shift;
     
    544548   return $ret if $mac_address eq 'unknow';
    545549
    546    my $research1 = $OID_NUMBER{'searchPort1'} . mac_address_hex_to_dec($mac_address);
    547    my $research2 = $OID_NUMBER{'searchPort2'} .'.'. $vlan_id . mac_address_hex_to_dec($mac_address);
     550   my $research1 = $OID_NUMBER{'searchPort1'} . mac_address_hex2dec($mac_address);
     551   my $research2 = $OID_NUMBER{'searchPort2'} .'.'. $vlan_id . mac_address_hex2dec($mac_address);
    548552   LOOP_ON_ALL_SWITCH:
    549553   for my $sw (@SWITCH_LIST) {
     
    578582         $ret->{$sw->{hostname}}{port}        = $swport_num;
    579583         $ret->{$sw->{hostname}}{port_hr}     = $swport_hr;
    580 
    581 #         $SWITCH_PORT_COUNT{$sw->{hostname}}->{$swport}++;
    582584         }
    583585
     
    702704   # On H3C, port number and port index are not the same
    703705   # Double SNMP request to get the name
     706   # First get the index, second get the name
    704707
    705708   my $research_index = $OID_NUMBER{ifIndex} .'.'. $swport_num;
     
    746749
    747750         my $current_port_name = snmp_get_switchport_index2hr($snmp_session, $current_port_index);
    748          $swport_hr .= "|$current_port_name"; # Join with | (: is already use)
     751         $swport_hr .= "$SEP_AGGREGATOR_PORT$current_port_name";
    749752         }
    750753      }
     
    759762   # Split for Aggregator port
    760763   # Keep only the Aggregator part
    761    ($swport_hr) = split /|/, $swport_hr;
     764   ($swport_hr) = split /$SEP_AGGREGATOR_PORT/, $swport_hr;
    762765
    763766   my $swport_num = $swport_hr;
    764    return $swport_num if $swport_num =~ m/^\d+$/; # direct return if already numeric (next loop is expensive)
     767   # direct return if already numeric (next loop is expensive) / old or simple switch
     768   return $swport_num if $swport_num =~ m/^\d+$/;
    765769
    766770   my $research_index = $OID_NUMBER{ifIndex}; # base OID
     
    18511855         }
    18521856
    1853       my $research1 = $OID_NUMBER{'searchPort1'} . mac_address_hex_to_dec($mac_address);
    1854       my $research2 = $OID_NUMBER{'searchPort2'} .'.'. 0 . mac_address_hex_to_dec($mac_address);
     1857      my $research1 = $OID_NUMBER{'searchPort1'} . mac_address_hex2dec($mac_address);
     1858      my $research2 = $OID_NUMBER{'searchPort2'} .'.'. 0 . mac_address_hex2dec($mac_address);
    18551859      print "Klask search OID $research1 on switch $sw_name\n";
    18561860      print "Klask search OID $research2 on switch $sw_name\n";
     
    23632367         }
    23642368
    2365       my $research1 = $OID_NUMBER{'searchPort1'} . mac_address_hex_to_dec($mac_address);
    2366       my $research2 = $OID_NUMBER{'searchPort2'} .'.'. $vlan_id . mac_address_hex_to_dec($mac_address);
     2369      my $research1 = $OID_NUMBER{'searchPort1'} . mac_address_hex2dec($mac_address);
     2370      my $research2 = $OID_NUMBER{'searchPort2'} .'.'. $vlan_id . mac_address_hex2dec($mac_address);
    23672371      print "Klask search OID $research1 on switch $sw_name\n" if $verbose;
    23682372      print "Klask search OID $research2 on switch $sw_name\n" if $verbose;
     
    28242828=head1 DESCRIPTION
    28252829
    2826 Klask is a small tool to find where is a host in a big network.
     2830Klask is a small tool to find where is connected a host in a big network
     2831and on which VLAN.
    28272832Klask mean search in brittany.
    28282833No hight level protocol like CDL, LLDP are use.
     
    31143119and also on input command line.
    31153120
     3121In the case of use an aggregator port (Po, Tk, Br ...),
     3122the real ports used are also return.
     3123
    31163124
    31173125=head1 FILES
Note: See TracChangeset for help on using the changeset viewer.