Changeset 3


Ignore:
Timestamp:
Dec 10, 2007, 5:55:32 PM (16 years ago)
Author:
g7moreau
Message:
  • Last mod from my active server
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r2 r3  
    22
    33use strict;
     4use warnings;
    45
    56use Net::SNMP;
     
    1011
    1112# aptitude install snmp fping libnet-cidr-lite-perl libnet-netmask-perl libnet-snmp-perl libnetaddr-ip-perl libyaml-perl
     13# libcrypt-des-perl libcrypt-hcesha-perl   libdigest-hmac-perl
    1214
    1315our $KLASK_DB  = '/var/cache/klask/klaskdb';
     
    8385   my $param_ip_or_host = shift;
    8486   my $interface = shift || '*';
     87   my $type      = shift || 'fast';
    8588
    8689   my %ret = (
     
    108111   $ret{timestamp}    = $timestamp if $timestamp;
    109112
     113   my $nowtimestamp = time();
     114
     115   if ( $type eq 'fast' and ( not defined $timestamp or $timestamp < ( $nowtimestamp - 3 * 3600 ) ) ) {
     116      $ret{mac_address} = 'unknow';
     117      return %ret;
     118      }
     119
    110120  # resultat de la commande arp
    111121   # tech7meylan.hmg.inpg.fr (194.254.66.240) at 00:14:22:45:28:A9 [ether] on eth0
     
    157167   INIT_EACH_SWITCH:
    158168   for my $sw (@switch) {
    159       my ($session, $error) = Net::SNMP->session(
    160          -hostname   => $sw->{hostname},
    161          -community  => $sw->{community} || $default{community} || 'public',
    162          -port       => $sw->{snmpport}  || $default{snmpport}  || 161,
    163          );
     169#print "$sw->{hostname} \n";
     170      my %session = ( -hostname   => $sw->{hostname} );
     171         $session{-version} = $sw->{version}   || 1;
     172         $session{-port}    = $sw->{snmpport}  || $default{snmpport}  || 161;
     173         if (exists $sw->{version} and $sw->{version} eq 3) {
     174            $session{-username} = $sw->{username} || 'snmpadmin';
     175#print "$sw->{hostname}  -- $session{-username}  \n";
     176#for (keys %session) {
     177#print "++ $_ -> $session{$_}\n";
     178#}
     179            }
     180         else {
     181            $session{-community} = $sw->{community} || $default{community} || 'public';
     182            }
     183
     184      $sw->{local_session} = \%session;
     185
     186      my ($session, $error) = Net::SNMP->session( %{$sw->{local_session}} );
     187#         -hostname   => $sw->{hostname},
     188#         -version    => $sw->{version}   || 1,
     189#         -community  => $sw->{community} || $default{community} || 'public',
     190#         -username   => $sw->{username}  || 'snmpadmin',
     191#         -port       => $sw->{snmpport}  || $default{snmpport}  || 161,
     192#         );
     193#         if (exists $sw->{version} and $sw->{version} eq 3) {
     194print "$error \n" if $error;
     195#}
    164196      my $result = $session->get_request(
    165197         -varbindlist => ['1.3.6.1.2.1.1.5.0']
    166198         );
    167199      $sw->{description} = $result->{"1.3.6.1.2.1.1.5.0"} || $sw->{hostname};
     200      #$sw->{location} = $result->{"1.3.6.1.2.1.1.6.0"} || $sw->{hostname};
     201      #$sw->{contact} = $result->{"1.3.6.1.2.1.1.4.0"} || $sw->{hostname};
    168202      $session->close;
    169203 
     
    230264   LOOP_ON_SWITCH:
    231265   for my $sw (@switch_search) {
    232       my ($session, $error) = Net::SNMP->session(
    233          -hostname   => $sw->{hostname},
    234          -community  => $sw->{community} || $default{community} || 'public',
    235          -port       => $sw->{snmpport}  || $default{snmpport}  || 161
    236          );
     266      my ($session, $error) = Net::SNMP->session( %{$sw->{local_session}} );
     267print "$error \n" if $error;
     268#         -hostname   => $sw->{hostname},
     269#         -community  => $sw->{community} || $default{community} || 'public',
     270#         -port       => $sw->{snmpport}  || $default{snmpport}  || 161
     271#         );
     272#print "$sw->{hostname} --  $research \n";
    237273      my $result = $session->get_request(
    238274         -varbindlist => [$research]
    239275         );
    240276#      if(defined($result)) {
    241       if (not defined($result)) {
     277      if (not defined($result) or $result->{$research} eq 'noSuchInstance') {
     278#print "$sw->{hostname} --  $research --".$session->error()."\n";
    242279         $session->close;
    243280         next LOOP_ON_SWITCH;
     
    291328
    292329   my $research = "1.3.6.1.2.1.17.4.3.1.2".arphextodec($arp);
     330#print " $arp -> $research\n";
    293331   LOOP_ON_ALL_SWITCH:
    294332   for my $sw (@switch) {
    295       my ($session, $error) = Net::SNMP->session(
    296          -hostname   => $sw->{hostname},
    297          -community  => $sw->{community} || $default{community} || 'public',
    298          -port       => $sw->{snmpport}  || $default{snmpport}  || 161
    299          );
     333      my ($session, $error) = Net::SNMP->session( %{$sw->{local_session}} );
     334print "$error \n" if $error;
     335#         -hostname   => $sw->{hostname},
     336#         -community  => $sw->{community} || $default{community} || 'public',
     337#         -port       => $sw->{snmpport}  || $default{snmpport}  || 161
     338#         );
     339#print "$sw->{hostname} --  $research \n";
    300340      my $result = $session->get_request(
    301341         -varbindlist => [$research]
    302342         );
    303       if(defined($result)){
     343      if(defined($result) and $result->{$research} ne 'noSuchInstance'){
     344#print "+++ $sw->{hostname} -> $result->{$research}\n" ;
    304345         my $swport = $result->{$research};
    305346
     
    312353#         $sw->{_port}{$swport}++;
    313354         }
     355      else {
     356#print "$sw->{hostname} --  $research --".$session->error()."\n";
     357         }
    314358
    315359      $session->close;
    316360      }
     361#print "end \n";
    317362   return $ret;
    318363   }
     
    518563            }
    519564
     565         # do not have a mac address
     566         if ($resol_arp{mac_address} eq 'unknow' or (exists $resol_arp{timestamps} and $resol_arp{timestamps} < ($timestamp - 3 * 3600))) {
     567            $computer_not_detected{$one_computer} = $current_interface;
     568            next LOOP_ON_COMPUTER;
     569            }
     570
    520571         my %where = find_switch_port($resol_arp{mac_address},$switch_proposal);
    521572
     
    528579
    529580         # do not have a mac address
    530          if ($resol_arp{mac_address} eq 'unknow') {
    531             $computer_not_detected{$one_computer} = $current_interface;
    532             next LOOP_ON_COMPUTER;
    533             }
     581#         if ($resol_arp{mac_address} eq 'unknow') {
     582#            $computer_not_detected{$one_computer} = $current_interface;
     583#            next LOOP_ON_COMPUTER;
     584#            }
    534585
    535586         # detected on a switch
     
    654705      my $sw_hostname = $computerdb->{$ip}{switch_hostname} || '';
    655706      next if $sw_hostname eq 'unknow';
    656       next if $sw_hostname eq '';
    657707 
    658708      my $sw_location = '';
     
    702752   DETECT_ALL_ROUTER:
    703753   for my $one_computer ('194.254.66.254') {
    704       my %resol_arp = resolve_ip_arp_host($one_computer);            #resolution arp
     754      my %resol_arp = resolve_ip_arp_host($one_computer,'*','low');            #resolution arp
    705755      next DETECT_ALL_ROUTER if $resol_arp{mac_address} eq 'unknow';
    706756     
     
    708758      }
    709759
     760print "etape 1\n";
    710761   ALL_ROUTER_IP_ADDRESS:
    711762   for my $ip (Net::Netmask::sort_by_ip_address('194.254.66.254')) {
     
    723774         }
    724775      }   
     776print "etape 2\n";
    725777
    726778#   print "Switch output port\n"; 
     
    740792      }
    741793
     794print "etape 3\n";
    742795   ALL_SWITCH:
    743796   for my $one_computer (@list_switch_ip) {
    744       my %resol_arp = resolve_ip_arp_host($one_computer); # arp resolution
     797      my %resol_arp = resolve_ip_arp_host($one_computer,'*','low'); # arp resolution
    745798      next ALL_SWITCH if $resol_arp{mac_address} eq 'unknow';
    746799     
     
    752805      }
    753806     
     807print "etape 4\n";
    754808   ALL_SWITCH_IP_ADDRESS:
    755809   for my $ip (Net::Netmask::sort_by_ip_address(@list_switch_ipv4)) {
     
    774828      }
    775829   
     830print "etape 5\n";
    776831   my %db_switch_connected_on_port = ();
    777832   my $maybe_more_than_one_switch_connected = 'yes';
     
    819874      }
    820875
     876print "etape 6\n";
    821877   my %db_switch_parent =();
    822878
Note: See TracChangeset for help on using the changeset viewer.