Files
2026-02-05 11:58:17 +01:00

46 lines
1.7 KiB
Python

import socket
import json
import re
# KONFIGURATION
LISTEN_IP = "0.0.0.0"
LISTEN_PORT = 1514
SYNO_IP = "192.168.x.x" # Deine Synology IP
SYNO_PORT = 514
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((LISTEN_IP, LISTEN_PORT))
out_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print(f"Relay aktiv: UDP {LISTEN_PORT} -> {SYNO_IP}:{SYNO_PORT}")
while True:
data, addr = sock.recvfrom(4096)
try:
# Extrahiere Syslog Header und Nachricht
msg = data.decode('utf-8', errors='ignore')
# Suche den JSON Teil (alles ab der ersten geschweiften Klammer)
json_match = re.search(r'(\{.*\})', msg)
if json_match:
header = msg[:json_match.start()]
payload = json.loads(json_match.group(1))
# Flachklopfen: Level und Message extrahieren
level = payload.get("level", "info").upper()
content = payload.get("msg", "")
# Alle anderen Felder als Key=Value anhängen
extras = " ".join([f"{k}={v}" for k, v in payload.items() if k not in ["level", "msg", "ts"]])
# Baue neue einzeilige Nachricht
clean_msg = f"{header} [{level}] {content} {extras}".replace('\n', ' ').replace('\r', '')
out_sock.sendto(clean_msg.encode('utf-8'), (SYNO_IP, SYNO_PORT))
else:
# Kein JSON? Einfach Zeilenumbrüche entfernen und durchreichen
out_sock.sendto(msg.replace('\n', ' ').encode('utf-8'), (SYNO_IP, SYNO_PORT))
except Exception as e:
# Im Fehlerfall original senden (nur ohne Umbrüche)
out_sock.sendto(data.replace(b'\n', b' '), (SYNO_IP, SYNO_PORT))