logs for it-screen added
This commit is contained in:
parent
6a2c4d48c4
commit
1f22546aa0
|
|
@ -67,9 +67,10 @@ DEVICE_ID_RE = re.compile(r"^g-box-[a-f0-9]{8}$")
|
||||||
AP_PASSWORD_RE = re.compile(r"^[a-z0-9]{8,}$")
|
AP_PASSWORD_RE = re.compile(r"^[a-z0-9]{8,}$")
|
||||||
ITSCREEN_SERVICE_UNIT = "itscreen-x11.service"
|
ITSCREEN_SERVICE_UNIT = "itscreen-x11.service"
|
||||||
ITSCREEN_CONFIG_PATH = "/var/lib/itscreen/config/it-screen/it-screen.conf"
|
ITSCREEN_CONFIG_PATH = "/var/lib/itscreen/config/it-screen/it-screen.conf"
|
||||||
|
ITSCREEN_LOG_PATH = "/var/lib/itscreen/logs/itscreen.log"
|
||||||
ITSCREEN_CONFIG_CACHE_TTL_SEC = 3.0
|
ITSCREEN_CONFIG_CACHE_TTL_SEC = 3.0
|
||||||
ITSCREEN_LOG_DEFAULT_LINES = 50
|
ITSCREEN_LOG_DEFAULT_LINES = 100
|
||||||
ITSCREEN_LOG_MAX_LINES = 300
|
ITSCREEN_LOG_MAX_LINES = 100
|
||||||
DEFAULT_AP_UI_PORT = "80"
|
DEFAULT_AP_UI_PORT = "80"
|
||||||
CAPTIVE_PORTAL_PATH = "/portal"
|
CAPTIVE_PORTAL_PATH = "/portal"
|
||||||
CAPTIVE_PORTAL_ALIASES = {
|
CAPTIVE_PORTAL_ALIASES = {
|
||||||
|
|
@ -2017,39 +2018,41 @@ def clamp_itscreen_log_lines(value, default=ITSCREEN_LOG_DEFAULT_LINES):
|
||||||
return max(1, min(parsed, ITSCREEN_LOG_MAX_LINES))
|
return max(1, min(parsed, ITSCREEN_LOG_MAX_LINES))
|
||||||
|
|
||||||
|
|
||||||
|
def read_tail_lines(path, count):
|
||||||
|
with open(path, "rb") as fh:
|
||||||
|
fh.seek(0, os.SEEK_END)
|
||||||
|
file_size = fh.tell()
|
||||||
|
block_size = 4096
|
||||||
|
chunks = []
|
||||||
|
lines_found = 0
|
||||||
|
position = file_size
|
||||||
|
|
||||||
|
while position > 0 and lines_found <= count:
|
||||||
|
read_size = min(block_size, position)
|
||||||
|
position -= read_size
|
||||||
|
fh.seek(position)
|
||||||
|
chunk = fh.read(read_size)
|
||||||
|
chunks.append(chunk)
|
||||||
|
lines_found += chunk.count(b"\n")
|
||||||
|
|
||||||
|
data = b"".join(reversed(chunks))
|
||||||
|
text = data.decode("utf-8", errors="replace")
|
||||||
|
return [line.rstrip("\r") for line in text.splitlines()[-count:]]
|
||||||
|
|
||||||
|
|
||||||
def read_itscreen_logs(lines):
|
def read_itscreen_logs(lines):
|
||||||
count = clamp_itscreen_log_lines(lines)
|
count = clamp_itscreen_log_lines(lines)
|
||||||
if not shutil.which("journalctl"):
|
|
||||||
raise RuntimeError("journalctl is unavailable")
|
|
||||||
cmd = [
|
|
||||||
"journalctl",
|
|
||||||
"-u",
|
|
||||||
ITSCREEN_SERVICE_UNIT,
|
|
||||||
"-n",
|
|
||||||
str(count),
|
|
||||||
"--no-pager",
|
|
||||||
"-o",
|
|
||||||
"short-iso",
|
|
||||||
]
|
|
||||||
try:
|
try:
|
||||||
proc = subprocess.run(
|
lines = read_tail_lines(ITSCREEN_LOG_PATH, count)
|
||||||
cmd,
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.PIPE,
|
|
||||||
text=True,
|
|
||||||
check=False,
|
|
||||||
timeout=10,
|
|
||||||
)
|
|
||||||
except FileNotFoundError as exc:
|
except FileNotFoundError as exc:
|
||||||
raise RuntimeError("journalctl is unavailable") from exc
|
raise RuntimeError(f"Log file is unavailable: {ITSCREEN_LOG_PATH}") from exc
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
raise RuntimeError(str(exc)) from exc
|
raise RuntimeError(str(exc)) from exc
|
||||||
if proc.returncode != 0:
|
|
||||||
raise RuntimeError((proc.stderr or "").strip() or "Failed to read service logs")
|
|
||||||
return {
|
return {
|
||||||
"ok": True,
|
"ok": True,
|
||||||
"unit": ITSCREEN_SERVICE_UNIT,
|
"unit": ITSCREEN_SERVICE_UNIT,
|
||||||
"lines": [line.rstrip("\n") for line in (proc.stdout or "").splitlines()],
|
"source": ITSCREEN_LOG_PATH,
|
||||||
|
"lines": lines,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -2209,59 +2212,31 @@ class Handler(BaseHTTPRequestHandler):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def stream_itscreen_logs(self):
|
def stream_itscreen_logs(self):
|
||||||
if not shutil.which("journalctl"):
|
if not os.path.exists(ITSCREEN_LOG_PATH):
|
||||||
self.send_json({"ok": False, "error": "Logs are unavailable"}, code=503)
|
self.send_json({"ok": False, "error": f"Logs are unavailable: {ITSCREEN_LOG_PATH}"}, code=503)
|
||||||
return
|
return
|
||||||
proc = None
|
log_file = None
|
||||||
try:
|
try:
|
||||||
proc = subprocess.Popen(
|
log_file = open(ITSCREEN_LOG_PATH, "r", encoding="utf-8", errors="replace")
|
||||||
[
|
log_file.seek(0, os.SEEK_END)
|
||||||
"journalctl",
|
|
||||||
"-u",
|
|
||||||
ITSCREEN_SERVICE_UNIT,
|
|
||||||
"-f",
|
|
||||||
"-n",
|
|
||||||
"0",
|
|
||||||
"--no-pager",
|
|
||||||
"-o",
|
|
||||||
"short-iso",
|
|
||||||
],
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.PIPE,
|
|
||||||
text=True,
|
|
||||||
bufsize=1,
|
|
||||||
)
|
|
||||||
self.send_sse_headers()
|
self.send_sse_headers()
|
||||||
self.wfile.write(b": connected\n\n")
|
self.wfile.write(b": connected\n\n")
|
||||||
self.wfile.flush()
|
self.wfile.flush()
|
||||||
last_keepalive_at = time.time()
|
last_keepalive_at = time.time()
|
||||||
while True:
|
while True:
|
||||||
if proc.poll() is not None:
|
line = log_file.readline()
|
||||||
stderr_text = ""
|
if not line:
|
||||||
try:
|
if not os.path.exists(ITSCREEN_LOG_PATH):
|
||||||
stderr_text = (proc.stderr.read() or "").strip() if proc.stderr else ""
|
payload = json.dumps({"message": f"Log file disappeared: {ITSCREEN_LOG_PATH}"})
|
||||||
except Exception:
|
|
||||||
stderr_text = ""
|
|
||||||
if proc.returncode not in (0, None):
|
|
||||||
payload = json.dumps({"message": stderr_text or "Log stream disconnected"})
|
|
||||||
self.wfile.write(f"event: error\ndata: {payload}\n\n".encode("utf-8"))
|
self.wfile.write(f"event: error\ndata: {payload}\n\n".encode("utf-8"))
|
||||||
self.wfile.flush()
|
self.wfile.flush()
|
||||||
break
|
break
|
||||||
ready = []
|
time.sleep(1.0)
|
||||||
if proc.stdout:
|
|
||||||
try:
|
|
||||||
ready, _, _ = select.select([proc.stdout], [], [], 1.0)
|
|
||||||
except Exception:
|
|
||||||
ready = []
|
|
||||||
if not ready:
|
|
||||||
if time.time() - last_keepalive_at >= 15:
|
if time.time() - last_keepalive_at >= 15:
|
||||||
self.wfile.write(b": keepalive\n\n")
|
self.wfile.write(b": keepalive\n\n")
|
||||||
self.wfile.flush()
|
self.wfile.flush()
|
||||||
last_keepalive_at = time.time()
|
last_keepalive_at = time.time()
|
||||||
continue
|
continue
|
||||||
line = proc.stdout.readline() if proc.stdout else ""
|
|
||||||
if not line:
|
|
||||||
continue
|
|
||||||
payload = json.dumps({"line": line.rstrip("\n")})
|
payload = json.dumps({"line": line.rstrip("\n")})
|
||||||
self.wfile.write(f"event: log\ndata: {payload}\n\n".encode("utf-8"))
|
self.wfile.write(f"event: log\ndata: {payload}\n\n".encode("utf-8"))
|
||||||
self.wfile.flush()
|
self.wfile.flush()
|
||||||
|
|
@ -2274,7 +2249,11 @@ class Handler(BaseHTTPRequestHandler):
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
finally:
|
finally:
|
||||||
terminate_process(proc)
|
try:
|
||||||
|
if log_file:
|
||||||
|
log_file.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
def handle_captive_portal_probe(self, parsed):
|
def handle_captive_portal_probe(self, parsed):
|
||||||
if not captive_portal_is_active():
|
if not captive_portal_is_active():
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue