Index: /trunk/klask
===================================================================
--- /trunk/klask	(revision 20)
+++ /trunk/klask	(revision 30)
@@ -5,5 +5,5 @@
 
 use Net::SNMP;
-use YAML qw(:all);
+use YAML;
 use Net::Netmask;
 use Net::CIDR::Lite;
@@ -12,9 +12,13 @@
 
 # apt-get install snmp fping libnet-cidr-lite-perl libnet-netmask-perl libnet-snmp-perl libnetaddr-ip-perl libyaml-perl
-# libcrypt-des-perl libcrypt-hcesha-perl   libdigest-hmac-perl
-
-my $KLASK_DB_FILE  = '/var/cache/klask/klaskdb';
-my $KLASK_SW_FILE  = '/var/cache/klask/switchdb';
+# libcrypt-des-perl libcrypt-hcesha-perl libdigest-hmac-perl
+# arping fping bind9-host arpwatch
+
+my $KLASK_VAR      = '/var/cache/klask';
 my $KLASK_CFG_FILE = '/etc/klask.conf';
+my $KLASK_DB_FILE  = "$KLASK_VAR/klaskdb";
+my $KLASK_SW_FILE  = "$KLASK_VAR/switchdb";
+
+test_running_environnement();
 
 my $KLASK_CFG = YAML::LoadFile("$KLASK_CFG_FILE");
@@ -24,7 +28,9 @@
 
 my %switch_level = ();
+my %SWITCH_DB = ();
 LEVEL_OF_EACH_SWITCH:
 for my $sw (@SWITCH){
    $switch_level{$sw->{hostname}} = $sw->{level} || $DEFAULT{switch_level}  || 2;
+   $SWITCH_DB{$sw->{hostname}} = $sw;
    }
 @SWITCH = sort { $switch_level{$b->{hostname}} <=> $switch_level{$a->{hostname}} } @{$KLASK_CFG->{switch}};  
@@ -63,7 +69,10 @@
    J3299A => { model => 'HP224M',     match => 'HP J3299A ProCurve Switch 224M'  },
    J4120A => { model => 'HP1600M',    match => 'HP J4120A ProCurve Switch 1600M' },
+   J9029A => { model => 'HP1800-8G',  match => 'PROCURVE J9029A'                 },
    J4093A => { model => 'HP2424M',    match => 'HP J4093A ProCurve Switch 2424M' },
    J4813A => { model => 'HP2524',     match => 'HP J4813A ProCurve Switch 2524'  },
-   J4900A => { model => 'HP2626',     match => 'HP J4900[A|B] ProCurve Switch 2626'  },
+   J4900A => { model => 'HP2626A',    match => 'HP J4900A ProCurve Switch 2626'  },
+   J4900B => { model => 'HP2626B',    match => 'HP J4900B ProCurve Switch 2626'  },
+   J4899B => { model => 'HP2650',     match => 'ProCurve J4899B Switch 2650'     },
    J9021A => { model => 'HP2810-24G', match => 'ProCurve J9021A Switch 2810-24G' },
    J4903A => { model => 'HP2824',     match => 'J4903A.+?Switch 2824,'           },
@@ -95,4 +104,9 @@
 exit;
 
+sub test_running_environnement {
+   die "Configuration file $KLASK_CFG_FILE does not exists. Klask need it !\n" if not -e "$KLASK_CFG_FILE";
+   die "Var folder $KLASK_VAR does not exists. Klask need it !\n"              if not -d "$KLASK_VAR";
+   }
+
 ###
 # fast ping dont l'objectif est de remplir la table arp de la machine
@@ -122,5 +136,5 @@
    #my $cmd = "grep  -e '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/arp.dat | sort +2rn | head -1";
 #   my $cmd = "grep  -he '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/*.dat | sort +2rn | head -1";
-   my $cmd = "grep  -he '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/$interface.dat | sort +2rn | head -1";
+   my $cmd = "grep  -he '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/$interface.dat | sort -rn -k 2 | head -1";
    my $cmd_arpwatch = `$cmd`;
    chomp $cmd_arpwatch;
