Procházet zdrojové kódy

bugfix: html - fixed display current vlan at port
backend: fixed survey mikrotik switches by snmp

rajven před 4 roky
rodič
revize
25063c6296

+ 1 - 1
html/admin/devices/editdevice.php

@@ -140,7 +140,7 @@ if ($device['device_type']==2) {
 ?>
 </tr>
 <td>Snmp Version</td>
-<td>Mac by snmp</td>
+<td><p title="Некоторые устройства отдают mac-таблицу по индексу порта в snmp, другие - по номеру.">Mac by snmp</p></td>
 <td>Discovery</td>
 <td>Nagios</td>
 <td>

+ 1 - 5
html/admin/devices/switchstatus.php

@@ -127,11 +127,7 @@ if ($switch['snmp_version']>0) {
         print "</td>\n";
         print "<td class=\"$cl\">" . $d_comment . "</td>\n";
         if ($snmp_ok) {
-            if ($switch['fdb_snmp_index']) {
-                $vlan = get_port_vlan($d_snmp, $switch['ip'], $switch['community'], $switch['snmp_version'], $switch['fdb_snmp_index']);
-            } else {
-                $vlan = get_port_vlan($d_port, $switch['ip'], $switch['community'], $switch['snmp_version'], $switch['fdb_snmp_index']);
-            }
+            $vlan = get_port_vlan($d_port, $d_snmp, $switch['ip'], $switch['community'], $switch['snmp_version'], $switch['fdb_snmp_index']);
             $ifname = get_snmp_ifname($switch['ip'], $switch['community'], $switch['snmp_version'], $d_snmp);
             $sfp_status = get_sfp_status($switch['vendor_id'], $d_snmp, $switch['ip'], $switch['community'], $switch['snmp_version'], $modules_oids);
             $poe_status = get_port_poe_state($switch['vendor_id'], $d_snmp, $switch['ip'], $switch['community'], $switch['snmp_version']);

+ 5 - 3
html/inc/common.php

@@ -2140,7 +2140,7 @@ function get_sfp_status($vendor_id, $port, $ip, $community, $version, $modules_o
     return;
 }
 
-function get_port_vlan($port, $ip, $community, $version)
+function get_port_vlan($port, $port_index, $ip, $community, $version, $fdb_by_snmp)
 {
     if (! isset($port)) {
         return;
@@ -2156,11 +2156,13 @@ function get_port_vlan($port, $ip, $community, $version)
     }
     // if (!is_up($ip)) { return; }
 
+    if ($fdb_by_snmp) { $port = $port_index; }
+
     global $port_vlan_oid;
     $port_oid = $port_vlan_oid . $port;
     $port_vlan = get_snmp($ip, $community, $version, $port_oid);
-    $port_vlan = intval(trim(str_replace('Gauge32:', '', $port_vlan)));
-
+    $port_vlan = preg_replace('/.*\:/','',$port_vlan);
+    $port_vlan = intval(trim($port_vlan));
     return $port_vlan;
 }
 

+ 18 - 0
scripts/Rstat/snmp.pm

@@ -24,6 +24,7 @@ get_fdb_table
 get_mac_table
 get_vlan_at_port
 get_switch_vlans
+get_snmp_ifindex
 get_interfaces
 get_router_state
 get_bgp
@@ -266,6 +267,23 @@ sub get_switch_vlans {
 
 #-------------------------------------------------------------------------------------
 
+sub get_snmp_ifindex {
+    my ($host,$community,$snmp) = @_;
+    ### open SNMP session
+    my ($snmp_session, $error) = Net::SNMP->session( -hostname  => $host, -community => $community, -version => $snmp, -timeout => 5);
+    return if (!defined($snmp_session));
+    my $if_index = $snmp_session->get_table($ifIndex);
+    my $result;
+    foreach my $row (keys(%$if_index)) {
+        my $value = $if_index->{$row};
+        $row=~s/^$ifIndex\.//;
+        $result->{$row}=$value;
+        };
+    return $result;
+}
+
+#-------------------------------------------------------------------------------------
+
 sub get_interfaces {
     my ($host,$community,$snmp,$skip_empty) = @_;
 #    return if (!HostIsLive($host));

+ 34 - 20
scripts/fetch_new_arp.pl

@@ -131,6 +131,7 @@ foreach my $arp_table (@arp_array) {
     }
 }
 
+
 db_log_verbose($dbh,'Arp discovery stopped.');
 }
 
@@ -201,7 +202,8 @@ sub {
 
 FDB_LOOP:
 foreach my $device (@device_list) {
-if (!HostIsLive($device->{ip})) { log_info("Host id: $device->{id} name: $device->{device_name} ip: $device->{ip} is down! Skip."); next; }
+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 $vlans = get_switch_vlans($device->{ip},$device->{community},$device->{snmp_version});
@@ -240,47 +242,59 @@ next if (!$fdb);
 
 my @device_ports = get_records_sql($dbh,"SELECT * FROM device_ports WHERE device_id=$dev_id");
 
+
 foreach my $port_data (@device_ports) {
     my $vlan = $port_data->{vlan};
     if (!$vlan) { $vlan=1; }
-
     my $fdb_port_index=$port_data->{port};
-
+    my $port_id = $port_data->{id};
     if (!$port_data->{snmp_index}) { $port_data->{snmp_index} = $port_data->{port}; }
     if ($device->{fdb_snmp_index}) { $fdb_port_index=$port_data->{snmp_index}; }
-
     my $current_vlan = $vlans->{$fdb_port_index};
     if (!$current_vlan) { $current_vlan=1; }
-
     if ($current_vlan != $vlan) {
 	my $dev_ports;
 	$dev_ports->{vlan}=$current_vlan;
-	update_record($dbh,'device_ports',$dev_ports,"device_id=$dev_id and port=$port_data->{port}");
-        db_log_verbose($dbh,"Vlan changed at device $dev_name [$port_data->{port}] old: $vlan current: $current_vlan");
+	update_record($dbh,'device_ports',$dev_ports,"id=$port_id");
+        db_log_verbose($dbh,"Vlan changed at device $dev_name [$port_data->{port}] id: $port_id old: $vlan current: $current_vlan");
         }
-
     next if ($port_data->{skip});
-
+    #snmp-индекс порта = номеру порта
     $port_snmp_index{$port_data->{snmp_index}}=$port_data->{port};
+    # номер порта = id записи порта в таблице
     $port_index{$port_data->{port}}=$port_data->{id};
+    # номер порта = id записи порта аплинка/даунлинка свича
     $port_links{$port_data->{port}}=$port_data->{target_port_id};
     $mac_port_count{$port_data->{port}}=0;
     }
 
+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;
+        }
+
 foreach my $mac (keys %$fdb) {
     #port from fdb table
     my $port = $fdb->{$mac};
     next if (!$port);
     #real port number
     if ($device->{fdb_snmp_index}) {
+        #если mac-таблица привязана к snmp-индексам портов, номер порта ставим в snmp-индекс порта
         if (!exists $port_snmp_index{$port}) { next; }
         $port=$port_snmp_index{$port};
         }
     if (!exists $port_index{$port}) { next; }
+    #mikrotik patch - skip mikrotik device mac
+    if ($sw_mac and $mac=~/^$sw_mac/i) { next; }
     $mac_port_count{$port}++;
+    #мак = номер порта
     $mac_address_table{$mac}=$port;
     }
 
+# обновляем число маков на порту
 foreach my $port (keys %mac_port_count) {
 if (!$port) { next; }
 if (!exists $port_index{$port}) { next; }
@@ -299,24 +313,26 @@ foreach my $mac (keys %mac_address_table) {
     if ($port_links{$port}>0) { next; }
 
     my $simple_mac=mac_simplify($mac);
+    my $mac_splitted=mac_splitted($mac);
+
     $mac_history{$simple_mac}{port_id}=$port_index{$port};
     $mac_history{$simple_mac}{dev_id}=$dev_id;
     if (!$mac_history{$simple_mac}{changed}) { $mac_history{$simple_mac}{changed}=0; }
 
     my $port_id=$port_index{$port};
 
-    if (exists $auth_table{full_table}{$mac} or exists $auth_table{oper_table}{$mac}) {
+    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}{$mac}) { $auth_id=$auth_table{oper_table}{$mac}; } else {
-                    $auth_id=$auth_table{full_table}{$mac};
+                if (exists $auth_table{oper_table}{$simple_mac}) { $auth_id=$auth_table{oper_table}{$simple_mac}; } else {
+                    $auth_id=$auth_table{full_table}{$simple_mac};
                     if ($debug) {
-                        db_log_debug($dbh,"Mac not found in oper ARP-table. Use old values auth_id: $auth_id [$mac] at device $dev_name [$port]");
+                        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]");
                         }
                     }
 
                 if (exists $connections{$auth_id}) {
                     if ($port_id == $connections{$auth_id}{port}) {
-                        if (exists $auth_table{oper_table}{$mac}) {
+                        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);
@@ -327,7 +343,7 @@ foreach my $mac (keys %mac_address_table) {
                     $connections{$auth_id}{port}=$port_id;
                     $mac_history{$simple_mac}{changed}=1;
                     $mac_history{$simple_mac}{auth_id}=$auth_id;
-                    db_log_info($dbh,"Found auth_id: $auth_id [$mac] at device $dev_name [$port]. Update connection");
+                    db_log_info($dbh,"Found auth_id: $auth_id [$mac_splitted] at device $dev_name [$port]. Update connection");
                     my $auth_rec;
                     $auth_rec->{last_found}=$now_str;
                     update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);
@@ -339,7 +355,7 @@ foreach my $mac (keys %mac_address_table) {
                     $mac_history{$simple_mac}{changed}=1;
                     $mac_history{$simple_mac}{auth_id}=$auth_id;
                     $connections{$auth_id}{port}=$port_id;
-                    db_log_info($dbh,"Found auth_id: $auth_id [$mac] at device $dev_name [$port]. Create connection.");
+                    db_log_info($dbh,"Found auth_id: $auth_id [$mac_splitted] at device $dev_name [$port]. Create connection.");
                     my $auth_rec;
                     $auth_rec->{last_found}=$now_str;
                     update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);
@@ -354,17 +370,15 @@ foreach my $mac (keys %mac_address_table) {
                         next if ($unknown_table{$simple_mac}{port_id} == $port_id and $unknown_table{$simple_mac}{device_id} == $dev_id);
                         $mac_history{$simple_mac}{changed}=1;
                         $mac_history{$simple_mac}{auth_id}=0;
-                        $mac=mac_splitted($mac);
-                        db_log_debug($dbh,"Unknown mac $mac moved to $dev_name [$port]") if ($debug);
+                        db_log_debug($dbh,"Unknown mac $mac_splitted moved to $dev_name [$port]") if ($debug);
                         my $unknown_rec;
                         $unknown_rec->{port_id}=$port_id;
                         $unknown_rec->{device_id}=$dev_id;
                         update_record($dbh,'Unknown_mac',$unknown_rec,"id=$unknown_table{$simple_mac}{unknown_id}");
                         } else {
-                        $mac=mac_splitted($mac);
                         $mac_history{$simple_mac}{changed}=1;
                         $mac_history{$simple_mac}{auth_id}=0;
-                        db_log_debug($dbh,"Unknown mac $mac found at $dev_name [$port]") if ($debug);
+                        db_log_debug($dbh,"Unknown mac $mac_splitted found at $dev_name [$port]") if ($debug);
                         my $unknown_rec;
                         $unknown_rec->{port_id}=$port_id;
                         $unknown_rec->{device_id}=$dev_id;