This commit is contained in:
2026-04-08 13:47:31 +03:00
parent 7289430fc3
commit 81964183e8

View File

@@ -125,6 +125,10 @@ NB_PROBLEM_LOCK = asyncio.Lock()
# Structured event log output (JSON Lines)
EVENT_LOG_PATH = os.environ.get("EVENT_LOG_PATH", "/opt/containers/nats-registration-listener/logs/registration_events.jsonl")
EVENODD = (os.environ.get("EVENODD", "") or "").strip().lower()
if EVENODD not in ("", "even", "odd"):
raise SystemExit("ERROR: EVENODD must be unset, 'even', or 'odd'")
# =========================
# Arg parsing
@@ -296,6 +300,18 @@ def normalize_mac(mac: str) -> Optional[str]:
return None
def mac_belongs_to_this_shard(mac_norm: Optional[str]) -> bool:
if EVENODD == "":
return True
if not mac_norm:
return True
md5_hex = hashlib.md5(mac_norm.encode("utf-8")).hexdigest()
last_nibble = int(md5_hex[-1], 16)
if EVENODD == "even":
return (last_nibble % 2) == 0
return (last_nibble % 2) == 1
IGNORE_MACS = {m for m in (normalize_mac(x) for x in IGNORE_MACS_RAW) if m}
@@ -584,11 +600,11 @@ async def main():
recent_payloads.pop(k, None)
product = mac = fw = "-"
obj = None
try:
text = payload.decode("utf-8", errors="replace")
obj = json.loads(text)
product, mac, fw = extract_fields(obj)
event["reg_age_s"] = extract_registration_age_s(obj)
except Exception:
pass
@@ -597,7 +613,15 @@ async def main():
event["fw"] = fw
mac_norm = normalize_mac(mac)
if not mac_belongs_to_this_shard(mac_norm):
return
event["mac_norm"] = mac_norm
if obj is not None:
try:
event["reg_age_s"] = extract_registration_age_s(obj)
except Exception:
pass
if mac_norm in IGNORE_MACS:
event["ignored"] = True
event["ignore_reason"] = "mac_ignore_list"