Unexpected Device Swapping

Adriano Miranda5 months ago

Hi guys,

I have noticed that sometimes Traccar reports incorrect positions to random devices. Upon investigation, I found a strange behavior in the logs.

We can see the connection ID T36d14dda suddenly changes the device from 86371906XXX (Teltonika device) to 197001XXX (Suntech device):

SCR-20240630-mgmx.png

I'm currently using version 5.12 and am updating to the latest version to check if the issue persists.

Anton Tananaev5 months ago

Let us know how it goes with the latest version.

Adriano Miranda5 months ago

Hello again, everyone. I have updated to the latest version (6.2) and so far, I haven't detected any occurrences of this behavior. However, I was curious and created a Python script to automatically check some old logs to try and understand when this started happening.

If anyone is interested in checking their own logs, here is the script. Just run it with Python and pass the directory containing the Traccar logs as a parameter (it will scan one by one, so if you don't want to scan all logs, separate the ones you want to examine into a separate directory):

import re
import sys
import os
import glob

# Check if the log folder path was passed as an argument
if len(sys.argv) < 2:
    print("Usage: python script.py <path_to_logs_folder>")
    sys.exit(1)

logs_folder = sys.argv[1]

# Dictionary to store the tracker ID by connection ID
connection_tracker = {}

# Regular expression to extract information
log_pattern = re.compile(r'$begin:math:display$(T[a-f0-9]+)$end:math:display$.*id: (\d+)')

# Function to process each log line
def process_log_line(line):
    anomalies_found = False
    match = log_pattern.search(line)
    if match:
        connection_id, tracker_id = match.groups()
        if connection_id in connection_tracker:
            if connection_tracker[connection_id] != tracker_id:
                print(f"[FOUND] Anomaly detected! Connection {connection_id} changed from ID {connection_tracker[connection_id]} to {tracker_id}")
                print(f"Line detail: {line}")
                anomalies_found = True
        connection_tracker[connection_id] = tracker_id
    return anomalies_found

# Process all log files in the specified folder that follow the naming pattern
log_files = glob.glob(os.path.join(logs_folder, 'tracker-server.log.*'))
for log_path in log_files:
    print(f"Processing {log_path}...")
    anomalies_detected = False
    with open(log_path, 'r') as file:
        for line in file:
            if process_log_line(line):
                anomalies_detected = True
    if not anomalies_detected:
        print(f"[OK] No anomalies found in {log_path}.")
    # Clear the dictionary for the next file
    connection_tracker.clear()