Changes in trunk/klask [20:30]


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r20 r30  
    55
    66use Net::SNMP;
    7 use YAML qw(:all);
     7use YAML;
    88use Net::Netmask;
    99use Net::CIDR::Lite;
     
    1212
    1313# apt-get install snmp fping libnet-cidr-lite-perl libnet-netmask-perl libnet-snmp-perl libnetaddr-ip-perl libyaml-perl
    14 # libcrypt-des-perl libcrypt-hcesha-perl   libdigest-hmac-perl
    15 
    16 my $KLASK_DB_FILE  = '/var/cache/klask/klaskdb';
    17 my $KLASK_SW_FILE  = '/var/cache/klask/switchdb';
     14# libcrypt-des-perl libcrypt-hcesha-perl libdigest-hmac-perl
     15# arping fping bind9-host arpwatch
     16
     17my $KLASK_VAR      = '/var/cache/klask';
    1818my $KLASK_CFG_FILE = '/etc/klask.conf';
     19my $KLASK_DB_FILE  = "$KLASK_VAR/klaskdb";
     20my $KLASK_SW_FILE  = "$KLASK_VAR/switchdb";
     21
     22test_running_environnement();
    1923
    2024my $KLASK_CFG = YAML::LoadFile("$KLASK_CFG_FILE");
     
    2428
    2529my %switch_level = ();
     30my %SWITCH_DB = ();
    2631LEVEL_OF_EACH_SWITCH:
    2732for my $sw (@SWITCH){
    2833   $switch_level{$sw->{hostname}} = $sw->{level} || $DEFAULT{switch_level}  || 2;
     34   $SWITCH_DB{$sw->{hostname}} = $sw;
    2935   }
    3036@SWITCH = sort { $switch_level{$b->{hostname}} <=> $switch_level{$a->{hostname}} } @{$KLASK_CFG->{switch}}; 
     
    6369   J3299A => { model => 'HP224M',     match => 'HP J3299A ProCurve Switch 224M'  },
    6470   J4120A => { model => 'HP1600M',    match => 'HP J4120A ProCurve Switch 1600M' },
     71   J9029A => { model => 'HP1800-8G',  match => 'PROCURVE J9029A'                 },
    6572   J4093A => { model => 'HP2424M',    match => 'HP J4093A ProCurve Switch 2424M' },
    6673   J4813A => { model => 'HP2524',     match => 'HP J4813A ProCurve Switch 2524'  },
    67    J4900A => { model => 'HP2626',     match => 'HP J4900[A|B] ProCurve Switch 2626'  },
     74   J4900A => { model => 'HP2626A',    match => 'HP J4900A ProCurve Switch 2626'  },
     75   J4900B => { model => 'HP2626B',    match => 'HP J4900B ProCurve Switch 2626'  },
     76   J4899B => { model => 'HP2650',     match => 'ProCurve J4899B Switch 2650'     },
    6877   J9021A => { model => 'HP2810-24G', match => 'ProCurve J9021A Switch 2810-24G' },
    6978   J4903A => { model => 'HP2824',     match => 'J4903A.+?Switch 2824,'           },
     
    95104exit;
    96105
     106sub test_running_environnement {
     107   die "Configuration file $KLASK_CFG_FILE does not exists. Klask need it !\n" if not -e "$KLASK_CFG_FILE";
     108   die "Var folder $KLASK_VAR does not exists. Klask need it !\n"              if not -d "$KLASK_VAR";
     109   }
     110
    97111###
    98112# fast ping dont l'objectif est de remplir la table arp de la machine
     
    122136   #my $cmd = "grep  -e '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/arp.dat | sort +2rn | head -1";
    123137#   my $cmd = "grep  -he '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/*.dat | sort +2rn | head -1";
    124    my $cmd = "grep  -he '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/$interface.dat | sort +2rn | head -1";
     138   my $cmd = "grep  -he '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/$interface.dat | sort -rn -k 2 | head -1";
    125139   my $cmd_arpwatch = `$cmd`;
    126140   chomp $cmd_arpwatch;
     
    180194# Find Surname of a switch
    181195sub get_switch_model {
    182    my $sw_description = shift || '?';
     196   my $sw_snmp_description = shift || 'unknow';
    183197   
    184198   for my $sw_kind (keys %SWITCH_KIND) {
    185       next if not $sw_description =~ m/$SWITCH_KIND{$sw_kind}->{match}/;
     199      next if not $sw_snmp_description =~ m/$SWITCH_KIND{$sw_kind}->{match}/;
    186200     
    187201      return $SWITCH_KIND{$sw_kind}->{model};
    188202      }
    189203     
    190    return $sw_description;
     204   return $sw_snmp_description;
    191205   }
    192206
     
    230244      #$sw->{contact} = $result->{"1.3.6.1.2.1.1.4.0"} || $sw->{hostname};
    231245      $session->close;
    232  
     246
     247      # Ligne à virer car on récupère maintenant le modèle du switch
    233248      my ($desc, $type) = split ':', $sw->{description}, 2;
    234249#      printf "%-25s 0--------->>>> %-25s %s\n", $sw->{hostname}, $desc, uc($type)."**" if $verbose;
     
    280295   return %ret if $arp eq 'unknow';;
    281296
    282    my @SWITCH_search = @SWITCH;
     297   my @switch_search = @SWITCH;
    283298   if ($switch_proposal ne '') {
    284299      for my $sw (@SWITCH) {
    285300         next if $sw->{hostname} ne $switch_proposal;
    286          unshift @SWITCH_search, $sw;
     301         unshift @switch_search, $sw;
    287302         last;
    288303         }
     
    292307   
    293308   LOOP_ON_SWITCH:
    294    for my $sw (@SWITCH_search) {
     309   for my $sw (@switch_search) {
    295310      my ($session, $error) = Net::SNMP->session( %{$sw->{local_session}} );
    296 print "$error \n" if $error;
    297 #         -hostname   => $sw->{hostname},
    298 #         -community  => $sw->{community} || $DEFAULT{community} || 'public',
    299 #         -port       => $sw->{snmpport}  || $DEFAULT{snmpport}  || 161
    300 #         );
    301 #print "$sw->{hostname} --  $research \n";
     311      print "$error \n" if $error;
     312
    302313      my $result = $session->get_request(
    303314         -varbindlist => [$research]
    304315         );
    305 #      if(defined($result)) {
    306316      if (not defined($result) or $result->{$research} eq 'noSuchInstance') {
    307 #print "$sw->{hostname} --  $research --".$session->error()."\n";
    308317         $session->close;
    309318         next LOOP_ON_SWITCH;
     
    322331         SWITCH_PORT_IGNORE:
    323332         foreach my $p (@{$sw->{portignore}}) {
    324             next SWITCH_PORT_IGNORE if $swport ne get_numerical_port($sw->{hostname},$p);
     333            next SWITCH_PORT_IGNORE if $swport ne get_numerical_port($sw->{model},$p);
    325334#            $flag = 1;
    326335            next LOOP_ON_SWITCH;
     
    329338            $ret{switch_hostname}    = $sw->{hostname};
    330339            $ret{switch_description} = $sw->{description};
    331             $ret{switch_port}        = get_human_readable_port($sw->{hostname}, $swport); # $swport;
     340            $ret{switch_port}        = get_human_readable_port($sw->{model}, $swport); # $swport;
    332341           
    333342            last LOOP_ON_SWITCH;
     
    368377         $ret->{$sw->{hostname}}{hostname}    = $sw->{hostname};
    369378         $ret->{$sw->{hostname}}{description} = $sw->{description};
    370          $ret->{$sw->{hostname}}{port}        = get_human_readable_port($sw->{hostname}, $swport);
     379         $ret->{$sw->{hostname}}{port}        = get_human_readable_port($sw->{model}, $swport);
    371380
    372381         $SWITCH_PORT_COUNT{$sw->{hostname}}->{$swport}++;
     
    432441
    433442sub get_human_readable_port {
    434    my $sw = shift;
    435    my $port = shift;
    436    
    437    return $port if not $sw eq 'sw8000-batA.hmg.priv';
    438    
    439    my $reste = (($port - 1) % 8) + 1;
    440    my $major = int( ($port - 1) / 8 );
     443   my $sw_model = shift;
     444   my $sw_port = shift;
     445   
     446   return $sw_port if not $sw_model eq 'HP8000M';
     447   
     448   my $reste = (($sw_port - 1) % 8) + 1;
     449   my $major = int( ($sw_port - 1) / 8 );
    441450
    442451   return "$INTERNAL_PORT_MAP{$major}$reste";
     
    444453
    445454sub get_numerical_port {
    446    my $sw  = shift;
    447    my $port = shift;
    448    
    449    return $port if not $sw eq 'sw8000-batA.hmg.priv';
    450 
    451    my $letter = substr($port, 0, 1);
     455   my $sw_model = shift;
     456   my $sw_port = shift;
     457   
     458   return $sw_port if not $sw_model eq 'HP8000';
     459
     460   my $letter = substr($sw_port, 0, 1);
    452461   
    453462#   return $port if $letter =~ m/\d/;
    454463   
    455    my $reste =  substr($port, 1);
     464   my $reste =  substr($sw_port, 1);
    456465   
    457466   return $INTERNAL_PORT_MAP_REV{$letter} * 8 + $reste;
     
    526535   my $computerdb = YAML::LoadFile("$KLASK_DB_FILE");
    527536   my $timestamp = time;
    528    
     537
    529538   my %computer_not_detected = ();
    530539   my $timestamp_last_week = $timestamp - (3600 * 24 * 7);
     
    534543   my $i = 0;
    535544   my $detected_computer = 0;
    536    
     545
    537546   init_switch_names('yes');    #nomme les switchs
     547
     548   { # Remplis le champs portignore des ports d'inter-connection pour chaque switch
     549   my $switch_connection = YAML::LoadFile("$KLASK_SW_FILE");
     550   my %db_switch_output_port       = %{$switch_connection->{output_port}};
     551   my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}};
     552   my %db_switch_chained_port = ();
     553   for my $swport (keys %db_switch_connected_on_port) {       
     554      my ($sw_connect,$port_connect) = split ':', $swport;
     555      $db_switch_chained_port{$sw_connect} .= "$port_connect:";
     556      }
     557   for my $sw (@SWITCH){
     558      push @{$sw->{portignore}}, $db_switch_output_port{$sw->{hostname}}  if exists $db_switch_output_port{$sw->{hostname}};
     559      if ( exists $db_switch_chained_port{$sw->{hostname}} ) {
     560         chop $db_switch_chained_port{$sw->{hostname}};
     561         push @{$sw->{portignore}}, split(':',$db_switch_chained_port{$sw->{hostname}});
     562         }
     563#      print "$sw->{hostname} ++ @{$sw->{portignore}}\n";
     564      }
     565   }
    538566
    539567   my %router_mac_ip = ();
     
    944972      connected_on_port => \%db_switch_connected_on_port,
    945973      link_with         => \%db_switch_link_with,
     974      switch_db         => \%SWITCH_DB,
    946975      };
    947976     
     
    10111040   my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}};
    10121041   my %db_switch_link_with         = %{$switch_connection->{link_with}};
    1013      
     1042   my %db_switch_global            = %{$switch_connection->{switch_db}};
     1043
    10141044   my %db_building= ();
    10151045   for my $sw (@SWITCH) {
     
    10441074            print "\"$sw:$db_switch_output_port{$sw}\" [label = $db_switch_output_port{$sw}, color = black, fillcolor = lightblue,  peripheries = 2, style = filled];\n";
    10451075
    1046             print "\"$sw\" [label = \"$sw\", color = black, fillcolor = palegreen, shape = rect, style = filled];\n";
     1076            my $swname  = $sw;
     1077               $swname .= '\n-\n'."$db_switch_global{$sw}->{model}" if exists $db_switch_global{$sw} and exists $db_switch_global{$sw}->{model};
     1078            print "\"$sw\" [label = \"$swname\", color = black, fillcolor = palegreen, shape = rect, style = filled];\n";
    10471079            print "\"location$b-$l\" -> \"$sw\" [len = 2, color = firebrick, arrowtail = dot]\n";
    10481080            print "\"$sw\" -> \"$sw:$db_switch_output_port{$sw}\" [len=2, style=bold, arrowhead = normal, arrowtail = invdot]\n";
Note: See TracChangeset for help on using the changeset viewer.