-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1829 lines (1641 loc) · 85.8 KB
/
Copy pathmain.py
File metadata and controls
1829 lines (1641 loc) · 85.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Hearth · FastAPI backend
#
# 双源、全只读、零生产影响:
# - obs-prometheus (PROMETHEUS_URL) : DCGM GPU(全 3 节点) + 2 台 Spark 的 node/hwmon
# - 宿主 node-exporter (NODE_EXPORTER_URL) : host 自身 node_* + 全部 hwmon 温度
# (if your obs Prometheus has no scrape job for this host, Hearth directly hits :9100/:9400)
#
# 不依赖任何 recording rule —— 聚合在本服务内用原始 PromQL 计算,
# 因此无需改 obs 的 prometheus.yml(严格不越界)。
#
# GET /api/health /cluster /nodes /nodes/{id} /models /models/{id}
# /alerts /logs /topology /stream(SSE)
import os
import re
import sys
import time
import json
import asyncio
from pathlib import Path
from typing import Any
from datetime import datetime, timezone
import httpx
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import tfevents # 零依赖 tfevents 标量解析(训练信号 Phase 2)
# ── Config ─────────────────────────────────────────────────────────
PROM_URL = os.environ.get("PROMETHEUS_URL", "http://host.docker.internal:9090")
NODEEXP_URL = os.environ.get("NODE_EXPORTER_URL", "http://host.docker.internal:9100")
# Atlas 这块 ASUS ROG 板 hwmon collector(asus WMI/EC + nct6798 走 ACPI 串行读)
# 单次 scrape 稳定 3-5s,EC 偶发争用会更久。直采超时必须远高于此,否则后台
# 双采(_atlas_node_live)任一次超时即把 Atlas 误判为 offline。
NODEEXP_TIMEOUT = float(os.environ.get("NODE_EXPORTER_TIMEOUT", "12.0"))
AM_URL = os.environ.get("ALERTMANAGER_URL", "http://host.docker.internal:9093")
LITELLM_URL = os.environ.get("LITELLM_URL", "http://host.docker.internal:4000")
LITELLM_KEY = os.environ.get("LITELLM_MASTER_KEY", "")
CORS = os.environ.get("CORS_ORIGINS", "*").split(",")
TICK_SEC = float(os.environ.get("TICK_SEC", "1.5"))
# ─────────────────────────────────────────────────────────────────────
# Topology loaded from YAML (`$HEARTH_CONFIG`, default /etc/hearth/config.yaml).
# Schema docs: docs/topology.md · example: config/hearth.example.yaml
#
# Each node carries a `kind` field that replaces the legacy GB10-special:
# - discrete : dedicated VRAM GPU (use DCGM FB_USED/FB_FREE for VRAM%)
# - unified-arm-soc: GPU shares system memory (GB10 / Jetson) — use
# node_exporter MemAvailable for VRAM%
# - apple-silicon : same unified-memory treatment as ARM SoC (mlx, Ollama on Metal)
#
# `node_source`: "obs" → metrics via the obs Prometheus we scrape from
# "direct"→ Hearth API scrapes the host's :9100/:9400 itself
# (used when this host isn't in the obs Prometheus job)
# Single-host default activates if the YAML is absent — single localhost node.
# ─────────────────────────────────────────────────────────────────────
def _default_config() -> dict:
"""Single-host localhost default — first `docker compose up` works with no YAML."""
return {
"display": {"cluster_name": "Home Cluster"},
"gateway": {"type": "litellm", "enabled": True,
"base_url": os.environ.get("LITELLM_URL", "http://host.docker.internal:4000"),
"master_key_env": "LITELLM_MASTER_KEY"},
"nodes": [{
"id": "node-1", "name": "localhost", "ip": "127.0.0.1",
"role_label": "node", "kind": "discrete",
"class": "GPU host",
"hw": {"gpu": "—", "vram_gb": 0, "cpu_cores": 0, "cpu_threads": 0, "ram_gb": 0},
"sources": {"node_exporter": "host.docker.internal:9100",
"dcgm": "host.docker.internal:9400"},
}],
"model_meta": {},
"model_topology": {},
}
def _load_config(path: str) -> dict:
"""Load Hearth YAML config; fall back to single-host default if absent/invalid."""
try:
import yaml # noqa: WPS433 — optional dep, fail soft
except ImportError:
print("[hearth] pyyaml not installed; using single-host default", file=sys.stderr)
return _default_config()
p = Path(path) if path else None
if p and p.exists():
try:
data = yaml.safe_load(p.read_text()) or {}
if isinstance(data, dict) and data.get("nodes"):
return data
print(f"[hearth] config {path} loaded but has no nodes; using default", file=sys.stderr)
except Exception as e:
print(f"[hearth] failed to parse {path}: {e}; using default", file=sys.stderr)
return _default_config()
def _node_from_yaml(y: dict) -> dict:
"""YAML node entry → internal flat dict (preserves legacy NODES shape so
the rest of main.py is untouched)."""
hw = y.get("hw") or {}
src = y.get("sources") or {}
obs_label = src.get("obs_node_label")
return {
"id": y["id"],
"name": y.get("name", y["id"]),
"ip": y.get("ip", ""),
"class": y.get("class", "GPU host"),
"role": y.get("role_label", y.get("role", "node")),
"kind": y.get("kind", "discrete"),
"obs_node": obs_label,
# node_metrics: "obs" | "direct" — override for hosts whose node_exporter
# the obs Prometheus can't reach (e.g. the obs host's own bridge-net
# hairpin). Such a node keeps obs_node_label (GPU via obs DCGM) but
# scrapes :9100 directly for CPU/mem/disk/net. Default: obs if labelled.
"node_source": src.get("node_metrics") or ("obs" if obs_label else "direct"),
"gpu": {"name": hw.get("gpu", "—"),
"mem": hw.get("vram_gb", 0),
"fp16": hw.get("fp16_tflops", 0),
"fp4": hw.get("fp4_tflops", 0)},
"cpu": {"model": hw.get("cpu_model", "—"),
"cores": hw.get("cpu_cores", 0),
"threads": hw.get("cpu_threads", hw.get("cpu_cores", 0))},
"ram": hw.get("ram_gb", 0),
"disk": hw.get("disk_gb", 0),
"net": hw.get("net", ""),
"services": y.get("services", []),
}
HEARTH_CONFIG_PATH = os.environ.get("HEARTH_CONFIG", "/etc/hearth/config.yaml")
HEARTH_CFG = _load_config(HEARTH_CONFIG_PATH)
NODES = [_node_from_yaml(n) for n in HEARTH_CFG.get("nodes", [])] or [
_node_from_yaml(n) for n in _default_config()["nodes"]
]
NODE_BY_ID = {n["id"]: n for n in NODES}
OBS_TO_ID = {n["obs_node"]: n["id"] for n in NODES if n["obs_node"]}
IP_TO_ID = {n["ip"]: n["id"] for n in NODES}
# kind lookup by obs_node label — replaces the legacy `obs_node != "rtx4090-pc"`
KIND_BY_OBS = {n["obs_node"]: n.get("kind", "discrete") for n in NODES if n["obs_node"]}
# First discrete-kind node's obs label, for cluster-level "the discrete GPU" lookups
DISCRETE_OBS = next((n["obs_node"] for n in NODES
if n.get("kind") == "discrete" and n.get("obs_node")), None)
app = FastAPI(title="Hearth API", version="0.1.0")
app.add_middleware(CORSMiddleware, allow_origins=CORS, allow_credentials=True,
allow_methods=["*"], allow_headers=["*"])
client = httpx.AsyncClient(timeout=8.0)
# ── PromQL (obs-prometheus, 只读) ───────────────────────────────────
async def promql(query: str) -> list[dict[str, Any]]:
try:
r = await client.get(f"{PROM_URL}/api/v1/query", params={"query": query})
r.raise_for_status()
d = r.json()
if d.get("status") != "success":
return []
return [{"metric": i["metric"], "value": float(i["value"][1])}
for i in d["data"]["result"]]
except Exception:
return []
async def promql_range(query: str, minutes: int = 5, step: int = 15):
now = time.time()
try:
r = await client.get(f"{PROM_URL}/api/v1/query_range",
params={"query": query, "start": now - minutes * 60,
"end": now, "step": step})
r.raise_for_status()
d = r.json()
if d.get("status") != "success":
return []
return [{"metric": i["metric"],
"values": [[float(t), float(v)] for t, v in i["values"]]}
for i in d["data"]["result"]]
except Exception:
return []
def _one(rs, default=0.0):
return rs[0]["value"] if rs else default
def _by(rs, label):
return {i["metric"].get(label, "?"): i["value"] for i in rs}
# ── Host node-exporter direct scrape (when obs Prometheus has no job for this host) ──
_PROM_LINE = re.compile(r'^([a-zA-Z_:][\w:]*)\{([^}]*)\}\s+([-\d.eE+]+)\s*$')
_PROM_BARE = re.compile(r'^([a-zA-Z_:][\w:]*)\s+([-\d.eE+]+)\s*$')
def _parse_labels(s: str) -> dict[str, str]:
out = {}
for m in re.finditer(r'(\w+)="((?:[^"\\]|\\.)*)"', s):
out[m.group(1)] = m.group(2).replace('\\"', '"').replace('\\\\', '\\')
return out
async def _scrape_node_exporter() -> dict[str, list[dict]]:
"""抓一次宿主 node-exporter,按指标名归并 [{labels, value}]。"""
out: dict[str, list[dict]] = {}
try:
r = await client.get(f"{NODEEXP_URL}/metrics", timeout=NODEEXP_TIMEOUT)
r.raise_for_status()
for line in r.text.splitlines():
if not line or line[0] == "#":
continue
m = _PROM_LINE.match(line)
if m:
name, lbl, val = m.group(1), _parse_labels(m.group(2)), m.group(3)
else:
b = _PROM_BARE.match(line)
if not b:
continue
name, lbl, val = b.group(1), {}, b.group(2)
try:
out.setdefault(name, []).append({"labels": lbl, "value": float(val)})
except ValueError:
pass
except Exception:
pass
return out
def _sum(rows, pred=lambda l: True):
return sum(x["value"] for x in rows if pred(x["labels"]))
# 模块归类:把 hwmon 传感器映射成人话硬件模块
def _classify_temp(chip: str, label: str) -> str:
c, l = chip.lower(), label.lower()
if "coretemp" in c or "k10temp" in c or "package" in l or "tctl" in l or "tccd" in l:
return "CPU"
if c.startswith("nvme") or "nvme" in c:
return "NVMe"
if "coolant" in l or "pump" in l or "water" in l:
return "水冷"
if "mac temp" in l or "phy temp" in l or "nic" in l or "mlx" in c:
return "网卡"
if "soc" in l or "gpu" in l:
return "SoC"
if "thermal_zone" in c or "acpitz" in c or "pch" in l or "systin" in l or "board" in l:
return "平台"
return "其他"
def _atlas_temps(scrape: dict) -> list[dict]:
"""从宿主 node-exporter 文本里抽出全部硬件模块温度。"""
labels_idx = {}
for x in scrape.get("node_hwmon_sensor_label", []):
lb = x["labels"]
labels_idx[(lb.get("chip", ""), lb.get("sensor", ""))] = lb.get("label", "")
temps = []
for x in scrape.get("node_hwmon_temp_celsius", []):
lb = x["labels"]
chip, sensor = lb.get("chip", ""), lb.get("sensor", "")
human = labels_idx.get((chip, sensor)) or sensor
if x["value"] <= 0 or x["value"] > 150: # 跳过无效/未连接传感器
continue
temps.append({"module": _classify_temp(chip, human),
"label": human, "chip": chip,
"celsius": round(x["value"], 1)})
# 按模块聚合给一个代表值(取最高)+ 保留明细
return sorted(temps, key=lambda t: (-t["celsius"]))
def _atlas_fans(scrape: dict) -> list[dict]:
"""从宿主 node-exporter 抽出风扇转速(带人话 label,按转速降序)。"""
labels_idx = {}
for x in scrape.get("node_hwmon_sensor_label", []):
lb = x["labels"]
labels_idx[(lb.get("chip", ""), lb.get("sensor", ""))] = lb.get("label", "")
fans = []
for x in scrape.get("node_hwmon_fan_rpm", []):
lb = x["labels"]
chip, sensor = lb.get("chip", ""), lb.get("sensor", "")
human = labels_idx.get((chip, sensor))
# 跳过既无 label 又 0 转的幽灵通道(主板上未接的风扇头)
if human is None and x["value"] <= 0:
continue
fans.append({"label": human or sensor, "chip": chip,
"rpm": int(round(x["value"]))})
return sorted(fans, key=lambda f: -f["rpm"])
# CPU% / network rates need two-sample diff
async def _atlas_node_live() -> dict:
s1 = await _scrape_node_exporter()
if not s1:
return {}
await asyncio.sleep(0.4)
s2 = await _scrape_node_exporter()
def cpu_total(s):
idle = _sum(s.get("node_cpu_seconds_total", []), lambda l: l.get("mode") == "idle")
tot = _sum(s.get("node_cpu_seconds_total", []))
return idle, tot
i1, t1 = cpu_total(s1)
i2, t2 = cpu_total(s2)
cpu = max(0.0, min(100.0, (1 - (i2 - i1) / (t2 - t1)) * 100)) if t2 > t1 else 0.0
memt = _sum(s2.get("node_memory_MemTotal_bytes", []))
mema = _sum(s2.get("node_memory_MemAvailable_bytes", []))
mem = (1 - mema / memt) * 100 if memt else 0.0
def fs(s, key):
return _sum(s.get(key, []), lambda l: l.get("mountpoint") == "/")
dsz = fs(s2, "node_filesystem_size_bytes")
dav = fs(s2, "node_filesystem_avail_bytes")
disk = (1 - dav / dsz) * 100 if dsz else 0.0
def net(s, key):
return _sum(s.get(key, []),
lambda l: not re.match(r"lo|docker|veth|br-", l.get("device", "")))
rx = (net(s2, "node_network_receive_bytes_total") -
net(s1, "node_network_receive_bytes_total")) / 0.4 / 1024 / 1024
tx = (net(s2, "node_network_transmit_bytes_total") -
net(s1, "node_network_transmit_bytes_total")) / 0.4 / 1024 / 1024
temps = _atlas_temps(s2)
fans = _atlas_fans(s2)
cpu_t = next((t["celsius"] for t in temps if t["module"] == "CPU"), 0)
boot = _sum(s2.get("node_boot_time_seconds", []))
return {"cpu": round(cpu, 1), "mem": round(mem, 1), "disk": round(disk, 1),
"netIn": round(max(0, rx), 2), "netOut": round(max(0, tx), 2),
"tempCpu": cpu_t, "temps": temps, "fans": fans,
"uptimeSec": int(time.time() - boot) if boot else 0}
# ── Health ─────────────────────────────────────────────────────────
@app.get("/api/health")
async def health():
try:
r = await client.get(f"{PROM_URL}/-/healthy")
prom_ok = r.status_code == 200
except Exception:
prom_ok = False
return {"ok": True, "prometheus": prom_ok,
"time": datetime.now(timezone.utc).isoformat()}
# ── Nodes ──────────────────────────────────────────────────────────
async def _obs_node_live() -> dict[str, dict]:
"""obs 里 2 台 Spark 的实时指标(DCGM + node + hwmon)。"""
# 仅查有真实源的指标。SM activity/PCIe = DCGM PROF 类(按纪律未采,
# 避免生产推理 GPU 上的 profiling 开销)→ 不再产出,前端相应移除(不伪造)。
(gpu, fb_u, fb_f, gtemp, mtemp, pwr,
cpu, memr, dsk, nin, nout, ibr, ibt) = await asyncio.gather(
promql("DCGM_FI_DEV_GPU_UTIL"),
promql("DCGM_FI_DEV_FB_USED"),
promql("DCGM_FI_DEV_FB_FREE"),
promql("DCGM_FI_DEV_GPU_TEMP"),
promql("DCGM_FI_DEV_MEMORY_TEMP"),
promql("DCGM_FI_DEV_POWER_USAGE"),
promql('100 - (avg by (node) (rate(node_cpu_seconds_total{mode="idle"}[1m])) * 100)'),
promql('(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100'),
promql('(1 - node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}) * 100'),
promql('sum by (node) (rate(node_network_receive_bytes_total{device!~"lo|docker.*|veth.*|br-.*"}[1m])) / 1048576'),
promql('sum by (node) (rate(node_network_transmit_bytes_total{device!~"lo|docker.*|veth.*|br-.*"}[1m])) / 1048576'),
# 东西向 CX-7 RoCE/RDMA 真实吞吐 (×4 = port_data 单位 lanes→bytes 约定)
promql('sum by (node) (rate(node_infiniband_port_data_received_bytes_total[1m])) * 4 / 1048576'),
promql('sum by (node) (rate(node_infiniband_port_data_transmitted_bytes_total[1m])) * 4 / 1048576'),
)
g, fu, ff, gt, mt, pw = map(lambda r: _by(r, "node"),
[gpu, fb_u, fb_f, gtemp, mtemp, pwr])
cp, me, dk, ni, no, ir, it = map(lambda r: _by(r, "node"),
[cpu, memr, dsk, nin, nout, ibr, ibt])
# hwmon 明细温度(带人话 label)按 node 聚
htemp = await promql(
'node_hwmon_temp_celsius * on (chip,sensor,node) group_left(label) '
'node_hwmon_sensor_label')
per_node_temps: dict[str, list] = {}
for it in htemp:
m = it["metric"]
nd = m.get("node")
if not nd or it["value"] <= 0 or it["value"] > 150:
continue
human = m.get("label") or m.get("sensor", "")
per_node_temps.setdefault(nd, []).append({
"module": _classify_temp(m.get("chip", ""), human),
"label": human, "chip": m.get("chip", ""),
"celsius": round(it["value"], 1)})
# 节点键集 = 所有指标并集(GPU_UTIL 此 DCGM 配置可能整体为空,
# (and a host with no obs node job; relying only on g/cp would drop nodes with only temp/power)
universe = set()
for mp in (g, fu, ff, gt, mt, pw, cp, me, dk, ni, no):
universe |= set(mp)
universe |= set(per_node_temps)
out = {}
for obs_node in universe:
vt = (fu.get(obs_node, 0) + ff.get(obs_node, 0)) or 1
temps = sorted(per_node_temps.get(obs_node, []), key=lambda t: -t["celsius"])
cpu_t = next((t["celsius"] for t in temps if t["module"] == "CPU"), 0)
# Node kind drives VRAM% interpretation:
# discrete → DCGM FB_USED/FB_TOTAL (dedicated VRAM)
# unified-arm-soc / apple-silicon → node_exporter MemAvailable (shared)
# The choice is by `kind` field on each node (config-driven), not by
# any hard-coded host name.
is_unified = KIND_BY_OBS.get(obs_node, "discrete") != "discrete"
vram_pct = me.get(obs_node, 0) if is_unified else (fu.get(obs_node, 0) / vt * 100)
out[obs_node] = {
"gpu": round(g.get(obs_node, 0), 1),
"vram": round(vram_pct, 1),
"vramKind": "unified" if is_unified else "discrete",
"tempGpu": round(gt.get(obs_node, 0), 1),
"tempMem": round(mt.get(obs_node, 0), 1),
"tempCpu": cpu_t,
"power": round(pw.get(obs_node, 0), 1),
"cpu": round(cp.get(obs_node, 0), 1),
"mem": round(me.get(obs_node, 0), 1),
"disk": round(dk.get(obs_node, 0), 1),
"netIn": round(ni.get(obs_node, 0), 2),
"netOut": round(no.get(obs_node, 0), 2),
"rdmaIn": round(ir.get(obs_node, 0), 2),
"rdmaOut": round(it.get(obs_node, 0), 2),
"temps": temps,
}
return out
async def _node_payload() -> list[dict]:
obs_live, direct = await asyncio.gather(_obs_node_live(), _atlas_node_live())
# `direct` is the host that runs the Hearth api itself (scraped via the
# api container's own /proc + /sys, not via obs Prometheus). The legacy
# name "_atlas_node_live" is preserved for now to minimize diff.
# GPU metrics for that host still go via obs DCGM (if it has one) —
# pulled out by its discrete-node obs label, set from config.
discrete_gpu = obs_live.get(DISCRETE_OBS or "", {})
out = []
for n in NODES:
live = {"gpu": 0, "vram": 0, "vramKind": "discrete", "tempGpu": 0,
"tempMem": 0, "tempCpu": 0, "power": 0, "cpu": 0, "mem": 0,
"disk": 0, "netIn": 0, "netOut": 0, "rdmaIn": 0,
"rdmaOut": 0, "temps": [], "fans": []}
if n.get("node_source") == "direct":
live.update({k: discrete_gpu.get(k, 0)
for k in ("gpu", "vram", "tempGpu", "tempMem", "power")})
live["vramKind"] = discrete_gpu.get("vramKind", "discrete")
if direct:
live.update({k: direct[k] for k in ("cpu", "mem", "disk", "netIn",
"netOut", "tempCpu", "temps", "fans")
if k in direct})
up = bool(direct) or bool(discrete_gpu)
elif n["obs_node"] and n["obs_node"] in obs_live:
live.update(obs_live[n["obs_node"]])
up = True
else:
up = False # node not in obs Prometheus job → honestly mark no-data
out.append({**{k: v for k, v in n.items() if k != "node_source"},
"live": live, "up": up})
return out
@app.get("/api/nodes")
async def nodes_list():
return await _node_payload()
@app.get("/api/nodes/{node_id}")
async def node_detail(node_id: str):
if node_id not in NODE_BY_ID:
raise HTTPException(404, f"unknown node: {node_id}")
node = next(n for n in await _node_payload() if n["id"] == node_id)
obs = NODE_BY_ID[node_id]["obs_node"]
if obs:
lbl = f'node="{obs}"'
gh, ch, mh, th, ph = await asyncio.gather(
promql_range(f"DCGM_FI_DEV_GPU_UTIL{{{lbl}}}"),
promql_range(f'100 - (avg(rate(node_cpu_seconds_total{{{lbl},mode="idle"}}[1m]))*100)'),
promql_range(f'(1 - node_memory_MemAvailable_bytes{{{lbl}}}/node_memory_MemTotal_bytes{{{lbl}}})*100'),
promql_range(f"DCGM_FI_DEV_GPU_TEMP{{{lbl}}}"),
promql_range(f"DCGM_FI_DEV_POWER_USAGE{{{lbl}}}"),
)
f = lambda r: [v for _, v in r[0]["values"]] if r else []
node["history"] = {"gpu": f(gh), "cpu": f(ch), "mem": f(mh),
"tempGpu": f(th), "power": f(ph)}
else:
node["history"] = {}
node["hostedModels"] = [m for m in await models_list()
if node_id in (m.get("nodes") or [])]
return node
# ── Cluster (聚合在本服务算,不依赖 recording rules) ─────────────────
@app.get("/api/cluster")
async def cluster():
total_vram = sum(n["gpu"]["mem"] for n in NODES)
total_ram = sum(n["ram"] for n in NODES)
(tps, rps, g_avg, g_max, fb_u, fb_t, pw, gt,
cpu, memu, memt) = await asyncio.gather(
promql("sum(rate(litellm_total_tokens_metric[1m]))"),
promql("sum(rate(litellm_total_requests_metric[1m]))"),
promql("avg(DCGM_FI_DEV_GPU_UTIL)"),
promql("max(DCGM_FI_DEV_GPU_UTIL)"),
promql("sum(DCGM_FI_DEV_FB_USED)/1024"), # atlas 独显 FB (GiB)
promql("sum(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes)/1073741824"), # Σ Spark GB10 统一内存已用 (GiB)
promql("sum(DCGM_FI_DEV_POWER_USAGE)"),
promql("max(DCGM_FI_DEV_GPU_TEMP)"),
promql('avg(100 - (avg by (node)(rate(node_cpu_seconds_total{mode="idle"}[1m]))*100))'),
promql("sum(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes)/1073741824"),
promql("sum(node_memory_MemTotal_bytes)/1073741824"),
)
tps_h, pow_h, temp_h = await asyncio.gather(
promql_range("sum(rate(litellm_total_tokens_metric[1m]))"),
promql_range("sum(DCGM_FI_DEV_POWER_USAGE)"),
promql_range("max(DCGM_FI_DEV_GPU_TEMP)"),
)
ser = lambda r: [v for _, v in r[0]["values"]] if r else []
roll = await _litellm_rollup() # Hero 累计 + 延迟摘要(LiteLLM OSS Postgres, 非企业版)
return {
"totals": {"nodes": len(NODES), "gpus": len(NODES),
"totalVram": total_vram, "totalRam": total_ram,
"totalCores": sum(n["cpu"]["cores"] for n in NODES),
"totalThreads": sum(n["cpu"]["threads"] for n in NODES),
"totalDisk": sum(n["disk"] for n in NODES),
"totalFp16": sum(n["gpu"]["fp16"] for n in NODES),
"totalFp4": sum(n["gpu"]["fp4"] for n in NODES),
"pflopsFp4": round(sum(n["gpu"]["fp4"] for n in NODES) / 1000, 2)},
"live": {"tpsNow": _one(tps), "rpsNow": _one(rps), "kvNow": 0,
"powNow": round(_one(pw), 1), "tempNow": round(_one(gt), 1),
"gpuAvg": round(_one(g_avg), 1), "gpuMax": round(_one(g_max), 1),
"vramUsed": round(_one(fb_u) + _one(fb_t), 1), # 独显FB + Σ GB10统一内存
"vramTotal": total_vram, # 目录: 24 + 4×128 = 536
"cpuAvg": round(_one(cpu), 1),
"memUsed": round(_one(memu), 1),
"memTotal": round(_one(memt), 1) or total_ram,
"latP50": roll["latP50"], "latP95": roll["latP95"]},
"history": {"tps": ser(tps_h), "rps": [], "kv": [],
"pow": ser(pow_h), "temp": ser(temp_h)},
"uptimeSec": int(_one(await promql(
"max(time() - node_boot_time_seconds)")) or 0),
"reqTotal": roll["reqTotal"], # vLLM 原生累计(非 litellm 企业版门控)
"tokTotal": roll["tokTotal"],
# ── HA-derived (OPTIONAL; null fields when ha-exporter is absent) ─
# Wall power = real socket-side W from smart plugs; tokens·W⁻¹ joins
# LiteLLM throughput with that. Rack env = temperature/humidity/AC.
# Every field is null when the underlying ha_* series is stale or
# missing — frontend treats null as "—", not 0.
"power": await _ha_power(),
"env": await _ha_env(),
}
# ── Training observability (Phase 1: 纯 obs 只读底座, 对训练零影响) ──────
# 训练节点 = kind "unified-arm-soc"(GB10). 全部指标来自已在采的 DCGM(L1, ~0% GPU
# 开销)+ node-exporter, 经 obs-prometheus 联邦只读 PromQL —— 不碰 Spark 节点、不改
# DCGM、不开 profiling。覆盖体系 Layer C(GPU 健康)/D(RoCE 互联)/E(静默 stall 检测)。
# Layer A/B(loss/grad-norm/step/ETA/MFU)需训练框架信号源, 见 docs/training-observability.md Phase 2。
_GB10_OBS = [n["obs_node"] for n in NODES
if n.get("kind") == "unified-arm-soc" and n.get("obs_node")]
# ── Phase 2: 训练框架信号(loss/grad-norm/step/ETA/acceptance)──────────
# 源 = leader rank-0 写的 TensorBoard tfevents(SpecForge --report-to tensorboard)。
# Hearth(Atlas)经免密 SSH 只读 cat 最新 tfevents → 零依赖解析 → 派生。对训练零影响
# (只读一个每 ~50 步更新的小文件,不碰 GPU/作业)。文件不存在时优雅返回 present=False。
# tag 约定(见 docs/training-observability.md / 训练记录 §4.5):train/ploss_* · grad_norm
# · lr · acceptance_rate_* · acc_*;step = Event.step。下一轮训练起才有 tfevents。
TRAIN_TFEVENTS_HOST = os.environ.get("TRAIN_TFEVENTS_HOST", "tony@192.168.1.156")
TRAIN_TFEVENTS_GLOB = os.environ.get("TRAIN_TFEVENTS_GLOB",
"/home/tony/m3-train-out/runs/*.tfevents.*")
TRAIN_TOTAL_STEPS = int(os.environ.get("TRAIN_TOTAL_STEPS", "6240")) # 20ep×312(batch4)
_TRAIN_SIG = {"ts": 0.0, "data": {"present": False}}
_TRAIN_SIG_TTL = 20.0 # tfevents 每 ~50 步(分钟级)更新, 无需频繁拉
async def _fetch_tfevents() -> bytes:
"""免密 SSH 从 leader 节点 cat 最新 tfevents(二进制安全)。失败/无文件 → b''。"""
host = TRAIN_TFEVENTS_HOST
if host in ("", "local"): # 本地路径模式(文件已在 Atlas)
import glob
fs = sorted(glob.glob(TRAIN_TFEVENTS_GLOB), key=os.path.getmtime)
if not fs:
return b""
try:
return Path(fs[-1]).read_bytes()
except Exception:
return b""
remote = (f'f=$(ls -t {TRAIN_TFEVENTS_GLOB} 2>/dev/null | head -1); '
f'[ -n "$f" ] && cat "$f"')
try:
proc = await asyncio.create_subprocess_exec(
"ssh", "-o", "BatchMode=yes", "-o", "ConnectTimeout=4", host, remote,
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.DEVNULL)
out, _ = await asyncio.wait_for(proc.communicate(), timeout=8.0)
return out or b""
except Exception:
return b""
def _mean_latest(series: dict, prefix: str):
"""对一组 prefix* 的 tag, 取各自最后值的均值(忽略缺失)。返回 (mean|None, step|None)。"""
vals, step = [], None
for tag, pts in series.items():
if tag.startswith(prefix) and pts:
vals.append(pts[-1][2]); step = max(step or 0, pts[-1][0])
return (sum(vals) / len(vals) if vals else None), step
def _mean_curve(series: dict, prefix: str, cap: int = 80):
"""对 prefix* 各 tag 按 step 对齐求均值 → 降采样到 ≤cap 点的曲线。"""
by_step = {}
for tag, pts in series.items():
if not tag.startswith(prefix):
continue
for step, _w, val in pts:
by_step.setdefault(step, []).append(val)
if not by_step:
return []
steps = sorted(by_step)
curve = [sum(by_step[s]) / len(by_step[s]) for s in steps]
if len(curve) > cap: # 均匀降采样保留趋势
idx = [round(i * (len(curve) - 1) / (cap - 1)) for i in range(cap)]
curve = [curve[i] for i in idx]
return [round(v, 4) for v in curve]
async def _train_signal() -> dict:
now = time.time()
if now - _TRAIN_SIG["ts"] < _TRAIN_SIG_TTL:
return _TRAIN_SIG["data"]
_TRAIN_SIG["ts"] = now
raw = await _fetch_tfevents()
if not raw:
_TRAIN_SIG["data"] = {"present": False}
return _TRAIN_SIG["data"]
try:
s = tfevents.series(raw)
except Exception:
_TRAIN_SIG["data"] = {"present": False}
return _TRAIN_SIG["data"]
if not s:
_TRAIN_SIG["data"] = {"present": False}
return _TRAIN_SIG["data"]
loss, step_l = _mean_latest(s, "train/ploss")
acc, step_a = _mean_latest(s, "train/acceptance_rate")
gn = (s.get("train/grad_norm") or [(0, 0, None)])[-1][2]
lr = (s.get("train/lr") or [(0, 0, None)])[-1][2]
step = max([p[-1][0] for p in s.values() if p] or [0])
# per-step 时间 = 最近两个不同 step 的 Δwall/Δstep(取 grad_norm 或任一密集 tag)
ref = s.get("train/grad_norm") or s.get("train/ploss_0") or next(iter(s.values()), [])
per_step = None
if len(ref) >= 2 and ref[-1][0] > ref[-2][0]:
dw, ds = ref[-1][1] - ref[-2][1], ref[-1][0] - ref[-2][0]
if ds > 0 and dw > 0:
per_step = dw / ds
eta = ((TRAIN_TOTAL_STEPS - step) * per_step) if (per_step and step < TRAIN_TOTAL_STEPS) else None
last_wall = max([p[-1][1] for p in s.values() if p] or [0])
_TRAIN_SIG["data"] = {
"present": True,
"step": int(step), "totalSteps": TRAIN_TOTAL_STEPS,
"progress": round(step / TRAIN_TOTAL_STEPS, 4) if TRAIN_TOTAL_STEPS else 0,
"loss": round(loss, 4) if loss is not None else None,
"lossSeries": _mean_curve(s, "train/ploss"),
"gradNorm": round(gn, 3) if gn is not None else None,
"lr": lr,
"acceptanceRate": round(acc, 4) if acc is not None else None,
"accSeries": _mean_curve(s, "train/acceptance_rate"),
"perStepSec": round(per_step, 2) if per_step else None,
"etaSec": int(eta) if eta else None,
"staleSec": int(now - last_wall) if last_wall else None, # 距最近写入(卡死侦测)
"mfu": None, # 框架不计算; 需 GB10 bf16 峰值, 待填(见训练记录 §4.5)
}
return _TRAIN_SIG["data"]
async def _training_payload() -> dict:
if not _GB10_OBS:
return {"nodes": [], "summary": {}}
# obs_node 标签均为安全字符(spark-34d3 等);不用 re.escape——它会把 `-` 转成 `\-`,
# 而 Prometheus RE2 拒绝该转义 → 查询报错变空值(踩过)。
sel = "|".join(_GB10_OBS)
nf = f'{{node=~"{sel}"}}'
(util, power, gtemp, mtemp, fb_used, fb_free, xid, ecc_sbe, ecc_dbe,
mem_pct, roce_rx, roce_tx, ib_down) = await asyncio.gather(
promql(f"DCGM_FI_DEV_GPU_UTIL{nf}"),
promql(f"DCGM_FI_DEV_POWER_USAGE{nf}"),
promql(f"DCGM_FI_DEV_GPU_TEMP{nf}"),
promql(f"DCGM_FI_DEV_MEMORY_TEMP{nf}"),
promql(f"DCGM_FI_DEV_FB_USED{nf}"),
promql(f"DCGM_FI_DEV_FB_FREE{nf}"),
promql(f"DCGM_FI_DEV_XID_ERRORS{nf}"),
promql(f"DCGM_FI_DEV_ECC_SBE_VOL_TOTAL{nf}"),
promql(f"DCGM_FI_DEV_ECC_DBE_VOL_TOTAL{nf}"),
# GB10 统一内存占用%(OOM 关键 —— 训练贴内存上限跑, 实测 OOM 会硬重启节点)
promql(f'(1 - node_memory_MemAvailable_bytes{nf} / node_memory_MemTotal_bytes{nf}) * 100'),
# 东西向 CX-7 RoCE 真实吞吐(IB port_data 计数, ×4 lanes→bytes 约定 → MB/s)
promql(f"sum by (node) (rate(node_infiniband_port_data_received_bytes_total{nf}[1m])) * 4 / 1048576"),
promql(f"sum by (node) (rate(node_infiniband_port_data_transmitted_bytes_total{nf}[1m])) * 4 / 1048576"),
promql(f"sum by (node) (node_infiniband_link_downed_total{nf})"),
)
U, P, GT, MT, FU, FF, XID, ES, ED, MEM, RX, TX, IBD = (
_by(x, "node") for x in (util, power, gtemp, mtemp, fb_used, fb_free,
xid, ecc_sbe, ecc_dbe, mem_pct, roce_rx, roce_tx, ib_down))
nodes = []
for o in _GB10_OBS:
fbu, fbf = FU.get(o, 0), FF.get(o, 0)
fbt = fbu + fbf
nodes.append({
"id": OBS_TO_ID.get(o, o), "obs": o,
"util": round(U.get(o, 0), 1), "power": round(P.get(o, 0), 1),
"tempGpu": round(GT.get(o, 0), 1), "tempMem": round(MT.get(o, 0), 1),
"memPct": round(MEM.get(o, 0), 1), # 统一内存占用%(OOM)
"fbUsedPct": round(fbu / fbt * 100, 1) if fbt else 0, # GPU framebuffer 占用%
"xid": int(XID.get(o, 0)), "eccSbe": int(ES.get(o, 0)), "eccDbe": int(ED.get(o, 0)),
"roceRxMBps": round(RX.get(o, 0), 1), "roceTxMBps": round(TX.get(o, 0), 1),
"ibLinkDowned": int(IBD.get(o, 0)),
})
if not nodes:
return {"ts": time.time(), "nodes": [], "summary": {}}
utils = [n["util"] for n in nodes]
powers = [n["power"] for n in nodes]
med_p = sorted(powers)[len(powers) // 2]
# 静默 stall 启发(NCCL 卡死特征: GPU util 满载但功率塌、RoCE 归零, 仪表盘"全绿"却零进度):
# 高 util + 东西向带宽近零 + 功率显著低于活跃中位 → 嫌疑(保守阈值, 仅提示非硬告警;
# 精确判活性须 Layer A 步进, 见 Phase 2)。
for n in nodes:
n["stallSuspect"] = bool(
n["util"] >= 90 and (n["roceRxMBps"] + n["roceTxMBps"]) < 2
and med_p > 0 and n["power"] < med_p * 0.6)
summary = {
"nodes": len(nodes),
"active": sum(1 for u in utils if u >= 50), # util≥50 视为参与训练
"utilAvg": round(sum(utils) / len(utils), 1),
"utilSkew": round(max(utils) - min(utils), 1), # straggler 粗信号(底座近似)
"powerTotal": round(sum(powers), 1),
"roceRxMBps": round(sum(n["roceRxMBps"] for n in nodes), 1),
"roceTxMBps": round(sum(n["roceTxMBps"] for n in nodes), 1),
"anyXid": any(n["xid"] for n in nodes),
"anyEccDbe": any(n["eccDbe"] for n in nodes),
"stallSuspect": any(n["stallSuspect"] for n in nodes),
}
signal = await _train_signal() # Phase 2 训练框架信号(缺则 present=False)
return {"ts": time.time(), "nodes": nodes, "summary": summary, "signal": signal}
@app.get("/api/training")
async def training():
return await _training_payload()
# ── HA-derived cluster fields (gracefully null when HA exporter absent) ─
async def _ha_power() -> dict:
# Direct aggregation in PromQL — works without recording rules so the
# obs Prometheus needs no extra config beyond the scrape job.
# `byNode` is the per-device breakdown the Telemetry table needs; the
# cluster Σ is then derived from byNode in the frontend so the rollup
# and the table are guaranteed consistent (no drift between PromQL
# rounds).
# Per-node W is the live snapshot from Prometheus. The two energy windows
# are Hearth-side rolling integrals over the trusted W series — the cuco
# entity's claimed kWh field doesn't actually accumulate (verified empty
# 24h history), so we ignore it. avg_over_time(W) × hours / 1000 = kWh.
# 24h window = "last day"; 30d window = "last 30 days" — sliding, not
# calendar-aligned, but immune to HA-side counter resets / TZ confusion.
gpu, eff, per_node_w, per_node_24h, per_node_30d = await asyncio.gather(
promql("sum(DCGM_FI_DEV_POWER_USAGE)"),
promql("sum(rate(litellm_total_tokens_metric[1m])) "
"/ clamp_min(sum(ha_node_wall_power_watts), 1)"),
promql("ha_node_wall_power_watts"),
# sum_over_time × step / 3600 / 1000 — integrates only the seconds
# that actually have samples. avg_over_time × window-length would
# extrapolate a 1-hour average to a 30-day total whenever the series
# is newly added; this form honestly reports "kWh accumulated since
# we started polling" and converges to the true window total over
# time. step = 15s (the obs scrape interval).
promql("sum_over_time(ha_node_wall_power_watts[24h]) * 15 / 3600 / 1000"),
promql("sum_over_time(ha_node_wall_power_watts[30d]) * 15 / 3600 / 1000"),
)
by_node = {k: round(float(v), 1) for k, v in _by(per_node_w, "node").items()}
by_node_d = {k: round(float(v), 2) for k, v in _by(per_node_24h, "node").items()}
by_node_m = {k: round(float(v), 2) for k, v in _by(per_node_30d, "node").items()}
wall_total = round(sum(by_node.values()), 1) if by_node else None
kwh_d_tot = round(sum(by_node_d.values()), 2) if by_node_d else None
kwh_m_tot = round(sum(by_node_m.values()), 2) if by_node_m else None
# Empty PromQL result = metric absent (e.g. exporter not configured for
# this entity). _one() defaults to 0.0 which would lie ("0 W of GPU"),
# so guard explicitly and emit null.
def _f(r, digits=1):
return None if not r else round(r[0]["value"], digits)
return {"wallW": wall_total, "gpuW": _f(gpu), "tokensPerW": _f(eff, 2),
"kwh24h": kwh_d_tot, "kwh30d": kwh_m_tot,
"byNode": by_node, "byNode24h": by_node_d, "byNode30d": by_node_m}
async def _ha_env() -> dict:
# Same Hearth-side rolling-window kWh for the rack AC — direct from the
# ha_rack_ac_power_watts series rather than the unreliable cuco kWh field.
# cabinetHeatProxyC: each spark cuco plug has an internal temp sensor.
# At ~70 W load self-heating is ~5°C, so the mean across all spark plugs
# is a usable PROXY for cabinet ambient — beats no signal at all, and
# uncalibrated absolute (±5°C) but trends are trustworthy. Replaces the
# bedroom sensor we accidentally pointed at earlier.
t, h, ac_w, ac_24h, ac_30d, ac_s, plug_temps = await asyncio.gather(
promql("ha_rack_temperature_celsius"),
promql("ha_rack_humidity_percent"),
promql("ha_rack_ac_power_watts"),
promql("sum_over_time(ha_rack_ac_power_watts[24h]) * 15 / 3600 / 1000"),
promql("sum_over_time(ha_rack_ac_power_watts[30d]) * 15 / 3600 / 1000"),
promql("ha_rack_ac_state"),
promql("ha_node_plug_temp_celsius"),
)
by_node_plug = {k: round(float(v), 1)
for k, v in _by(plug_temps, "node").items()}
proxy = round(sum(by_node_plug.values()) / len(by_node_plug), 1) \
if by_node_plug else None
# Empty PromQL result = no such metric (entity not configured). Emit null
# rather than the 0.0 default, so the frontend can render "—" instead of
# claiming the rack is at 0°C / off when the sensor simply doesn't exist.
def _f(r, digits=1):
return None if not r else round(r[0]["value"], digits)
return {"rackTempC": _f(t), "rackRH": _f(h, 0),
"acW": _f(ac_w), "acKwh24h": _f(ac_24h, 2), "acKwh30d": _f(ac_30d, 2),
"acOn": None if not ac_s else bool(ac_s[0]["value"]),
"byNodePlugTempC": by_node_plug, "cabinetHeatProxyC": proxy}
# ── Models (真实:直采 vLLM 原生 /metrics;LiteLLM prometheus 企业版门控不可用) ──
# 真实模型来自 litellm /v1/models(comfyui mode 当前态)。仅 vLLM 后端暴露
# 当前真实部署(2026-05-19 核实,部署随用户调整会变;目录须随真相更新):
# - qwen3-coder(运行中,主模型)= Qwen3-Coder-Next-FP8,vLLM @ .188:8888
# + .189:8888(spark-03/04),网关名 qwen3-coder-next,直采 .188:8888/metrics。
# - deepseek-v4-flash 已停(网关推理 500,路由 .156:8000 无指标)→ metrics_url
# 指其真实后端,停着就诚实显 no-metrics/离线,重启自动恢复;不再盗用别人端点。
# - gemma / qwen3-vl 仍在网关但走 .156:8001/8002(无 vLLM /metrics)→
# metrics_url=None 诚实标"无实时指标源",不伪造数字。
# - qwen3.5-122b-abliterated 已从网关移除 → 不再列(幽灵条目=信息不对)。
# ── 模型自动发现(LiteLLM 网关驱动,不再手工维护目录)──────────────
# 真相源 = 网关 /model/info(route→backend) + /health(backend up/down)。
# 部署随用户调整,监控自动反映、无需改代码、不会漏显也不会显错。
# MODEL_META 只做"好看"的静态修饰(显示名/厂商/标签);未知模型自动从
# id 推导,绝不因此漏显或标错状态。
_ALIAS_ROUTES = {"default", "code", "agent", "long", "vision", "fast",
"reason", "reasoning", "embed", "embedding", "rerank", "vl"}
MODEL_META = {
"qwen3-coder-next": {"display": "Qwen3-Coder-Next", "vendor": "Alibaba",
"kind": "chat", "tags": ["coding"]},
"deepseek-v4-flash": {"display": "DeepSeek-V4-Flash", "vendor": "DeepSeek",
"kind": "chat", "tags": ["reasoning"]},
"deepseek-v4-flash-pp4": {"display": "DeepSeek-V4-Flash · PP4",
"vendor": "DeepSeek", "kind": "chat", "tags": ["reasoning", "test"]},
"minimax-m2.7": {"display": "MiniMax-M2.7", "vendor": "MiniMax",
"kind": "chat", "tags": ["reasoning"]},
"gemma-4-31b-abliterated": {"display": "Gemma-4-31B-abliterated",
"vendor": "Google", "kind": "vision", "tags": ["vision", "abliterated"]},
"qwen3-vl-abliterated": {"display": "Qwen3-VL-8B-abliterated",
"vendor": "Alibaba", "kind": "vision", "tags": ["vision", "abliterated"]},
}
def _host_of(api_base: str) -> str:
m = re.search(r"//([^:/]+)", api_base or "")
return m.group(1) if m else ""
def _meta_for(route: str) -> dict:
if route in MODEL_META:
return dict(MODEL_META[route])
low = route.lower()
vendor = ("Alibaba" if "qwen" in low else "DeepSeek" if "deepseek" in low
else "MiniMax" if "minimax" in low else "Google" if "gemma" in low
else "Meta" if "llama" in low else "—")
return {"display": route.replace("_", " ").replace("-", " ").title(),
"vendor": vendor,
"kind": "vision" if ("vl" in low or "vision" in low) else "chat",
"tags": []}
async def _gw_get(path: str, timeout: float):
try:
r = await client.get(f"{LITELLM_URL}{path}",
headers={"Authorization": f"Bearer {LITELLM_KEY}"},
timeout=timeout)
r.raise_for_status()
return r.json()
except Exception:
return None
async def _ctx_of(base: str) -> int:
try:
r = await client.get(f"{base}/v1/models", timeout=3.0)
r.raise_for_status()
return int(r.json()["data"][0].get("max_model_len") or 0)
except Exception:
return 0
async def _served_name(base: str) -> str:
"""后端(vLLM/OpenAI 兼容)自报的 served-model-name = /v1/models .data[0].id。
这是该 endpoint 真正加载的权重身份,优先于网关上可能过时的路由别名——
运维换载模型时常只新增网关路由、忘删旧路由,导致一个 base 挂多名,
若按字母序挑主名会显错(实测 .156:8000 已换 minimax-m3,旧 deepseek-v4-flash
路由仍在 → 字母序误选 deepseek)。"""
try:
r = await client.get(f"{base}/v1/models", timeout=3.0)
r.raise_for_status()
return str(r.json()["data"][0].get("id") or "")
except Exception:
return ""
_DISCO = {"ts": 0.0, "models": None}
_DISCO_TTL = 25.0 # 部署拓扑变化慢;富指标(tps/kv)仍每 snapshot 直采
_DISCO_TASK = None
async def _discover() -> list[dict]:
"""网关 /model/info + /health → 逻辑模型列表(按主 route 折叠别名/副本)。
每条: id/route/display/vendor/kind/tags/nodes/up/vllm_bases/ctx/framework。"""
info = await _gw_get("/model/info", 8.0) or {}
# /health 仅作辅助/兜底信号;超时收紧,避免网关 /health 偶发卡顿拖慢
# 整个发现周期(直接探测各后端才是主判定路径)
health = await _gw_get("/health", 8.0) or {}
up_map: dict[str, bool] = {}
for key, flag in (("healthy_endpoints", True), ("unhealthy_endpoints", False)):
for e in (health.get(key) or []):
ab = (e.get("api_base") or "").rstrip("/")
up_map[ab[:-3] if ab.endswith("/v1") else ab] = flag
route_bases: dict[str, set] = {}
for m in (info.get("data") or []):
rt = m.get("model_name") or ""
ab = ((m.get("litellm_params") or {}).get("api_base") or "").rstrip("/")
if not rt or not ab:
continue
route_bases.setdefault(rt, set()).add(ab[:-3] if ab.endswith("/v1") else ab)
base_routes: dict[str, set] = {}
for rt, bs in route_bases.items():
for b in bs:
base_routes.setdefault(b, set()).add(rt)
# 后端自报的 served-model-name 是"实际加载了什么"的真相,优先于网关别名
served = {b: await _served_name(b) for b in base_routes}
def _primary(routes: set, sv: str) -> str:
# 后端实际加载的模型名优先(抗网关路由漂移)。后端 served-name 常带量化
# 后缀(minimax-m3-awq)而网关路由不带(minimax-m3),故精确匹配之外再做
# 边界前缀容错:取与 served-name 在 `-` 段边界上互为前缀、且最长的路由。
if sv:
nsv = sv.lower().replace("_", "-")
best = None
for r in routes:
nr = r.lower().replace("_", "-")
if nsv == nr or nsv.startswith(nr + "-") or nr.startswith(nsv + "-"):
if best is None or len(nr) > len(best.lower()):
best = r
if best:
return best
non_alias = sorted(r for r in routes if r not in _ALIAS_ROUTES)
return non_alias[0] if non_alias else sorted(routes)[0]
models: dict[str, dict] = {}
for b, routes in base_routes.items():
prt = _primary(routes, served.get(b, ""))
meta = _meta_for(prt)
mm = models.setdefault(prt, {
"id": prt, "route": f"litellm/{prt}", "display": meta["display"],
"vendor": meta["vendor"], "kind": meta["kind"],
"tags": list(meta["tags"]), "params": "—", "quant": "—",
"framework": "—", "vram": 0, "ctx": 0,
"_nodes": set(), "_aliases": set(), "_bases": [],
"up": False, "vllm_bases": [], "llamacpp_bases": [], "sglang_bases": []})
mm["_bases"].append(b)
host = _host_of(b)
if host in IP_TO_ID:
mm["_nodes"].add(IP_TO_ID[host])
for r in routes:
if r != prt:
mm["_aliases"].add(r)
if up_map.get(b):
mm["up"] = True
# up 判定:直接探后端为主(/metrics 或 /v1/models 可达即活),/health 仅做
# 辅助 / 兜底——避免单点故障(网关 /health 偶发超时 22s)把所有模型误标
# stopped。直接探测自给自足,网关挂了监控仍如实反映后端真相。
for mm in models.values():
for b in mm["_bases"]:
sc = await _scrape_vllm(b)
if any(str(k).startswith("vllm:") for k in sc) or sc.get("__e2e_buckets"):
mm["vllm_bases"].append(b); mm["up"] = True
continue
sc2 = await _scrape_llamacpp(b)
if sc2: # 有 llamacpp:* 行
mm["llamacpp_bases"].append(b); mm["up"] = True
continue
sc3 = await _scrape_sglang(b)
if any(str(k).startswith("sglang:") for k in sc3) or sc3.get("__e2e_buckets"):
mm["sglang_bases"].append(b); mm["up"] = True
continue
try: # 无指标但 /v1/models 通 → 在线
r = await client.get(f"{b}/v1/models", timeout=3.0)
if r.status_code == 200:
mm["up"] = True
except Exception: