Changeset 261 for trunk/klask
- Timestamp:
- Sep 14, 2017, 8:50:26 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/klask
r260 r261 45 45 LEVEL_OF_EACH_SWITCH: 46 46 for my $sw (@SWITCH_LIST) { 47 $SWITCH_LEVEL{$sw->{ hostname}} = $sw->{'level'} || $DEFAULT{'switch_level'} || 2;48 $SWITCH_DB{$sw->{ hostname}} = $sw;47 $SWITCH_LEVEL{$sw->{'hostname'}} = $sw->{'level'} || $DEFAULT{'switch_level'} || 2; 48 $SWITCH_DB{$sw->{'hostname'}} = $sw; 49 49 50 50 # SNMP parameter initialisation 51 my %session = ( -hostname => $sw->{ hostname} );51 my %session = ( -hostname => $sw->{'hostname'} ); 52 52 $session{-version} = $sw->{'version'} || 1; 53 53 $session{-port} = $sw->{'snmpport'} || $DEFAULT{'snmpport'} || 161; … … 60 60 $sw->{'snmp_param_session'} = \%session; 61 61 } 62 @SWITCH_LIST = reverse sort { $SWITCH_LEVEL{$a->{ hostname}} <=> $SWITCH_LEVEL{$b->{hostname}} } @{$KLASK_CFG->{'switch'}};62 @SWITCH_LIST = reverse sort { $SWITCH_LEVEL{$a->{'hostname'}} <=> $SWITCH_LEVEL{$b->{'hostname'}} } @{$KLASK_CFG->{'switch'}}; 63 63 64 64 #my %SWITCH_PORT_COUNT = (); … … 273 273 my $packed_ip = scalar gethostbyname($param_ip_or_host); 274 274 return %ret if not defined $packed_ip; 275 $ret{ ipv4_address} = inet_ntoa($packed_ip);276 #if ($ret{ ipv4_address} !~ m/$RE_IPv4_ADDRESS/) {277 # print "Error: for computer $param_ip_or_host on interface $interface, IP $ret{ ipv4_address} is not valide\n";275 $ret{'ipv4_address'} = inet_ntoa($packed_ip); 276 #if ($ret{'ipv4_address'} !~ m/$RE_IPv4_ADDRESS/) { 277 # print "Error: for computer $param_ip_or_host on interface $interface, IP $ret{'ipv4_address'} is not valide\n"; 278 278 # return %ret; 279 279 # } … … 281 281 # perl -MSocket -E 'say scalar gethostbyaddr(inet_aton("194.254.66.240"), AF_INET)' 282 282 my $hostname_fq = scalar gethostbyaddr($packed_ip, AF_INET) if $already eq 'yes'; 283 $ret{ hostname_fq} = $hostname_fq if defined $hostname_fq;283 $ret{'hostname_fq'} = $hostname_fq if defined $hostname_fq; 284 284 285 285 # my $cmd = q{grep -he '\b} . $param_ip_or_host . q{\b' } . "/var/lib/arpwatch/$interface.dat | sort -rn -k 3,3 | head -1"; 286 #my $cmd = q{grep -he '\b} . $ret{ ipv4_address} . q{\b' } . "/var/lib/arpwatch/$interface.dat | sort -rn -k 3,3 | head -1";287 my $cmd = q{grep -He '\b} . $ret{ ipv4_address} . q{\b' } . "/var/lib/arpwatch/$interface.dat" . '| sed -e \'s|^/var/lib/arpwatch/\(.*\)\.dat:|\1 |;\' | sort -rn -k 4,4 | head -1';286 #my $cmd = q{grep -he '\b} . $ret{'ipv4_address'} . q{\b' } . "/var/lib/arpwatch/$interface.dat | sort -rn -k 3,3 | head -1"; 287 my $cmd = q{grep -He '\b} . $ret{'ipv4_address'} . q{\b' } . "/var/lib/arpwatch/$interface.dat" . '| sed -e \'s|^/var/lib/arpwatch/\(.*\)\.dat:|\1 |;\' | sort -rn -k 4,4 | head -1'; 288 288 #grep -He 194.254.66.252 /var/lib/arpwatch/*.dat | sed -e 's|^/var/lib/arpwatch/\(.*\)\.dat:|\1\t|;' | sort -rn -k 4,4 | head -1 289 289 … … 292 292 my ($interface2, $arp, $ip, $timestamp, $host) = split m/ \s+ /xms, $cmd_arpwatch; 293 293 294 $ret{ interface} = $interface2 || $interface;295 $ret{ mac_address} = $arp if $arp;296 $ret{ timestamp} = $timestamp if $timestamp;294 $ret{'interface'} = $interface2 || $interface; 295 $ret{'mac_address'} = $arp if $arp; 296 $ret{'timestamp'} = $timestamp if $timestamp; 297 297 298 298 my $nowtimestamp = time; 299 299 300 300 if ( $type eq 'fast' and ( not defined $timestamp or $timestamp < ( $nowtimestamp - 45 * 60 ) ) ) { # 45 min 301 $ret{ mac_address} = 'unknow';301 $ret{'mac_address'} = 'unknow'; 302 302 return %ret; 303 303 } … … 312 312 # LANG=C ip neigh show to 194.254.66.62 313 313 # 194.254.66.62 dev eth331 lladdr 00:08:7c:bb:0f:c0 REACHABLE 314 # my $cmd_arp = shell_command "arp -a $param_ip_or_host -i $ret{ interface}";314 # my $cmd_arp = shell_command "arp -a $param_ip_or_host -i $ret{'interface'}"; 315 315 # if ( $cmd_arp =~ m{ (\S*) \s \( ( $RE_IPv4_ADDRESS ) \) \s at \s ( $RE_MAC_ADDRESS ) }xms ) { 316 # ( $ret{ hostname_fq}, $ret{ipv4_address}, $ret{mac_address} ) = ($1, $2, $3);316 # ( $ret{'hostname_fq'}, $ret{'ipv4_address'}, $ret{'mac_address'} ) = ($1, $2, $3); 317 317 # } 318 if ($ret{ mac_address} eq 'unknow') {318 if ($ret{'mac_address'} eq 'unknow') { 319 319 # Last chance to have the mac_address 320 if ($ret{ interface} eq '*') {321 my $cmd_arp = shell_command "ip neigh show to $ret{ ipv4_address}";320 if ($ret{'interface'} eq '*') { 321 my $cmd_arp = shell_command "ip neigh show to $ret{'ipv4_address'}"; 322 322 if ( $cmd_arp =~ m{ ^$RE_IPv4_ADDRESS \s dev \s ([\w\d\.\:]+) \s lladdr \s ( $RE_MAC_ADDRESS ) \s }xms ) { 323 ($ret{ interface}, $ret{mac_address}) = ($1, $2);323 ($ret{'interface'}, $ret{'mac_address'}) = ($1, $2); 324 324 } 325 325 } 326 326 else { 327 my $cmd_arp = shell_command "ip neigh show to $ret{ ipv4_address} dev $ret{interface}";327 my $cmd_arp = shell_command "ip neigh show to $ret{'ipv4_address'} dev $ret{'interface'}"; 328 328 if ( $cmd_arp =~ m{ ^$RE_IPv4_ADDRESS \s lladdr \s ( $RE_MAC_ADDRESS ) \s }xms ) { 329 $ret{ mac_address} = $1;329 $ret{'mac_address'} = $1; 330 330 } 331 331 } … … 333 333 334 334 # Normalize MAC Address 335 if ($ret{ mac_address} ne 'unknow') {335 if ($ret{'mac_address'} ne 'unknow') { 336 336 my @paquets = (); 337 for ( split m/ : /xms, $ret{ mac_address} ) {337 for ( split m/ : /xms, $ret{'mac_address'} ) { 338 338 my @chars = split m//xms, uc "00$_"; 339 339 push @paquets, "$chars[-2]$chars[-1]"; 340 340 } 341 $ret{ mac_address} = join q{:}, @paquets;341 $ret{'mac_address'} = join q{:}, @paquets; 342 342 } 343 343 … … 375 375 my $result = $session->get_request( 376 376 -varbindlist => [ 377 $OID_NUMBER{ sysDescription},378 $OID_NUMBER{ sysName},379 $OID_NUMBER{ sysContact},380 $OID_NUMBER{ sysLocation},377 $OID_NUMBER{'sysDescription'}, 378 $OID_NUMBER{'sysName'}, 379 $OID_NUMBER{'sysContact'}, 380 $OID_NUMBER{'sysLocation'}, 381 381 ] 382 382 ); … … 385 385 $session->close(); 386 386 # Remove bad switch 387 @SWITCH_LIST = grep { $_->{ hostname} ne $sw->{hostname} } @SWITCH_LIST;388 delete $SWITCH_LEVEL{$sw->{ hostname}} if exists $SWITCH_LEVEL{$sw->{hostname}};389 delete $SWITCH_DB{$sw->{ hostname}} if exists $SWITCH_DB{$sw->{hostname}};387 @SWITCH_LIST = grep { $_->{'hostname'} ne $sw->{'hostname'} } @SWITCH_LIST; 388 delete $SWITCH_LEVEL{$sw->{'hostname'}} if exists $SWITCH_LEVEL{$sw->{'hostname'}}; 389 delete $SWITCH_DB{$sw->{'hostname'}} if exists $SWITCH_DB{$sw->{'hostname'}}; 390 390 next INIT_EACH_SWITCH; 391 391 } 392 392 393 $sw->{description} = $result->{$OID_NUMBER{ sysName}} || $sw->{hostname};394 $sw->{model} = get_switch_model($result->{$OID_NUMBER{ sysDescription}});393 $sw->{description} = $result->{$OID_NUMBER{'sysName'}} || $sw->{'hostname'}; 394 $sw->{model} = get_switch_model($result->{$OID_NUMBER{'sysDescription'}}); 395 395 if ($verb_description) { 396 my $desc = $result->{$OID_NUMBER{ sysDescription}};396 my $desc = $result->{$OID_NUMBER{'sysDescription'}}; 397 397 $desc =~ s/[\n\r]/ /g; 398 398 print " description: $desc\n" 399 399 } 400 400 if ($check_hostname) { 401 my ($hostname) = split /\./, $sw->{ hostname}, 2;402 print " $hostname - error internal hostname: $sw->{ hostname}\n" if $result->{$OID_NUMBER{sysName}} ne $hostname;401 my ($hostname) = split /\./, $sw->{'hostname'}, 2; 402 print " $hostname - error internal hostname: $sw->{'hostname'}\n" if $result->{$OID_NUMBER{'sysName'}} ne $hostname; 403 403 } 404 404 if ($check_location) { 405 my $location = $result->{$OID_NUMBER{ sysLocation}};405 my $location = $result->{$OID_NUMBER{'sysLocation'}}; 406 406 $location =~ s/^"(.+)"$/$1/; 407 print " $sw->{ hostname} - error location: '$location' -> '$sw->{location}'\n" if $location ne $sw->{location};408 } 409 #$sw->{ location} = $result->{"1.3.6.1.2.1.1.6.0"} || $sw->{hostname};410 #$sw->{contact} = $result->{"1.3.6.1.2.1.1.4.0"} || $sw->{ hostname};407 print " $sw->{'hostname'} - error location: '$location' -> '$sw->{'location'}'\n" if $location ne $sw->{'location'}; 408 } 409 #$sw->{'location'} = $result->{"1.3.6.1.2.1.1.6.0"} || $sw->{'hostname'}; 410 #$sw->{contact} = $result->{"1.3.6.1.2.1.1.4.0"} || $sw->{'hostname'}; 411 411 $session->close; 412 412 413 413 # Ligne à virer car on récupère maintenant le modèle du switch 414 414 #my ($desc, $type) = split m/ : /xms, $sw->{description}, 2; 415 printf "%-26s 0--------->>>> %-25s %s\n", $sw->{ hostname}, $sw->{description}, $sw->{model} if $verbose;415 printf "%-26s 0--------->>>> %-25s %s\n", $sw->{'hostname'}, $sw->{description}, $sw->{model} if $verbose; 416 416 } 417 417 … … 490 490 491 491 my %ret; 492 $ret{ switch_description} = 'unknow';493 $ret{ switch_port} = '0';492 $ret{'switch_description'} = 'unknow'; 493 $ret{'switch_port'} = '0'; 494 494 495 495 return %ret if $mac_address eq 'unknow';; … … 498 498 if ($switch_proposal ne q{}) { 499 499 for my $sw (@SWITCH_LIST) { 500 next if $sw->{ hostname} ne $switch_proposal;500 next if $sw->{'hostname'} ne $switch_proposal; 501 501 unshift @switch_search, $sw; 502 502 last; … … 545 545 } 546 546 547 $ret{ switch_hostname} = $sw->{hostname};548 $ret{ switch_description} = $sw->{description};549 $ret{ switch_port} = $swport_num;550 $ret{ switch_port_hr} = $swport_hr; # human readable547 $ret{'switch_hostname'} = $sw->{'hostname'}; 548 $ret{'switch_description'} = $sw->{description}; 549 $ret{'switch_port'} = $swport_num; 550 $ret{'switch_port_hr'} = $swport_hr; # human readable 551 551 552 552 last LOOP_ON_SWITCH; … … 594 594 } 595 595 596 $ret->{$sw->{ hostname}} = {};597 $ret->{$sw->{ hostname}}{hostname} = $sw->{hostname};598 $ret->{$sw->{ hostname}}{description} = $sw->{description};599 $ret->{$sw->{ hostname}}{port} = $swport_num;600 $ret->{$sw->{ hostname}}{port_hr} = $swport_hr;596 $ret->{$sw->{'hostname'}} = {}; 597 $ret->{$sw->{'hostname'}}{'hostname'} = $sw->{'hostname'}; 598 $ret->{$sw->{'hostname'}}{description} = $sw->{description}; 599 $ret->{$sw->{'hostname'}}{port} = $swport_num; 600 $ret->{$sw->{'hostname'}}{'port_hr'} = $swport_hr; 601 601 } 602 602 … … 616 616 my $vlan_name = shift; 617 617 618 return $KLASK_CFG->{network}{$vlan_name}{ interface};618 return $KLASK_CFG->{network}{$vlan_name}{'interface'}; 619 619 } 620 620 … … 639 639 640 640 for my $vlan_name (keys %{$KLASK_CFG->{network}}) { 641 next if $KLASK_CFG->{network}{$vlan_name}{ interface} ne $interface;641 next if $KLASK_CFG->{network}{$vlan_name}{'interface'} ne $interface; 642 642 return $vlan_name; 643 643 } … … 723 723 # First get the index, second get the name 724 724 725 my $research_index = $OID_NUMBER{ ifIndex} .'.'. $swport_num;725 my $research_index = $OID_NUMBER{'ifIndex'} .'.'. $swport_num; 726 726 my $result_index = $snmp_session->get_request( 727 727 -varbindlist => [$research_index] … … 738 738 my ($snmp_session, $swport_index) = @_; 739 739 740 my $research_hr = $OID_NUMBER{ ifName} .'.'. $swport_index;740 my $research_hr = $OID_NUMBER{'ifName'} .'.'. $swport_index; 741 741 my $result_hr = $snmp_session->get_request( 742 742 -varbindlist => [$research_hr] … … 747 747 # Aggregator port 748 748 if ($swport_hr =~ m/^(Trk|Br|Po)/) { 749 my $research_index = $OID_NUMBER{ ifAggregator}; # base OID749 my $research_index = $OID_NUMBER{'ifAggregator'}; # base OID 750 750 my @args = ( -varbindlist => [$research_index]); 751 751 LOOP_ON_OID_PORT: … … 785 785 return $swport_num if $swport_num =~ m/^\d+$/; 786 786 787 my $research_index = $OID_NUMBER{ ifIndex}; # base OID787 my $research_index = $OID_NUMBER{'ifIndex'}; # base OID 788 788 my @args = ( -varbindlist => [$research_index]); 789 789 LOOP_ON_OID_PORT: … … 799 799 @args = (-varbindlist => [$oid_current]); 800 800 801 my $oid_ifName = $OID_NUMBER{ ifName} .'.'. $port_ifIndex;801 my $oid_ifName = $OID_NUMBER{'ifName'} .'.'. $port_ifIndex; 802 802 my $result = $snmp_session->get_request(-varbindlist => [$oid_ifName]); 803 803 next LOOP_ON_OID_PORT if not defined $result; … … 823 823 my %vlan = (); # Hash vlan number => vlan name 824 824 825 my $research_index = $OID_NUMBER{ vlanName}; # base OID825 my $research_index = $OID_NUMBER{'vlanName'}; # base OID 826 826 my @args = ( -varbindlist => [$research_index]); 827 827 LOOP_ON_VLAN: … … 850 850 for my $ip (keys %{$computerdb}) { 851 851 852 next LOOP_ON_IP_ADDRESS if exists $computerdb->{$ip}{ switch_port_hr} and defined $computerdb->{$ip}{switch_port_hr};853 854 $computerdb->{$ip}{ switch_port_hr} = $computerdb->{$ip}{switch_port};852 next LOOP_ON_IP_ADDRESS if exists $computerdb->{$ip}{'switch_port_hr'} and defined $computerdb->{$ip}{'switch_port_hr'}; 853 854 $computerdb->{$ip}{'switch_port_hr'} = $computerdb->{$ip}{'switch_port'}; 855 855 } 856 856 … … 923 923 for my $clientname (@computer) { 924 924 my %resol_arp = resolve_ip_arp_host($clientname); #resolution arp 925 my $vlan_name = get_current_vlan_name_for_interface($resol_arp{ interface});925 my $vlan_name = get_current_vlan_name_for_interface($resol_arp{'interface'}); 926 926 my $vlan_id = get_current_vlan_id($vlan_name); 927 my %where = find_switch_port($resol_arp{ mac_address}, '', $vlan_id); #retrouve l'emplacement928 929 next LOOP_ON_COMPUTER if $where{ switch_description} eq 'unknow' or $resol_arp{hostname_fq} eq 'unknow' or $resol_arp{mac_address} eq 'unknow';927 my %where = find_switch_port($resol_arp{'mac_address'}, '', $vlan_id); #retrouve l'emplacement 928 929 next LOOP_ON_COMPUTER if $where{'switch_description'} eq 'unknow' or $resol_arp{'hostname_fq'} eq 'unknow' or $resol_arp{'mac_address'} eq 'unknow'; 930 930 931 931 printf '%-22s %2s %-30s %-15s %18s', 932 $where{ switch_hostname},933 $where{ switch_port_hr},934 $resol_arp{ hostname_fq},935 $resol_arp{ ipv4_address},936 $resol_arp{ mac_address}."\n";932 $where{'switch_hostname'}, 933 $where{'switch_port_hr'}, 934 $resol_arp{'hostname_fq'}, 935 $resol_arp{'ipv4_address'}, 936 $resol_arp{'mac_address'}."\n"; 937 937 } 938 938 return; … … 971 971 for my $clientname (@computer) { 972 972 my %resol_arp = resolve_ip_arp_host($clientname); #resolution arp 973 my $ip = $resol_arp{ ipv4_address};973 my $ip = $resol_arp{'ipv4_address'}; 974 974 975 975 next LOOP_ON_COMPUTER unless exists $computerdb->{$ip}; 976 976 977 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{ timestamp};977 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{'timestamp'}; 978 978 $year += 1900; 979 979 $mon++; … … 981 981 982 982 printf "%-22s %2s %-30s %-15s %-18s %s\n", 983 $computerdb->{$ip}{ switch_hostname},984 $computerdb->{$ip}{ switch_port_hr},985 $computerdb->{$ip}{ hostname_fq},983 $computerdb->{$ip}{'switch_hostname'}, 984 $computerdb->{$ip}{'switch_port_hr'}, 985 $computerdb->{$ip}{'hostname_fq'}, 986 986 $ip, 987 $computerdb->{$ip}{ mac_address},987 $computerdb->{$ip}{'mac_address'}, 988 988 $date; 989 989 } … … 1001 1001 LOOP_ON_COMPUTER: 1002 1002 for my $ip (keys %{$computerdb}) { 1003 next LOOP_ON_COMPUTER if $mac ne $computerdb->{$ip}{ mac_address};1004 1005 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{ timestamp};1003 next LOOP_ON_COMPUTER if $mac ne $computerdb->{$ip}{'mac_address'}; 1004 1005 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{'timestamp'}; 1006 1006 $year += 1900; 1007 1007 $mon++; … … 1009 1009 1010 1010 printf "%-22s %2s %-30s %-15s %-18s %s\n", 1011 $computerdb->{$ip}{ switch_hostname},1012 $computerdb->{$ip}{ switch_port_hr},1013 $computerdb->{$ip}{ hostname_fq},1011 $computerdb->{$ip}{'switch_hostname'}, 1012 $computerdb->{$ip}{'switch_port_hr'}, 1013 $computerdb->{$ip}{'hostname_fq'}, 1014 1014 $ip, 1015 $computerdb->{$ip}{ mac_address},1015 $computerdb->{$ip}{'mac_address'}, 1016 1016 $date; 1017 1017 #next LOOP_ON_MAC; … … 1057 1057 { # Remplis le champs portignore des ports d'inter-connection pour chaque switch 1058 1058 my $switch_connection = YAML::Syck::LoadFile("$KLASK_SW_FILE"); 1059 my %db_switch_output_port = %{$switch_connection->{ output_port}};1060 my %db_switch_connected_on_port = %{$switch_connection->{ connected_on_port}};1059 my %db_switch_output_port = %{$switch_connection->{'output_port'}}; 1060 my %db_switch_connected_on_port = %{$switch_connection->{'connected_on_port'}}; 1061 1061 my %db_switch_chained_port = (); 1062 1062 for my $swport (keys %db_switch_connected_on_port) { … … 1065 1065 } 1066 1066 for my $sw (@SWITCH_LIST) { 1067 push @{$sw->{portignore}}, $db_switch_output_port{$sw->{ hostname}} if exists $db_switch_output_port{$sw->{hostname}};1068 if ( exists $db_switch_chained_port{$sw->{ hostname}} ) {1069 chop $db_switch_chained_port{$sw->{ hostname}};1070 push @{$sw->{portignore}}, split m/ : /xms, $db_switch_chained_port{$sw->{ hostname}};1071 } 1072 # print "$sw->{ hostname} ++ @{$sw->{portignore}}\n";1067 push @{$sw->{portignore}}, $db_switch_output_port{$sw->{'hostname'}} if exists $db_switch_output_port{$sw->{'hostname'}}; 1068 if ( exists $db_switch_chained_port{$sw->{'hostname'}} ) { 1069 chop $db_switch_chained_port{$sw->{'hostname'}}; 1070 push @{$sw->{portignore}}, split m/ : /xms, $db_switch_chained_port{$sw->{'hostname'}}; 1071 } 1072 # print "$sw->{'hostname'} ++ @{$sw->{portignore}}\n"; 1073 1073 } 1074 1074 } … … 1079 1079 for my $one_router ( get_list_main_router(@network) ) { 1080 1080 my %resol_arp = resolve_ip_arp_host($one_router); 1081 $router_mac_ip{ $resol_arp{ mac_address} } = $resol_arp{ipv4_address};1081 $router_mac_ip{ $resol_arp{'mac_address'} } = $resol_arp{'ipv4_address'}; 1082 1082 } 1083 1083 … … 1112 1112 1113 1113 # do not search on router connection (why ?) 1114 if ( exists $router_mac_ip{$resol_arp{ mac_address}}) {1114 if ( exists $router_mac_ip{$resol_arp{'mac_address'}}) { 1115 1115 $computer_not_detected{$one_computer} = $current_net; 1116 1116 next LOOP_ON_COMPUTER; … … 1118 1118 1119 1119 # do not search on switch inter-connection 1120 if (exists $SWITCH_LEVEL{$resol_arp{ hostname_fq}}) {1120 if (exists $SWITCH_LEVEL{$resol_arp{'hostname_fq'}}) { 1121 1121 $computer_not_detected{$one_computer} = $current_net; 1122 1122 next LOOP_ON_COMPUTER; … … 1124 1124 1125 1125 my $switch_proposal = q{}; 1126 if (exists $computerdb->{$resol_arp{ ipv4_address}} and exists $computerdb->{$resol_arp{ipv4_address}}{switch_hostname}) {1127 $switch_proposal = $computerdb->{$resol_arp{ ipv4_address}}{switch_hostname};1126 if (exists $computerdb->{$resol_arp{'ipv4_address'}} and exists $computerdb->{$resol_arp{'ipv4_address'}}{'switch_hostname'}) { 1127 $switch_proposal = $computerdb->{$resol_arp{'ipv4_address'}}{'switch_hostname'}; 1128 1128 } 1129 1129 1130 1130 # do not have a mac address 1131 if ($resol_arp{ mac_address} eq 'unknow' or (exists $resol_arp{timestamps} and $resol_arp{timestamps} < ($timestamp - 3 * 3600))) {1131 if ($resol_arp{'mac_address'} eq 'unknow' or (exists $resol_arp{'timestamps'} and $resol_arp{'timestamps'} < ($timestamp - 3 * 3600))) { 1132 1132 $computer_not_detected{$one_computer} = $current_net; 1133 1133 next LOOP_ON_COMPUTER; 1134 1134 } 1135 1135 1136 my $vlan_name = get_current_vlan_name_for_interface($resol_arp{ interface});1136 my $vlan_name = get_current_vlan_name_for_interface($resol_arp{'interface'}); 1137 1137 my $vlan_id = get_current_vlan_id($vlan_name); 1138 my %where = find_switch_port($resol_arp{ mac_address},$switch_proposal,$vlan_id);1138 my %where = find_switch_port($resol_arp{'mac_address'}, $switch_proposal,$vlan_id); 1139 1139 1140 1140 #192.168.24.156: … … 1146 1146 1147 1147 # do not have a mac address 1148 # if ($resol_arp{ mac_address} eq 'unknow') {1148 # if ($resol_arp{'mac_address'} eq 'unknow') { 1149 1149 # $computer_not_detected{$one_computer} = $current_interface; 1150 1150 # next LOOP_ON_COMPUTER; … … 1152 1152 1153 1153 # detected on a switch 1154 if ($where{ switch_description} ne 'unknow') {1154 if ($where{'switch_description'} ne 'unknow') { 1155 1155 $detected_computer++; 1156 $computerdb->{$resol_arp{ ipv4_address}} = {1157 hostname_fq => $resol_arp{ hostname_fq},1158 mac_address => $resol_arp{ mac_address},1159 switch_hostname => $where{ switch_hostname},1160 switch_description => $where{ switch_description},1161 switch_port => $where{ switch_port},1162 switch_port_hr => $where{ switch_port_hr},1156 $computerdb->{$resol_arp{'ipv4_address'}} = { 1157 hostname_fq => $resol_arp{'hostname_fq'}, 1158 mac_address => $resol_arp{'mac_address'}, 1159 switch_hostname => $where{'switch_hostname'}, 1160 switch_description => $where{'switch_description'}, 1161 switch_port => $where{'switch_port'}, 1162 switch_port_hr => $where{'switch_port_hr'}, 1163 1163 timestamp => $timestamp, 1164 1164 network => $current_net, … … 1168 1168 1169 1169 # new in the database but where it is ? 1170 if (not exists $computerdb->{$resol_arp{ ipv4_address}}) {1170 if (not exists $computerdb->{$resol_arp{'ipv4_address'}}) { 1171 1171 $detected_computer++; 1172 $computerdb->{$resol_arp{ ipv4_address}} = {1173 hostname_fq => $resol_arp{ hostname_fq},1174 mac_address => $resol_arp{ mac_address},1175 switch_hostname => $where{ switch_hostname},1176 switch_description => $where{ switch_description},1177 switch_port => $where{ switch_port},1178 switch_port_hr => $where{ switch_port_hr},1179 timestamp => $resol_arp{ timestamp},1172 $computerdb->{$resol_arp{'ipv4_address'}} = { 1173 hostname_fq => $resol_arp{'hostname_fq'}, 1174 mac_address => $resol_arp{'mac_address'}, 1175 switch_hostname => $where{'switch_hostname'}, 1176 switch_description => $where{'switch_description'}, 1177 switch_port => $where{'switch_port'}, 1178 switch_port_hr => $where{'switch_port_hr'}, 1179 timestamp => $resol_arp{'timestamp'}, 1180 1180 network => $current_net, 1181 1181 }; … … 1183 1183 1184 1184 # mise a jour du nom de la machine si modification dans le dns 1185 $computerdb->{$resol_arp{ ipv4_address}}{hostname_fq} = $resol_arp{hostname_fq};1185 $computerdb->{$resol_arp{'ipv4_address'}}{'hostname_fq'} = $resol_arp{'hostname_fq'}; 1186 1186 1187 1187 # mise à jour de la date de détection si détection plus récente par arpwatch 1188 $computerdb->{$resol_arp{ ipv4_address}}{timestamp} = $resol_arp{timestamp} if exists $resol_arp{timestamp} and $computerdb->{$resol_arp{ipv4_address}}{timestamp} < $resol_arp{timestamp};1188 $computerdb->{$resol_arp{'ipv4_address'}}{'timestamp'} = $resol_arp{'timestamp'} if exists $resol_arp{'timestamp'} and $computerdb->{$resol_arp{'ipv4_address'}}{'timestamp'} < $resol_arp{'timestamp'}; 1189 1189 1190 1190 # relance un arping sur la machine si celle-ci n'a pas été détectée depuis plus d'une semaine 1191 # push @computer_not_detected, $resol_arp{ ipv4_address} if $computerdb->{$resol_arp{ipv4_address}}{timestamp} < $timestamp_last_week;1192 $computer_not_detected{$resol_arp{ ipv4_address}} = $current_net if $computerdb->{$resol_arp{ipv4_address}}{timestamp} < $timestamp_last_week;1191 # push @computer_not_detected, $resol_arp{'ipv4_address'} if $computerdb->{$resol_arp{'ipv4_address'}}{'timestamp'} < $timestamp_last_week; 1192 $computer_not_detected{$resol_arp{'ipv4_address'}} = $current_net if $computerdb->{$resol_arp{'ipv4_address'}}{'timestamp'} < $timestamp_last_week; 1193 1193 1194 1194 } … … 1230 1230 my %resol_arp = resolve_ip_arp_host($one_computer); 1231 1231 1232 delete $computerdb->{$resol_arp{ ipv4_address}} if exists $computerdb->{$resol_arp{ipv4_address}};1232 delete $computerdb->{$resol_arp{'ipv4_address'}} if exists $computerdb->{$resol_arp{'ipv4_address'}}; 1233 1233 } 1234 1234 … … 1275 1275 not exists $computerdb->{$ip}; 1276 1276 1277 #&& $computerdb->{$ip}{ timestamp} > $timestamp_barrier;1278 my $ip_timestamp = $computerdb->{$ip}{ timestamp};1279 my $ip_mac = $computerdb->{$ip}{ mac_address};1280 my $ip_hostname_fq = $computerdb->{$ip}{ hostname_fq};1277 #&& $computerdb->{$ip}{'timestamp'} > $timestamp_barrier; 1278 my $ip_timestamp = $computerdb->{$ip}{'timestamp'}; 1279 my $ip_mac = $computerdb->{$ip}{'mac_address'}; 1280 my $ip_hostname_fq = $computerdb->{$ip}{'hostname_fq'}; 1281 1281 1282 1282 $mactimedb{$ip_mac} ||= { … … 1288 1288 1289 1289 if ( 1290 ( $mactimedb{$ip_mac}->{ timestamp} - $ip_timestamp > $timestamp_barrier1290 ( $mactimedb{$ip_mac}->{'timestamp'} - $ip_timestamp > $timestamp_barrier 1291 1291 or ( 1292 $mactimedb{$ip_mac}->{ timestamp} > $ip_timestamp1293 and $timestamp - $mactimedb{$ip_mac}->{ timestamp} > $timestamp_3month1292 $mactimedb{$ip_mac}->{'timestamp'} > $ip_timestamp 1293 and $timestamp - $mactimedb{$ip_mac}->{'timestamp'} > $timestamp_3month 1294 1294 ) 1295 1295 ) 1296 1296 and ( 1297 not $mactimedb{$ip_mac}->{ hostname_fq} =~ m/$RE_FLOAT_HOSTNAME/1297 not $mactimedb{$ip_mac}->{'hostname_fq'} =~ m/$RE_FLOAT_HOSTNAME/ 1298 1298 or $ip_hostname_fq =~ m/$RE_FLOAT_HOSTNAME/ 1299 1299 )) { … … 1304 1304 1305 1305 elsif ( 1306 ( $ip_timestamp - $mactimedb{$ip_mac}->{ timestamp} > $timestamp_barrier1306 ( $ip_timestamp - $mactimedb{$ip_mac}->{'timestamp'} > $timestamp_barrier 1307 1307 or ( 1308 $ip_timestamp > $mactimedb{$ip_mac}->{ timestamp}1308 $ip_timestamp > $mactimedb{$ip_mac}->{'timestamp'} 1309 1309 and $timestamp - $ip_timestamp > $timestamp_3month 1310 1310 ) … … 1312 1312 and ( 1313 1313 not $ip_hostname_fq =~ m/$RE_FLOAT_HOSTNAME/ 1314 or $mactimedb{$ip_mac}->{ hostname_fq} =~ m/$RE_FLOAT_HOSTNAME/1314 or $mactimedb{$ip_mac}->{'hostname_fq'} =~ m/$RE_FLOAT_HOSTNAME/ 1315 1315 )) { 1316 1316 print "remove ip ".$mactimedb{$ip_mac}->{ip}."\n" if $verbose; … … 1319 1319 } 1320 1320 1321 if ( $ip_timestamp > $mactimedb{$ip_mac}->{ timestamp}) {1321 if ( $ip_timestamp > $mactimedb{$ip_mac}->{'timestamp'}) { 1322 1322 $mactimedb{$ip_mac} = { 1323 1323 ip => $ip, … … 1333 1333 LOOP_ON_IP_ADDRESS: 1334 1334 for my $ip (keys %{$computerdb}) { 1335 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{ hostname_fq} ne 'unknow';1335 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'hostname_fq'} ne 'unknow'; 1336 1336 1337 1337 my $packed_ip = scalar gethostbyname($ip); … … 1341 1341 next LOOP_ON_IP_ADDRESS if not defined $hostname_fq; 1342 1342 1343 $computerdb->{$ip}{ hostname_fq} = $hostname_fq;1343 $computerdb->{$ip}{'hostname_fq'} = $hostname_fq; 1344 1344 $database_has_changed++; 1345 1345 } … … 1389 1389 1390 1390 # to be improve in the future 1391 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{ hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself !1392 1393 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{ timestamp};1391 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'hostname_fq'} eq ($computerdb->{$ip}{'switch_hostname'} || $computerdb->{$ip}{'switch_description'}); # switch on himself ! 1392 1393 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{'timestamp'}; 1394 1394 $year += 1900; 1395 1395 $mon++; … … 1400 1400 1401 1401 my $arrow ='<-----------'; 1402 $arrow ='<===========' if $computerdb->{$ip}{ switch_port_hr} =~ m/^(Trk|Br|Po)/;1402 $arrow ='<===========' if $computerdb->{$ip}{'switch_port_hr'} =~ m/^(Trk|Br|Po)/; 1403 1403 1404 1404 printf "%-28s %8s %12s %-40s %-15s %-18s %-16s %s\n", 1405 $computerdb->{$ip}{ switch_hostname} || $computerdb->{$ip}{switch_description},1406 $computerdb->{$ip}{ switch_port_hr},1405 $computerdb->{$ip}{'switch_hostname'} || $computerdb->{$ip}{'switch_description'}, 1406 $computerdb->{$ip}{'switch_port_hr'}, 1407 1407 $arrow, 1408 $computerdb->{$ip}{ hostname_fq},1408 $computerdb->{$ip}{'hostname_fq'}, 1409 1409 $ip, 1410 $computerdb->{$ip}{ mac_address},1410 $computerdb->{$ip}{'mac_address'}, 1411 1411 $date, 1412 1412 $vlan; … … 1459 1459 1460 1460 # to be improve in the future 1461 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{ hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself !1462 1463 $mac_count{$computerdb->{$ip}{ mac_address}}++;1461 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'hostname_fq'} eq ($computerdb->{$ip}{'switch_hostname'} || $computerdb->{$ip}{'switch_description'}); # switch on himself ! 1462 1463 $mac_count{$computerdb->{$ip}{'mac_address'}}++; 1464 1464 } 1465 1465 … … 1470 1470 1471 1471 # to be improve in the future 1472 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{ hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself !1473 1474 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{ timestamp};1472 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'hostname_fq'} eq ($computerdb->{$ip}{'switch_hostname'} || $computerdb->{$ip}{'switch_description'}); # switch on himself ! 1473 1474 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{'timestamp'}; 1475 1475 $year += 1900; 1476 1476 $mon++; … … 1482 1482 1483 1483 my $arrow ='←'; 1484 $arrow ='⇐' if $computerdb->{$ip}{ switch_port_hr} =~ m/^(Trk|Br|Po)/;1485 1486 my $switch_hostname = $computerdb->{$ip}{ switch_hostname} || $computerdb->{$ip}{switch_description} || 'unkown';1484 $arrow ='⇐' if $computerdb->{$ip}{'switch_port_hr'} =~ m/^(Trk|Br|Po)/; 1485 1486 my $switch_hostname = $computerdb->{$ip}{'switch_hostname'} || $computerdb->{$ip}{'switch_description'} || 'unkown'; 1487 1487 chomp $switch_hostname; 1488 my $switch_hostname_sort = sprintf '%s %3s' ,$switch_hostname, $computerdb->{$ip}{ switch_port_hr};1488 my $switch_hostname_sort = sprintf '%s %3s' ,$switch_hostname, $computerdb->{$ip}{'switch_port_hr'}; 1489 1489 1490 1490 my $ip_sort = sprintf '%03i%03i%03i%03i', split m/ \. /xms, $ip; 1491 1491 1492 my $mac_sort = sprintf '%04i-%s', 9999 - $mac_count{$computerdb->{$ip}{ mac_address}}, $computerdb->{$ip}{mac_address};1493 1494 $computerdb->{$ip}{ hostname_fq} = 'unknow' if $computerdb->{$ip}{hostname_fq} =~ m/^ \d+ \. \d+ \. \d+ \. \d+ $/xms;1495 my ( $host_short ) = split m/ \. /xms, $computerdb->{$ip}{ hostname_fq};1492 my $mac_sort = sprintf '%04i-%s', 9999 - $mac_count{$computerdb->{$ip}{'mac_address'}}, $computerdb->{$ip}{'mac_address'}; 1493 1494 $computerdb->{$ip}{'hostname_fq'} = 'unknow' if $computerdb->{$ip}{'hostname_fq'} =~ m/^ \d+ \. \d+ \. \d+ \. \d+ $/xms; 1495 my ( $host_short ) = split m/ \. /xms, $computerdb->{$ip}{'hostname_fq'}; 1496 1496 1497 1497 my $vlan = ''; 1498 1498 $vlan = $computerdb->{$ip}{network}.' ('.get_current_vlan_id($computerdb->{$ip}{network}).')' if $computerdb->{$ip}{network}; 1499 1499 1500 my $parent_port_hr = format_aggregator4html($computerdb->{$ip}{ switch_port_hr});1500 my $parent_port_hr = format_aggregator4html($computerdb->{$ip}{'switch_port_hr'}); 1501 1501 1502 1502 print <<"END_HTML"; … … 1505 1505 <td class="bklask-port">$parent_port_hr</td> 1506 1506 <td colspan="2">$arrow</td> 1507 <td sorttable_customkey="$host_short">$computerdb->{$ip}{ hostname_fq}</td>1507 <td sorttable_customkey="$host_short">$computerdb->{$ip}{'hostname_fq'}</td> 1508 1508 <td sorttable_customkey="$ip_sort">$ip</td> 1509 <td sorttable_customkey="$mac_sort">$computerdb->{$ip}{ mac_address}</td>1509 <td sorttable_customkey="$mac_sort">$computerdb->{$ip}{'mac_address'}</td> 1510 1510 <td>$vlan</td> 1511 1511 <td>$date</td> … … 1516 1516 my $switch_connection = YAML::Syck::LoadFile("$KLASK_SW_FILE"); 1517 1517 1518 my %db_switch_output_port = %{$switch_connection->{ output_port}};1519 my %db_switch_parent = %{$switch_connection->{ parent}};1520 my %db_switch_connected_on_port = %{$switch_connection->{ connected_on_port}};1521 my %db_switch = %{$switch_connection->{ switch_db}};1518 my %db_switch_output_port = %{$switch_connection->{'output_port'}}; 1519 my %db_switch_parent = %{$switch_connection->{'parent'}}; 1520 my %db_switch_connected_on_port = %{$switch_connection->{'connected_on_port'}}; 1521 my %db_switch = %{$switch_connection->{'switch_db'}}; 1522 1522 1523 1523 for my $sw (sort keys %db_switch_output_port) { … … 1531 1531 1532 1532 if (exists $db_switch_parent{$sw}) { 1533 my $mac_address = $db_switch{$db_switch_parent{$sw}->{'switch'}}->{mac_address};1534 my $ipv4_address = $db_switch{$db_switch_parent{$sw}->{'switch'}}->{ ipv4_address};1535 my $timestamp = $db_switch{$db_switch_parent{$sw}->{'switch'}}->{timestamp};1533 my $mac_address = $db_switch{$db_switch_parent{$sw}->{'switch'}}->{'mac_address'}; 1534 my $ipv4_address = $db_switch{$db_switch_parent{$sw}->{'switch'}}->{'ipv4_address'}; 1535 my $timestamp = $db_switch{$db_switch_parent{$sw}->{'switch'}}->{'timestamp'}; 1536 1536 1537 1537 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $timestamp; … … 1544 1544 my $mac_sort = sprintf '%04i-%s', 9999, $mac_address; 1545 1545 1546 my ( $host_short ) = sprintf '%s %3s' , split(m/ \. /xms, $db_switch_parent{$sw}->{'switch'}, 1), $db_switch_parent{$sw}->{ port_hr};1546 my ( $host_short ) = sprintf '%s %3s' , split(m/ \. /xms, $db_switch_parent{$sw}->{'switch'}, 1), $db_switch_parent{$sw}->{'port_hr'}; 1547 1547 1548 1548 my $parent_port_hr = format_aggregator4html($db_switch_output_port{$sw}); 1549 my $child_port_hr = format_aggregator4html($db_switch_parent{$sw}->{ port_hr});1549 my $child_port_hr = format_aggregator4html($db_switch_parent{$sw}->{'port_hr'}); 1550 1550 1551 1551 print <<"END_HTML"; … … 1586 1586 my $switch_hostname_sort = sprintf '%s %3s' ,$sw_connect, $port_connect; 1587 1587 1588 my $mac_address = $db_switch{$sw}->{ mac_address};1589 my $ipv4_address = $db_switch{$sw}->{ ipv4_address};1590 my $timestamp = $db_switch{$sw}->{ timestamp};1588 my $mac_address = $db_switch{$sw}->{'mac_address'}; 1589 my $ipv4_address = $db_switch{$sw}->{'ipv4_address'}; 1590 my $timestamp = $db_switch{$sw}->{'timestamp'}; 1591 1591 1592 1592 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $timestamp; … … 1670 1670 for my $ip (keys %{$computerdb}) { 1671 1671 # to be improve in the future 1672 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{ hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself !1673 next LOOP_ON_IP_ADDRESS if ($computerdb->{$ip}{ switch_hostname} || $computerdb->{$ip}{switch_description}) eq 'unknow';1674 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{ switch_port} eq '0';1675 1676 my $ip_timestamp = $computerdb->{$ip}{ timestamp};1677 my $ip_mac = $computerdb->{$ip}{ mac_address};1678 my $ip_hostname_fq = $computerdb->{$ip}{ hostname_fq};1672 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'hostname_fq'} eq ($computerdb->{$ip}{'switch_hostname'} || $computerdb->{$ip}{'switch_description'}); # switch on himself ! 1673 next LOOP_ON_IP_ADDRESS if ($computerdb->{$ip}{'switch_hostname'} || $computerdb->{$ip}{'switch_description'}) eq 'unknow'; 1674 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'switch_port'} eq '0'; 1675 1676 my $ip_timestamp = $computerdb->{$ip}{'timestamp'}; 1677 my $ip_mac = $computerdb->{$ip}{'mac_address'}; 1678 my $ip_hostname_fq = $computerdb->{$ip}{'hostname_fq'}; 1679 1679 1680 1680 my $swpt = sprintf "%-28s %2s", 1681 $computerdb->{$ip}{ switch_hostname} || $computerdb->{$ip}{switch_description},1682 $computerdb->{$ip}{ switch_port_hr};1681 $computerdb->{$ip}{'switch_hostname'} || $computerdb->{$ip}{'switch_description'}, 1682 $computerdb->{$ip}{'switch_port_hr'}; 1683 1683 $switchportdb{$swpt} ||= { 1684 1684 ip => $ip, … … 1691 1691 # if float computer, set date 15 day before warning... 1692 1692 my $ip_timestamp_mod = $ip_timestamp; 1693 my $ip_timestamp_ref = $switchportdb{$swpt}->{ timestamp};1693 my $ip_timestamp_ref = $switchportdb{$swpt}->{'timestamp'}; 1694 1694 $ip_timestamp_mod -= $days_before_alert * 24 * 3600 if $ip_hostname_fq =~ m/$RE_FLOAT_HOSTNAME/; 1695 $ip_timestamp_ref -= $days_before_alert * 24 * 3600 if $switchportdb{$swpt}->{ hostname_fq} =~ m/$RE_FLOAT_HOSTNAME/;1695 $ip_timestamp_ref -= $days_before_alert * 24 * 3600 if $switchportdb{$swpt}->{'hostname_fq'} =~ m/$RE_FLOAT_HOSTNAME/; 1696 1696 1697 1697 if ($ip_timestamp_mod > $ip_timestamp_ref) { … … 1709 1709 for my $swpt (keys %switchportdb) { 1710 1710 next LOOP_ON_RECENT_COMPUTER if $swpt =~ m/^\s*0$/; 1711 next LOOP_ON_RECENT_COMPUTER if $switchportdb{$swpt}->{ hostname_fq} !~ m/$RE_FLOAT_HOSTNAME/;1711 next LOOP_ON_RECENT_COMPUTER if $switchportdb{$swpt}->{'hostname_fq'} !~ m/$RE_FLOAT_HOSTNAME/; 1712 1712 1713 1713 my $src_ip = $switchportdb{$swpt}->{ip}; … … 1715 1715 LOOP_ON_IP_ADDRESS: 1716 1716 for my $ip (keys %{$computerdb}) { 1717 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{ mac_address} ne $switchportdb{$swpt}->{mac_address};1718 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{ hostname_fq} =~ m/$RE_FLOAT_HOSTNAME/;1719 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{ timestamp} < $src_timestamp;1717 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'mac_address'} ne $switchportdb{$swpt}->{'mac_address'}; 1718 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'hostname_fq'} =~ m/$RE_FLOAT_HOSTNAME/; 1719 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'timestamp'} < $src_timestamp; 1720 1720 1721 1721 $src_ip = $ip; 1722 $src_timestamp = $computerdb->{$ip}{ timestamp};1722 $src_timestamp = $computerdb->{$ip}{'timestamp'}; 1723 1723 } 1724 1724 1725 1725 # keep only if float computer is the most recent 1726 1726 next LOOP_ON_RECENT_COMPUTER if $src_timestamp == 0; 1727 next LOOP_ON_RECENT_COMPUTER if $switchportdb{$swpt}->{ timestamp} < $src_timestamp;1728 1729 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $switchportdb{$swpt}->{ timestamp};1727 next LOOP_ON_RECENT_COMPUTER if $switchportdb{$swpt}->{'timestamp'} < $src_timestamp; 1728 1729 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $switchportdb{$swpt}->{'timestamp'}; 1730 1730 $year += 1900; 1731 1731 $mon++; 1732 1732 my $date = sprintf '%04i-%02i-%02i/%02i:%02i', $year, $mon, $mday, $hour, $min; 1733 1733 1734 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$src_ip}{ timestamp};1734 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$src_ip}{'timestamp'}; 1735 1735 $year += 1900; 1736 1736 $mon++; … … 1740 1740 1741 1741 printf "%s / %-10s +-> %-10s(%i) %s %s %s %s\n", 1742 $swpt, $switchportdb{$swpt}->{ vlan}, $computerdb->{$src_ip}{network}, $vlan_id,1742 $swpt, $switchportdb{$swpt}->{'vlan'}, $computerdb->{$src_ip}{network}, $vlan_id, 1743 1743 $date, 1744 1744 $src_date, 1745 $computerdb->{$src_ip}{ mac_address},1746 $computerdb->{$src_ip}{ hostname_fq};1745 $computerdb->{$src_ip}{'mac_address'}, 1746 $computerdb->{$src_ip}{'hostname_fq'}; 1747 1747 } 1748 1748 } … … 1880 1880 } 1881 1881 1882 $switch_name = join(',', map {$_->{ hostname}} @SWITCH_LIST ) if $switch_name eq q{*};1882 $switch_name = join(',', map {$_->{'hostname'}} @SWITCH_LIST ) if $switch_name eq q{*}; 1883 1883 1884 1884 for my $sw_name (split /,/, $switch_name) { … … 2010 2010 } 2011 2011 2012 $switch_name = join(',', map {$_->{ hostname}} @SWITCH_LIST ) if $switch_name eq q{*};2012 $switch_name = join(',', map {$_->{'hostname'}} @SWITCH_LIST ) if $switch_name eq q{*}; 2013 2013 2014 2014 for my $sw_name (split /,/, $switch_name) { … … 2017 2017 } 2018 2018 2019 my $search_vlan_name = $OID_NUMBER{ vlanName} . ".$vlan_id";2019 my $search_vlan_name = $OID_NUMBER{'vlanName'} . ".$vlan_id"; 2020 2020 2021 2021 my $sw = $SWITCH_DB{$sw_name}; … … 2048 2048 } 2049 2049 2050 $switch_name = join(',', map {$_->{ hostname}} @SWITCH_LIST ) if $switch_name eq q{*};2050 $switch_name = join(',', map {$_->{'hostname'}} @SWITCH_LIST ) if $switch_name eq q{*}; 2051 2051 2052 2052 for my $sw_name (split /,/, $switch_name) { … … 2077 2077 for my $ip (Net::Netmask::sort_by_ip_address(keys %{$computerdb})) { 2078 2078 2079 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{ hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself !2080 2081 my $sw_hostname = $computerdb->{$ip}{ switch_hostname} || q{};2079 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'hostname_fq'} eq ($computerdb->{$ip}{'switch_hostname'} || $computerdb->{$ip}{'switch_description'}); # switch on himself ! 2080 2081 my $sw_hostname = $computerdb->{$ip}{'switch_hostname'} || q{}; 2082 2082 next LOOP_ON_IP_ADDRESS if $sw_hostname eq 'unknow'; 2083 2083 … … 2085 2085 LOOP_ON_ALL_SWITCH: 2086 2086 for my $sw (@SWITCH_LIST) { 2087 next LOOP_ON_ALL_SWITCH if $sw_hostname ne $sw->{ hostname};2088 $sw_location = $sw->{ location};2087 next LOOP_ON_ALL_SWITCH if $sw_hostname ne $sw->{'hostname'}; 2088 $sw_location = $sw->{'location'}; 2089 2089 last; 2090 2090 } … … 2136 2136 2137 2137 if (exists $computerdb->{$ip}) { 2138 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{ timestamp} > $timestamp_barrier;2139 2140 my $mac_address = $computerdb->{$ip}{ mac_address};2138 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'timestamp'} > $timestamp_barrier; 2139 2140 my $mac_address = $computerdb->{$ip}{'mac_address'}; 2141 2141 LOOP_ON_DATABASE: 2142 2142 for my $ip_db (keys %{$computerdb}) { 2143 next LOOP_ON_DATABASE if $computerdb->{$ip_db}{ mac_address} ne $mac_address;2144 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip_db}{ timestamp} > $timestamp_barrier;2143 next LOOP_ON_DATABASE if $computerdb->{$ip_db}{'mac_address'} ne $mac_address; 2144 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip_db}{'timestamp'} > $timestamp_barrier; 2145 2145 } 2146 2146 } … … 2148 2148 my $ip_date_last_detection = ''; 2149 2149 if (exists $computerdb->{$ip}) { 2150 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{ timestamp};2150 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{'timestamp'}; 2151 2151 $year += 1900; 2152 2152 $mon++; … … 2161 2161 2162 2162 $result_ip{$ip} ||= {}; 2163 $result_ip{$ip}->{ date_last_detection} = $ip_date_last_detection;2164 $result_ip{$ip}->{ hostname_fq} = $hostname_fq;2165 $result_ip{$ip}->{ vlan} = $vlan;2163 $result_ip{$ip}->{'date_last_detection'} = $ip_date_last_detection; 2164 $result_ip{$ip}->{'hostname_fq'} = $hostname_fq; 2165 $result_ip{$ip}->{'vlan'} = $vlan; 2166 2166 2167 2167 printf "VERBOSE_1: %-15s %-12s %s\n", $ip, $vlan, $hostname_fq if $verbose; … … 2180 2180 LOOP_ON_IP_ADDRESS: 2181 2181 for my $ip (Net::Netmask::sort_by_ip_address(keys %result_ip)) { 2182 my $vlan_nameid = $result_ip{$ip}->{ vlan}.'('.get_current_vlan_id($result_ip{$ip}->{vlan}).')';2183 printf "%-15s %-40s %-16s %s\n", $ip, $result_ip{$ip}->{ hostname_fq}, $result_ip{$ip}->{date_last_detection}, $vlan_nameid;2182 my $vlan_nameid = $result_ip{$ip}->{'vlan'}.'('.get_current_vlan_id($result_ip{$ip}->{'vlan'}).')'; 2183 printf "%-15s %-40s %-16s %s\n", $ip, $result_ip{$ip}->{'hostname_fq'}, $result_ip{$ip}->{'date_last_detection'}, $vlan_nameid; 2184 2184 } 2185 2185 } … … 2219 2219 2220 2220 my $ip_sort = sprintf '%03i%03i%03i%03i', split m/ \. /xms, $ip; 2221 my ( $host_short ) = split m/ \. /xms, $result_ip{$ip}->{ hostname_fq};2222 2223 my $vlan_nameid = $result_ip{$ip}->{ vlan}.'('.get_current_vlan_id($result_ip{$ip}->{vlan}).')';2221 my ( $host_short ) = split m/ \. /xms, $result_ip{$ip}->{'hostname_fq'}; 2222 2223 my $vlan_nameid = $result_ip{$ip}->{'vlan'}.'('.get_current_vlan_id($result_ip{$ip}->{'vlan'}).')'; 2224 2224 2225 2225 print <<"END_HTML"; 2226 2226 <tr class="$typerow"> 2227 2227 <td sorttable_customkey="$ip_sort">$ip</td> 2228 <td sorttable_customkey="$host_short">$result_ip{$ip}->{ hostname_fq}</td>2228 <td sorttable_customkey="$host_short">$result_ip{$ip}->{'hostname_fq'}</td> 2229 2229 <td>$vlan_nameid</td> 2230 <td>$result_ip{$ip}->{ date_last_detection}</td>2230 <td>$result_ip{$ip}->{'date_last_detection'}</td> 2231 2231 </tr> 2232 2232 END_HTML … … 2392 2392 2393 2393 $mac_address = normalize_mac_address($mac_address); 2394 $switch_name = join(',', map {$_->{ hostname}} @SWITCH_LIST ) if $switch_name eq q{*} or $switch_name eq q{all};2394 $switch_name = join(',', map {$_->{'hostname'}} @SWITCH_LIST ) if $switch_name eq q{*} or $switch_name eq q{all}; 2395 2395 2396 2396 for my $sw_name (split /,/, $switch_name) { … … 2454 2454 my %resol_arp = resolve_ip_arp_host($one_router, q{*}, q{low}); # resolution arp 2455 2455 2456 next DETECT_ALL_ROUTER if $resol_arp{ mac_address} eq 'unknow';2457 print "VERBOSE_1: Router detected $resol_arp{ ipv4_address} - $resol_arp{mac_address}\n" if $verbose;2458 2459 my $vlan_name = get_current_vlan_name_for_interface($resol_arp{ interface});2456 next DETECT_ALL_ROUTER if $resol_arp{'mac_address'} eq 'unknow'; 2457 print "VERBOSE_1: Router detected $resol_arp{'ipv4_address'} - $resol_arp{'mac_address'}\n" if $verbose; 2458 2459 my $vlan_name = get_current_vlan_name_for_interface($resol_arp{'interface'}); 2460 2460 my $vlan_id = get_current_vlan_id($vlan_name); 2461 $where{$resol_arp{ ipv4_address}} = find_all_switch_port($resol_arp{mac_address},$vlan_id); # retrouve les emplacements des routeurs2461 $where{$resol_arp{'ipv4_address'}} = find_all_switch_port($resol_arp{'mac_address'},$vlan_id); # retrouve les emplacements des routeurs 2462 2462 } 2463 2463 … … 2474 2474 next ALL_SWITCH_CONNECTED if $switch->{port} eq '0'; 2475 2475 2476 $db_switch_output_port{$switch->{ hostname}} = $switch->{port_hr};2477 print "VERBOSE_2: output port $switch->{ hostname} : $switch->{port_hr}\n" if $verbose;2476 $db_switch_output_port{$switch->{'hostname'}} = $switch->{'port_hr'}; 2477 print "VERBOSE_2: output port $switch->{'hostname'} : $switch->{'port_hr'}\n" if $verbose; 2478 2478 } 2479 2479 } … … 2484 2484 my @list_switch_ipv4 = (); 2485 2485 for my $sw (@SWITCH_LIST) { 2486 push @list_all_switch, $sw->{ hostname};2486 push @list_all_switch, $sw->{'hostname'}; 2487 2487 } 2488 2488 … … 2495 2495 my $fake_ip = $SWITCH_DB{$one_switch}{'fake-ip'}; 2496 2496 fast_ping($fake_ip); 2497 print "WARNING: fake ip on switch $one_switch -> $fake_ip / $resol_arp{ ipv4_address}\n" if $verbose;2497 print "WARNING: fake ip on switch $one_switch -> $fake_ip / $resol_arp{'ipv4_address'}\n" if $verbose; 2498 2498 my %resol_arp_alt = resolve_ip_arp_host($fake_ip, q{*}, q{low}); # arp resolution 2499 if ($resol_arp_alt{ mac_address} ne 'unknow') {2500 $resol_arp{ mac_address} = $resol_arp_alt{mac_address};2501 $resol_arp{ interface} = $resol_arp_alt{interface};2502 $resol_arp{ ipv4_address} .= '*';2499 if ($resol_arp_alt{'mac_address'} ne 'unknow') { 2500 $resol_arp{'mac_address'} = $resol_arp_alt{'mac_address'}; 2501 $resol_arp{'interface'} = $resol_arp_alt{'interface'}; 2502 $resol_arp{'ipv4_address'} .= '*'; 2503 2503 # Force a MAC trace on switch 2504 system "arping -c 1 -w 1 -rR -i $resol_arp_alt{ interface} $fake_ip > /dev/null 2>&1";2504 system "arping -c 1 -w 1 -rR -i $resol_arp_alt{'interface'} $fake_ip > /dev/null 2>&1"; 2505 2505 } 2506 2506 } 2507 2507 print "Info: switch loop $one_switch\n" if $verbose; 2508 next ALL_SWITCH if $resol_arp{ mac_address} eq 'unknow';2509 2510 push @list_switch_ipv4, $resol_arp{ ipv4_address};2511 2512 my $vlan_name = get_current_vlan_name_for_interface($resol_arp{ interface});2508 next ALL_SWITCH if $resol_arp{'mac_address'} eq 'unknow'; 2509 2510 push @list_switch_ipv4, $resol_arp{'ipv4_address'}; 2511 2512 my $vlan_name = get_current_vlan_name_for_interface($resol_arp{'interface'}); 2513 2513 my $vlan_id = get_current_vlan_id($vlan_name); 2514 $where{$resol_arp{ ipv4_address}} = find_all_switch_port($resol_arp{mac_address},$vlan_id); # find port on all switch2514 $where{$resol_arp{'ipv4_address'}} = find_all_switch_port($resol_arp{'mac_address'},$vlan_id); # find port on all switch 2515 2515 2516 2516 if ($verbose) { 2517 print "VERBOSE_3: $one_switch $resol_arp{ ipv4_address} $resol_arp{mac_address}\n";2517 print "VERBOSE_3: $one_switch $resol_arp{'ipv4_address'} $resol_arp{'mac_address'}\n"; 2518 2518 print "VERBOSE_3: $one_switch --- ", 2519 join(' + ', keys %{$where{$resol_arp{ ipv4_address}}}),2519 join(' + ', keys %{$where{$resol_arp{'ipv4_address'}}}), 2520 2520 "\n"; 2521 2521 } 2522 2522 2523 $db_switch_ip_hostnamefq{$resol_arp{ ipv4_address}} = $resol_arp{hostname_fq};2524 print "VERBOSE_4: db_switch_ip_hostnamefq $resol_arp{ ipv4_address} -> $resol_arp{hostname_fq}\n" if $verbose;2525 2526 $SWITCH_DB{$one_switch}->{ ipv4_address} = $resol_arp{ipv4_address};2527 $SWITCH_DB{$one_switch}->{ mac_address} = $resol_arp{mac_address};2528 $SWITCH_DB{$one_switch}->{ timestamp} = $timestamp;2523 $db_switch_ip_hostnamefq{$resol_arp{'ipv4_address'}} = $resol_arp{'hostname_fq'}; 2524 print "VERBOSE_4: db_switch_ip_hostnamefq $resol_arp{'ipv4_address'} -> $resol_arp{'hostname_fq'}\n" if $verbose; 2525 2526 $SWITCH_DB{$one_switch}->{'ipv4_address'} = $resol_arp{'ipv4_address'}; 2527 $SWITCH_DB{$one_switch}->{'mac_address'} = $resol_arp{'mac_address'}; 2528 $SWITCH_DB{$one_switch}->{'timestamp'} = $timestamp; 2529 2529 } 2530 2530 … … 2542 2542 2543 2543 my $switch = $where{$ip}->{$switch_detected}; 2544 print "VERBOSE_6: $db_switch_ip_hostnamefq{$ip} -> $switch->{ hostname} : $switch->{port_hr}\n" if $verbose;2544 print "VERBOSE_6: $db_switch_ip_hostnamefq{$ip} -> $switch->{'hostname'} : $switch->{'port_hr'}\n" if $verbose; 2545 2545 2546 2546 next if $switch->{port} eq '0'; 2547 next if $switch->{ port_hr} eq $db_switch_output_port{$switch->{hostname}};2548 next if $switch->{ hostname} eq $db_switch_ip_hostnamefq{$ip}; # $computerdb->{$ip}{hostname};2547 next if $switch->{'port_hr'} eq $db_switch_output_port{$switch->{'hostname'}}; 2548 next if $switch->{'hostname'} eq $db_switch_ip_hostnamefq{$ip}; # $computerdb->{$ip}{'hostname'}; 2549 2549 2550 2550 $db_switch_link_with{ $db_switch_ip_hostnamefq{$ip} } ||= {}; 2551 $db_switch_link_with{ $db_switch_ip_hostnamefq{$ip} }->{ $switch->{ hostname} } = $switch->{port_hr};2551 $db_switch_link_with{ $db_switch_ip_hostnamefq{$ip} }->{ $switch->{'hostname'} } = $switch->{'port_hr'}; 2552 2552 print "VERBOSE_7: +++++\n" if $verbose; 2553 2553 } … … 2622 2622 for my $sw (sort keys %db_switch_output_port) { 2623 2623 if (exists $db_switch_parent{$sw}) { 2624 printf "%-28s %2s +--> %2s %-25s\n", $sw, $db_switch_output_port{$sw}, $db_switch_parent{$sw}->{ port_hr}, $db_switch_parent{$sw}->{'switch'};2624 printf "%-28s %2s +--> %2s %-25s\n", $sw, $db_switch_output_port{$sw}, $db_switch_parent{$sw}->{'port_hr'}, $db_switch_parent{$sw}->{'switch'}; 2625 2625 } 2626 2626 else { … … 2685 2685 my $switch_connection = YAML::Syck::LoadFile("$KLASK_SW_FILE"); 2686 2686 2687 my %db_switch_output_port = %{$switch_connection->{ output_port}};2688 my %db_switch_parent = %{$switch_connection->{ parent}};2689 my %db_switch_connected_on_port = %{$switch_connection->{ connected_on_port}};2687 my %db_switch_output_port = %{$switch_connection->{'output_port'}}; 2688 my %db_switch_parent = %{$switch_connection->{'parent'}}; 2689 my %db_switch_connected_on_port = %{$switch_connection->{'connected_on_port'}}; 2690 2690 2691 2691 print "Switch output port and parent port connection\n"; … … 2695 2695 $arrow ='==>' if $db_switch_output_port{$sw} =~ m/^(Trk|Br|Po)/; 2696 2696 if (exists $db_switch_parent{$sw}) { 2697 printf "%-28s %8s %3s %-8s %-25s\n", $sw, $db_switch_output_port{$sw}, $arrow, $db_switch_parent{$sw}->{ port_hr}, $db_switch_parent{$sw}->{'switch'};2697 printf "%-28s %8s %3s %-8s %-25s\n", $sw, $db_switch_output_port{$sw}, $arrow, $db_switch_parent{$sw}->{'port_hr'}, $db_switch_parent{$sw}->{'switch'}; 2698 2698 } 2699 2699 else { … … 2726 2726 my $switch_connection = YAML::Syck::LoadFile("$KLASK_SW_FILE"); 2727 2727 2728 my %db_switch_output_port = %{$switch_connection->{ output_port}};2729 my %db_switch_parent = %{$switch_connection->{ parent}};2730 my %db_switch_connected_on_port = %{$switch_connection->{ connected_on_port}};2731 my %db_switch_link_with = %{$switch_connection->{ link_with}};2732 my %db_switch_global = %{$switch_connection->{ switch_db}};2733 my $timestamp = $switch_connection->{'timestamp'};2728 my %db_switch_output_port = %{$switch_connection->{'output_port'}}; 2729 my %db_switch_parent = %{$switch_connection->{'parent'}}; 2730 my %db_switch_connected_on_port = %{$switch_connection->{'connected_on_port'}}; 2731 my %db_switch_link_with = %{$switch_connection->{'link_with'}}; 2732 my %db_switch_global = %{$switch_connection->{'switch_db'}}; 2733 my $timestamp = $switch_connection->{'timestamp'}; 2734 2734 2735 2735 my %db_building= (); 2736 2736 for my $sw (@SWITCH_LIST) { 2737 my ($building, $location) = split m/ \/ /xms, $sw->{ location}, 2;2737 my ($building, $location) = split m/ \/ /xms, $sw->{'location'}, 2; 2738 2738 $db_building{$building} ||= {}; 2739 2739 $db_building{$building}->{$location} ||= {}; 2740 $db_building{$building}->{$location}{ $sw->{ hostname} } = 'y';2740 $db_building{$building}->{$location}{ $sw->{'hostname'} } = 'y'; 2741 2741 } 2742 2742
Note: See TracChangeset
for help on using the changeset viewer.