def parse_tracking_output()

in scripts/chrony_exporter.py [0:0]


def parse_tracking_output():
    """Parses `chronyc tracking` and calculates clock error."""
    try:
        result = subprocess.run(['chronyc', 'tracking'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
        output = result.stdout.decode('utf-8')

        last_offset = root_dispersion = root_delay = None

        for line in output.splitlines():
            if line.startswith('Last offset'):
                match = re.search(r'([-+]?[0-9]*\.?[0-9]+)', line)
                if match:
                    last_offset = abs(float(match.group(1)))
            elif line.startswith('Root dispersion'):
                match = re.search(r'([-+]?[0-9]*\.?[0-9]+)', line)
                if match:
                    root_dispersion = float(match.group(1))
            elif line.startswith('Root delay'):
                match = re.search(r'([-+]?[0-9]*\.?[0-9]+)', line)
                if match:
                    root_delay = float(match.group(1))

        if None not in (last_offset, root_dispersion, root_delay):
            clock_error_sec = last_offset + root_dispersion + (0.5 * root_delay)
            return clock_error_sec * 1000  # convert to ms
    except subprocess.CalledProcessError as e:
        print(f"chronyc error: {e}")
    except Exception as e:
        print(f"Unexpected error: {e}")
    return None