Changeset 3
- Timestamp:
- Dec 10, 2007, 5:55:32 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/klask
r2 r3 2 2 3 3 use strict; 4 use warnings; 4 5 5 6 use Net::SNMP; … … 10 11 11 12 # 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 12 14 13 15 our $KLASK_DB = '/var/cache/klask/klaskdb'; … … 83 85 my $param_ip_or_host = shift; 84 86 my $interface = shift || '*'; 87 my $type = shift || 'fast'; 85 88 86 89 my %ret = ( … … 108 111 $ret{timestamp} = $timestamp if $timestamp; 109 112 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 110 120 # resultat de la commande arp 111 121 # tech7meylan.hmg.inpg.fr (194.254.66.240) at 00:14:22:45:28:A9 [ether] on eth0 … … 157 167 INIT_EACH_SWITCH: 158 168 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) { 194 print "$error \n" if $error; 195 #} 164 196 my $result = $session->get_request( 165 197 -varbindlist => ['1.3.6.1.2.1.1.5.0'] 166 198 ); 167 199 $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}; 168 202 $session->close; 169 203 … … 230 264 LOOP_ON_SWITCH: 231 265 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}} ); 267 print "$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"; 237 273 my $result = $session->get_request( 238 274 -varbindlist => [$research] 239 275 ); 240 276 # 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"; 242 279 $session->close; 243 280 next LOOP_ON_SWITCH; … … 291 328 292 329 my $research = "1.3.6.1.2.1.17.4.3.1.2".arphextodec($arp); 330 #print " $arp -> $research\n"; 293 331 LOOP_ON_ALL_SWITCH: 294 332 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}} ); 334 print "$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"; 300 340 my $result = $session->get_request( 301 341 -varbindlist => [$research] 302 342 ); 303 if(defined($result)){ 343 if(defined($result) and $result->{$research} ne 'noSuchInstance'){ 344 #print "+++ $sw->{hostname} -> $result->{$research}\n" ; 304 345 my $swport = $result->{$research}; 305 346 … … 312 353 # $sw->{_port}{$swport}++; 313 354 } 355 else { 356 #print "$sw->{hostname} -- $research --".$session->error()."\n"; 357 } 314 358 315 359 $session->close; 316 360 } 361 #print "end \n"; 317 362 return $ret; 318 363 } … … 518 563 } 519 564 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 520 571 my %where = find_switch_port($resol_arp{mac_address},$switch_proposal); 521 572 … … 528 579 529 580 # 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 # } 534 585 535 586 # detected on a switch … … 654 705 my $sw_hostname = $computerdb->{$ip}{switch_hostname} || ''; 655 706 next if $sw_hostname eq 'unknow'; 656 next if $sw_hostname eq '';657 707 658 708 my $sw_location = ''; … … 702 752 DETECT_ALL_ROUTER: 703 753 for my $one_computer ('194.254.66.254') { 704 my %resol_arp = resolve_ip_arp_host($one_computer ); #resolution arp754 my %resol_arp = resolve_ip_arp_host($one_computer,'*','low'); #resolution arp 705 755 next DETECT_ALL_ROUTER if $resol_arp{mac_address} eq 'unknow'; 706 756 … … 708 758 } 709 759 760 print "etape 1\n"; 710 761 ALL_ROUTER_IP_ADDRESS: 711 762 for my $ip (Net::Netmask::sort_by_ip_address('194.254.66.254')) { … … 723 774 } 724 775 } 776 print "etape 2\n"; 725 777 726 778 # print "Switch output port\n"; … … 740 792 } 741 793 794 print "etape 3\n"; 742 795 ALL_SWITCH: 743 796 for my $one_computer (@list_switch_ip) { 744 my %resol_arp = resolve_ip_arp_host($one_computer ); # arp resolution797 my %resol_arp = resolve_ip_arp_host($one_computer,'*','low'); # arp resolution 745 798 next ALL_SWITCH if $resol_arp{mac_address} eq 'unknow'; 746 799 … … 752 805 } 753 806 807 print "etape 4\n"; 754 808 ALL_SWITCH_IP_ADDRESS: 755 809 for my $ip (Net::Netmask::sort_by_ip_address(@list_switch_ipv4)) { … … 774 828 } 775 829 830 print "etape 5\n"; 776 831 my %db_switch_connected_on_port = (); 777 832 my $maybe_more_than_one_switch_connected = 'yes'; … … 819 874 } 820 875 876 print "etape 6\n"; 821 877 my %db_switch_parent =(); 822 878
Note: See TracChangeset
for help on using the changeset viewer.