Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b197a366ec |
No files matched your search
@@ -1,30 +0,0 @@
|
||||
# CPU Dashboard — monitor (Python)
|
||||
|
||||
A btop-style CPU dashboard in one 1024x768 window, built only from
|
||||
LightGUI XFrame monitor widgets:
|
||||
|
||||
- **XCoreGrid** — per-core mini history graphs
|
||||
- **XTachometer** — big total-CPU % gauge (redline 80%)
|
||||
- **XHistoryGraph** — total CPU % over the last 60 s
|
||||
- **XLoadBadge** — load average 1/5/15 min + uptime
|
||||
- **XUsageBar** — total CPU % bar
|
||||
|
||||
Live data comes from `/proc/stat` (per-core busy %, via
|
||||
`XCoreGrid.refresh_cores()`) and `/proc/loadavg` (via
|
||||
`XLoadBadge.refresh_load()`), sampled every 500 ms. Pushing live
|
||||
samples starts fresh series, so the deterministic demo data is
|
||||
replaced on the first live sample.
|
||||
|
||||
## Run
|
||||
|
||||
```sh
|
||||
PYTHONPATH=. python3 examples/monitor_cpu_dashboard/main.py
|
||||
```
|
||||
|
||||
Esc, `q`, or closing the window quits.
|
||||
|
||||
## Source
|
||||
|
||||
- App: `examples/monitor_cpu_dashboard/main.py`
|
||||
- Widgets: `lightgui/xframe/monitor.py`
|
||||
- Repo: https://github.com/cnmcdee/lightgui_python
|
||||
@@ -1,138 +0,0 @@
|
||||
#!/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())
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 48 KiB |
@@ -0,0 +1,47 @@
|
||||
# Disk Monitor — Python (XFrame)
|
||||
|
||||
A live disk monitor in one 1024x768 window: per-mount usage bars,
|
||||
a Used / Free stack bar for the largest mount, and a read/write
|
||||
throughput history graph. All data is live — `/proc/mounts` +
|
||||
`os.statvfs` for usage, `/proc/diskstats` (sectors differenced once
|
||||
a second) for MB/s. Refreshes every second via `GLib.timeout_add`;
|
||||
close the window to quit.
|
||||
|
||||
## Widgets used
|
||||
|
||||
| Widget | Role |
|
||||
| --- | --- |
|
||||
| XUsageBar | One per disk mount: usage % with total size |
|
||||
| XStackBar | Largest mount split into Used / Free (GiB) |
|
||||
| XHistoryGraph | Read MB/s + Write MB/s history, 60 s window |
|
||||
|
||||
Seeded demo series/segments are replaced by live data on the first
|
||||
tick (`push()` / `set_segments()` start fresh own data, never the
|
||||
demo). The harness background is the widgets' near-black panel color
|
||||
— XStackBar paints no panel of its own, so a white background would
|
||||
wash its light-grey legend out. Note the Python XStackBar draws no
|
||||
title (its draw ignores the `label` prop); the C/Rust ports show
|
||||
"/ disk usage" there.
|
||||
|
||||
**Why no XSysBox:** it has no clear-history API and its
|
||||
constructor seeds 60 demo samples, so it cannot show live disk data
|
||||
without the seeded demo leaking into the history.
|
||||
|
||||
## Build & run
|
||||
|
||||
```sh
|
||||
PYTHONPATH=/root/monitor-wt/disk-monitor/lightgui_python python3 examples/monitor_disk_monitor/main.py
|
||||
```
|
||||
|
||||
## Screenshot
|
||||
|
||||

