douyinWEB/scripts/_diag_nomadvlog_flap.py
2026-09-13 22:10:49 -05:00

107 lines
4.7 KiB
Python

# -*- coding: utf-8 -*-
"""Diagnose nomadvlog start/stop flap on dylive."""
from __future__ import annotations
import json
import sys
import http.cookiejar
import urllib.request
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
BASE = "https://dylive.nomadro.com"
GID_HINT = "nomadvlog"
REMOTE = r"""
set -e
GID=$(sqlite3 /opt/douyinWEB/backend/data/monitor.db "SELECT id FROM yt_assign_groups WHERE lower(name) LIKE '%nomadvlog%' LIMIT 1;")
echo "GID=$GID"
echo "=== group ==="
sqlite3 -header -column /opt/douyinWEB/backend/data/monitor.db "SELECT id,name,category,enabled,neko_instance_id,substr(youtube_channel_id,1,20) AS yt FROM yt_assign_groups WHERE id='$GID';"
echo "=== relay_state ==="
sqlite3 -header -column /opt/douyinWEB/backend/data/monitor.db "SELECT group_id,status,source_room_id,anchor_name,ffmpeg_pid,substr(error,1,120) AS err,datetime(updated_at,'unixepoch','+8 hours') AS bj_upd,datetime(started_at,'unixepoch','+8 hours') AS bj_start FROM youtube_relay_state WHERE group_id='$GID';"
echo "=== recent_log tail ==="
sqlite3 /opt/douyinWEB/backend/data/monitor.db "SELECT recent_log FROM youtube_relay_state WHERE group_id='$GID';" | tail -c 4000
echo
echo "=== events last 18h mentioning nomadvlog / relay ==="
sqlite3 -header -column /opt/douyinWEB/backend/data/monitor.db "
SELECT datetime(created_at,'unixepoch','+8 hours') AS bj, kind, level, substr(message,1,140) AS msg
FROM events
WHERE created_at > strftime('%s','now')-18*3600
AND (
message LIKE '%nomadvlog%'
OR message LIKE '%$GID%'
OR (kind LIKE '%relay%' OR kind LIKE '%youtube%' OR kind LIKE '%live%') AND message LIKE '%停%'
OR message LIKE '%source_switch%'
OR message LIKE '%outside_slot%'
OR message LIKE '%开播%'
OR message LIKE '%推流%'
)
ORDER BY created_at DESC LIMIT 80;
"
echo "=== settings ==="
sqlite3 -header -column /opt/douyinWEB/backend/data/monitor.db "
SELECT key,value FROM kv_settings
WHERE key IN (
'youtube_relay_enabled','youtube_relay_time_slot_mode','youtube_relay_exclusive_live',
'youtube_relay_respect_schedule','youtube_relay_on_air_deadline_sec'
);
"
echo "=== schedule active now ==="
cd /opt/douyinWEB/backend
/opt/douyinWEB/backend/.venv/bin/python - <<'PY'
import sys
sys.path.insert(0,'/opt/douyinWEB/backend')
from app import yt_assign_db
sched=yt_assign_db.build_rotation_schedule(include_urls=False, keyed_only=True)
print('beijing_hint', sched.get('beijing_hint') or sched.get('now_beijing') or sched.get('push_date'))
for c in (sched.get('channels') or []):
name=str(c.get('name') or '')
if 'nomadvlog' in name.lower() or c.get('active'):
print('CH', name, 'active=',c.get('active'), 'window=',c.get('window') or c.get('slot') or c.get('label'), 'gid=',c.get('group_id'), 'aliases=',c.get('alias_group_ids'))
print('windows', (sched.get('windows') or sched.get('slots') or '')[:500] if isinstance(sched.get('windows') or sched.get('slots'), str) else json.dumps(sched.get('windows') or sched.get('slots') or [], ensure_ascii=False)[:800])
import json
PY
echo "=== journal relay/nomadvlog last 200 ==="
journalctl -u douyinweb-api.service --since '18 hours ago' --no-pager 2>/dev/null | grep -iE 'nomadvlog|source_switch|outside_slot|on_air|invalid|halt|exclusive|45f9bd79' | tail -120 || true
"""
def main():
# public API peek
cj = http.cookiejar.CookieJar()
op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
data = json.dumps({"username": "hackrobot", "password": "Xiao4669805@"}).encode()
op.open(
urllib.request.Request(
BASE + "/api/auth/login",
data=data,
method="POST",
headers={"Content-Type": "application/json"},
),
timeout=30,
)
with op.open(urllib.request.Request(BASE + "/api/youtube/relay/status"), timeout=60) as r:
st = json.loads(r.read().decode())
print("PUBLIC auto=", (st.get("settings") or {}).get("youtube_relay_enabled"))
for lane in st.get("lanes") or []:
if "nomadvlog" in str(lane.get("name") or "").lower():
print("PUBLIC lane", json.dumps({k: lane.get(k) for k in ["name","status","error","source_room_id","anchor_name","ffmpeg_pid","manual_hold","recent_log"]}, ensure_ascii=False)[:1200])
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=45, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
with sftp.file("/tmp/_dy_nomadvlog_diag.sh", "w") as f:
f.write(REMOTE.replace("\r\n", "\n"))
sftp.close()
_, o, e = c.exec_command("bash /tmp/_dy_nomadvlog_diag.sh", timeout=180)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
if __name__ == "__main__":
main()