import json, re, subprocess, sys, time, urllib.request, urllib.error from datetime import datetime BASE="http://127.0.0.1:9007"; TOKEN=sys.argv[1] targets = [l.split() for l in sys.stdin.read().strip().split("\n") if l.strip()] def call(m,p): real = re.sub(r"\{[^/}]+\}","1",p) h={"Authorization":"Bearer "+TOKEN,"tenant-id":"1"}; b=None if m in ("POST","PUT"): b=b"{}"; h["Content-Type"]="application/json" req=urllib.request.Request(BASE+real,data=b,headers=h,method=m) try: with urllib.request.urlopen(req,timeout=60) as r: raw=r.read(3000).decode("utf-8","replace") except urllib.error.HTTPError as e: raw=e.read(3000).decode("utf-8","replace") except Exception as e: return "EXC "+str(e)[:80] try: j=json.loads(raw); return "code=%s msg=%s" % (j.get("code"), (j.get("msg") or "")[:120]) except Exception: return "RAW "+raw[:120] def exc_since(ts): out=subprocess.run(["docker","logs","--since",ts,"mom-kyzn"],stdout=subprocess.PIPE,stderr=subprocess.STDOUT,timeout=120) t=re.sub(r"\x1b\[[0-9;]*m","",out.stdout.decode("utf-8","replace")) ls=t.split("\n") for i,l in enumerate(ls): if "defaultExceptionHandler" in l: for l2 in ls[i+1:i+120]: if re.match(r"^[a-zA-Z].*?(Exception|Error)", l2): return l2.strip()[:200] return "" for m,p in targets: ts=datetime.now().strftime("%Y-%m-%dT%H:%M:%S"); time.sleep(0.2) r=call(m,p); time.sleep(0.6) print("%-52s %-58s %s" % (p, r, exc_since(ts)), flush=True)