root 3 месяцев назад
Родитель
Сommit
beb6d84277
4 измененных файлов с 57 добавлено и 62 удалено
  1. 49 39
      scripts/dhcp-log.pl
  2. 4 19
      scripts/eyelib/common.pm
  3. 2 2
      scripts/fetch_new_arp.pl
  4. 2 2
      scripts/garbage.pl

+ 49 - 39
scripts/dhcp-log.pl

@@ -157,22 +157,32 @@ sub run {
                 $logline =~ s/[^\p{L}\p{N}\p{P}\p{Z}]//g;
                 $logline =~ s/[^\p{L}\p{N}\p{P}\p{Z}]//g;
                 log_debug("Line after filtering: $logline");
                 log_debug("Line after filtering: $logline");
 
 
+                my @field_names = qw(
+                    type mac ip hostname timestamp
+                    tags sup_hostname old_hostname
+                    circuit_id remote_id client_id
+                    decoded_circuit_id decoded_remote_id
+                );
+
                 # Parse fields by semicolon
                 # Parse fields by semicolon
-                my (
-                    $type, $mac, $ip, $hostname, $timestamp,
-                    $tags, $sup_hostname, $old_hostname,
-                    $circuit_id, $remote_id, $client_id,
-                    $decoded_circuit_id, $decoded_remote_id
-                ) = split(/;/, $logline);
+                my @values = split(/;/, $logline);
+
+                my %dhcp_event;
+                log_verbose("GET::");
+                @dhcp_event{@field_names} = @values;
+                for my $name (@field_names) {
+                    my $val = defined $dhcp_event{$name} ? $dhcp_event{$name} : '';
+                    log_verbose("Param '$name': $val");
+                }
 
 
                 # Skip lines without valid event type
                 # Skip lines without valid event type
-                next unless $type && $type =~ /^(old|add|del)$/i;
+                next unless $dhcp_event{'type'} && $dhcp_event{'type'} =~ /^(old|add|del)$/i;
 
 
-                log_debug("Processing DHCP event: type='$type', MAC='$mac', IP='$ip'");
+                log_debug("Processing DHCP event: type='$dhcp_event{'type'}', MAC='$dhcp_event{'mac'}', IP='$dhcp_event{'ip'}'");
 
 
                 # Suppress duplicate events within $mute_time window
                 # Suppress duplicate events within $mute_time window
-                if (exists $leases{$ip} && $leases{$ip}{type} eq $type && (time() - $leases{$ip}{last_time} <= $mute_time)) {
-                    log_debug("Skipping duplicate: IP=$ip, type=$type (within $mute_time sec window)");
+                if (exists $leases{$dhcp_event{'ip'}} && $leases{$dhcp_event{'ip'}}{type} eq $dhcp_event{'type'} && (time() - $leases{$dhcp_event{'ip'}}{last_time} <= $mute_time)) {
+                    log_debug("Skipping duplicate: IP=$dhcp_event{'ip'}, type=$dhcp_event{'type'} (within $mute_time sec window)");
                     next;
                     next;
                 }
                 }
 
 
@@ -183,12 +193,12 @@ sub run {
                 }
                 }
 
 
                 # Process DHCP request: update/create DB record
                 # Process DHCP request: update/create DB record
-                my $dhcp_record = process_dhcp_request($hdb, $type, $mac, $ip, $hostname, $client_id, $decoded_circuit_id, $decoded_remote_id);
+                my $dhcp_record = process_dhcp_request($hdb, $dhcp_event{'type'}, $dhcp_event{'mac'}, $dhcp_event{'ip'}, $dhcp_event{'hostname'}, $dhcp_event{'client_id'}, $dhcp_event{'decoded_circuit_id'}, $dhcp_event{'$decoded_remote_id'});
                 next unless $dhcp_record;
                 next unless $dhcp_record;
 
 
                 # Cache to suppress duplicates
                 # Cache to suppress duplicates
-                $leases{$ip} = {
-                    type => $type,
+                $leases{$dhcp_event{'ip'}} = {
+                    type => $dhcp_event{'type'},
                     last_time => time()
                     last_time => time()
                 };
                 };
                 my $auth_id = $dhcp_record->{auth_id};
                 my $auth_id = $dhcp_record->{auth_id};
