XCoreGrid + XTachometer + XHistoryGraph + XLoadBadge + XUsageBar in one 1024x768 window, live /proc/stat + /proc/loadavg sampled every 500 ms via refresh_cores()/refresh_load(). Pushing live samples starts fresh series, so the seeded demo data is replaced on the first live sample.
139 lines
3.9 KiB
Python
139 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
"""CPU Dashboard - monitor (Python).
|
|
|
|
A btop-style CPU dashboard in one 1024x768 window, built only from
|
|
LightGUI XFrame monitor widgets (XCoreGrid, XTachometer,
|
|
XHistoryGraph, XLoadBadge, XUsageBar). Live data from /proc/stat
|
|
(per-core busy %, via XCoreGrid.refresh_cores()) and /proc/loadavg
|
|
(via XLoadBadge.refresh_load()), sampled every 500 ms. The
|
|
deterministic demo data each widget draws with no data pushed is
|
|
replaced on the first live sample: push() / push_core() start fresh
|
|
series, and the histories are primed with the first live value.
|
|
|
|
Run with:
|
|
PYTHONPATH=/root/monitor-wt/cpu-dashboard/lightgui_python python3 \\
|
|
examples/monitor_cpu_dashboard/main.py
|
|
(or from the repo root with PYTHONPATH=. )
|
|
"""
|
|
|
|
import gi
|
|
|
|
gi.require_version("Gtk", "3.0")
|
|
gi.require_version("Gdk", "3.0")
|
|
|
|
from gi.repository import Gdk, GLib, Gtk # noqa: E402
|
|
from lightgui.xframe.core import create, parse_color # noqa: E402
|
|
|
|
_TITLE = "CPU Dashboard - monitor"
|
|
_DESIGN_SIZE = (1024, 768)
|
|
_BACKGROUND = ""
|
|
_WIDGETS = []
|
|
_AREA = []
|
|
_PRIMED = [False]
|
|
|
|
|
|
def _build_widgets():
|
|
"""Create every placed widget with its props."""
|
|
widgets = []
|
|
|
|
badge = create("XLoadBadge", "LoadBadge_001", 16, 16, 320, 84)
|
|
badge.set_prop("label", "Load avg")
|
|
badge.refresh_load()
|
|
widgets.append(badge)
|
|
|
|
bar = create("XUsageBar", "UsageBar_001", 352, 16, 656, 84)
|
|
bar.set_prop("label", "Total CPU")
|
|
bar.set_prop("minimum", "0")
|
|
bar.set_prop("maximum", "100")
|
|
widgets.append(bar)
|
|
|
|
grid = create("XCoreGrid", "CoreGrid_001", 16, 116, 640, 300)
|
|
grid.set_prop("label", "CPU cores")
|
|
grid.set_prop("cores", "8")
|
|
widgets.append(grid)
|
|
|
|
tacho = create("XTachometer", "Tachometer_001", 672, 116, 336, 300)
|
|
tacho.set_prop("label", "CPU %")
|
|
tacho.set_prop("minimum", "0")
|
|
tacho.set_prop("maximum", "100")
|
|
tacho.set_prop("redline", "80")
|
|
tacho.set_prop("unit", "%")
|
|
widgets.append(tacho)
|
|
|
|
hist = create("XHistoryGraph", "HistoryGraph_001", 16, 432, 992, 320)
|
|
hist.set_prop("label", "Total CPU history")
|
|
hist.set_prop("window", "60")
|
|
hist.set_prop("minimum", "0")
|
|
hist.set_prop("maximum", "100")
|
|
hist.set_prop("series-names", "CPU")
|
|
widgets.append(hist)
|
|
|
|
return widgets
|
|
|
|
|
|
def _tick():
|
|
"""500 ms tick: feed live samples, then redraw."""
|
|
badge, bar, grid, tacho, hist = _WIDGETS
|
|
values = grid.refresh_cores()
|
|
if values:
|
|
total = sum(values) / len(values)
|
|
if not _PRIMED[0]:
|
|
# First live sample: prime the histories with it so no
|
|
# seeded demo data is visible (60 = window capacity).
|
|
for core, value in enumerate(values):
|
|
for _ in range(59):
|
|
grid.push_core(core, value)
|
|
for _ in range(60):
|
|
hist.push(total)
|
|
_PRIMED[0] = True
|
|
else:
|
|
hist.push(total)
|
|
tacho.set_value(total)
|
|
bar.set_value(total)
|
|
badge.refresh_load()
|
|
if _AREA:
|
|
_AREA[0].queue_draw()
|
|
return True
|
|
|
|
|
|
def _on_draw(area, cr):
|
|
bg = parse_color(_BACKGROUND, None)
|
|
if bg is None:
|
|
bg = (1.0, 1.0, 1.0)
|
|
cr.set_source_rgb(*bg)
|
|
cr.paint()
|
|
for widget in _WIDGETS:
|
|
widget.draw(cr)
|
|
return False
|
|
|
|
|
|
def _on_key_press(window, event):
|
|
if event.keyval in (Gdk.KEY_Escape, Gdk.KEY_q):
|
|
Gtk.main_quit()
|
|
return True
|
|
return False
|
|
|
|
|
|
def main():
|
|
window = Gtk.Window(title=_TITLE)
|
|
window.set_default_size(*_DESIGN_SIZE)
|
|
window.set_resizable(False)
|
|
window.connect("destroy", Gtk.main_quit)
|
|
window.connect("key-press-event", _on_key_press)
|
|
area = Gtk.DrawingArea()
|
|
area.set_hexpand(True)
|
|
area.set_vexpand(True)
|
|
area.connect("draw", _on_draw)
|
|
window.add(area)
|
|
_WIDGETS.extend(_build_widgets())
|
|
_AREA.append(area)
|
|
GLib.timeout_add(500, _tick)
|
|
window.show_all()
|
|
Gtk.main()
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
sys.exit(main())
|