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