Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11f7dd70e6 | ||
|
|
b4add9ba7b |
No files matched your search
@@ -0,0 +1,30 @@
|
||||
# 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
|
||||
@@ -0,0 +1,138 @@
|
||||
#!/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.
|
After Width: | Height: | Size: 48 KiB |
@@ -1,50 +0,0 @@
|
||||
# Network Monitor — monitor (Python)
|
||||
|
||||
A live network monitor in one 1024x768 window, built only from the
|
||||
LightGUI XFrame monitor widgets (Stage 2 of the btop-widget project).
|
||||
|
||||
## What it shows
|
||||
|
||||
Live traffic for the main interface (`enp3s0` on hot_dallas, otherwise
|
||||
the first non-loopback interface with traffic), read from
|
||||
`/proc/net/dev` and differenced once a second:
|
||||
|
||||
- **XNetGraph** (top) — up/down graph in bytes/s, current rates plus
|
||||
session totals in the legend.
|
||||
- **XSysBox** (middle, x2) — Download / Upload big-number boxes in
|
||||
KiB/s, each with its own history sparkline and usage bar. The range
|
||||
tracks the session peak (x1.15, floor 100 KiB/s).
|
||||
- **XHistoryGraph** — down/up rate history in KiB/s, 60 s window,
|
||||
current / min / max / average per series.
|
||||
- **XUsageBar** (bottom) — total throughput as a percentage of the
|
||||
link speed (from `/sys/class/net/<iface>/speed`, 1 Gb/s fallback).
|
||||
|
||||
## Live data, not demo data
|
||||
|
||||
The widgets draw deterministic demo data until live samples are
|
||||
pushed; `push()` / `push_net()` start a fresh live series (the demo
|
||||
is never extended), so everything on screen after the first tick is
|
||||
measured traffic. The 1 s tick matches `push_net()`, which
|
||||
accumulates one second of totals per push.
|
||||
|
||||
## Run
|
||||
|
||||
```sh
|
||||
PYTHONPATH=. python3 examples/monitor_network_monitor/main.py
|
||||
# quit by closing the window
|
||||
```
|
||||
|
||||
## Tests
|
||||
|
||||
```sh
|
||||
DISPLAY=:99 python3 -m unittest discover -s tests
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
- C: `examples/monitor/network-monitor/main.cpp` in
|
||||
[lightgui](https://github.com/cnmcdee/lightgui)
|
||||
- Rust: `crates/xframe-demo/examples/monitor_network_monitor/main.rs` in
|
||||
[lightgui_rust](https://github.com/cnmcdee/lightgui_rust)
|
||||
- Python: `examples/monitor_network_monitor/main.py` in
|
||||
[lightgui_python](https://github.com/cnmcdee/lightgui_python)
|
||||
@@ -1,223 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Network Monitor - monitor (Stage 2, btop-widget project).
|
||||
|
||||
One 1024x768 window showing LIVE traffic for the main interface
|
||||
(enp3s0 on hot_dallas, else the first non-loopback interface with
|
||||
traffic), read from /proc/net/dev and differenced once a second:
|
||||
|
||||
XNetGraph up/down graph, current rates + session totals
|
||||
XSysBox x2 Download / Upload big-number boxes (KiB/s + history)
|
||||
XHistoryGraph rate history, down/up in KiB/s (60 s window)
|
||||
XUsageBar total throughput as % of the link speed
|
||||
|
||||
The widgets draw deterministic demo data until live samples are
|
||||
pushed; push()/push_net() start a fresh live series (the demo is
|
||||
never extended), so everything on screen after the first tick is
|
||||
measured traffic. The 1 s tick matches push_net(), which accumulates
|
||||
one second of totals per push.
|
||||
|
||||
Run with:
|
||||
PYTHONPATH=/root/monitor-wt/network-monitor/lightgui_python \\
|
||||
python3 examples/monitor_network_monitor/main.py
|
||||
(or from the repo root with PYTHONPATH=.)
|
||||
|
||||
Quit: close the window.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
import gi
|
||||
|
||||
gi.require_version("Gtk", "3.0")
|
||||
gi.require_version("Gdk", "3.0")
|
||||
|
||||
from gi.repository import GLib, Gtk # noqa: E402
|
||||
from lightgui.xframe.core import create, parse_color # noqa: E402
|
||||
|
||||
TITLE = "Network Monitor - monitor"
|
||||
WIN_W, WIN_H = 1024, 768
|
||||
|
||||
|
||||
def read_counters(iface):
|
||||
"""(rx_bytes, tx_bytes) for iface from /proc/net/dev, or None."""
|
||||
try:
|
||||
with open("/proc/net/dev") as f:
|
||||
lines = f.readlines()
|
||||
except OSError:
|
||||
return None
|
||||
for line in lines:
|
||||
if ":" not in line:
|
||||
continue
|
||||
name, rest = line.split(":", 1)
|
||||
if name.strip() != iface:
|
||||
continue
|
||||
fields = rest.split()
|
||||
if len(fields) < 9:
|
||||
return None
|
||||
try:
|
||||
return (int(fields[0]), int(fields[8]))
|
||||
except ValueError:
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
def pick_iface():
|
||||
"""Prefer enp3s0; else the first non-loopback interface with traffic."""
|
||||
if read_counters("enp3s0") is not None:
|
||||
return "enp3s0"
|
||||
try:
|
||||
with open("/proc/net/dev") as f:
|
||||
lines = f.readlines()
|
||||
except OSError:
|
||||
return "enp3s0"
|
||||
fallback = "enp3s0"
|
||||
for line in lines:
|
||||
if ":" not in line:
|
||||
continue
|
||||
name, rest = line.split(":", 1)
|
||||
name = name.strip()
|
||||
if not name or name == "lo":
|
||||
continue
|
||||
if fallback == "enp3s0":
|
||||
fallback = name
|
||||
fields = rest.split()
|
||||
try:
|
||||
rx = int(fields[0]) if fields else 0
|
||||
tx = int(fields[8]) if len(fields) > 8 else 0
|
||||
except ValueError:
|
||||
continue
|
||||
if rx > 0 or tx > 0:
|
||||
return name
|
||||
return fallback
|
||||
|
||||
|
||||
def link_speed_bps(iface):
|
||||
"""Link speed in bits/s from /sys/class/net/<iface>/speed (Mbit/s)."""
|
||||
try:
|
||||
with open("/sys/class/net/%s/speed" % iface) as f:
|
||||
mbps = int(f.read().strip())
|
||||
if mbps > 0:
|
||||
return mbps * 1e6
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
return 1e9 # unknown: assume 1 Gb/s
|
||||
|
||||
|
||||
IFACE = pick_iface()
|
||||
LINK_BPS = link_speed_bps(IFACE)
|
||||
|
||||
_WIDGETS = []
|
||||
_AREA = []
|
||||
_PREV = [None, None] # (rx, tx) counters from the previous tick
|
||||
_PEAK = [100.0, 100.0] # peak down/up KiB/s (floor 100), tracks x1.15
|
||||
|
||||
|
||||
def _build_widgets():
|
||||
"""Create the five monitor widgets with live-data props."""
|
||||
widgets = []
|
||||
net = create("XNetGraph", "NetworkMonitor_001", 12, 12, 1000, 300)
|
||||
net.set_prop("label", "Network - %s" % IFACE)
|
||||
widgets.append(net)
|
||||
|
||||
down = create("XSysBox", "NetworkMonitor_002", 12, 324, 492, 180)
|
||||
down.set_prop("label", "Download - %s" % IFACE)
|
||||
down.set_prop("unit", " KiB/s")
|
||||
down.set_prop("minimum", "0")
|
||||
down.set_prop("maximum", "100")
|
||||
down.set_prop("value", "0")
|
||||
widgets.append(down)
|
||||
|
||||
up = create("XSysBox", "NetworkMonitor_003", 520, 324, 492, 180)
|
||||
up.set_prop("label", "Upload - %s" % IFACE)
|
||||
up.set_prop("unit", " KiB/s")
|
||||
up.set_prop("minimum", "0")
|
||||
up.set_prop("maximum", "100")
|
||||
up.set_prop("value", "0")
|
||||
widgets.append(up)
|
||||
|
||||
hist = create("XHistoryGraph", "NetworkMonitor_004", 12, 516, 1000, 172)
|
||||
hist.set_prop("label", "Rate history (KiB/s)")
|
||||
hist.set_prop("series-names", "Down KiB/s|Up KiB/s")
|
||||
hist.set_prop("minimum", "0")
|
||||
hist.set_prop("maximum", "100")
|
||||
widgets.append(hist)
|
||||
|
||||
usage = create("XUsageBar", "NetworkMonitor_005", 12, 700, 1000, 44)
|
||||
usage.set_prop("label", "Link use - %s (%.0f Gb/s)"
|
||||
% (IFACE, LINK_BPS / 1e9))
|
||||
usage.set_prop("minimum", "0")
|
||||
usage.set_prop("maximum", "100")
|
||||
usage.set_prop("value", "0")
|
||||
widgets.append(usage)
|
||||
return widgets
|
||||
|
||||
|
||||
def _on_draw(area, cr):
|
||||
bg = parse_color("", 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 _tick():
|
||||
"""One live sample per second: difference /proc/net/dev, push, redraw."""
|
||||
now = read_counters(IFACE)
|
||||
if now is None:
|
||||
return True
|
||||
prev = _PREV[0]
|
||||
_PREV[0] = now
|
||||
if prev is None:
|
||||
return True
|
||||
down_bps = float(now[0] - prev[0]) if now[0] >= prev[0] else 0.0
|
||||
up_bps = float(now[1] - prev[1]) if now[1] >= prev[1] else 0.0
|
||||
down_kb = down_bps / 1024.0
|
||||
up_kb = up_bps / 1024.0
|
||||
_PEAK[0] = max(_PEAK[0], down_kb * 1.15)
|
||||
_PEAK[1] = max(_PEAK[1], up_kb * 1.15)
|
||||
|
||||
net, down, up, hist, usage = _WIDGETS
|
||||
net.push_net(down_bps, up_bps)
|
||||
down.set_prop("maximum", "%.1f" % _PEAK[0])
|
||||
down.set_prop("value", "%.1f" % down_kb)
|
||||
down.push(down_kb)
|
||||
up.set_prop("maximum", "%.1f" % _PEAK[1])
|
||||
up.set_prop("value", "%.1f" % up_kb)
|
||||
up.push(up_kb)
|
||||
hist.set_prop("maximum", "%.1f" % max(_PEAK[0], _PEAK[1]))
|
||||
hist.push(down_kb, series=0)
|
||||
hist.push(up_kb, series=1)
|
||||
usage.set_prop("value", "%.1f"
|
||||
% min(100.0, (down_bps + up_bps) / LINK_BPS * 100.0))
|
||||
if _AREA:
|
||||
_AREA[0].queue_draw()
|
||||
return True
|
||||
|
||||
|
||||
def main():
|
||||
window = Gtk.Window(title=TITLE)
|
||||
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)
|
||||
_PREV[0] = read_counters(IFACE) # baseline; first tick differences it
|
||||
GLib.timeout_add(1000, _tick)
|
||||
window.show_all()
|
||||
Gtk.main()
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 46 KiB |
@@ -1193,6 +1193,7 @@ 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