Explorar el Código

returned the union of two branches of the mac table

Roman Dmitriev hace 2 años
padre
commit
0184dbf2b5
Se han modificado 2 ficheros con 71 adiciones y 55 borrados
  1. 48 41
      scripts/Rstat/snmp.pm
  2. 23 14
      scripts/fetch_new_arp.pl

+ 48 - 41
scripts/Rstat/snmp.pm

@@ -198,11 +198,11 @@ if (!$mk_ros_version or $mk_ros_version > 6468) {
         foreach my $row (keys(%$index_map_table)) {
             my $port_index = $index_map_table->{$row};
             next if (!$port_index);
-   	        my $value;
+                my $value;
             if ($row=~/\.([0-9]{1,10})$/) { $value = $1; }
             next if (!$value);
             $ifmib_map->{$value}=$port_index;
-       	    }
+            }
         }
     }
 
@@ -210,12 +210,12 @@ if (!$ifmib_map) {
     my $index_table =  snmp_get_oid($ip, $community, $ifIndex, $version);
     if (!$index_table) { $index_table =  snmp_walk_oid($ip, $community, $ifIndex, $version); }
     foreach my $row (keys(%$index_table)) {
-	    my $port_index = $index_table->{$row};
-	    next if (!$port_index);
+            my $port_index = $index_table->{$row};
+            next if (!$port_index);
         my $value;
         if ($row=~/\.([0-9]{1,10})$/) { $value = $1; }
-	    next if (!$value);
-    	$ifmib_map->{$value}=$value;
+            next if (!$value);
+        $ifmib_map->{$value}=$value;
         };
     }
 return $ifmib_map;
@@ -235,24 +235,24 @@ sub get_mac_table {
     if (!$fdb_table1) { $fdb_table1=snmp_walk_oid($host,$community,$oid,$version,undef); }
     if ($fdb_table1) {
         foreach my $row (keys(%$fdb_table1)) {
-	        my $port_index = $fdb_table1->{$row};
+                my $port_index = $fdb_table1->{$row};
             next if (!$port_index);
-	        my $mac;
-	        if ($row=~/\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/) {
-        	    $mac=sprintf "%02x%02x%02x%02x%02x%02x",$1,$2,$3,$4,$5,$6;
-        	    }
+                my $mac;
+                if ($row=~/\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/) {
+                    $mac=sprintf "%02x%02x%02x%02x%02x%02x",$1,$2,$3,$4,$5,$6;
+                    }
             next if (!$mac);
             if ($index_map and exists $index_map->{$port_index}) { $port_index = $index_map->{$port_index}; }
-	        $fdb->{$mac}=$port_index;
-	        };
+                $fdb->{$mac}=$port_index;
+                };
         return $fdb;
-	    }
+            }
 }
 
 #-------------------------------------------------------------------------------------
 
 sub get_fdb_table {
-    my ($host,$community,$version) = @_;
+    my ($host,$community,$version,$iflist) = @_;
 #    return if (!HostIsLive($host));
     my $port = 161;
     my $timeout = 5;
@@ -260,40 +260,48 @@ sub get_fdb_table {
 
     my $ifindex_map = get_ifmib_index_table($host,$community,$version);
     my $fdb1=get_mac_table($host,$community,$fdb_table_oid,$version,$ifindex_map);
+    my $fdb2=get_mac_table($host,$community,$fdb_table_oid2,$version);
 
-    my $fdb;
-    if ($fdb1) { $fdb = $fdb1; } else {
-        my $fdb2=get_mac_table($host,$community,$fdb_table_oid2,$version);
-        if ($fdb2) { $fdb = $fdb2; }
+    my $fdb3;
+
+    if ($fdb2 and $iflist) {
+        foreach my $mac (keys %$fdb2) {
+            if (exists $iflist->{$fdb2->{$mac}}) { $fdb3->{$mac}=$iflist->{$fdb2->{$mac}}; }
+            }
         }
 
+    my $fdb;
+    if ($fdb1 and !$fdb3) { $fdb = $fdb1; }
+    if (!$fdb1 and $fdb3) { $fdb = $fdb3; }
+    if ($fdb1 and $fdb3) { $fdb = { %$fdb1,%$fdb3 }; }
+
     #maybe cisco?!
     if (!$fdb) {
-	    my $vlan_table=snmp_get_oid($host,$community,$cisco_vlan_oid,$version);
+        my $vlan_table=snmp_get_oid($host,$community,$cisco_vlan_oid,$version);
         if (!$vlan_table) { $vlan_table=snmp_walk_oid($host,$community,$cisco_vlan_oid,$version); }
         #fuck!
         if (!$vlan_table) { return; }
         my %fdb_vlan;
-	    foreach my $vlan_oid (keys %$vlan_table) {
-	        next if (!$vlan_oid);
-	        my $vlan_id;
-	        if ($vlan_oid=~/\.([0-9]{1,4})$/) { $vlan_id=$1; }
-	        next if (!$vlan_id);
-	        next if ($vlan_id>1000 and $vlan_id<=1009);
-	        $fdb_vlan{$vlan_id}=get_mac_table($host,$community.'@'.$vlan_id,$fdb_table_oid,$version,$ifindex_map);
-	        if (!$fdb_vlan{$vlan_id}) { $fdb_vlan{$vlan_id}=get_mac_table($host,$community.'@'.$vlan_id,$fdb_table_oid2,$version,$ifindex_map); }
-	        }
-	    foreach my $vlan_id (keys %fdb_vlan) {
-	        next if (!exists $fdb_vlan{$vlan_id});
-	        if (defined $fdb_vlan{$vlan_id}) {
-		        my %tmp=%{$fdb_vlan{$vlan_id}};
-		        foreach my $mac (keys %tmp) {
-			        next if (!$mac);
-			        $fdb->{$mac}=$tmp{$mac};
-			        }
-		    }
-	    }
-	}
+            foreach my $vlan_oid (keys %$vlan_table) {
+                next if (!$vlan_oid);
+                my $vlan_id;
+                if ($vlan_oid=~/\.([0-9]{1,4})$/) { $vlan_id=$1; }
+                next if (!$vlan_id);
+                next if ($vlan_id>1000 and $vlan_id<=1009);
+                $fdb_vlan{$vlan_id}=get_mac_table($host,$community.'@'.$vlan_id,$fdb_table_oid,$version,$ifindex_map);
+                if (!$fdb_vlan{$vlan_id}) { $fdb_vlan{$vlan_id}=get_mac_table($host,$community.'@'.$vlan_id,$fdb_table_oid2,$version,$ifindex_map); }
+                }
+            foreach my $vlan_id (keys %fdb_vlan) {
+                next if (!exists $fdb_vlan{$vlan_id});
+                if (defined $fdb_vlan{$vlan_id}) {
+                        my %tmp=%{$fdb_vlan{$vlan_id}};
+                        foreach my $mac (keys %tmp) {
+                                next if (!$mac);
+                                $fdb->{$mac}=$tmp{$mac};
+                                }
+                    }
+            }
+        }
     return $fdb;
 }
 
@@ -502,4 +510,3 @@ return;
 
 1;
 }
