Changes in trunk/klask [30:20]


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r30 r20  
    55
    66use Net::SNMP;
    7 use YAML;
     7use YAML qw(:all);
    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 # arping fping bind9-host arpwatch
    16 
    17 my $KLASK_VAR      = '/var/cache/klask';
     14# libcrypt-des-perl libcrypt-hcesha-perl   libdigest-hmac-perl
     15
     16my $KLASK_DB_FILE  = '/var/cache/klask/klaskdb';
     17my $KLASK_SW_FILE  = '/var/cache/klask/switchdb';
    1818my $KLASK_CFG_FILE = '/etc/klask.conf';
    19 my $KLASK_DB_FILE  = "$KLASK_VAR/klaskdb";
    20 my $KLASK_SW_FILE  = "$KLASK_VAR/switchdb";
    21 
    22 test_running_environnement();
    2319
    2420my $KLASK_CFG = YAML::LoadFile("$KLASK_CFG_FILE");
     
    2824
    2925my %switch_level = ();
    30 my %SWITCH_DB = ();
    3126LEVEL_OF_EACH_SWITCH:
    3227for my $sw (@SWITCH){
    3328   $switch_level{$sw->{hostname}} = $sw->{level} || $DEFAULT{switch_level}  || 2;
    34    $SWITCH_DB{$sw->{hostname}} = $sw;
    3529   }
    3630@SWITCH = sort { $switch_level{$b->{hostname}} <=> $switch_level{$a->{hostname}} } @{$KLASK_CFG->{switch}}; 
     
    6963   J3299A => { model => 'HP224M',     match => 'HP J3299A ProCurve Switch 224M'  },
    7064   J4120A => { model => 'HP1600M',    match => 'HP J4120A ProCurve Switch 1600M' },
    71    J9029A => { model => 'HP1800-8G',  match => 'PROCURVE J9029A'                 },
    7265   J4093A => { model => 'HP2424M',    match => 'HP J4093A ProCurve Switch 2424M' },
    7366   J4813A => { model => 'HP2524',     match => 'HP J4813A ProCurve Switch 2524'  },
    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'     },
     67   J4900A => { model => 'HP2626',     match => 'HP J4900[A|B] ProCurve Switch 2626'  },
    7768   J9021A => { model => 'HP2810-24G', match => 'ProCurve J9021A Switch 2810-24G' },
    7869   J4903A => { model => 'HP2824',     match => 'J4903A.+?Switch 2824,'           },
     
    10495exit;
    10596
    106 sub 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 
    11197###
    11298# fast ping dont l'objectif est de remplir la table arp de la machine
     
    136122   #my $cmd = "grep  -e '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/arp.dat | sort +2rn | head -1";
    137123#   my $cmd = "grep  -he '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/*.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";
     124   my $cmd = "grep  -he '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/$interface.dat | sort +2rn | head -1";
    139125   my $cmd_arpwatch = `$cmd`;
    140126   chomp $cmd_arpwatch;
     
    194180# Find Surname of a switch
    195181sub get_switch_model {
    196    my $sw_snmp_description = shift || 'unknow';
     182   my $sw_description = shift || '?';
    197183   
    198184   for my $sw_kind (keys %SWITCH_KIND) {
    199       next if not $sw_snmp_description =~ m/$SWITCH_KIND{$sw_kind}->{match}/;
     185      next if not $sw_description =~ m/$SWITCH_KIND{$sw_kind}->{match}/;
    200186     
    201187      return $SWITCH_KIND{$sw_kind}->{model};
    202188      }
    203189     
    204    return $sw_snmp_description;
     190   return $sw_description;
    205191   }
    206192
     
    244230      #$sw->{contact} = $result->{"1.3.6.1.2.1.1.4.0"} || $sw->{hostname};
    245231      $session->close;
    246 
    247       # Ligne à virer car on récupère maintenant le modèle du switch
     232 
    248233      my ($desc, $type) = split ':', $sw->{description}, 2;
    249234#      printf "%-25s 0--------->>>> %-25s %s\n", $sw->{hostname}, $desc, uc($type)."**" if $verbose;
     
    295280   return %ret if $arp eq 'unknow';;
    296281
    297    my @switch_search = @SWITCH;
     282   my @SWITCH_search = @SWITCH;
    298283   if ($switch_proposal ne '') {
    299284      for my $sw (@SWITCH) {
    300285         next if $sw->{hostname} ne $switch_proposal;
    301          unshift @switch_search, $sw;
     286         unshift @SWITCH_search, $sw;
    302287         last;
    303288         }
     
    307292   
    308293   LOOP_ON_SWITCH:
    309    for my $sw (@switch_search) {
     294   for my $sw (@SWITCH_search) {
    310295      my ($session, $error) = Net::SNMP->session( %{$sw->{local_session}} );
    311       print "$error \n" if $error;
    312 
     296print "$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";
    313302      my $result = $session->get_request(
    314303         -varbindlist => [$research]
    315304         );
     305#      if(defined($result)) {
    316306      if (not defined($result) or $result->{$research} eq 'noSuchInstance') {
     307#print "$sw->{hostname} --  $research --".$session->error()."\n";
    317308         $session->close;
    318309         next LOOP_ON_SWITCH;
     
    331322         SWITCH_PORT_IGNORE:
    332323         foreach my $p (@{$sw->{portignore}}) {
    333             next SWITCH_PORT_IGNORE if $swport ne get_numerical_port($sw->{model},$p);
     324            next SWITCH_PORT_IGNORE if $swport ne get_numerical_port($sw->{hostname},$p);
    334325#            $flag = 1;
    335326            next LOOP_ON_SWITCH;
     
    338329            $ret{switch_hostname}    = $sw->{hostname};
    339330            $ret{switch_description} = $sw->{description};
    340             $ret{switch_port}        = get_human_readable_port($sw->{model}, $swport); # $swport;
     331            $ret{switch_port}        = get_human_readable_port($sw->{hostname}, $swport); # $swport;
    341332           
    342333            last LOOP_ON_SWITCH;
     
    377368         $ret->{$sw->{hostname}}{hostname}    = $sw->{hostname};
    378369         $ret->{$sw->{hostname}}{description} = $sw->{description};
    379          $ret->{$sw->{hostname}}{port}        = get_human_readable_port($sw->{model}, $swport);
     370         $ret->{$sw->{hostname}}{port}        = get_human_readable_port($sw->{hostname}, $swport);
    380371
    381372         $SWITCH_PORT_COUNT{$sw->{hostname}}->{$swport}++;
     
    441432
    442433sub get_human_readable_port {
    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 );
     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 );
    450441
    451442   return "$INTERNAL_PORT_MAP{$major}$reste";
     
    453444
    454445sub get_numerical_port {
    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);
     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);
    461452   
    462453#   return $port if $letter =~ m/\d/;
    463454   
    464    my $reste =  substr($sw_port, 1);
     455   my $reste =  substr($port, 1);
    465456   
    466457   return $INTERNAL_PORT_MAP_REV{$letter} * 8 + $reste;
     
    535526   my $computerdb = YAML::LoadFile("$KLASK_DB_FILE");
    536527   my $timestamp = time;
    537 
     528   
    538529   my %computer_not_detected = ();
    539530   my $timestamp_last_week = $timestamp - (3600 * 24 * 7);
     
    543534   my $i = 0;
    544535   my $detected_computer = 0;
    545 
     536   
    546537   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    }
    566538
    567539   my %router_mac_ip = ();
     
    972944      connected_on_port => \%db_switch_connected_on_port,
    973945      link_with         => \%db_switch_link_with,
    974       switch_db         => \%SWITCH_DB,
    975946      };
    976947     
     
    10401011   my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}};
    10411012   my %db_switch_link_with         = %{$switch_connection->{link_with}};
    1042    my %db_switch_global            = %{$switch_connection->{switch_db}};
    1043 
     1013     
    10441014   my %db_building= ();
    10451015   for my $sw (@SWITCH) {
     
    10741044            print "\"$sw:$db_switch_output_port{$sw}\" [label = $db_switch_output_port{$sw}, color = black, fillcolor = lightblue,  peripheries = 2, style = filled];\n";
    10751045
    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";
     1046            print "\"$sw\" [label = \"$sw\", color = black, fillcolor = palegreen, shape = rect, style = filled];\n";
    10791047            print "\"location$b-$l\" -> \"$sw\" [len = 2, color = firebrick, arrowtail = dot]\n";
    10801048            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.