|
||||
|
||||
## Source
|
||||
|
||||
- App: `examples/monitor_disk_monitor/main.py`
|
||||
- Repo: https://github.com/cnmcdee/lightgui_python
|
||||
- C port: `examples/monitor/disk-monitor/main.cpp`
|
||||
in https://github.com/cnmcdee/lightgui
|
||||
- Rust port: `crates/xframe-demo/examples/monitor_disk_monitor/main.rs`
|
||||
in https://github.com/cnmcdee/lightgui_rust
|
||||
@@ -0,0 +1,197 @@
|
||||
#!/usr/bin/env python3
|
||||
# Disk Monitor - monitor (Python/XFrame).
|
||||
#
|
||||
# One 1024x768 window: a live XUsageBar per disk mount (/proc/mounts
|
||||
# + os.statvfs), an XStackBar splitting the largest mount into
|
||||
# Used / Free (GiB), and an XHistoryGraph of disk read/write
|
||||
# throughput in MB/s (/proc/diskstats, sectors differenced once a
|
||||
# second). Refreshes every second; close the window to quit.
|
||||
# Seeded demo series/segments are replaced by live data on the first
|
||||
# tick (push()/set_segments() start fresh own data, never the demo).
|
||||
#
|
||||
# XSysBox is deliberately NOT used: it has no clear-history API and
|
||||
# its constructor seeds 60 demo samples, so it cannot show live disk
|
||||
# data without the seeded demo leaking into the history.
|
||||
#
|
||||
# Run with:
|
||||
# PYTHONPATH=/root/monitor-wt/disk-monitor/lightgui_python \
|
||||
# python3 examples/monitor_disk_monitor/main.py
|
||||
# (or from the repo root with PYTHONPATH=.)
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
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 # noqa: E402
|
||||
|
||||
WIN_W, WIN_H = 1024, 768
|
||||
MARGIN = 16
|
||||
BAR_H, BAR_GAP = 44, 8
|
||||
STACK_H = 170
|
||||
REAL_FS = ("ext4", "xfs", "btrfs")
|
||||
GIB = 1024.0 ** 3
|
||||
|
||||
_WIDGETS = []
|
||||
_AREA = []
|
||||
_MOUNTS = [] # dicts: dev/point/base/total/used/free/pct
|
||||
_PREV_IO = [None] # (read_sectors, write_sectors)
|
||||
|
||||
|
||||
def _base_device(dev):
|
||||
"""Strip a partition suffix: sda2 -> sda, nvme0n1p2 -> nvme0n1."""
|
||||
name = dev.rsplit("/", 1)[-1]
|
||||
while name and name[-1].isdigit():
|
||||
name = name[:-1]
|
||||
if len(name) >= 2 and name[-1] == "p" and name[-2].isdigit():
|
||||
name = name[:-1]
|
||||
return name
|
||||
|
||||
|
||||
def _read_mounts():
|
||||
mounts = []
|
||||
try:
|
||||
with open("/proc/mounts") as f:
|
||||
lines = f.read().splitlines()
|
||||
except OSError:
|
||||
return mounts
|
||||
for line in lines:
|
||||
parts = line.split()
|
||||
if len(parts) < 3 or parts[2] not in REAL_FS:
|
||||
continue
|
||||
point = parts[1]
|
||||
if any(m["point"] == point for m in mounts):
|
||||
continue
|
||||
try:
|
||||
sv = os.statvfs(point)
|
||||
except OSError:
|
||||
continue
|
||||
total = sv.f_blocks * sv.f_frsize
|
||||
free = sv.f_bfree * sv.f_frsize
|
||||
used = total - free
|
||||
mounts.append({
|
||||
"dev": parts[0], "point": point,
|
||||
"base": _base_device(parts[0]),
|
||||
"total": total / GIB, "used": used / GIB,
|
||||
"free": free / GIB,
|
||||
"pct": used / total * 100.0 if total else 0.0,
|
||||
})
|
||||
if len(mounts) >= 4:
|
||||
break
|
||||
return mounts
|
||||
|
||||
|
||||
def _read_io():
|
||||
"""(read_sectors, write_sectors) summed over the backing disks."""
|
||||
bases = {m["base"] for m in _MOUNTS}
|
||||
rd = wr = 0
|
||||
ok = False
|
||||
try:
|
||||
with open("/proc/diskstats") as f:
|
||||
lines = f.read().splitlines()
|
||||
except OSError:
|
||||
return None
|
||||
for line in lines:
|
||||
parts = line.split()
|
||||
if len(parts) < 10 or parts[2] not in bases:
|
||||
continue
|
||||
rd += int(parts[5])
|
||||
wr += int(parts[9])
|
||||
ok = True
|
||||
return (rd, wr) if ok else None
|
||||
|
||||
|
||||
def _build_widgets():
|
||||
widgets = []
|
||||
n = len(_MOUNTS)
|
||||
stack_y = MARGIN + n * (BAR_H + BAR_GAP) + 8
|
||||
hist_y = stack_y + STACK_H + 8
|
||||
hist_h = WIN_H - MARGIN - hist_y
|
||||
col_w = WIN_W - 2 * MARGIN
|
||||
for i, m in enumerate(_MOUNTS):
|
||||
w = create("XUsageBar", "XUsageBar_%03d" % (i + 1),
|
||||
MARGIN, MARGIN + i * (BAR_H + BAR_GAP), col_w, BAR_H)
|
||||
w.set_prop("label", "%s %.0f GiB" % (m["point"], m["total"]))
|
||||
w.set_value(m["pct"])
|
||||
widgets.append(w)
|
||||
stack = create("XStackBar", "XStackBar_001",
|
||||
MARGIN, stack_y, col_w, STACK_H)
|
||||
stack.set_prop("label", "%s disk usage" % _MOUNTS[0]["point"])
|
||||
stack.set_segments([("Used", _MOUNTS[0]["used"], (0.0, 0.59, 0.24)),
|
||||
("Free", _MOUNTS[0]["free"], (0.78, 0.78, 0.78))])
|
||||
widgets.append(stack)
|
||||
hist = create("XHistoryGraph", "XHistoryGraph_001",
|
||||
MARGIN, hist_y, col_w, hist_h)
|
||||
hist.set_prop("label", "Disk IO - MB/s (read / write)")
|
||||
hist.set_prop("series-count", "2")
|
||||
hist.set_prop("series-names", "Read MB/s|Write MB/s")
|
||||
widgets.append(hist)
|
||||
return widgets
|
||||
|
||||
|
||||
def _tick():
|
||||
live = _read_mounts()
|
||||
for i, m in enumerate(_MOUNTS):
|
||||
if i >= len(live):
|
||||
break
|
||||
m.update(live[i])
|
||||
_WIDGETS[i].set_value(m["pct"])
|
||||
stack = _WIDGETS[len(_MOUNTS)]
|
||||
stack.set_segments([("Used", _MOUNTS[0]["used"], (0.0, 0.59, 0.24)),
|
||||
("Free", _MOUNTS[0]["free"], (0.78, 0.78, 0.78))])
|
||||
hist = _WIDGETS[len(_MOUNTS) + 1]
|
||||
io = _read_io()
|
||||
# 1 s tick, 512-byte sectors -> MB/s.
|
||||
if io is not None and _PREV_IO[0] is not None:
|
||||
hist.push((io[0] - _PREV_IO[0][0]) * 512.0 / 1e6, series=0)
|
||||
hist.push((io[1] - _PREV_IO[0][1]) * 512.0 / 1e6, series=1)
|
||||
_PREV_IO[0] = io
|
||||
if _AREA:
|
||||
_AREA[0].queue_draw()
|
||||
return True
|
||||
|
||||
|
||||
def _on_draw(area, cr):
|
||||
# Dark btop-style harness background (0.07, 0.07, 0.09): the
|
||||
# monitor widgets draw light-grey text on near-black panels, and
|
||||
# XStackBar paints no panel of its own, so a white background
|
||||
# would wash its legend out.
|
||||
cr.set_source_rgb(0.07, 0.07, 0.09)
|
||||
cr.paint()
|
||||
for widget in _WIDGETS:
|
||||
widget.draw(cr)
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
_MOUNTS.extend(_read_mounts())
|
||||
if not _MOUNTS:
|
||||
_MOUNTS.append({"dev": "?", "point": "/", "base": "",
|
||||
"total": 0.0, "used": 0.0, "free": 0.0,
|
||||
"pct": 0.0})
|
||||
_PREV_IO[0] = _read_io()
|
||||
window = Gtk.Window(title="Disk Monitor - monitor")
|
||||
window.set_default_size(WIN_W, WIN_H)
|
||||
window.set_resizable(False)
|
||||
window.connect("destroy", Gtk.main_quit)
|
||||
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
|
||||
window.add(vbox)
|
||||
area = Gtk.DrawingArea()
|
||||
area.set_hexpand(True)
|
||||
area.set_vexpand(True)
|
||||
area.connect("draw", _on_draw)
|
||||
vbox.pack_start(area, True, True, 0)
|
||||
_WIDGETS.extend(_build_widgets())
|
||||
_AREA.append(area)
|
||||
GLib.timeout_add(1000, _tick)
|
||||
window.show_all()
|
||||
Gtk.main()
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
@@ -1193,7 +1193,6 @@ class MonitorWidget(Widget):
|
||||
ly = cy + (r - 26) * math.sin(ang) + 3
|
||||
_wtext(cr, self, label, lx, ly, size=9, rgb=_TEXT,
|
||||
align="center")
|
||||
cr.restore()
|
||||
# Peak-hold tick.
|
||||
cr.save()
|
||||
_rgb(cr, _WHITE)
|
||||
|
||||
Reference in new issue
Block a user