-

+ 23 - 14
scripts/fetch_new_arp.pl

@@ -198,13 +198,23 @@ sub {
     }
 );
 
+my %dev_ifindex=();
+
+foreach my $device (@device_list) {
+    my $dev_id = $device->{id};
+    my @device_ports = get_records_sql($dbh,"SELECT * FROM device_ports WHERE device_id=$dev_id");
+    foreach my $port_data (@device_ports) {
+        if (!$port_data->{snmp_index}) { $port_data->{snmp_index} = $port_data->{port}; }
+        $dev_ifindex{$dev_id}->{$port_data->{port}}=$port_data->{snmp_index};
+    }
+}
 
 FDB_LOOP:
 foreach my $device (@device_list) {
 my $int_list = get_snmp_ifindex($device->{ip},$device->{community},$device->{snmp_version});
 if (!$int_list) { log_info("Host id: $device->{id} name: $device->{device_name} ip: $device->{ip} is down! Skip."); next; }
 $pm_fdb->start() and next FDB_LOOP;
-my $fdb=get_fdb_table($device->{ip},$device->{community},$device->{snmp_version});
+my $fdb=get_fdb_table($device->{ip},$device->{community},$device->{snmp_version},$dev_ifindex{$device->{id}});
 my $result;
 $result->{id}=$device->{id};
 $result->{fdb} = $fdb;
@@ -242,7 +252,6 @@ foreach my $port_data (@device_ports) {
     my $port_id = $port_data->{id};
     if (!$port_data->{snmp_index}) { $port_data->{snmp_index} = $port_data->{port}; }
     $fdb_port_index=$port_data->{snmp_index};
-	my $dev_ports;
     next if ($port_data->{skip});
     #snmp-индекс порта = номеру порта
     $port_snmp_index{$port_data->{snmp_index}}=$port_data->{port};
@@ -255,11 +264,11 @@ foreach my $port_data (@device_ports) {
 
 my $sw_mac;
 if ($device->{vendor_id} eq '9') {
-        #get device mac
-        my $sw_auth = get_record_sql($dbh,"SELECT mac FROM User_auth WHERE deleted=0 and ip='".$device->{ip}."'");
-        $sw_mac = mac_simplify($sw_auth->{mac});
-        $sw_mac =~s/.{2}$//s;
-        }
+    #get device mac
+    my $sw_auth = get_record_sql($dbh,"SELECT mac FROM User_auth WHERE deleted=0 and ip='".$device->{ip}."'");
+    $sw_mac = mac_simplify($sw_auth->{mac});
+    $sw_mac =~s/.{2}$//s;
+    }
 
 foreach my $mac (keys %$fdb) {
     #port from fdb table
@@ -308,8 +317,8 @@ foreach my $mac (keys %mac_address_table) {
     if (exists $auth_table{full_table}{$simple_mac} or exists $auth_table{oper_table}{$simple_mac}) {
                 my $auth_id;
                 if (exists $auth_table{oper_table}{$simple_mac}) {
-            	    $auth_id=$auth_table{oper_table}{$simple_mac};
-            	    } else {
+                    $auth_id=$auth_table{oper_table}{$simple_mac};
+                    } else {
                     $auth_id=$auth_table{full_table}{$simple_mac};
                     db_log_debug($dbh,"Mac not found in oper ARP-table. Use old values auth_id: $auth_id [$simple_mac] at device $dev_name [$port]",$auth_id);
                     }
@@ -317,10 +326,10 @@ foreach my $mac (keys %mac_address_table) {
                 if (exists $connections{$auth_id}) {
                     if ($port_id == $connections{$auth_id}{port}) {
                         if (exists $auth_table{oper_table}{$simple_mac}) {
-                    	    my $auth_rec;
-                    	    $auth_rec->{last_found}=$now_str;
-	                        update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);
-                    	    }
+                            my $auth_rec;
+                            $auth_rec->{last_found}=$now_str;
+                                update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);
+                            }
                         next;
                         }
 
@@ -396,4 +405,4 @@ insert_record($dbh,'mac_history',$history_rec);
 
 $dbh->disconnect;
 
-exit 0;
+exit 0;