@@ -196,15 +206,15 @@ sub run {
                 # === SWITCH AND PORT IDENTIFICATION LOGIC ===
                 # === SWITCH AND PORT IDENTIFICATION LOGIC ===
 
 
                 my ($switch, $switch_port);
                 my ($switch, $switch_port);
-                my ($t_remote_id, $t_circuit_id) = ($remote_id, $circuit_id);
+                my ($t_remote_id, $t_circuit_id) = ($dhcp_event{'remote_id'}, $dhcp_event{'circuit_id'});
 
 
                 # Only process connection events (add/old)
                 # Only process connection events (add/old)
-                if ($type =~ /^(add|old)$/i) {
+                if ($dhcp_event{'type'} =~ /^(add|old)$/i) {
                     log_debug("Attempting to identify switch using Option 82 data...");
                     log_debug("Attempting to identify switch using Option 82 data...");
 
 
                     # 1. Try decoded_remote_id as MAC address
                     # 1. Try decoded_remote_id as MAC address
-                    if ($decoded_remote_id) {
-                        $t_remote_id = $decoded_remote_id;
+                    if ($dhcp_event{'$decoded_remote_id'}) {
+                        $t_remote_id = $dhcp_event{'$decoded_remote_id'};
                         $t_remote_id .= "0" x (12 - length($t_remote_id)) if length($t_remote_id) < 12;
                         $t_remote_id .= "0" x (12 - length($t_remote_id)) if length($t_remote_id) < 12;
                         $t_remote_id = mac_splitted(isc_mac_simplify($t_remote_id));
                         $t_remote_id = mac_splitted(isc_mac_simplify($t_remote_id));
                         my $devSQL = "SELECT D.id, D.device_name, D.ip, A.mac " .
                         my $devSQL = "SELECT D.id, D.device_name, D.ip, A.mac " .
@@ -213,17 +223,17 @@ sub run {
                                      "AND A.mac = ?";
                                      "AND A.mac = ?";
                         $switch = get_record_sql($hdb, $devSQL, $t_remote_id);
                         $switch = get_record_sql($hdb, $devSQL, $t_remote_id);
                         if ($switch) {
                         if ($switch) {
-                            $remote_id = $t_remote_id;
-                            $circuit_id = $decoded_circuit_id;
-                            $dhcp_record->{circuit_id} = $circuit_id;
-                            $dhcp_record->{remote_id} = $remote_id;
+                            $dhcp_event{'remote_id'} = $t_remote_id;
+                            $dhcp_event{'circuit_id'} = $dhcp_event{'decoded_circuit_id'};
+                            $dhcp_record->{circuit_id} = $dhcp_event{'circuit_id'};
+                            $dhcp_record->{remote_id} = $dhcp_event{'remote_id'};
                             log_debug("Switch found via decoded_remote_id: " . $switch->{device_name});
                             log_debug("Switch found via decoded_remote_id: " . $switch->{device_name});
                         }
                         }
                     }
                     }
 
 
                     # 2. If not found, try raw remote_id as MAC
                     # 2. If not found, try raw remote_id as MAC
-                    if (!$switch && $remote_id) {
-                        $t_remote_id = $remote_id;
+                    if (!$switch && $dhcp_event{'remote_id'}) {
+                        $t_remote_id = $dhcp_event{'remote_id'};
                         $t_remote_id .= "0" x (12 - length($t_remote_id)) if length($t_remote_id) < 12;
                         $t_remote_id .= "0" x (12 - length($t_remote_id)) if length($t_remote_id) < 12;
                         $t_remote_id = mac_splitted(isc_mac_simplify($t_remote_id));
                         $t_remote_id = mac_splitted(isc_mac_simplify($t_remote_id));
                         my $devSQL = "SELECT D.id, D.device_name, D.ip, A.mac " .
                         my $devSQL = "SELECT D.id, D.device_name, D.ip, A.mac " .
@@ -232,16 +242,16 @@ sub run {
                                      "AND A.mac = ?";
                                      "AND A.mac = ?";
                         $switch = get_record_sql($hdb, $devSQL, $t_remote_id);
                         $switch = get_record_sql($hdb, $devSQL, $t_remote_id);
                         if ($switch) {
                         if ($switch) {
-                            $remote_id = $t_remote_id;
-                            $dhcp_record->{circuit_id} = $circuit_id;
-                            $dhcp_record->{remote_id} = $remote_id;
+                            $dhcp_event{'remote_id'} = $t_remote_id;
+                            $dhcp_record->{circuit_id} = $dhcp_event{'circuit_id'};
+                            $dhcp_record->{remote_id} = $dhcp_event{'remote_id'};
                             log_debug("Switch found via remote_id: " . $switch->{device_name});
                             log_debug("Switch found via remote_id: " . $switch->{device_name});
                         }
                         }
                     }
                     }
 
 
                     # 3. If still not found, try remote_id as device name prefix
                     # 3. If still not found, try remote_id as device name prefix
-                    if (!$switch && $remote_id) {
-                        my @id_words = split(/ /, $remote_id);
+                    if (!$switch && $dhcp_event{'remote_id'}) {
+                        my @id_words = split(/ /, $dhcp_event{'remote_id'});
                         if ($id_words[0]) {
                         if ($id_words[0]) {
                             my $devSQL = "SELECT D.id, D.device_name, D.ip, A.mac " .
                             my $devSQL = "SELECT D.id, D.device_name, D.ip, A.mac " .
                                          "FROM devices AS D, user_auth AS A " .
                                          "FROM devices AS D, user_auth AS A " .
@@ -255,8 +265,8 @@ sub run {
                     }
                     }
 
 
                     # 4. Special case: MikroTik (circuit-id may contain device name)
                     # 4. Special case: MikroTik (circuit-id may contain device name)
-                    if (!$switch && $circuit_id) {
-                        my @id_words = split(/ /, $circuit_id);
+                    if (!$switch && $dhcp_event{'circuit_id'}) {
+                        my @id_words = split(/ /, $dhcp_event{'circuit_id'});
                         if ($id_words[0]) {
                         if ($id_words[0]) {
                             my $devSQL = "SELECT D.id, D.device_name, D.ip, A.mac " .
                             my $devSQL = "SELECT D.id, D.device_name, D.ip, A.mac " .
                                          "FROM devices AS D, user_auth AS A " .
                                          "FROM devices AS D, user_auth AS A " .
@@ -265,9 +275,9 @@ sub run {
                             $switch = get_record_sql($hdb, $devSQL, $id_words[0] . '%');
                             $switch = get_record_sql($hdb, $devSQL, $id_words[0] . '%');
                             if ($switch) {
                             if ($switch) {
                                 # MikroTik often swaps circuit-id and remote-id
                                 # MikroTik often swaps circuit-id and remote-id
-                                ($circuit_id, $remote_id) = ($remote_id, $t_circuit_id);
-                                $dhcp_record->{circuit_id} = $circuit_id;
-                                $dhcp_record->{remote_id} = $remote_id;
+                                ($dhcp_event{'circuit_id'}, $dhcp_event{'remote_id'}) = ($dhcp_event{'remote_id'}, $t_circuit_id);
+                                $dhcp_record->{circuit_id} = $dhcp_event{'circuit_id'};
+                                $dhcp_record->{remote_id} = $dhcp_event{'remote_id'};
                                 log_debug("Detected MikroTik — swapped circuit-id and remote-id");
                                 log_debug("Detected MikroTik — swapped circuit-id and remote-id");
                             }
                             }
                         }
                         }
@@ -275,7 +285,7 @@ sub run {
 
 
                     # === LOG IF NO SWITCH MATCH FOUND ===
                     # === LOG IF NO SWITCH MATCH FOUND ===
                     unless ($switch) {
                     unless ($switch) {
-                        log_warning("No matching switch found for DHCP event: IP=$ip, MAC=$mac, remote_id='$remote_id', circuit_id='$circuit_id'");
+                        log_warning("No matching switch found for DHCP event: IP=$dhcp_event{'ip'}, MAC=$dhcp_event{'mac'}, remote_id='$dhcp_event{'remote_id'}', circuit_id='$dhcp_event{'circuit_id'}'");
                     }
                     }
 
 
                     # === PORT IDENTIFICATION ===
                     # === PORT IDENTIFICATION ===
@@ -303,8 +313,8 @@ sub run {
                         }
                         }
 
 
                         # If not found by name, try hex port (last 2 bytes of decoded_circuit_id)
                         # If not found by name, try hex port (last 2 bytes of decoded_circuit_id)
-                        if (!$switch_port && $decoded_circuit_id) {
-                            my $hex_port = substr($decoded_circuit_id, -2);
+                        if (!$switch_port && $dhcp_event{'decoded_circuit_id'}) {
+                            my $hex_port = substr($dhcp_event{'decoded_circuit_id'}, -2);
                             if ($hex_port && $hex_port =~ /^[0-9a-fA-F]{2}$/) {
                             if ($hex_port && $hex_port =~ /^[0-9a-fA-F]{2}$/) {
                                 my $t_port = hex($hex_port);
                                 my $t_port = hex($hex_port);
                                 $switch_port = $device_ports_h{$t_port} if exists $device_ports_h{$t_port};
                                 $switch_port = $device_ports_h{$t_port} if exists $device_ports_h{$t_port};
@@ -314,7 +324,7 @@ sub run {
 
 
                         # Log and update connection
                         # Log and update connection
                         if ($switch_port) {
                         if ($switch_port) {
-                            db_log_verbose($hdb, "DHCP $type: IP=$ip, MAC=$mac " . $switch->{device_name} . " / " . $switch_port->{ifname});
+                            db_log_verbose($hdb, "DHCP $dhcp_event{'type'}: IP=$dhcp_event{'ip'}, MAC=$dhcp_event{'mac'} " . $switch->{device_name} . " / " . $switch_port->{ifname});
 
 
                             # Check if connection already exists
                             # Check if connection already exists
                             my $connection = get_records_sql($hdb, "SELECT * FROM connections WHERE auth_id = ?", $auth_id);
                             my $connection = get_records_sql($hdb, "SELECT * FROM connections WHERE auth_id = ?", $auth_id);
@@ -328,8 +338,8 @@ sub run {
                                 log_debug("New connection created: auth_id=$auth_id");
                                 log_debug("New connection created: auth_id=$auth_id");
                             }
                             }
                         } else {
                         } else {
-                            db_log_verbose($hdb, "DHCP $type: IP=$ip, MAC=$mac " . $switch->{device_name} . " (port not identified)");
-                            log_warning("Failed to identify port for IP=$ip on switch=" . $switch->{device_name});
+                            db_log_verbose($hdb, "DHCP $dhcp_event{'type'}: IP=$dhcp_event{'ip'}, MAC=$dhcp_event{'mac'} " . $switch->{device_name} . " (port not identified)");
+                            log_warning("Failed to identify port for IP=$dhcp_event{'ip'} on switch=" . $switch->{device_name});
                         }
                         }
                     }
                     }
 
 

+ 4 - 19
scripts/eyelib/common.pm

@@ -275,7 +275,7 @@ sub delete_user {
     return 0 unless $id =~ /^\d+$/ && $id > 0;
     return 0 unless $id =~ /^\d+$/ && $id > 0;
 
 
     # Удаляем основную запись пользователя
     # Удаляем основную запись пользователя
-    my $changes = delete_record($db, "user_list", "id = ?", $id);
+    my $changes = delete_record($db, "user_list", "permanent = 1 AND id = ?", $id);
     return 0 unless $changes;  # если не удалось — выходим
     return 0 unless $changes;  # если не удалось — выходим
 
 
     # Удаляем все IP-записи (user_auth)
     # Удаляем все IP-записи (user_auth)
@@ -1874,7 +1874,7 @@ sub process_dhcp_request {
     my $ip_aton = StrToIp($ip);
     my $ip_aton = StrToIp($ip);
     return unless defined $ip_aton;
     return unless defined $ip_aton;
 
 
-    $mac = mac_splitted(isc_mac_simplify($mac));
+    $mac = mac_splitted($mac);
 
 
     my $dhcp_event_time = GetNowTime($timestamp);
     my $dhcp_event_time = GetNowTime($timestamp);
 
 
@@ -1891,14 +1891,11 @@ sub process_dhcp_request {
         circuit_id    => $circuit_id,
         circuit_id    => $circuit_id,
         client_id     => $client_id,
         client_id     => $client_id,
         remote_id     => $remote_id,
         remote_id     => $remote_id,
-        hotspot       => is_hotspot($db, $ip),  # Исправлено: $db вместо $dbh
+        hotspot       => is_hotspot($db, $ip),
     };
     };
 
 
     # --- Ищем существующую запись ---
     # --- Ищем существующую запись ---
-    my $auth_record = get_record_sql($db,
-        "SELECT * FROM user_auth WHERE ip = ? AND mac = ? AND deleted = 0 ORDER BY last_found DESC",
-        $ip, $mac
-    );
+    my $auth_record = get_record_sql($db,"SELECT * FROM user_auth WHERE ip = ? AND mac = ? AND deleted = 0 ORDER BY last_found DESC", $ip, $mac );
 
 
     # Если запись не найдена и тип 'del' — выходим
     # Если запись не найдена и тип 'del' — выходим
     if (!$auth_record && $type eq 'del') {
     if (!$auth_record && $type eq 'del') {
@@ -1924,18 +1921,6 @@ sub process_dhcp_request {
     $dhcp_record->{auth_id} = $auth_id;
     $dhcp_record->{auth_id} = $auth_id;
     $dhcp_record->{auth_ou_id} = $auth_ou_id;
     $dhcp_record->{auth_ou_id} = $auth_ou_id;
 
 
-    # --- Логирование отладки ---
-    log_debug(uc($type) . ">>");
-    log_debug("MAC:        " . $dhcp_record->{mac});
-    log_debug("IP:         " . $dhcp_record->{ip});
-    log_debug("CIRCUIT-ID: " . $dhcp_record->{circuit_id});
-    log_debug("REMOTE-ID:  " . $dhcp_record->{remote_id});
-    log_debug("HOSTNAME:   " . $dhcp_record->{hostname});
-    log_debug("TYPE:       " . $dhcp_record->{type});
-    log_debug("TIME:       " . $dhcp_event_time);
-    log_debug("AUTH_ID:    " . $auth_id);
-    log_debug("END GET");
-
     update_dns_record_by_dhcp($db, $dhcp_record, $auth_record);
     update_dns_record_by_dhcp($db, $dhcp_record, $auth_record);
 
 
     # --- Обработка ADD ---
     # --- Обработка ADD ---

+ 2 - 2
scripts/fetch_new_arp.pl

@@ -495,7 +495,7 @@ foreach my $device (@device_list) {
                 my $unknown_rec;
                 my $unknown_rec;
                 $unknown_rec->{port_id} = $port_id;
                 $unknown_rec->{port_id} = $port_id;
                 $unknown_rec->{device_id} = $dev_id;
                 $unknown_rec->{device_id} = $dev_id;
-                $unknown_rec->{mac} = mac_dotted($simple_mac);
+                $unknown_rec->{mac} = mac_splitted($simple_mac);
                 insert_record($dbh, 'unknown_mac', $unknown_rec);
                 insert_record($dbh, 'unknown_mac', $unknown_rec);
             }
             }
         }
         }
@@ -514,7 +514,7 @@ foreach my $mac (keys %mac_history) {
     my $history_rec;
     my $history_rec;
     $history_rec->{device_id} = $h_dev_id;
     $history_rec->{device_id} = $h_dev_id;
     $history_rec->{port_id}   = $h_port_id;
     $history_rec->{port_id}   = $h_port_id;
-    $history_rec->{mac}       = mac_dotted($mac);
+    $history_rec->{mac}       = mac_splitted($mac);
     $history_rec->{ip}        = $h_ip if ($h_ip);
     $history_rec->{ip}        = $h_ip if ($h_ip);
     $history_rec->{auth_id}   = $h_auth_id;
     $history_rec->{auth_id}   = $h_auth_id;
     insert_record($dbh, 'mac_history', $history_rec);
     insert_record($dbh, 'mac_history', $history_rec);

+ 2 - 2
scripts/garbage.pl

@@ -252,7 +252,7 @@ $users_sql = "SELECT mac FROM user_auth WHERE deleted = 0";
 @users_auth = get_records_sql($dbh, $users_sql);
 @users_auth = get_records_sql($dbh, $users_sql);
 foreach my $row (@users_auth) {
 foreach my $row (@users_auth) {
     next if (!$row->{mac});
     next if (!$row->{mac});
-    do_sql($dbh, "DELETE FROM unknown_mac WHERE mac = ?", mac_dotted($row->{mac}));
+    do_sql($dbh, "DELETE FROM unknown_mac WHERE mac = ?", mac_splitted($row->{mac}));
 }
 }
 
 
 # Clean old detailed traffic records (based on global $history setting)
 # Clean old detailed traffic records (based on global $history setting)
@@ -327,7 +327,7 @@ my %auth_table;
 my @auth_full_list = get_records_sql($dbh, $auth_sql);
 my @auth_full_list = get_records_sql($dbh, $auth_sql);
 foreach my $auth (@auth_full_list) {
 foreach my $auth (@auth_full_list) {
     next if (!$auth);
     next if (!$auth);
-    my $auth_mac = mac_dotted($auth->{mac});
+    my $auth_mac = mac_splitted($auth->{mac});
     next if (exists $auth_table{$auth_mac});
     next if (exists $auth_table{$auth_mac});
     next if (!exists $connections{$auth->{id}});
     next if (!exists $connections{$auth->{id}});