@@ -180,13 +194,13 @@
 # Find Surname of a switch
 sub get_switch_model {
-   my $sw_description = shift || '?';
+   my $sw_snmp_description = shift || 'unknow';
    
    for my $sw_kind (keys %SWITCH_KIND) {
-      next if not $sw_description =~ m/$SWITCH_KIND{$sw_kind}->{match}/;
+      next if not $sw_snmp_description =~ m/$SWITCH_KIND{$sw_kind}->{match}/;
       
       return $SWITCH_KIND{$sw_kind}->{model};
       }
       
-   return $sw_description;
+   return $sw_snmp_description;
    }
 
@@ -230,5 +244,6 @@
       #$sw->{contact} = $result->{"1.3.6.1.2.1.1.4.0"} || $sw->{hostname};
       $session->close;
- 
+
+      # Ligne à virer car on récupère maintenant le modèle du switch 
       my ($desc, $type) = split ':', $sw->{description}, 2;
 #      printf "%-25s 0--------->>>> %-25s %s\n", $sw->{hostname}, $desc, uc($type)."**" if $verbose;
@@ -280,9 +295,9 @@
    return %ret if $arp eq 'unknow';;
 
-   my @SWITCH_search = @SWITCH;
+   my @switch_search = @SWITCH;
    if ($switch_proposal ne '') {
       for my $sw (@SWITCH) {
          next if $sw->{hostname} ne $switch_proposal;
-         unshift @SWITCH_search, $sw;
+         unshift @switch_search, $sw;
          last;
          }
@@ -292,18 +307,12 @@
    
    LOOP_ON_SWITCH:
-   for my $sw (@SWITCH_search) {
+   for my $sw (@switch_search) {
       my ($session, $error) = Net::SNMP->session( %{$sw->{local_session}} );
-print "$error \n" if $error;
-#         -hostname   => $sw->{hostname},
-#         -community  => $sw->{community} || $DEFAULT{community} || 'public',
-#         -port       => $sw->{snmpport}  || $DEFAULT{snmpport}  || 161
-#         );
-#print "$sw->{hostname} --  $research \n";
+      print "$error \n" if $error;
+
       my $result = $session->get_request(
          -varbindlist => [$research]
          );
-#      if(defined($result)) {
       if (not defined($result) or $result->{$research} eq 'noSuchInstance') {
-#print "$sw->{hostname} --  $research --".$session->error()."\n";
          $session->close;
          next LOOP_ON_SWITCH;
@@ -322,5 +331,5 @@
          SWITCH_PORT_IGNORE:
          foreach my $p (@{$sw->{portignore}}) {
-            next SWITCH_PORT_IGNORE if $swport ne get_numerical_port($sw->{hostname},$p);
+            next SWITCH_PORT_IGNORE if $swport ne get_numerical_port($sw->{model},$p);
 #            $flag = 1;
             next LOOP_ON_SWITCH;
@@ -329,5 +338,5 @@
             $ret{switch_hostname}    = $sw->{hostname};
             $ret{switch_description} = $sw->{description};
-            $ret{switch_port}        = get_human_readable_port($sw->{hostname}, $swport); # $swport;
+            $ret{switch_port}        = get_human_readable_port($sw->{model}, $swport); # $swport;
             
             last LOOP_ON_SWITCH;
@@ -368,5 +377,5 @@
          $ret->{$sw->{hostname}}{hostname}    = $sw->{hostname};
          $ret->{$sw->{hostname}}{description} = $sw->{description};
-         $ret->{$sw->{hostname}}{port}        = get_human_readable_port($sw->{hostname}, $swport);
+         $ret->{$sw->{hostname}}{port}        = get_human_readable_port($sw->{model}, $swport);
 
          $SWITCH_PORT_COUNT{$sw->{hostname}}->{$swport}++;
@@ -432,11 +441,11 @@
 
 sub get_human_readable_port {
-   my $sw = shift;
-   my $port = shift;
-   
-   return $port if not $sw eq 'sw8000-batA.hmg.priv';
-   
-   my $reste = (($port - 1) % 8) + 1;
-   my $major = int( ($port - 1) / 8 );
+   my $sw_model = shift;
+   my $sw_port  = shift;
+   
+   return $sw_port if not $sw_model eq 'HP8000M';
+   
+   my $reste = (($sw_port - 1) % 8) + 1;
+   my $major = int( ($sw_port - 1) / 8 );
 
    return "$INTERNAL_PORT_MAP{$major}$reste";
@@ -444,14 +453,14 @@
 
 sub get_numerical_port {
-   my $sw   = shift;
-   my $port = shift;
-   
-   return $port if not $sw eq 'sw8000-batA.hmg.priv';
-
-   my $letter = substr($port, 0, 1);
+   my $sw_model = shift;
+   my $sw_port  = shift;
+   
+   return $sw_port if not $sw_model eq 'HP8000';
+
+   my $letter = substr($sw_port, 0, 1);
    
 #   return $port if $letter =~ m/\d/;
    
-   my $reste =  substr($port, 1);
+   my $reste =  substr($sw_port, 1);
    
    return $INTERNAL_PORT_MAP_REV{$letter} * 8 + $reste;
@@ -526,5 +535,5 @@
    my $computerdb = YAML::LoadFile("$KLASK_DB_FILE");
    my $timestamp = time;
-   
+
    my %computer_not_detected = ();
    my $timestamp_last_week = $timestamp - (3600 * 24 * 7);
@@ -534,6 +543,25 @@
    my $i = 0;
    my $detected_computer = 0;
-   
+
    init_switch_names('yes');    #nomme les switchs
+
+   { # Remplis le champs portignore des ports d'inter-connection pour chaque switch
+   my $switch_connection = YAML::LoadFile("$KLASK_SW_FILE");
+   my %db_switch_output_port       = %{$switch_connection->{output_port}};
+   my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}};
+   my %db_switch_chained_port = ();
+   for my $swport (keys %db_switch_connected_on_port) {        
+      my ($sw_connect,$port_connect) = split ':', $swport;
+      $db_switch_chained_port{$sw_connect} .= "$port_connect:";
+      }
+   for my $sw (@SWITCH){
+      push @{$sw->{portignore}}, $db_switch_output_port{$sw->{hostname}}  if exists $db_switch_output_port{$sw->{hostname}};
+      if ( exists $db_switch_chained_port{$sw->{hostname}} ) {
+         chop $db_switch_chained_port{$sw->{hostname}};
+         push @{$sw->{portignore}}, split(':',$db_switch_chained_port{$sw->{hostname}});
+         }
+#      print "$sw->{hostname} ++ @{$sw->{portignore}}\n";
+      }
+   }
 
    my %router_mac_ip = ();
@@ -944,4 +972,5 @@
       connected_on_port => \%db_switch_connected_on_port,
       link_with         => \%db_switch_link_with,
+      switch_db         => \%SWITCH_DB,
       };
       
@@ -1011,5 +1040,6 @@
    my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}};
    my %db_switch_link_with         = %{$switch_connection->{link_with}};
-      
+   my %db_switch_global            = %{$switch_connection->{switch_db}};
+
    my %db_building= ();
    for my $sw (@SWITCH) {
@@ -1044,5 +1074,7 @@
             print "\"$sw:$db_switch_output_port{$sw}\" [label = $db_switch_output_port{$sw}, color = black, fillcolor = lightblue,  peripheries = 2, style = filled];\n";
 
-            print "\"$sw\" [label = \"$sw\", color = black, fillcolor = palegreen, shape = rect, style = filled];\n";
+            my $swname  = $sw;
+               $swname .= '\n-\n'."$db_switch_global{$sw}->{model}" if exists $db_switch_global{$sw} and exists $db_switch_global{$sw}->{model};
+            print "\"$sw\" [label = \"$swname\", color = black, fillcolor = palegreen, shape = rect, style = filled];\n";
             print "\"location$b-$l\" -> \"$sw\" [len = 2, color = firebrick, arrowtail = dot]\n";
             print "\"$sw\" -> \"$sw:$db_switch_output_port{$sw}\" [len=2, style=bold, arrowhead = normal, arrowtail = invdot]\n";
