Changeset 63
- Timestamp:
- Sep 4, 2009, 4:45:10 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/klask
r62 r63 7 7 use strict; 8 8 use warnings; 9 9 use version; our $VERSION = qv('0.5.5'); 10 11 use Readonly; 12 use Filehandle; 10 13 use Net::SNMP; 11 14 #use YAML; … … 29 32 my $KLASK_CFG = YAML::Syck::LoadFile("$KLASK_CFG_FILE"); 30 33 31 my %DEFAULT = %{ $KLASK_CFG->{default}};32 my @SWITCH = @{ $KLASK_CFG->{switch}};34 my %DEFAULT = %{ $KLASK_CFG->{default} }; 35 my @SWITCH = @{ $KLASK_CFG->{switch} }; 33 36 34 37 my %switch_level = (); 35 my %SWITCH_DB = ();38 my %SWITCH_DB = (); 36 39 LEVEL_OF_EACH_SWITCH: 37 40 for my $sw (@SWITCH){ … … 39 42 $SWITCH_DB{$sw->{hostname}} = $sw; 40 43 } 41 @SWITCH = sort { $switch_level{$b->{hostname}} <=> $switch_level{$a->{hostname}} } @{$KLASK_CFG->{switch}};44 @SWITCH = reverse sort { $switch_level{$a->{hostname}} <=> $switch_level{$b->{hostname}} } @{$KLASK_CFG->{switch}}; 42 45 43 46 my %SWITCH_PORT_COUNT = (); … … 60 63 ); 61 64 62 my %INTERNAL_PORT_MAP =(65 Readonly my %INTERNAL_PORT_MAP => ( 63 66 0 => 'A', 64 67 1 => 'B', … … 70 73 7 => 'H', 71 74 ); 72 my %INTERNAL_PORT_MAP_REV =reverse %INTERNAL_PORT_MAP;73 74 my %SWITCH_KIND =(75 Readonly my %INTERNAL_PORT_MAP_REV => reverse %INTERNAL_PORT_MAP; 76 77 Readonly my %SWITCH_KIND => ( 75 78 J3299A => { model => 'HP224M', match => 'HP J3299A ProCurve Switch 224M' }, 76 79 J4120A => { model => 'HP1600M', match => 'HP J4120A ProCurve Switch 1600M' }, … … 87 90 BS350T => { model => 'BS350T', match => 'BayStack 350T HW' }, 88 91 ); 89 90 my %OID_NUMBER =(91 sysDescr 92 sysName => '1.3.6.1.2.1.1.5.0',93 sysContact => '1.3.6.1.2.1.1.4.0',94 sysLocation => '1.3.6.1.2.1.1.6.0',92 93 Readonly my %OID_NUMBER => ( 94 sysDescription => '1.3.6.1.2.1.1.1.0', 95 sysName => '1.3.6.1.2.1.1.5.0', 96 sysContact => '1.3.6.1.2.1.1.4.0', 97 sysLocation => '1.3.6.1.2.1.1.6.0', 95 98 ); 99 100 Readonly my $RE_MAC_ADDRESS => qr{ [0-9,A-Z]{2} : [0-9,A-Z]{2} : [0-9,A-Z]{2} : [0-9,A-Z]{2} : [0-9,A-Z]{2} : [0-9,A-Z]{2} }xms; 101 Readonly my $RE_IPv4_ADDRESS => qr{ [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} }xms; 102 96 103 97 104 ################ … … 104 111 } 105 112 else { 106 print STDERR"klask: command $cmd not found\n\n";113 print {*STDERR} "klask: command $cmd not found\n\n"; 107 114 $CMD_DB{help}->(); 108 115 exit 1; … … 114 121 die "Configuration file $KLASK_CFG_FILE does not exists. Klask need it !\n" if not -e "$KLASK_CFG_FILE"; 115 122 die "Var folder $KLASK_VAR does not exists. Klask need it !\n" if not -d "$KLASK_VAR"; 123 return; 116 124 } 117 125 118 126 sub test_switchdb_environnement { 119 127 die "Switch database $KLASK_SW_FILE does not exists. Launch updatesw before this command !\n" if not -e "$KLASK_SW_FILE"; 128 return; 120 129 } 121 130 122 131 sub test_maindb_environnement { 123 132 die "Main database $KLASK_DB_FILE does not exists. Launch updatedb before this command !\n" if not -e "$KLASK_DB_FILE"; 133 return; 124 134 } 125 135 … … 128 138 sub fastping { 129 139 system "fping -c 1 @_ >/dev/null 2>&1"; 140 return; 141 } 142 143 sub shell_command { 144 my $cmd = shift; 145 146 my $fh = new Filehandle; 147 open $fh, q{-|}, "$cmd" or die "Can't exec $cmd\n"; 148 my $result = <$fh>; 149 close $fh; 150 chomp $result; 151 return $result; 130 152 } 131 153 … … 134 156 sub resolve_ip_arp_host { 135 157 my $param_ip_or_host = shift; 136 my $interface = shift || '*';137 my $type = shift || 'fast';158 my $interface = shift || q{*}; 159 my $type = shift || q{fast}; 138 160 139 161 my %ret = ( … … 144 166 145 167 # my $cmdarping = `arping -c 1 -w 1 -rR $param 2>/dev/null`; 146 if ( not $param_ip_or_host =~ m/^\d+\.\d+\.\d+\.\d+$/) {147 $param_ip_or_host =~ s/ \..*//;168 if ( not $param_ip_or_host =~ m/^\d+ \. \d+ \. \d+ \. \d+$/xms ) { 169 $param_ip_or_host =~ s/ \. .* //xms; 148 170 } 149 171 … … 154 176 # my $cmd = "grep -e '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/arp.dat | sort +2rn | head -1"; 155 177 # my $cmd = "grep -he '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/*.dat | sort +2rn | head -1"; 156 my $cmd = "grep -he '" . '\b' . "$param_ip_or_host" . '\b' . "'/var/lib/arpwatch/$interface.dat | sort -rn -k 3,3 | head -1";178 my $cmd = q{grep -he '\b} . $param_ip_or_host . q{\b' } . "/var/lib/arpwatch/$interface.dat | sort -rn -k 3,3 | head -1"; 157 179 my $cmd_arpwatch = shell_command $cmd; 158 180 my ($arp, $ip, $timestamp, $host) = split m/ \s+ /xms, $cmd_arpwatch; 159 160 #print "OOO $cmd\n";161 #print "TTT arp $arp -> $ip pour host $host\n";162 181 163 182 $ret{ipv4_address} = $ip if $ip; … … 165 184 $ret{timestamp} = $timestamp if $timestamp; 166 185 167 my $nowtimestamp = time ();186 my $nowtimestamp = time; 168 187 169 188 if ( $type eq 'fast' and ( not defined $timestamp or $timestamp < ( $nowtimestamp - 3 * 3600 ) ) ) { … … 172 191 } 173 192 174 # resultat de la commande arp193 # resultat de la commande arp 175 194 # tech7meylan.hmg.inpg.fr (194.254.66.240) at 00:14:22:45:28:A9 [ether] on eth0 176 195 # sw2-batF0-legi.hmg.priv (192.168.22.112) at 00:30:c1:76:9c:01 [ether] on eth0.37 177 my $cmd_arp = `arp -a $param_ip_or_host 2>/dev/null`; 178 chomp $cmd_arp; 179 $cmd_arp =~ /(\S*)\s\(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\)\sat\s([0-9,A-Z]{2}:[0-9,A-Z]{2}:[0-9,A-Z]{2}:[0-9,A-Z]{2}:[0-9,A-Z]{2}:[0-9,A-Z]{2})/; 180 $ret{hostname_fq} = $1 if(defined($1)); 181 $ret{ipv4_address} = $2 if(defined($2)); 182 $ret{mac_address} = $3 if(defined($3)); 183 #print "RET1 $ret{ipv4_address} -> $ret{mac_address} : $ret{hostname_fq}\n"; 184 185 # if ($ret{ipv4_address} eq '0.0.0.0' and $ret{mac_address} eq 'unknow'and $ret{hostname_fq} eq 'unknow') { 186 # resultat de la commande host si le parametre est ip 187 # 250.66.254.194.in-addr.arpa domain name pointer legihp2100.hmg.inpg.fr. 188 my $cmd_host = `host $param_ip_or_host 2>/dev/null`; 189 chomp $cmd_host; 190 $cmd_host =~ m/domain\sname\spointer\s(\S+)\.$/; 191 $ret{hostname_fq} = $1 if defined $1; 192 193 # resultat de la commande host si parametre est hostname 194 # tech7meylan.hmg.inpg.fr has address 194.254.66.240 195 $cmd_host =~ m/(\S*)\shas\saddress\s([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$/; 196 $ret{hostname_fq} = $1 if defined $1; 197 $ret{ipv4_address} = $2 if defined $2; 198 199 $cmd_host =~ m/\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.in-addr\.arpa\s/; 200 $ret{ipv4_address} = "$4.$3.$2.$1" if defined $1 and defined $2 and defined $3 and defined $4; 201 $ret{hostname_fq} = $param_ip_or_host if not defined $1 and $ret{hostname_fq} eq 'unknow'; 202 # } 203 204 #print "RET2 $ret{ipv4_address} -> $ret{mac_address} : $ret{hostname_fq}\n"; 205 unless ($ret{mac_address} eq 'unknow') { 196 my $cmd_arp = shell_command "arp -a $param_ip_or_host"; 197 if ( $cmd_arp =~ m{ (\S*) \s \( ( $RE_IPv4_ADDRESS ) \) \s at \s ( $RE_MAC_ADDRESS ) }xms ) { 198 ( $ret{hostname_fq}, $ret{ipv4_address}, $ret{mac_address} ) = ($1, $2, $3); 199 } 200 201 # resultat de la commande host si le parametre est ip 202 # 250.66.254.194.in-addr.arpa domain name pointer legihp2100.hmg.inpg.fr. 203 my $cmd_host = shell_command "host $param_ip_or_host"; 204 if ( $cmd_host =~ m/domain \s name \s pointer \s (\S+) \.$/xms ) { 205 $ret{hostname_fq} = $1; 206 } 207 208 # resultat de la commande host si parametre est hostname 209 # tech7meylan.hmg.inpg.fr has address 194.254.66.240 210 if ( $cmd_host =~ m/(\S*) \s has \s address \s ( $RE_IPv4_ADDRESS )$/xms ) { 211 ( $ret{hostname_fq}, $ret{ipv4_address} ) = ($1, $2); 212 } 213 214 if ( $cmd_host =~ m/ \b ( $RE_IPv4_ADDRESS ) \. in-addr \. arpa \s/xms ) { 215 $ret{ipv4_address} = $1; 216 } 217 #$ret{hostname_fq} = $param_ip_or_host if not defined $1 and $ret{hostname_fq} eq 'unknow'; 218 219 if ($ret{mac_address} ne 'unknow') { 206 220 my @paquets = (); 207 foreach ( split (/:/, $ret{mac_address})) {208 my @chars = split //, uc("00$_");221 foreach ( split m/ : /xms, $ret{mac_address} ) { 222 my @chars = split m//xms, uc "00$_"; 209 223 push @paquets, "$chars[-2]$chars[-1]"; 210 224 } 211 $ret{mac_address} = join ':', @paquets; 212 } 213 214 #print "RET3 $ret{ipv4_address} -> $ret{mac_address} : $ret{hostname_fq}\n"; 225 $ret{mac_address} = join q{:}, @paquets; 226 } 227 215 228 return %ret; 216 229 } … … 219 232 sub get_switch_model { 220 233 my $sw_snmp_description = shift || 'unknow'; 221 234 222 235 for my $sw_kind (keys %SWITCH_KIND) { 223 next if not $sw_snmp_description =~ m/$SWITCH_KIND{$sw_kind}->{match}/ ;224 236 next if not $sw_snmp_description =~ m/$SWITCH_KIND{$sw_kind}->{match}/xms; 237 225 238 return $SWITCH_KIND{$sw_kind}->{model}; 226 239 } 227 240 228 241 return $sw_snmp_description; 229 242 } … … 233 246 sub init_switch_names { 234 247 my $verbose = shift; 235 248 236 249 printf "%-25s %-25s %s\n",'Switch','Description','Type'; 237 # print "Switch description\n" if $verbose;238 250 print "-------------------------------------------------------------------------\n" if $verbose; 239 251 … … 243 255 $session{-version} = $sw->{version} || 1; 244 256 $session{-port} = $sw->{snmpport} || $DEFAULT{snmpport} || 161; 245 if (exists $sw->{version} and $sw->{version} eq 3) {257 if (exists $sw->{version} and $sw->{version} eq '3') { 246 258 $session{-username} = $sw->{username} || 'snmpadmin'; 247 259 } … … 257 269 my $result = $session->get_request( 258 270 -varbindlist => [ 259 $OID_NUMBER{sysDescr },271 $OID_NUMBER{sysDescription}, 260 272 $OID_NUMBER{sysName}, 261 273 $OID_NUMBER{sysContact}, … … 264 276 ); 265 277 $sw->{description} = $result->{$OID_NUMBER{sysName}} || $sw->{hostname}; 266 $sw->{model} = get_switch_model( $result->{$OID_NUMBER{sysDescr }});278 $sw->{model} = get_switch_model( $result->{$OID_NUMBER{sysDescription}}); 267 279 #$sw->{location} = $result->{"1.3.6.1.2.1.1.6.0"} || $sw->{hostname}; 268 280 #$sw->{contact} = $result->{"1.3.6.1.2.1.1.4.0"} || $sw->{hostname}; 269 281 $session->close; 270 282 271 # Ligne à virer car on récupère maintenant le modèle du switch 272 my ($desc, $type) = split ':', $sw->{description}, 2; 273 # printf "%-25s 0--------->>>> %-25s %s\n", $sw->{hostname}, $desc, uc($type)."**" if $verbose; 283 # Ligne à virer car on récupère maintenant le modèle du switch 284 my ($desc, $type) = split m/ : /xms, $sw->{description}, 2; 274 285 printf "%-25s 0--------->>>> %-25s %s\n", $sw->{hostname}, $desc, $sw->{model} if $verbose; 275 286 } 276 287 277 288 print "\n" if $verbose; 289 return; 278 290 } 279 291 … … 282 294 sub hex_to_dec { 283 295 #00:0F:1F:43:E4:2B 284 my $car = '00' . uc (shift);296 my $car = '00' . uc shift; 285 297 286 298 return '00' if $car eq '00UNKNOW'; … … 288 300 '0'=>'0', '1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', 289 301 '5'=>'5', '6'=>'6', '7'=>'7', '8'=>'8', '9'=>'9', 290 'A'=>'10', 'B'=>'11', 'C'=>'12', 'D'=>'13', 'E'=>'14', 'F'=>'15' 302 'A'=>'10', 'B'=>'11', 'C'=>'12', 'D'=>'13', 'E'=>'14', 'F'=>'15', 291 303 ); 292 my @chars = split (//, $car);304 my @chars = split m//xms, $car; 293 305 return $table{$chars[-2]}*16 + $table{$chars[-1]}; 294 306 } … … 300 312 my $arp = shift; 301 313 302 my @paquets = split /:/, $arp;303 my $return = '';314 my @paquets = split m/ : /xms, $arp; 315 my $return = q{}; 304 316 foreach(@paquets) { 305 $return .= ".".hex_to_dec($_);317 $return .= q{.} . hex_to_dec($_); 306 318 } 307 319 return $return; … … 311 323 # va rechercher le port et le switch sur lequel est la machine 312 324 sub find_switch_port { 313 my $arp = shift;314 my $switch_proposal = shift || '';315 325 my $arp = shift; 326 my $switch_proposal = shift || q{}; 327 316 328 my %ret; 317 $ret{switch_description} = "unknow";318 $ret{switch_port} = "0";329 $ret{switch_description} = 'unknow'; 330 $ret{switch_port} = '0'; 319 331 320 332 return %ret if $arp eq 'unknow';; 321 333 322 334 my @switch_search = @SWITCH; 323 if ($switch_proposal ne '') {335 if ($switch_proposal ne q{}) { 324 336 for my $sw (@SWITCH) { 325 337 next if $sw->{hostname} ne $switch_proposal; … … 329 341 } 330 342 331 my $research = "1.3.6.1.2.1.17.4.3.1.2".arp_hex_to_dec($arp);332 343 my $research = '1.3.6.1.2.1.17.4.3.1.2' . arp_hex_to_dec($arp); 344 333 345 LOOP_ON_SWITCH: 334 346 for my $sw (@switch_search) { … … 339 351 -varbindlist => [$research] 340 352 ); 341 if (not defined ($result)or $result->{$research} eq 'noSuchInstance') {353 if (not defined $result or $result->{$research} eq 'noSuchInstance') { 342 354 $session->close; 343 355 next LOOP_ON_SWITCH; … … 348 360 349 361 # IMPORTANT !! 350 # ceci empeche la detection sur certains port ... 362 # ceci empeche la detection sur certains port ... 351 363 # en effet les switch sont relies entre eux par un cable reseau et du coup 352 364 # tous les arp de toutes les machines sont presentes sur ces ports (ceux choisis ici sont les miens) … … 364 376 $ret{switch_description} = $sw->{description}; 365 377 $ret{switch_port} = get_human_readable_port($sw->{model}, $swport); # $swport; 366 378 367 379 last LOOP_ON_SWITCH; 368 380 # } … … 386 398 } 387 399 388 my $research = "1.3.6.1.2.1.17.4.3.1.2".arp_hex_to_dec($arp);400 my $research = '1.3.6.1.2.1.17.4.3.1.2' . arp_hex_to_dec($arp); 389 401 LOOP_ON_ALL_SWITCH: 390 402 for my $sw (@SWITCH) { … … 396 408 ); 397 409 398 if(defined ($result)and $result->{$research} ne 'noSuchInstance'){410 if(defined $result and $result->{$research} ne 'noSuchInstance'){ 399 411 my $swport = $result->{$research}; 400 412 … … 422 434 return $KLASK_CFG->{network}{$network}{interface}; 423 435 } 424 436 425 437 ### 426 438 # liste l'ensemble des adresses ip d'un réseau … … 433 445 my @line = @{$KLASK_CFG->{network}{$net}{'ip-subnet'}}; 434 446 for my $cmd (@line) { 435 for my $method (keys % $cmd){447 for my $method (keys %{$cmd}){ 436 448 $cidrlist->add_any($cmd->{$method}) if $method eq 'add'; 437 449 } … … 443 455 for my $cidr ($cidrlist->list()) { 444 456 my $net = new NetAddr::IP $cidr; 445 for my $ip (@ $net) {446 $ip =~ s #/32##;457 for my $ip (@{$net}) { 458 $ip =~ s{ /32 }{}xms; 447 459 push @res, $ip; 448 460 } … … 468 480 my $sw_model = shift; 469 481 my $sw_port = shift; 470 482 471 483 return $sw_port if not $sw_model eq 'HP8000M'; 472 484 473 485 my $reste = (($sw_port - 1) % 8) + 1; 474 my $major = int ( ($sw_port - 1) / 8);486 my $major = int (($sw_port - 1) / 8); 475 487 476 488 return "$INTERNAL_PORT_MAP{$major}$reste"; … … 480 492 my $sw_model = shift; 481 493 my $sw_port = shift; 482 494 483 495 return $sw_port if not $sw_model eq 'HP8000'; 484 496 485 my $letter = substr ($sw_port, 0, 1);486 497 my $letter = substr $sw_port, 0, 1; 498 487 499 # return $port if $letter =~ m/\d/; 488 489 my $reste = substr ($sw_port, 1);490 500 501 my $reste = substr $sw_port, 1; 502 491 503 return $INTERNAL_PORT_MAP_REV{$letter} * 8 + $reste; 492 504 } … … 498 510 sub cmd_help { 499 511 500 print << END;512 print <<'END'; 501 513 klask - ports manager and finder for switch 502 514 … … 514 526 klask status switch port 515 527 END 528 return; 516 529 } 517 530 518 531 sub cmd_version { 519 532 520 print << END;533 print <<'END'; 521 534 Klask - ports manager and finder for switch 522 535 Copyright (C) 2005-2008 Gabriel Moreau … … 526 539 print ' $Date$'."\n"; 527 540 print ' $Id$'."\n"; 541 return; 528 542 } 529 543 530 544 sub cmd_search { 531 545 my @computer = @_; 532 546 533 547 init_switch_names(); #nomme les switchs 534 548 fastping(@computer); … … 536 550 my %resol_arp = resolve_ip_arp_host($clientname); #resolution arp 537 551 my %where = find_switch_port($resol_arp{mac_address}); #retrouve l'emplacement 538 printf "%-22s %2i %-30s %-15s %18s", $where{switch_description}, $where{switch_port}, $resol_arp{hostname_fq}, $resol_arp{ipv4_address}, $resol_arp{mac_address}."\n"552 printf '%-22s %2i %-30s %-15s %18s', $where{switch_description}, $where{switch_port}, $resol_arp{hostname_fq}, $resol_arp{ipv4_address}, $resol_arp{mac_address}."\n" 539 553 unless $where{switch_description} eq 'unknow' and $resol_arp{hostname_fq} eq 'unknow' and $resol_arp{mac_address} eq 'unknow'; 540 554 } 555 return; 541 556 } 542 557 … … 546 561 fastping(@computer); 547 562 my $computerdb = YAML::Syck::LoadFile("$KLASK_DB_FILE"); 548 563 549 564 LOOP_ON_COMPUTER: 550 565 for my $clientname (@computer) { 551 566 my %resol_arp = resolve_ip_arp_host($clientname); #resolution arp 552 567 my $ip = $resol_arp{ipv4_address}; 553 568 554 569 next LOOP_ON_COMPUTER unless exists $computerdb->{$ip}; 555 556 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime ($computerdb->{$ip}{timestamp});570 571 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{timestamp}; 557 572 $year += 1900; 558 573 $mon++; 559 my $date = sprintf "%04i-%02i-%02i %02i:%02i", $year,$mon,$mday,$hour,$min;574 my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; 560 575 561 576 printf "%-22s %2s %-30s %-15s %-18s %s\n", … … 567 582 $date; 568 583 } 584 return; 569 585 } 570 586 … … 583 599 584 600 my $number_of_computer = get_list_ip(@network); # + 1; 585 my $size_of_database = keys % $computerdb;601 my $size_of_database = keys %{$computerdb}; 586 602 $size_of_database = 1 if $size_of_database == 0; 587 603 my $i = 0; … … 595 611 my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}}; 596 612 my %db_switch_chained_port = (); 597 for my $swport (keys %db_switch_connected_on_port) { 598 my ($sw_connect,$port_connect) = split ':', $swport;613 for my $swport (keys %db_switch_connected_on_port) { 614 my ($sw_connect,$port_connect) = split m/ : /xms, $swport; 599 615 $db_switch_chained_port{$sw_connect} .= "$port_connect:"; 600 616 } … … 603 619 if ( exists $db_switch_chained_port{$sw->{hostname}} ) { 604 620 chop $db_switch_chained_port{$sw->{hostname}}; 605 push @{$sw->{portignore}}, split (':',$db_switch_chained_port{$sw->{hostname}});621 push @{$sw->{portignore}}, split m/ : /xms, $db_switch_chained_port{$sw->{hostname}}; 606 622 } 607 623 # print "$sw->{hostname} ++ @{$sw->{portignore}}\n"; … … 628 644 for my $one_computer (@computer) { 629 645 $i++; 630 631 my $total_percent = int (($i*100)/$number_of_computer);646 647 my $total_percent = int (($i*100)/$number_of_computer); 632 648 633 649 my $localtime = time - $timestamp; 634 my ($sec,$min) = localtime ($localtime);650 my ($sec,$min) = localtime $localtime; 635 651 636 652 my $time_elapse = 0; 637 653 $time_elapse = $localtime * ( 100 - $total_percent) / $total_percent if $total_percent != 0; 638 my ($sec_elapse,$min_elapse) = localtime ($time_elapse);654 my ($sec_elapse,$min_elapse) = localtime $time_elapse; 639 655 640 656 printf "\rComputer scanned: %4i/%i (%2i%%)", $i, $number_of_computer, $total_percent; 641 657 # printf ", Computer detected: %4i/%i (%2i%%)", $detected_computer, $size_of_database, int(($detected_computer*100)/$size_of_database); 642 printf ", detected: %4i/%i (%2i%%)", $detected_computer, $size_of_database, int(($detected_computer*100)/$size_of_database);643 printf " [Time: %02i:%02i / %02i:%02i]", int($localtime/60), $localtime % 60, int($time_elapse/60), $time_elapse % 60;644 # printf " [%02i:%02i/%02i:%02i]", int($localtime/60), $localtime % 60, int($time_elapse/60), $time_elapse % 60;645 printf " %-14s", $one_computer;658 printf ', detected: %4i/%i (%2i%%)', $detected_computer, $size_of_database, int(($detected_computer*100)/$size_of_database); 659 printf ' [Time: %02i:%02i / %02i:%02i]', int($localtime/60), $localtime % 60, int($time_elapse/60), $time_elapse % 60; 660 # printf ' [%02i:%02i/%02i:%02i]', int($localtime/60), $localtime % 60, int($time_elapse/60), $time_elapse % 60; 661 printf ' %-14s', $one_computer; 646 662 647 663 my %resol_arp = resolve_ip_arp_host($one_computer,$current_interface); 648 664 649 665 # do not search on router connection (why ?) 650 666 if ( exists $router_mac_ip{$resol_arp{mac_address}}) { … … 659 675 } 660 676 661 my $switch_proposal = '';677 my $switch_proposal = q{}; 662 678 if (exists $computerdb->{$resol_arp{ipv4_address}} and exists $computerdb->{$resol_arp{ipv4_address}}{switch_hostname}) { 663 679 $switch_proposal = $computerdb->{$resol_arp{ipv4_address}}{switch_hostname}; … … 716 732 # mise a jour du nom de la machine si modification dans le dns 717 733 $computerdb->{$resol_arp{ipv4_address}}{hostname_fq} = $resol_arp{hostname_fq}; 718 734 719 735 # mise à jour de la date de détection si détection plus récente par arpwatch 720 736 $computerdb->{$resol_arp{ipv4_address}}{timestamp} = $resol_arp{timestamp} if exists $resol_arp{timestamp} and $computerdb->{$resol_arp{ipv4_address}}{timestamp} < $resol_arp{timestamp}; 721 737 722 # provisoire car changement de nom des attributs723 # $computerdb->{$resol_arp{ipv4_address}}{mac_address} = $computerdb->{$resol_arp{ipv4_address}}{arp};724 # $computerdb->{$resol_arp{ipv4_address}}{switch_description} = $computerdb->{$resol_arp{ipv4_address}}{switch};725 # $computerdb->{$resol_arp{ipv4_address}}{switch_port} = $computerdb->{$resol_arp{ipv4_address}}{port};726 727 738 # relance un arping sur la machine si celle-ci n'a pas été détectée depuis plus d'une semaine 728 739 # push @computer_not_detected, $resol_arp{ipv4_address} if $computerdb->{$resol_arp{ipv4_address}}{timestamp} < $timestamp_last_week; 729 740 $computer_not_detected{$resol_arp{ipv4_address}} = $current_interface if $computerdb->{$resol_arp{ipv4_address}}{timestamp} < $timestamp_last_week; 730 741 731 742 } 732 743 } … … 736 747 737 748 my $dirdb = $KLASK_DB_FILE; 738 $dirdb =~ s #/[^/]*$##;749 $dirdb =~ s{ / [^/]* $}{}xms; 739 750 mkdir "$dirdb", 0755 unless -d "$dirdb"; 740 751 YAML::Syck::DumpFile("$KLASK_DB_FILE", $computerdb); … … 745 756 # print "arping -c 1 -w 1 -rR -i $interface $one_computer 2>/dev/null\n"; 746 757 } 758 return; 747 759 } 748 760 … … 763 775 764 776 my $dirdb = $KLASK_DB_FILE; 765 $dirdb =~ s #/[^/]*$##;777 $dirdb =~ s{ / [^/]* $}{}xms; 766 778 mkdir "$dirdb", 0755 unless -d "$dirdb"; 767 779 YAML::Syck::DumpFile("$KLASK_DB_FILE", $computerdb); 780 return; 768 781 } 769 782 … … 783 796 784 797 $format = 'txt' if not defined $possible_format{$format}; 785 798 786 799 $possible_format{$format}->(@ARGV); 800 return; 787 801 } 788 802 … … 796 810 797 811 LOOP_ON_IP_ADDRESS: 798 foreach my $ip (Net::Netmask::sort_by_ip_address(keys % $computerdb)) {799 812 foreach my $ip (Net::Netmask::sort_by_ip_address(keys %{$computerdb})) { 813 800 814 # next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq 'unknow'; 801 815 … … 805 819 # dans le futur 806 820 # next if $computerdb->{$ip}{hostname_fq} eq 'unknow'; 807 808 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime ($computerdb->{$ip}{timestamp});821 822 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{timestamp}; 809 823 $year += 1900; 810 824 $mon++; 811 my $date = sprintf "%04i-%02i-%02i %02i:%02i", $year,$mon,$mday,$hour,$min;825 my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; 812 826 813 827 printf "%-25s %2s <------- %-30s %-15s %-18s %s\n", … … 819 833 $date; 820 834 } 835 return; 821 836 } 822 837 … … 829 844 #<script src="sorttable-klask.js"></script> 830 845 831 print << END;846 print <<'END_HTML'; 832 847 <table class="sortable" summary="Klask export database"> 833 848 <caption>Klask database</caption> … … 855 870 </tfoot> 856 871 <tbody> 857 END 872 END_HTML 858 873 859 874 my %mac_count = (); 860 875 LOOP_ON_IP_ADDRESS: 861 foreach my $ip (keys % $computerdb) {862 876 foreach my $ip (keys %{$computerdb}) { 877 863 878 # to be improve in the future 864 879 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself ! 865 880 866 881 $mac_count{$computerdb->{$ip}{mac_address}}++; 867 882 } … … 870 885 871 886 LOOP_ON_IP_ADDRESS: 872 foreach my $ip (Net::Netmask::sort_by_ip_address(keys % $computerdb)) {873 887 foreach my $ip (Net::Netmask::sort_by_ip_address(keys %{$computerdb})) { 888 874 889 # to be improve in the future 875 890 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself ! 876 891 877 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime ($computerdb->{$ip}{timestamp});892 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{timestamp}; 878 893 $year += 1900; 879 894 $mon++; 880 my $date = sprintf "%04i-%02i-%02i %02i:%02i", $year,$mon,$mday,$hour,$min;895 my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; 881 896 882 897 # $odd_or_even++; 883 898 # my $typerow = $odd_or_even % 2 ? 'odd' : 'even'; 884 $typerow = $typerow eq 'even' ? 'odd' : 'even'; 899 $typerow = $typerow eq 'even' ? 'odd' : 'even'; 885 900 886 901 my $switch_hostname = $computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description} || 'unkown'; 887 902 chomp $switch_hostname; 888 my $switch_hostname_sort = sprintf "%s %3s",$switch_hostname, $computerdb->{$ip}{switch_port};889 890 my $ip_sort = sprintf "%03i%03i%03i%03i", split( /\./, $ip);891 892 my $mac_sort = sprintf "%04i-%s", 9999 - $mac_count{$computerdb->{$ip}{mac_address}}, $computerdb->{$ip}{mac_address};893 894 $computerdb->{$ip}{hostname_fq} = 'unknow' if $computerdb->{$ip}{hostname_fq} =~ m/^ \d+\.\d+\.\d+\.\d+$/;895 my ( $host_short ) = split /\./, $computerdb->{$ip}{hostname_fq};896 897 print << END;903 my $switch_hostname_sort = sprintf '%s %3s' ,$switch_hostname, $computerdb->{$ip}{switch_port}; 904 905 my $ip_sort = sprintf '%03i%03i%03i%03i', split m/ \. /xms, $ip; 906 907 my $mac_sort = sprintf '%04i-%s', 9999 - $mac_count{$computerdb->{$ip}{mac_address}}, $computerdb->{$ip}{mac_address}; 908 909 $computerdb->{$ip}{hostname_fq} = 'unknow' if $computerdb->{$ip}{hostname_fq} =~ m/^ \d+ \. \d+ \. \d+ \. \d+ $/xms; 910 my ( $host_short ) = split m/ \. /xms, $computerdb->{$ip}{hostname_fq}; 911 912 print <<"END_HTML"; 898 913 <tr class="$typerow"> 899 914 <td sorttable_customkey="$switch_hostname_sort">$switch_hostname</td> … … 905 920 <td>$date</td> 906 921 </tr> 907 END 922 END_HTML 908 923 } 909 924 … … 917 932 for my $sw (sort keys %db_switch_output_port) { 918 933 919 my $switch_hostname_sort = sprintf "%s %3s",$sw, $db_switch_output_port{$sw};920 921 $typerow = $typerow eq 'even' ? 'odd' : 'even'; 934 my $switch_hostname_sort = sprintf '%s %3s' ,$sw, $db_switch_output_port{$sw}; 935 936 $typerow = $typerow eq 'even' ? 'odd' : 'even'; 922 937 923 938 if (exists $db_switch_parent{$sw}) { … … 930 945 $year += 1900; 931 946 $mon++; 932 my $date = sprintf "%04i-%02i-%02i %02i:%02i", $year,$mon,$mday,$hour,$min; 933 934 my $ip_sort = sprintf "%03i%03i%03i%03i", split( /\./, $ipv4_address); 935 936 my $mac_sort = sprintf "%04i-%s", 9999, $mac_address; 937 938 939 my ( $host_short ) = sprintf "%s %3s" , split(/\./, $db_switch_parent{$sw}->{switch}, 1), $db_switch_parent{$sw}->{port}; 940 941 print <<END; 947 my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; 948 949 my $ip_sort = sprintf '%03i%03i%03i%03i', split m/ \. /xms, $ipv4_address; 950 951 my $mac_sort = sprintf '%04i-%s', 9999, $mac_address; 952 953 my ( $host_short ) = sprintf '%s %3s' , split(m/ \. /xms, $db_switch_parent{$sw}->{switch}, 1), $db_switch_parent{$sw}->{port}; 954 955 print <<"END_HTML"; 942 956 <tr class="$typerow"> 943 957 <td sorttable_customkey="$switch_hostname_sort">$sw</td> … … 949 963 <td>$date</td> 950 964 </tr> 951 END 965 END_HTML 952 966 } 953 967 else { 954 # printf "%-25s %2s +--> router\n", $sw, $db_switch_output_port{$sw}; 955 print <<END; 968 print <<"END_HTML"; 956 969 <tr class="$typerow"> 957 970 <td sorttable_customkey="$switch_hostname_sort">$sw</td> … … 963 976 <td></td> 964 977 </tr> 965 END 978 END_HTML 966 979 } 967 980 } 968 981 969 982 for my $swport (sort keys %db_switch_connected_on_port) { 970 my ($sw_connect,$port_connect) = split ':', $swport;983 my ($sw_connect,$port_connect) = split m/ : /xms, $swport; 971 984 for my $sw (keys %{$db_switch_connected_on_port{$swport}}) { 972 985 973 my $switch_hostname_sort = sprintf "%s %3s",$sw_connect, $port_connect;986 my $switch_hostname_sort = sprintf '%s %3s' ,$sw_connect, $port_connect; 974 987 975 988 my $mac_address = $db_switch{$sw}->{mac_address}; … … 980 993 $year += 1900; 981 994 $mon++; 982 my $date = sprintf "%04i-%02i-%02i %02i:%02i", $year,$mon,$mday,$hour,$min;983 984 my $ip_sort = sprintf "%03i%03i%03i%03i", split( /\./, $ipv4_address);985 986 my $mac_sort = sprintf "%04i-%s", 9999, $mac_address;987 988 $typerow = $typerow eq 'even' ? 'odd' : 'even'; 995 my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year,$mon,$mday,$hour,$min; 996 997 my $ip_sort = sprintf '%03i%03i%03i%03i', split m/ \. /xms, $ipv4_address; 998 999 my $mac_sort = sprintf '%04i-%s', 9999, $mac_address; 1000 1001 $typerow = $typerow eq 'even' ? 'odd' : 'even'; 989 1002 990 1003 if (exists $db_switch_output_port{$sw}) { 991 1004 992 my ( $host_short ) = sprintf "%s %3s" , split(/\./, $sw, 1), $db_switch_output_port{$sw};993 994 print <<END;1005 my ( $host_short ) = sprintf '%s %3s' , split( m/\./xms, $sw, 1), $db_switch_output_port{$sw}; 1006 1007 print <<"END_HTML"; 995 1008 <tr class="$typerow"> 996 1009 <td sorttable_customkey="$switch_hostname_sort">$sw_connect</td> … … 1002 1015 <td>$date</td> 1003 1016 </tr> 1004 END 1017 END_HTML 1005 1018 } 1006 1019 else { 1007 print <<END;1020 print <<"END_HTML"; 1008 1021 <tr class="$typerow"> 1009 1022 <td sorttable_customkey="$switch_hostname_sort">$sw_connect</td> … … 1015 1028 <td>$date</td> 1016 1029 </tr> 1017 END 1018 } 1019 } 1020 } 1021 1022 1023 print <<END; 1030 END_HTML 1031 } 1032 } 1033 } 1034 1035 print <<'END_HTML'; 1024 1036 </tbody> 1025 1037 </table> 1026 END 1038 END_HTML 1039 return; 1027 1040 } 1028 1041 … … 1031 1044 1032 1045 LOOP_ON_IP_ADDRESS: 1033 foreach my $ip (Net::Netmask::sort_by_ip_address(keys % $computerdb)) {1046 foreach my $ip (Net::Netmask::sort_by_ip_address(keys %{$computerdb})) { 1034 1047 1035 1048 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself ! 1036 1049 1037 my $sw_hostname = $computerdb->{$ip}{switch_hostname} || '';1050 my $sw_hostname = $computerdb->{$ip}{switch_hostname} || q{}; 1038 1051 next if $sw_hostname eq 'unknow'; 1039 1040 my $sw_location = '';1052 1053 my $sw_location = q{}; 1041 1054 for my $sw (@SWITCH) { 1042 1055 next if $sw_hostname ne $sw->{hostname}; … … 1045 1058 } 1046 1059 1047 printf "%s: \"%s\"\n", $ip, $sw_location if not $sw_location eq ''; 1048 } 1060 printf "%s: \"%s\"\n", $ip, $sw_location if not $sw_location eq q{}; 1061 } 1062 return; 1049 1063 } 1050 1064 … … 1052 1066 my $switch = shift; 1053 1067 my $port = shift; 1054 1055 #snmpset -v 1 -c community X.X.X.X 1.3.6.1.2.1.2.2.1.7.NoPort = 1 (up) 1056 #snmpset -v 1 -c community X.X.X.X 1.3.6.1.2.1.2.2.1.7.NoPort = 2 (down) 1068 1069 #snmpset -v 1 -c community X.X.X.X 1.3.6.1.2.1.2.2.1.7.NoPort = 1 (up) 1070 #snmpset -v 1 -c community X.X.X.X 1.3.6.1.2.1.2.2.1.7.NoPort = 2 (down) 1057 1071 system "snmpset -v 1 -c public $switch 1.3.6.1.2.1.2.2.1.7.$port = 1"; 1072 return; 1058 1073 } 1059 1074 … … 1061 1076 my $switch = shift; 1062 1077 my $port = shift; 1063 1078 1064 1079 system "snmpset -v 1 -c public $switch 1.3.6.1.2.1.2.2.1.7.$port = 2"; 1080 return; 1065 1081 } 1066 1082 … … 1068 1084 my $switch = shift; 1069 1085 my $port = shift; 1070 1086 1071 1087 system "snmpget -v 1 -c public $switch 1.3.6.1.2.1.2.2.1.7.$port"; 1088 return; 1072 1089 } 1073 1090 1074 1091 sub cmd_search_mac_on_switch { 1075 my $switch_name = shift || '';1076 my $mac_address = shift || '';1077 1078 if ($switch_name eq '' or $mac_address eq '') {1092 my $switch_name = shift || q{}; 1093 my $mac_address = shift || q{}; 1094 1095 if ($switch_name eq q{} or $mac_address eq q{}) { 1079 1096 die "Usage: klask search-mac-on-switch SWITCH_NAME MAC_ADDRESS\n"; 1080 1097 } 1081 1098 1082 1099 if (not defined $SWITCH_DB{$switch_name}) { 1083 die "Switch $switch_name must be defined in klask configuration file\n"; 1100 die "Switch $switch_name must be defined in klask configuration file\n"; 1084 1101 } 1085 1102 … … 1088 1105 $session{-version} = $sw->{version} || 1; 1089 1106 $session{-port} = $sw->{snmpport} || $DEFAULT{snmpport} || 161; 1090 if (exists $sw->{version} and $sw->{version} eq 3) {1107 if (exists $sw->{version} and $sw->{version} eq '3') { 1091 1108 $session{-username} = $sw->{username} || 'snmpadmin'; 1092 1109 } … … 1095 1112 } 1096 1113 1097 my $research = "1.3.6.1.2.1.17.4.3.1.2".arp_hex_to_dec($mac_address);1114 my $research = '1.3.6.1.2.1.17.4.3.1.2' . arp_hex_to_dec($mac_address); 1098 1115 print "Klask search OID $research on switch $switch_name\n"; 1099 1116 … … 1104 1121 -varbindlist => [$research] 1105 1122 ); 1106 1107 if (not defined ($result)or $result->{$research} eq 'noSuchInstance') {1123 1124 if (not defined $result or $result->{$research} eq 'noSuchInstance') { 1108 1125 print "Klask do not find MAC $mac_address on switch $switch_name\n"; 1109 1126 $session->close; … … 1114 1131 1115 1132 print "Klask find MAC $mac_address on switch $switch_name port $swport\n"; 1133 return; 1116 1134 } 1117 1135 … … 1128 1146 # for my $one_computer ('194.254.66.254') { 1129 1147 for my $one_router ( get_list_main_router(get_list_network()) ) { 1130 #print "TT$one_router\n"; 1131 my %resol_arp = resolve_ip_arp_host($one_router,'*','low'); # resolution arp 1148 my %resol_arp = resolve_ip_arp_host($one_router, q{*}, q{low}); # resolution arp 1132 1149 next DETECT_ALL_ROUTER if $resol_arp{mac_address} eq 'unknow'; 1133 #print "$one_router\n";1134 1150 $where{$resol_arp{ipv4_address}} = find_all_switch_port($resol_arp{mac_address}); # retrouve les emplacements des routeurs 1135 1151 } … … 1137 1153 ALL_ROUTER_IP_ADDRESS: 1138 1154 for my $ip (Net::Netmask::sort_by_ip_address(keys %where)) { # '194.254.66.254')) { 1139 1155 1140 1156 next ALL_ROUTER_IP_ADDRESS if not exists $where{$ip}; # /a priori/ idiot car ne sers à rien... 1141 1157 … … 1146 1162 1147 1163 next ALL_SWITCH_CONNECTED if $switch->{port} eq '0'; 1148 1164 1149 1165 $db_switch_output_port{$switch->{hostname}} = $switch->{port}; 1150 1166 } 1151 } 1152 1153 # print "Switch output port\n"; 1154 # print "------------------\n"; 1155 # for my $sw (sort keys %db_switch_output_port) { 1156 # printf "%-25s %2s\n", $sw, $db_switch_output_port{$sw}; 1157 # } 1158 # print "\n"; 1159 1167 } 1160 1168 1161 1169 my %db_switch_link_with = (); … … 1171 1179 ALL_SWITCH: 1172 1180 for my $one_computer (@list_switch_ip) { 1173 my %resol_arp = resolve_ip_arp_host($one_computer, '*','low'); # arp resolution1181 my %resol_arp = resolve_ip_arp_host($one_computer, q{*}, q{low}); # arp resolution 1174 1182 next ALL_SWITCH if $resol_arp{mac_address} eq 'unknow'; 1175 1183 1176 1184 push @list_switch_ipv4,$resol_arp{ipv4_address}; 1177 1185 1178 1186 $where{$resol_arp{ipv4_address}} = find_all_switch_port($resol_arp{mac_address}); # find port on all switch 1179 1187 … … 1184 1192 $SWITCH_DB{$one_computer}->{timestamp} = $timestamp; 1185 1193 } 1186 1194 1187 1195 ALL_SWITCH_IP_ADDRESS: 1188 1196 for my $ip (Net::Netmask::sort_by_ip_address(@list_switch_ipv4)) { 1189 1197 1190 1198 next ALL_SWITCH_IP_ADDRESS if not exists $where{$ip}; 1191 1199 … … 1206 1214 1207 1215 } 1208 1216 1209 1217 my %db_switch_connected_on_port = (); 1210 1218 my $maybe_more_than_one_switch_connected = 'yes'; 1211 1219 1212 1220 while ($maybe_more_than_one_switch_connected eq 'yes') { 1213 1221 for my $sw (keys %db_switch_link_with) { 1214 1222 for my $connect (keys %{$db_switch_link_with{$sw}}) { 1215 1223 1216 1224 my $port = $db_switch_link_with{$sw}->{$connect}; 1217 1225 1218 1226 $db_switch_connected_on_port{"$connect:$port"} ||= {}; 1219 1227 $db_switch_connected_on_port{"$connect:$port"}->{$sw}++; # Just to define the key … … 1225 1233 SWITCH_AND_PORT: 1226 1234 for my $swport (keys %db_switch_connected_on_port) { 1227 1235 1228 1236 next if keys %{$db_switch_connected_on_port{$swport}} == 1; 1229 1237 1230 1238 $maybe_more_than_one_switch_connected = 'yes'; 1231 1239 1232 my ($sw_connect,$port_connect) = split ':', $swport;1240 my ($sw_connect,$port_connect) = split m/ : /xms, $swport; 1233 1241 my @sw_on_same_port = keys %{$db_switch_connected_on_port{$swport}}; 1234 1242 1235 1243 CONNECTED: 1236 1244 for my $sw_connected (@sw_on_same_port) { 1237 1245 1238 1246 next CONNECTED if not keys %{$db_switch_link_with{$sw_connected}} == 1; 1239 1247 1240 1248 $db_switch_connected_on_port{$swport} = {$sw_connected => 1}; 1241 1249 1242 1250 for my $other_sw (@sw_on_same_port) { 1243 1251 next if $other_sw eq $sw_connected; 1244 1252 1245 1253 delete $db_switch_link_with{$other_sw}->{$sw_connect}; 1246 1254 } 1247 1255 1248 1256 # We can not do better for this switch for this loop 1249 1257 next SWITCH_AND_PORT; … … 1256 1264 for my $sw (keys %db_switch_link_with) { 1257 1265 for my $connect (keys %{$db_switch_link_with{$sw}}) { 1258 1266 1259 1267 my $port = $db_switch_link_with{$sw}->{$connect}; 1260 1268 1261 1269 $db_switch_connected_on_port{"$connect:$port"} ||= {}; 1262 1270 $db_switch_connected_on_port{"$connect:$port"}->{$sw} = $port; 1263 1271 1264 1272 $db_switch_parent{$sw} = {switch => $connect, port => $port}; 1265 1273 } 1266 1274 } 1267 1275 1268 print "Switch output port and parent port connection\n"; 1276 print "Switch output port and parent port connection\n"; 1269 1277 print "---------------------------------------------\n"; 1270 1278 for my $sw (sort keys %db_switch_output_port) { … … 1280 1288 print "Switch parent and children port inter-connection\n"; 1281 1289 print "------------------------------------------------\n"; 1282 for my $swport (sort keys %db_switch_connected_on_port) { 1283 my ($sw_connect,$port_connect) = split ':', $swport;1290 for my $swport (sort keys %db_switch_connected_on_port) { 1291 my ($sw_connect,$port_connect) = split m/ : /xms, $swport; 1284 1292 for my $sw (keys %{$db_switch_connected_on_port{$swport}}) { 1285 1293 if (exists $db_switch_output_port{$sw}) { … … 1299 1307 switch_db => \%SWITCH_DB, 1300 1308 }; 1301 1309 1302 1310 YAML::Syck::DumpFile("$KLASK_SW_FILE", $switch_connection); 1311 return; 1303 1312 } 1304 1313 … … 1320 1329 1321 1330 $format = 'txt' if not defined $possible_format{$format}; 1322 1331 1323 1332 $possible_format{$format}->(@ARGV); 1333 return; 1324 1334 } 1325 1335 … … 1332 1342 my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}}; 1333 1343 1334 print "Switch output port and parent port connection\n"; 1344 print "Switch output port and parent port connection\n"; 1335 1345 print "---------------------------------------------\n"; 1336 1346 for my $sw (sort keys %db_switch_output_port) { … … 1346 1356 print "Switch parent and children port inter-connection\n"; 1347 1357 print "------------------------------------------------\n"; 1348 for my $swport (sort keys %db_switch_connected_on_port) { 1349 my ($sw_connect,$port_connect) = split ':', $swport;1358 for my $swport (sort keys %db_switch_connected_on_port) { 1359 my ($sw_connect,$port_connect) = split m/ : /xms, $swport; 1350 1360 for my $sw (keys %{$db_switch_connected_on_port{$swport}}) { 1351 1361 if (exists $db_switch_output_port{$sw}) { … … 1357 1367 } 1358 1368 } 1369 return; 1359 1370 } 1360 1371 … … 1362 1373 1363 1374 my $switch_connection = YAML::Syck::LoadFile("$KLASK_SW_FILE"); 1364 1375 1365 1376 my %db_switch_output_port = %{$switch_connection->{output_port}}; 1366 1377 my %db_switch_parent = %{$switch_connection->{parent}}; … … 1371 1382 my %db_building= (); 1372 1383 for my $sw (@SWITCH) { 1373 my ($building, $location) = split /\//, $sw->{location}, 2;1384 my ($building, $location) = split m/ \/ /xms, $sw->{location}, 2; 1374 1385 $db_building{$building} ||= {}; 1375 1386 $db_building{$building}->{$location} ||= {}; 1376 1387 $db_building{$building}->{$location}{ $sw->{hostname} } = 'y'; 1377 1388 } 1378 1379 1389 1390 1380 1391 print "digraph G {\n"; 1381 1392 … … 1386 1397 for my $building (keys %db_building) { 1387 1398 $b++; 1388 1399 1389 1400 print "\"building$b\" [label = \"$building\", color = black, fillcolor = gold, style = filled];\n"; 1390 1401 print "site -> \"building$b\" [len = 2, color = firebrick];\n"; … … 1393 1404 for my $loc (keys %{$db_building{$building}}) { 1394 1405 $l++; 1395 1396 print "\"location$b-$l\" [label = \"$building" .'/'.join('\n',split("/",$loc))."\", color = black, fillcolor = orange, style = filled];\n";1406 1407 print "\"location$b-$l\" [label = \"$building" . q{/} . join(q{\n}, split(m{ / }xms, $loc)) . "\", color = black, fillcolor = orange, style = filled];\n"; 1397 1408 # print "\"location$b-$l\" [label = \"$building / $loc\", color = black, fillcolor = orange, style = filled];\n"; 1398 1409 print "\"building$b\" -> \"location$b-$l\" [len = 2, color = firebrick]\n"; … … 1403 1414 1404 1415 my $swname = $sw; 1405 $swname .= '\n-\n'."$db_switch_global{$sw}->{model}" if exists $db_switch_global{$sw} and exists $db_switch_global{$sw}->{model};1416 $swname .= q{\n-\n} . "$db_switch_global{$sw}->{model}" if exists $db_switch_global{$sw} and exists $db_switch_global{$sw}->{model}; 1406 1417 print "\"$sw\" [label = \"$swname\", color = black, fillcolor = palegreen, shape = rect, style = filled];\n"; 1407 1418 print "\"location$b-$l\" -> \"$sw\" [len = 2, color = firebrick, arrowtail = dot]\n"; … … 1410 1421 1411 1422 for my $swport (keys %db_switch_connected_on_port) { 1412 my ($sw_connect,$port_connect) = split ':', $swport;1423 my ($sw_connect,$port_connect) = split m/ : /xms, $swport; 1413 1424 next if not $sw_connect eq $sw; 1414 1425 next if $port_connect eq $db_switch_output_port{$sw}; … … 1420 1431 } 1421 1432 1422 # print "Switch output port and parent port connection\n"; 1433 # print "Switch output port and parent port connection\n"; 1423 1434 # print "---------------------------------------------\n"; 1424 1435 for my $sw (sort keys %db_switch_output_port) { … … 1434 1445 # print "Switch parent and children port inter-connection\n"; 1435 1446 # print "------------------------------------------------\n"; 1436 for my $swport (sort keys %db_switch_connected_on_port) { 1437 my ($sw_connect,$port_connect) = split ':', $swport;1447 for my $swport (sort keys %db_switch_connected_on_port) { 1448 my ($sw_connect,$port_connect) = split m/ : /xms, $swport; 1438 1449 for my $sw (keys %{$db_switch_connected_on_port{$swport}}) { 1439 1450 if (exists $db_switch_output_port{$sw}) { … … 1447 1458 1448 1459 print "}\n"; 1460 return; 1449 1461 } 1450 1462 … … 1457 1469 1458 1470 1459 =head1 SYNOPSIS1471 =head1 USAGE 1460 1472 1461 1473 klask updatedb … … 1591 1603 1592 1604 1593 =head1 COPYRIGHT 1594 1595 Copyright (C) 2005-2008 Gabriel Moreau. 1596 1597 1598 =head1 LICENCE 1605 =head1 LICENSE AND COPYRIGHT 1599 1606 1600 1607 GPL version 2 or later and Perl equivalent 1601 1608 1609 Copyright (C) 2005-2009 Gabriel Moreau.
Note: See TracChangeset
for help on using the changeset viewer.