- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
/trunk/klask
r20 r30 5 5 6 6 use Net::SNMP; 7 use YAML qw(:all);7 use YAML; 8 8 use Net::Netmask; 9 9 use Net::CIDR::Lite; … … 12 12 13 13 # 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 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 17 my $KLASK_VAR = '/var/cache/klask'; 18 18 my $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(); 19 23 20 24 my $KLASK_CFG = YAML::LoadFile("$KLASK_CFG_FILE"); … … 24 28 25 29 my %switch_level = (); 30 my %SWITCH_DB = (); 26 31 LEVEL_OF_EACH_SWITCH: 27 32 for my $sw (@SWITCH){ 28 33 $switch_level{$sw->{hostname}} = $sw->{level} || $DEFAULT{switch_level} || 2; 34 $SWITCH_DB{$sw->{hostname}} = $sw; 29 35 } 30 36 @SWITCH = sort { $switch_level{$b->{hostname}} <=> $switch_level{$a->{hostname}} } @{$KLASK_CFG->{switch}}; … … 63 69 J3299A => { model => 'HP224M', match => 'HP J3299A ProCurve Switch 224M' }, 64 70 J4120A => { model => 'HP1600M', match => 'HP J4120A ProCurve Switch 1600M' }, 71 J9029A => { model => 'HP1800-8G', match => 'PROCURVE J9029A' }, 65 72 J4093A => { model => 'HP2424M', match => 'HP J4093A ProCurve Switch 2424M' }, 66 73 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' }, 68 77 J9021A => { model => 'HP2810-24G', match => 'ProCurve J9021A Switch 2810-24G' }, 69 78 J4903A => { model => 'HP2824', match => 'J4903A.+?Switch 2824,' }, … … 95 104 exit; 96 105 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 97 111 ### 98 112 # fast ping dont l'objectif est de remplir la table arp de la machine … … 122 136 #my $cmd = "grep -e '".'\b'."$param_ip_or_host".'\b'."' /var/lib/arpwatch/arp.dat | sort +2rn | head -1"; 123 137 # 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"; 125 139 my $cmd_arpwatch = `$cmd`; 126 140 chomp $cmd_arpwatch; … … 180 194 # Find Surname of a switch 181 195 sub get_switch_model { 182 my $sw_ description = shift || '?';196 my $sw_snmp_description = shift || 'unknow'; 183 197 184 198 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}/; 186 200 187 201 return $SWITCH_KIND{$sw_kind}->{model}; 188 202 } 189 203 190 return $sw_ description;204 return $sw_snmp_description; 191 205 } 192 206 … … 230 244 #$sw->{contact} = $result->{"1.3.6.1.2.1.1.4.0"} || $sw->{hostname}; 231 245 $session->close; 232 246 247 # Ligne à virer car on récupère maintenant le modèle du switch 233 248 my ($desc, $type) = split ':', $sw->{description}, 2; 234 249 # printf "%-25s 0--------->>>> %-25s %s\n", $sw->{hostname}, $desc, uc($type)."**" if $verbose; … … 280 295 return %ret if $arp eq 'unknow';; 281 296 282 my @ SWITCH_search = @SWITCH;297 my @switch_search = @SWITCH; 283 298 if ($switch_proposal ne '') { 284 299 for my $sw (@SWITCH) { 285 300 next if $sw->{hostname} ne $switch_proposal; 286 unshift @ SWITCH_search, $sw;301 unshift @switch_search, $sw; 287 302 last; 288 303 } … … 292 307 293 308 LOOP_ON_SWITCH: 294 for my $sw (@ SWITCH_search) {309 for my $sw (@switch_search) { 295 310 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 302 313 my $result = $session->get_request( 303 314 -varbindlist => [$research] 304 315 ); 305 # if(defined($result)) {306 316 if (not defined($result) or $result->{$research} eq 'noSuchInstance') { 307 #print "$sw->{hostname} -- $research --".$session->error()."\n";308 317 $session->close; 309 318 next LOOP_ON_SWITCH; … … 322 331 SWITCH_PORT_IGNORE: 323 332 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); 325 334 # $flag = 1; 326 335 next LOOP_ON_SWITCH; … … 329 338 $ret{switch_hostname} = $sw->{hostname}; 330 339 $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; 332 341 333 342 last LOOP_ON_SWITCH; … … 368 377 $ret->{$sw->{hostname}}{hostname} = $sw->{hostname}; 369 378 $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); 371 380 372 381 $SWITCH_PORT_COUNT{$sw->{hostname}}->{$swport}++; … … 432 441 433 442 sub 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 ); 441 450 442 451 return "$INTERNAL_PORT_MAP{$major}$reste"; … … 444 453 445 454 sub get_numerical_port { 446 my $sw 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); 452 461 453 462 # return $port if $letter =~ m/\d/; 454 463 455 my $reste = substr($ port, 1);464 my $reste = substr($sw_port, 1); 456 465 457 466 return $INTERNAL_PORT_MAP_REV{$letter} * 8 + $reste; … … 526 535 my $computerdb = YAML::LoadFile("$KLASK_DB_FILE"); 527 536 my $timestamp = time; 528 537 529 538 my %computer_not_detected = (); 530 539 my $timestamp_last_week = $timestamp - (3600 * 24 * 7); … … 534 543 my $i = 0; 535 544 my $detected_computer = 0; 536 545 537 546 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 } 538 566 539 567 my %router_mac_ip = (); … … 944 972 connected_on_port => \%db_switch_connected_on_port, 945 973 link_with => \%db_switch_link_with, 974 switch_db => \%SWITCH_DB, 946 975 }; 947 976 … … 1011 1040 my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}}; 1012 1041 my %db_switch_link_with = %{$switch_connection->{link_with}}; 1013 1042 my %db_switch_global = %{$switch_connection->{switch_db}}; 1043 1014 1044 my %db_building= (); 1015 1045 for my $sw (@SWITCH) { … … 1044 1074 print "\"$sw:$db_switch_output_port{$sw}\" [label = $db_switch_output_port{$sw}, color = black, fillcolor = lightblue, peripheries = 2, style = filled];\n"; 1045 1075 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"; 1047 1079 print "\"location$b-$l\" -> \"$sw\" [len = 2, color = firebrick, arrowtail = dot]\n"; 1048 1080 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.