Changeset 63 for trunk


Ignore:
Timestamp:
Sep 4, 2009, 4:45:10 PM (15 years ago)
Author:
g7moreau
Message:
  • Cancel last commit (error done before...)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r62 r63  
    77use strict;
    88use warnings;
    9 
     9use version; our $VERSION = qv('0.5.5');
     10
     11use Readonly;
     12use Filehandle;
    1013use Net::SNMP;
    1114#use YAML;
     
    2932my $KLASK_CFG = YAML::Syck::LoadFile("$KLASK_CFG_FILE");
    3033
    31 my %DEFAULT = %{$KLASK_CFG->{default}};
    32 my @SWITCH  = @{$KLASK_CFG->{switch}};
     34my %DEFAULT = %{ $KLASK_CFG->{default} };
     35my @SWITCH  = @{ $KLASK_CFG->{switch}  };
    3336
    3437my %switch_level = ();
    35 my %SWITCH_DB = ();
     38my %SWITCH_DB    = ();
    3639LEVEL_OF_EACH_SWITCH:
    3740for my $sw (@SWITCH){
     
    3942   $SWITCH_DB{$sw->{hostname}} = $sw;
    4043   }
    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}};
    4245
    4346my %SWITCH_PORT_COUNT = ();
     
    6063   );
    6164
    62 my %INTERNAL_PORT_MAP = (
     65Readonly my %INTERNAL_PORT_MAP => (
    6366   0 => 'A',
    6467   1 => 'B',
     
    7073   7 => 'H',
    7174   );
    72 my %INTERNAL_PORT_MAP_REV = reverse %INTERNAL_PORT_MAP;
    73 
    74 my %SWITCH_KIND = (
     75Readonly my %INTERNAL_PORT_MAP_REV => reverse %INTERNAL_PORT_MAP;
     76
     77Readonly my %SWITCH_KIND => (
    7578   J3299A => { model => 'HP224M',     match => 'HP J3299A ProCurve Switch 224M'  },
    7679   J4120A => { model => 'HP1600M',    match => 'HP J4120A ProCurve Switch 1600M' },
     
    8790   BS350T => { model => 'BS350T',     match => 'BayStack 350T HW'                },
    8891   );
    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',
     92
     93Readonly 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',
    9598   );
     99
     100Readonly 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;
     101Readonly my $RE_IPv4_ADDRESS => qr{ [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} }xms;
     102
    96103
    97104################
     
    104111   }
    105112else {
    106    print STDERR "klask: command $cmd not found\n\n";
     113   print {*STDERR} "klask: command $cmd not found\n\n";
    107114   $CMD_DB{help}->();
    108115   exit 1;
     
    114121   die "Configuration file $KLASK_CFG_FILE does not exists. Klask need it !\n" if not -e "$KLASK_CFG_FILE";
    115122   die "Var folder $KLASK_VAR does not exists. Klask need it !\n"              if not -d "$KLASK_VAR";
     123   return;
    116124   }
    117125
    118126sub test_switchdb_environnement {
    119127   die "Switch database $KLASK_SW_FILE does not exists. Launch updatesw before this command !\n" if not -e "$KLASK_SW_FILE";
     128   return;
    120129   }
    121130
    122131sub test_maindb_environnement {
    123132   die "Main database $KLASK_DB_FILE does not exists. Launch updatedb before this command !\n" if not -e "$KLASK_DB_FILE";
     133   return;
    124134   }
    125135
     
    128138sub fastping {
    129139   system "fping -c 1 @_ >/dev/null 2>&1";
     140   return;
     141   }
     142
     143sub 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;
    130152   }
    131153
     
    134156sub resolve_ip_arp_host {
    135157   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};
    138160
    139161   my %ret = (
     
    144166
    145167#   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;
    148170      }
    149171
     
    154176   # my $cmd = "grep  -e '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/arp.dat | sort +2rn | head -1";
    155177   # 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";
    157179   my $cmd_arpwatch = shell_command $cmd;
    158180   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";
    162181
    163182   $ret{ipv4_address} = $ip        if $ip;
     
    165184   $ret{timestamp}    = $timestamp if $timestamp;
    166185
    167    my $nowtimestamp = time();
     186   my $nowtimestamp = time;
    168187
    169188   if ( $type eq 'fast' and ( not defined $timestamp or $timestamp < ( $nowtimestamp - 3 * 3600 ) ) ) {
     
    172191      }
    173192
    174   # resultat de la commande arp
     193   # resultat de la commande arp
    175194   # tech7meylan.hmg.inpg.fr (194.254.66.240) at 00:14:22:45:28:A9 [ether] on eth0
    176195   # 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') {
    206220      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$_";
    209223         push @paquets, "$chars[-2]$chars[-1]";
    210224         }
    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
    215228   return %ret;
    216229   }
     
    219232sub get_switch_model {
    220233   my $sw_snmp_description = shift || 'unknow';
    221    
     234
    222235   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
    225238      return $SWITCH_KIND{$sw_kind}->{model};
    226239      }
    227      
     240
    228241   return $sw_snmp_description;
    229242   }
     
    233246sub init_switch_names {
    234247   my $verbose = shift;
    235    
     248
    236249   printf "%-25s                %-25s %s\n",'Switch','Description','Type';
    237 #   print "Switch description\n" if $verbose;
    238250   print "-------------------------------------------------------------------------\n" if $verbose;
    239251
     
    243255         $session{-version} = $sw->{version}   || 1;
    244256         $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') {
    246258            $session{-username} = $sw->{username} || 'snmpadmin';
    247259            }
     
    257269      my $result = $session->get_request(
    258270         -varbindlist => [
    259             $OID_NUMBER{sysDescr},
     271            $OID_NUMBER{sysDescription},
    260272            $OID_NUMBER{sysName},
    261273            $OID_NUMBER{sysContact},
     
    264276         );
    265277      $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}});
    267279      #$sw->{location} = $result->{"1.3.6.1.2.1.1.6.0"} || $sw->{hostname};
    268280      #$sw->{contact} = $result->{"1.3.6.1.2.1.1.4.0"} || $sw->{hostname};
    269281      $session->close;
    270282
    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;
    274285      printf "%-25s 0--------->>>> %-25s %s\n", $sw->{hostname}, $desc, $sw->{model} if $verbose;
    275286      }
    276287
    277288   print "\n" if $verbose;
     289   return;
    278290   }
    279291
     
    282294sub hex_to_dec {
    283295   #00:0F:1F:43:E4:2B
    284    my $car = '00' . uc(shift);
     296   my $car = '00' . uc shift;
    285297
    286298   return '00' if $car eq '00UNKNOW';
     
    288300      '0'=>'0',  '1'=>'1',  '2'=>'2',  '3'=>'3',  '4'=>'4',
    289301      '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',
    291303      );
    292    my @chars = split(//, $car);
     304   my @chars = split m//xms, $car;
    293305   return $table{$chars[-2]}*16 + $table{$chars[-1]};
    294306   }
     
    300312   my $arp = shift;
    301313
    302    my @paquets = split /:/, $arp;
    303    my $return = '';
     314   my @paquets = split m/ : /xms, $arp;
     315   my $return = q{};
    304316   foreach(@paquets) {
    305       $return .= ".".hex_to_dec($_);
     317      $return .= q{.} . hex_to_dec($_);
    306318      }
    307319   return $return;
     
    311323# va rechercher le port et le switch sur lequel est la machine
    312324sub find_switch_port {
    313    my $arp = shift;
    314    my $switch_proposal = shift || '';
    315    
     325   my $arp             = shift;
     326   my $switch_proposal = shift || q{};
     327
    316328   my %ret;
    317    $ret{switch_description} = "unknow";
    318    $ret{switch_port} = "0";
     329   $ret{switch_description} = 'unknow';
     330   $ret{switch_port} = '0';
    319331
    320332   return %ret if $arp eq 'unknow';;
    321333
    322334   my @switch_search = @SWITCH;
    323    if ($switch_proposal ne '') {
     335   if ($switch_proposal ne q{}) {
    324336      for my $sw (@SWITCH) {
    325337         next if $sw->{hostname} ne $switch_proposal;
     
    329341      }
    330342
    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
    333345   LOOP_ON_SWITCH:
    334346   for my $sw (@switch_search) {
     
    339351         -varbindlist => [$research]
    340352         );
    341       if (not defined($result) or $result->{$research} eq 'noSuchInstance') {
     353      if (not defined $result or $result->{$research} eq 'noSuchInstance') {
    342354         $session->close;
    343355         next LOOP_ON_SWITCH;
     
    348360
    349361         # IMPORTANT !!
    350          # ceci empeche la detection sur certains port ... 
     362         # ceci empeche la detection sur certains port ...
    351363         # en effet les switch sont relies entre eux par un cable reseau et du coup
    352364         # tous les arp de toutes les machines sont presentes sur ces ports (ceux choisis ici sont les miens)
     
    364376            $ret{switch_description} = $sw->{description};
    365377            $ret{switch_port}        = get_human_readable_port($sw->{model}, $swport); # $swport;
    366            
     378
    367379            last LOOP_ON_SWITCH;
    368380#            }
     
    386398      }
    387399
    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);
    389401   LOOP_ON_ALL_SWITCH:
    390402   for my $sw (@SWITCH) {
     
    396408         );
    397409
    398       if(defined($result) and $result->{$research} ne 'noSuchInstance'){
     410      if(defined $result and $result->{$research} ne 'noSuchInstance'){
    399411         my $swport = $result->{$research};
    400412
     
    422434   return $KLASK_CFG->{network}{$network}{interface};
    423435   }
    424  
     436
    425437###
    426438# liste l'ensemble des adresses ip d'un réseau
     
    433445      my @line  = @{$KLASK_CFG->{network}{$net}{'ip-subnet'}};
    434446      for my $cmd (@line) {
    435          for my $method (keys %$cmd){
     447         for my $method (keys %{$cmd}){
    436448            $cidrlist->add_any($cmd->{$method}) if $method eq 'add';
    437449            }
     
    443455   for my $cidr ($cidrlist->list()) {
    444456      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;
    447459         push @res,  $ip;
    448460         }
     
    468480   my $sw_model = shift;
    469481   my $sw_port  = shift;
    470    
     482
    471483   return $sw_port if not $sw_model eq 'HP8000M';
    472    
     484
    473485   my $reste = (($sw_port - 1) % 8) + 1;
    474    my $major = int( ($sw_port - 1) / 8 );
     486   my $major = int (($sw_port - 1) / 8);
    475487
    476488   return "$INTERNAL_PORT_MAP{$major}$reste";
     
    480492   my $sw_model = shift;
    481493   my $sw_port  = shift;
    482    
     494
    483495   return $sw_port if not $sw_model eq 'HP8000';
    484496
    485    my $letter = substr($sw_port, 0, 1);
    486    
     497   my $letter = substr $sw_port, 0, 1;
     498
    487499#   return $port if $letter =~ m/\d/;
    488    
    489    my $reste =  substr($sw_port, 1);
    490    
     500
     501   my $reste =  substr $sw_port, 1;
     502
    491503   return $INTERNAL_PORT_MAP_REV{$letter} * 8 + $reste;
    492504   }
     
    498510sub cmd_help {
    499511
    500 print <<END;
     512print <<'END';
    501513klask - ports manager and finder for switch
    502514
     
    514526 klask status  switch port
    515527END
     528   return;
    516529   }
    517530
    518531sub cmd_version {
    519532
    520 print <<END;
     533print <<'END';
    521534Klask - ports manager and finder for switch
    522535Copyright (C) 2005-2008 Gabriel Moreau
     
    526539   print ' $Date$'."\n";
    527540   print ' $Id$'."\n";
     541   return;
    528542   }
    529543
    530544sub cmd_search {
    531545   my @computer = @_;
    532    
     546
    533547   init_switch_names();    #nomme les switchs
    534548   fastping(@computer);
     
    536550      my %resol_arp = resolve_ip_arp_host($clientname);          #resolution arp
    537551      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"
    539553         unless $where{switch_description} eq 'unknow' and $resol_arp{hostname_fq} eq 'unknow' and $resol_arp{mac_address} eq 'unknow';
    540554      }
     555   return;
    541556   }
    542557
     
    546561   fastping(@computer);
    547562   my $computerdb = YAML::Syck::LoadFile("$KLASK_DB_FILE");
    548    
     563
    549564   LOOP_ON_COMPUTER:
    550565   for my $clientname (@computer) {
    551566      my %resol_arp = resolve_ip_arp_host($clientname);      #resolution arp
    552567      my $ip = $resol_arp{ipv4_address};
    553      
     568
    554569      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};
    557572      $year += 1900;
    558573      $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;
    560575
    561576      printf "%-22s %2s %-30s %-15s %-18s %s\n",
     
    567582         $date;
    568583      }
     584   return;
    569585   }
    570586
     
    583599
    584600   my $number_of_computer = get_list_ip(@network); # + 1;
    585    my $size_of_database   = keys %$computerdb;
     601   my $size_of_database   = keys %{$computerdb};
    586602      $size_of_database   = 1 if $size_of_database == 0;
    587603   my $i = 0;
     
    595611   my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}};
    596612   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;
    599615      $db_switch_chained_port{$sw_connect} .= "$port_connect:";
    600616      }
     
    603619      if ( exists $db_switch_chained_port{$sw->{hostname}} ) {
    604620         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}};
    606622         }
    607623#      print "$sw->{hostname} ++ @{$sw->{portignore}}\n";
     
    628644      for my $one_computer (@computer) {
    629645         $i++;
    630          
    631          my $total_percent = int(($i*100)/$number_of_computer);
     646
     647         my $total_percent = int (($i*100)/$number_of_computer);
    632648
    633649         my $localtime = time - $timestamp;
    634          my ($sec,$min) = localtime($localtime);
     650         my ($sec,$min) = localtime $localtime;
    635651
    636652         my $time_elapse = 0;
    637653            $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;
    639655
    640656         printf "\rComputer scanned: %4i/%i (%2i%%)",  $i,                 $number_of_computer, $total_percent;
    641657#         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;
    646662
    647663         my %resol_arp = resolve_ip_arp_host($one_computer,$current_interface);
    648          
     664
    649665         # do not search on router connection (why ?)
    650666         if ( exists $router_mac_ip{$resol_arp{mac_address}}) {
     
    659675            }
    660676
    661          my $switch_proposal = '';
     677         my $switch_proposal = q{};
    662678         if (exists $computerdb->{$resol_arp{ipv4_address}} and exists $computerdb->{$resol_arp{ipv4_address}}{switch_hostname}) {
    663679            $switch_proposal = $computerdb->{$resol_arp{ipv4_address}}{switch_hostname};
     
    716732         # mise a jour du nom de la machine si modification dans le dns
    717733         $computerdb->{$resol_arp{ipv4_address}}{hostname_fq} = $resol_arp{hostname_fq};
    718        
     734
    719735         # mise à jour de la date de détection si détection plus récente par arpwatch
    720736         $computerdb->{$resol_arp{ipv4_address}}{timestamp}   = $resol_arp{timestamp} if exists $resol_arp{timestamp} and $computerdb->{$resol_arp{ipv4_address}}{timestamp} < $resol_arp{timestamp};
    721737
    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        
    727738         # relance un arping sur la machine si celle-ci n'a pas été détectée depuis plus d'une semaine
    728739#         push @computer_not_detected, $resol_arp{ipv4_address} if $computerdb->{$resol_arp{ipv4_address}}{timestamp} < $timestamp_last_week;
    729740         $computer_not_detected{$resol_arp{ipv4_address}} = $current_interface if $computerdb->{$resol_arp{ipv4_address}}{timestamp} < $timestamp_last_week;
    730        
     741
    731742         }
    732743      }
     
    736747
    737748   my $dirdb = $KLASK_DB_FILE;
    738       $dirdb =~ s#/[^/]*$##;
     749      $dirdb =~ s{ / [^/]* $}{}xms;
    739750   mkdir "$dirdb", 0755 unless -d "$dirdb";
    740751   YAML::Syck::DumpFile("$KLASK_DB_FILE", $computerdb);
     
    745756#      print  "arping -c 1 -w 1 -rR -i $interface $one_computer 2>/dev/null\n";
    746757      }
     758   return;
    747759   }
    748760
     
    763775
    764776   my $dirdb = $KLASK_DB_FILE;
    765       $dirdb =~ s#/[^/]*$##;
     777      $dirdb =~ s{ / [^/]* $}{}xms;
    766778   mkdir "$dirdb", 0755 unless -d "$dirdb";
    767779   YAML::Syck::DumpFile("$KLASK_DB_FILE", $computerdb);
     780   return;
    768781   }
    769782
     
    783796
    784797   $format = 'txt' if not defined $possible_format{$format};
    785    
     798
    786799   $possible_format{$format}->(@ARGV);
     800   return;
    787801   }
    788802
     
    796810
    797811   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
    800814#      next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq 'unknow';
    801815
     
    805819# dans le futur
    806820#      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};
    809823      $year += 1900;
    810824      $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;
    812826
    813827      printf "%-25s  %2s  <-------  %-30s %-15s %-18s %s\n",
     
    819833         $date;
    820834      }
     835   return;
    821836   }
    822837
     
    829844#<script src="sorttable-klask.js"></script>
    830845
    831    print <<END;
     846   print <<'END_HTML';
    832847<table class="sortable" summary="Klask export database">
    833848 <caption>Klask database</caption>
     
    855870 </tfoot>
    856871 <tbody>
    857 END
     872END_HTML
    858873
    859874   my %mac_count = ();
    860875   LOOP_ON_IP_ADDRESS:
    861    foreach my $ip (keys %$computerdb) {
    862    
     876   foreach my $ip (keys %{$computerdb}) {
     877
    863878      # to be improve in the future
    864879      next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself !
    865      
     880
    866881      $mac_count{$computerdb->{$ip}{mac_address}}++;
    867882      }
     
    870885
    871886   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
    874889      # to be improve in the future
    875890      next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself !
    876891
    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};
    878893      $year += 1900;
    879894      $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;
    881896
    882897#      $odd_or_even++;
    883898#      my $typerow = $odd_or_even % 2 ? 'odd' : 'even';
    884       $typerow = $typerow eq 'even' ? 'odd' : 'even'; 
     899      $typerow = $typerow eq 'even' ? 'odd' : 'even';
    885900
    886901      my $switch_hostname = $computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description} || 'unkown';
    887902      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";
    898913  <tr class="$typerow">
    899914   <td sorttable_customkey="$switch_hostname_sort">$switch_hostname</td>
     
    905920   <td>$date</td>
    906921  </tr>
    907 END
     922END_HTML
    908923      }
    909924
     
    917932   for my $sw (sort keys %db_switch_output_port) {
    918933
    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';
    922937
    923938      if (exists $db_switch_parent{$sw}) {
     
    930945      $year += 1900;
    931946      $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";
    942956  <tr class="$typerow">
    943957   <td sorttable_customkey="$switch_hostname_sort">$sw</td>
     
    949963   <td>$date</td>
    950964  </tr>
    951 END
     965END_HTML
    952966         }
    953967      else {
    954 #         printf "%-25s  %2s  +-->  router\n", $sw, $db_switch_output_port{$sw};
    955 print <<END;
     968         print <<"END_HTML";
    956969  <tr class="$typerow">
    957970   <td sorttable_customkey="$switch_hostname_sort">$sw</td>
     
    963976   <td></td>
    964977  </tr>
    965 END
     978END_HTML
    966979         }
    967980      }
    968981
    969982   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;
    971984      for my $sw (keys %{$db_switch_connected_on_port{$swport}}) {
    972985
    973          my $switch_hostname_sort = sprintf "%s %3s" ,$sw_connect, $port_connect;
     986         my $switch_hostname_sort = sprintf '%s %3s' ,$sw_connect, $port_connect;
    974987
    975988      my $mac_address = $db_switch{$sw}->{mac_address};
     
    980993      $year += 1900;
    981994      $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';
    9891002
    9901003         if (exists $db_switch_output_port{$sw}) {
    9911004
    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";
    9951008  <tr class="$typerow">
    9961009   <td sorttable_customkey="$switch_hostname_sort">$sw_connect</td>
     
    10021015   <td>$date</td>
    10031016  </tr>
    1004 END
     1017END_HTML
    10051018            }
    10061019         else {
    1007 print <<END;
     1020            print <<"END_HTML";
    10081021  <tr class="$typerow">
    10091022   <td sorttable_customkey="$switch_hostname_sort">$sw_connect</td>
     
    10151028   <td>$date</td>
    10161029  </tr>
    1017 END
    1018             }
    1019          }
    1020       }
    1021 
    1022 
    1023    print <<END;
     1030END_HTML
     1031            }
     1032         }
     1033      }
     1034
     1035   print <<'END_HTML';
    10241036 </tbody>
    10251037</table>
    1026 END
     1038END_HTML
     1039   return;
    10271040   }
    10281041
     
    10311044
    10321045   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})) {
    10341047
    10351048      next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself !
    10361049
    1037       my $sw_hostname = $computerdb->{$ip}{switch_hostname} || '';
     1050      my $sw_hostname = $computerdb->{$ip}{switch_hostname} || q{};
    10381051      next if $sw_hostname eq 'unknow';
    1039  
    1040       my $sw_location = '';
     1052
     1053      my $sw_location = q{};
    10411054      for my $sw (@SWITCH) {
    10421055         next if $sw_hostname ne $sw->{hostname};
     
    10451058         }
    10461059
    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;
    10491063   }
    10501064
     
    10521066   my $switch = shift;
    10531067   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)
    10571071   system "snmpset -v 1 -c public $switch 1.3.6.1.2.1.2.2.1.7.$port = 1";
     1072   return;
    10581073   }
    10591074
     
    10611076   my $switch = shift;
    10621077   my $port   = shift;
    1063    
     1078
    10641079   system "snmpset -v 1 -c public $switch 1.3.6.1.2.1.2.2.1.7.$port = 2";
     1080   return;
    10651081   }
    10661082
     
    10681084   my $switch = shift;
    10691085   my $port   = shift;
    1070    
     1086
    10711087   system "snmpget -v 1 -c public $switch 1.3.6.1.2.1.2.2.1.7.$port";
     1088   return;
    10721089   }
    10731090
    10741091sub 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{}) {
    10791096      die "Usage: klask search-mac-on-switch SWITCH_NAME MAC_ADDRESS\n";
    10801097      }
    10811098
    10821099   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";
    10841101      }
    10851102
     
    10881105      $session{-version} = $sw->{version}   || 1;
    10891106      $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') {
    10911108      $session{-username} = $sw->{username} || 'snmpadmin';
    10921109      }
     
    10951112      }
    10961113
    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);
    10981115   print "Klask search OID $research on switch $switch_name\n";
    10991116
     
    11041121      -varbindlist => [$research]
    11051122      );
    1106    
    1107    if (not defined($result) or $result->{$research} eq 'noSuchInstance') {
     1123
     1124   if (not defined $result or $result->{$research} eq 'noSuchInstance') {
    11081125      print "Klask do not find MAC $mac_address on switch $switch_name\n";
    11091126      $session->close;
     
    11141131
    11151132   print "Klask find MAC $mac_address on switch $switch_name port $swport\n";
     1133   return;
    11161134   }
    11171135
     
    11281146#   for my $one_computer ('194.254.66.254') {
    11291147   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
    11321149      next DETECT_ALL_ROUTER if $resol_arp{mac_address} eq 'unknow';
    1133 #print "$one_router\n";     
    11341150      $where{$resol_arp{ipv4_address}} = find_all_switch_port($resol_arp{mac_address}); # retrouve les emplacements des routeurs
    11351151      }
     
    11371153   ALL_ROUTER_IP_ADDRESS:
    11381154   for my $ip (Net::Netmask::sort_by_ip_address(keys %where)) { # '194.254.66.254')) {
    1139    
     1155
    11401156      next ALL_ROUTER_IP_ADDRESS if not exists $where{$ip}; # /a priori/ idiot car ne sers à rien...
    11411157
     
    11461162
    11471163         next ALL_SWITCH_CONNECTED if $switch->{port} eq '0';
    1148          
     1164
    11491165         $db_switch_output_port{$switch->{hostname}} = $switch->{port};
    11501166         }
    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      }
    11601168
    11611169   my %db_switch_link_with = ();
     
    11711179   ALL_SWITCH:
    11721180   for my $one_computer (@list_switch_ip) {
    1173       my %resol_arp = resolve_ip_arp_host($one_computer,'*','low'); # arp resolution
     1181      my %resol_arp = resolve_ip_arp_host($one_computer, q{*}, q{low}); # arp resolution
    11741182      next ALL_SWITCH if $resol_arp{mac_address} eq 'unknow';
    1175      
     1183
    11761184      push @list_switch_ipv4,$resol_arp{ipv4_address};
    1177      
     1185
    11781186      $where{$resol_arp{ipv4_address}} = find_all_switch_port($resol_arp{mac_address}); # find port on all switch
    11791187
     
    11841192      $SWITCH_DB{$one_computer}->{timestamp}    = $timestamp;
    11851193      }
    1186      
     1194
    11871195   ALL_SWITCH_IP_ADDRESS:
    11881196   for my $ip (Net::Netmask::sort_by_ip_address(@list_switch_ipv4)) {
    1189    
     1197
    11901198      next ALL_SWITCH_IP_ADDRESS if not exists $where{$ip};
    11911199
     
    12061214
    12071215      }
    1208    
     1216
    12091217   my %db_switch_connected_on_port = ();
    12101218   my $maybe_more_than_one_switch_connected = 'yes';
    1211    
     1219
    12121220   while ($maybe_more_than_one_switch_connected eq 'yes') {
    12131221      for my $sw (keys %db_switch_link_with) {
    12141222         for my $connect (keys %{$db_switch_link_with{$sw}}) {
    1215          
     1223
    12161224            my $port = $db_switch_link_with{$sw}->{$connect};
    1217          
     1225
    12181226            $db_switch_connected_on_port{"$connect:$port"} ||= {};
    12191227            $db_switch_connected_on_port{"$connect:$port"}->{$sw}++; # Just to define the key
     
    12251233      SWITCH_AND_PORT:
    12261234      for my $swport (keys %db_switch_connected_on_port) {
    1227          
     1235
    12281236         next if keys %{$db_switch_connected_on_port{$swport}} == 1;
    1229          
     1237
    12301238         $maybe_more_than_one_switch_connected = 'yes';
    12311239
    1232          my ($sw_connect,$port_connect) = split ':', $swport;
     1240         my ($sw_connect,$port_connect) = split m/ : /xms, $swport;
    12331241         my @sw_on_same_port = keys %{$db_switch_connected_on_port{$swport}};
    12341242
    12351243         CONNECTED:
    12361244         for my $sw_connected (@sw_on_same_port) {
    1237            
     1245
    12381246            next CONNECTED if not keys %{$db_switch_link_with{$sw_connected}} == 1;
    1239            
     1247
    12401248            $db_switch_connected_on_port{$swport} = {$sw_connected => 1};
    1241            
     1249
    12421250            for my $other_sw (@sw_on_same_port) {
    12431251               next if $other_sw eq $sw_connected;
    1244                
     1252
    12451253               delete $db_switch_link_with{$other_sw}->{$sw_connect};
    12461254               }
    1247            
     1255
    12481256            # We can not do better for this switch for this loop
    12491257            next SWITCH_AND_PORT;
     
    12561264   for my $sw (keys %db_switch_link_with) {
    12571265      for my $connect (keys %{$db_switch_link_with{$sw}}) {
    1258      
     1266
    12591267         my $port = $db_switch_link_with{$sw}->{$connect};
    1260      
     1268
    12611269         $db_switch_connected_on_port{"$connect:$port"} ||= {};
    12621270         $db_switch_connected_on_port{"$connect:$port"}->{$sw} = $port;
    1263        
     1271
    12641272         $db_switch_parent{$sw} = {switch => $connect, port => $port};
    12651273         }
    12661274      }
    12671275
    1268    print "Switch output port and parent port connection\n"; 
     1276   print "Switch output port and parent port connection\n";
    12691277   print "---------------------------------------------\n";
    12701278   for my $sw (sort keys %db_switch_output_port) {
     
    12801288   print "Switch parent and children port inter-connection\n";
    12811289   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;
    12841292      for my $sw (keys %{$db_switch_connected_on_port{$swport}}) {
    12851293         if (exists $db_switch_output_port{$sw}) {
     
    12991307      switch_db         => \%SWITCH_DB,
    13001308      };
    1301      
     1309
    13021310   YAML::Syck::DumpFile("$KLASK_SW_FILE", $switch_connection);
     1311   return;
    13031312   }
    13041313
     
    13201329
    13211330   $format = 'txt' if not defined $possible_format{$format};
    1322    
     1331
    13231332   $possible_format{$format}->(@ARGV);
     1333   return;
    13241334   }
    13251335
     
    13321342   my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}};
    13331343
    1334    print "Switch output port and parent port connection\n"; 
     1344   print "Switch output port and parent port connection\n";
    13351345   print "---------------------------------------------\n";
    13361346   for my $sw (sort keys %db_switch_output_port) {
     
    13461356   print "Switch parent and children port inter-connection\n";
    13471357   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;
    13501360      for my $sw (keys %{$db_switch_connected_on_port{$swport}}) {
    13511361         if (exists $db_switch_output_port{$sw}) {
     
    13571367         }
    13581368      }
     1369   return;
    13591370   }
    13601371
     
    13621373
    13631374   my $switch_connection = YAML::Syck::LoadFile("$KLASK_SW_FILE");
    1364    
     1375
    13651376   my %db_switch_output_port       = %{$switch_connection->{output_port}};
    13661377   my %db_switch_parent            = %{$switch_connection->{parent}};
     
    13711382   my %db_building= ();
    13721383   for my $sw (@SWITCH) {
    1373       my ($building, $location) = split /\//, $sw->{location}, 2;
     1384      my ($building, $location) = split m/ \/ /xms, $sw->{location}, 2;
    13741385      $db_building{$building} ||= {};
    13751386      $db_building{$building}->{$location} ||= {};
    13761387      $db_building{$building}->{$location}{ $sw->{hostname} } = 'y';
    13771388      }
    1378  
    1379  
     1389
     1390
    13801391   print "digraph G {\n";
    13811392
     
    13861397   for my $building (keys %db_building) {
    13871398      $b++;
    1388      
     1399
    13891400      print "\"building$b\" [label = \"$building\", color = black, fillcolor = gold, style = filled];\n";
    13901401      print "site -> \"building$b\" [len = 2, color = firebrick];\n";
     
    13931404      for my $loc (keys %{$db_building{$building}}) {
    13941405         $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";
    13971408#         print "\"location$b-$l\" [label = \"$building / $loc\", color = black, fillcolor = orange, style = filled];\n";
    13981409         print "\"building$b\" -> \"location$b-$l\" [len = 2, color = firebrick]\n";
     
    14031414
    14041415            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};
    14061417            print "\"$sw\" [label = \"$swname\", color = black, fillcolor = palegreen, shape = rect, style = filled];\n";
    14071418            print "\"location$b-$l\" -> \"$sw\" [len = 2, color = firebrick, arrowtail = dot]\n";
     
    14101421
    14111422            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;
    14131424               next if not $sw_connect eq $sw;
    14141425               next if $port_connect eq $db_switch_output_port{$sw};
     
    14201431      }
    14211432
    1422 #   print "Switch output port and parent port connection\n"; 
     1433#   print "Switch output port and parent port connection\n";
    14231434#   print "---------------------------------------------\n";
    14241435   for my $sw (sort keys %db_switch_output_port) {
     
    14341445#   print "Switch parent and children port inter-connection\n";
    14351446#   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;
    14381449      for my $sw (keys %{$db_switch_connected_on_port{$swport}}) {
    14391450         if (exists $db_switch_output_port{$sw}) {
     
    14471458
    14481459print "}\n";
     1460   return;
    14491461   }
    14501462
     
    14571469
    14581470
    1459 =head1 SYNOPSIS
     1471=head1 USAGE
    14601472
    14611473 klask updatedb
     
    15911603
    15921604
    1593 =head1 COPYRIGHT
    1594        
    1595 Copyright (C) 2005-2008 Gabriel Moreau.
    1596 
    1597 
    1598 =head1 LICENCE
     1605=head1 LICENSE AND COPYRIGHT
    15991606
    16001607GPL version 2 or later and Perl equivalent
    16011608
     1609Copyright (C) 2005-2009 Gabriel Moreau.
Note: See TracChangeset for help on using the changeset viewer.