Files
html/api/investigate-500.py
2026-04-16 14:55:04 +02:00

47 lines
1.8 KiB
Python
Executable File

#!/usr/bin/env python3
import json, requests, subprocess, sys, time, re
from collections import Counter
SMART = "/var/www/html/api/screens-health-smart.json"
if not __import__('os').path.exists(SMART):
print(json.dumps({"error":"no smart_classify yet, run smart_classify_safe first"}))
sys.exit(0)
j = json.load(open(SMART))
broken = [p for p in j.get("real_problems",[]) if p.get("status") == "TRULY_BROKEN"]
sample = broken[:8]
# Re-test each sample carefully and capture timing + FPM log
results = []
log_marker = time.strftime("%Y-%m-%dT%H:%M")
for s in sample:
url = s["url"]
t0 = time.time()
try:
r = requests.post(url, json={}, timeout=8)
elapsed = time.time() - t0
body_excerpt = r.text[:400] if r.text else "(empty body)"
results.append({"url": url, "code": r.status_code, "ms": int(elapsed*1000), "body": body_excerpt})
except Exception as e:
results.append({"url": url, "error": str(e)[:200]})
# Get last FPM errors related to our sample paths
err_log = subprocess.run("sudo tail -100 /var/log/php8.5-fpm.log 2>/dev/null", shell=True, capture_output=True, text=True, timeout=5).stdout
# Find shared common include candidates
includes = Counter()
for s in sample[:8]:
path = "/var/www/html" + s["url"].replace("https://weval-consulting.com","")
try:
with open(path) as f:
for line in f.readlines()[:20]:
m = re.search(r'(?:require|include)(?:_once)?[ (]+["\']([^"\']+)["\']', line)
if m: includes[m.group(1)] += 1
except: pass
print(json.dumps({
"sample_results": results,
"common_includes": dict(includes.most_common(10)),
"fpm_log_tail": [l for l in err_log.split("\n") if 'WARNING' in l or 'ERROR' in l][-10:],
"load_now": subprocess.run("uptime", shell=True, capture_output=True, text=True).stdout.strip()
}, indent=2)[:5000])