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