Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9bf5a7ce51 |
No files matched your search
@@ -0,0 +1,25 @@
|
||||
# Network Monitor - monitor (C / XFrame)
|
||||
#
|
||||
# Build: make
|
||||
# Run: ./network-monitor (quit: q / Escape / window close)
|
||||
# Clean: make clean
|
||||
|
||||
CXX ?= g++
|
||||
CXXFLAGS ?= -std=c++17 -O2 -Wall
|
||||
|
||||
ROOT := ../../..
|
||||
INCLUDES := -I$(ROOT)/widgets/components/XNetGraph \
|
||||
-I$(ROOT)/widgets/components/XHistoryGraph \
|
||||
-I$(ROOT)/widgets/components/XUsageBar \
|
||||
-I$(ROOT)/widgets/components/XSysBox
|
||||
LIBS := $(shell pkg-config --libs x11)
|
||||
|
||||
all: network-monitor
|
||||
|
||||
network-monitor: main.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) -o $@ main.cpp $(LIBS)
|
||||
|
||||
clean:
|
||||
rm -f network-monitor
|
||||
|
||||
.PHONY: all clean
|
||||
@@ -0,0 +1,45 @@
|
||||
# Network Monitor — monitor (C)
|
||||
|
||||
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 every 500 ms:
|
||||
|
||||
- **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 preload seeded demo series. The app flushes them at
|
||||
startup: the first measured rate (two `/proc/net/dev` reads 500 ms
|
||||
apart) is pushed 120x into XNetGraph (which has no clear API) and its
|
||||
totals reset, the history graph is cleared and re-seeded with down/up
|
||||
series, and each XSysBox gets 60 live `set_value()` calls. Everything
|
||||
on screen after that is measured traffic.
|
||||
|
||||
## Build and run
|
||||
|
||||
```sh
|
||||
make # builds ./network-monitor
|
||||
./network-monitor # quit with q, Escape, or window close
|
||||
```
|
||||
|
||||
## 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)
|
||||
@@ -0,0 +1,311 @@
|
||||
// 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 every 500 ms:
|
||||
//
|
||||
// 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
|
||||
//
|
||||
// Widgets preload seeded demo data; the app flushes it at startup
|
||||
// (XNetGraph has no clear API, so the first measured rate is pushed
|
||||
// 120x to fill its window, then reset_totals(); XHistoryGraph is
|
||||
// cleared and re-seeded with down/up series; each XSysBox gets 60
|
||||
// live set_value() calls). Everything on screen after that is live.
|
||||
//
|
||||
// Build: make (see Makefile) Run: ./network-monitor
|
||||
// Quit: q / Escape / window close.
|
||||
|
||||
#include "XNetGraph.h"
|
||||
#include "XHistoryGraph.h"
|
||||
#include "XUsageBar.h"
|
||||
#include "XSysBox.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <sys/select.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
const char* kTitle = "Network Monitor - monitor";
|
||||
const int kWinW = 1024;
|
||||
const int kWinH = 768;
|
||||
|
||||
// Read rx/tx byte counters for iface from /proc/net/dev.
|
||||
bool read_counters(const std::string& iface, unsigned long long& rx, unsigned long long& tx)
|
||||
{
|
||||
FILE* f = std::fopen("/proc/net/dev", "r");
|
||||
if (!f) return false;
|
||||
char line[512];
|
||||
bool ok = false;
|
||||
while (std::fgets(line, sizeof(line), f))
|
||||
{
|
||||
char* colon = std::strchr(line, ':');
|
||||
if (!colon) continue;
|
||||
std::string name(line, colon);
|
||||
// Trim spaces.
|
||||
size_t b = name.find_first_not_of(" \t");
|
||||
size_t e = name.find_last_not_of(" \t");
|
||||
if (b == std::string::npos) continue;
|
||||
name = name.substr(b, e - b + 1);
|
||||
if (name != iface) continue;
|
||||
unsigned long long v[16] = {0};
|
||||
int n = std::sscanf(colon + 1, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
|
||||
&v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7],
|
||||
&v[8], &v[9], &v[10], &v[11], &v[12], &v[13], &v[14], &v[15]);
|
||||
if (n >= 9)
|
||||
{
|
||||
rx = v[0];
|
||||
tx = v[8];
|
||||
ok = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
std::fclose(f);
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool iface_exists(const std::string& iface)
|
||||
{
|
||||
unsigned long long rx = 0, tx = 0;
|
||||
return read_counters(iface, rx, tx);
|
||||
}
|
||||
|
||||
// Prefer enp3s0; else the first non-loopback interface with traffic.
|
||||
std::string pick_iface()
|
||||
{
|
||||
if (iface_exists("enp3s0")) return "enp3s0";
|
||||
FILE* f = std::fopen("/proc/net/dev", "r");
|
||||
if (!f) return "enp3s0";
|
||||
char line[512];
|
||||
std::string fallback = "enp3s0";
|
||||
while (std::fgets(line, sizeof(line), f))
|
||||
{
|
||||
char* colon = std::strchr(line, ':');
|
||||
if (!colon) continue;
|
||||
std::string name(line, colon);
|
||||
size_t b = name.find_first_not_of(" \t");
|
||||
size_t e = name.find_last_not_of(" \t");
|
||||
if (b == std::string::npos) continue;
|
||||
name = name.substr(b, e - b + 1);
|
||||
if (name == "lo") continue;
|
||||
unsigned long long v[16] = {0};
|
||||
std::sscanf(colon + 1, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
|
||||
&v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7],
|
||||
&v[8], &v[9], &v[10], &v[11], &v[12], &v[13], &v[14], &v[15]);
|
||||
if (fallback == "enp3s0") fallback = name;
|
||||
if (v[0] > 0 || v[8] > 0) { std::fclose(f); return name; }
|
||||
}
|
||||
std::fclose(f);
|
||||
return fallback;
|
||||
}
|
||||
|
||||
// Link speed in bits/s from /sys/class/net/<iface>/speed (Mbit/s);
|
||||
// unknown/unreadable -> assume 1 Gbit/s.
|
||||
double link_speed_bps(const std::string& iface)
|
||||
{
|
||||
std::string path = "/sys/class/net/" + iface + "/speed";
|
||||
FILE* f = std::fopen(path.c_str(), "r");
|
||||
if (!f) return 1e9;
|
||||
long mbps = 0;
|
||||
if (std::fscanf(f, "%ld", &mbps) != 1 || mbps <= 0) mbps = 1000;
|
||||
std::fclose(f);
|
||||
return (double)mbps * 1e6;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string iface = pick_iface();
|
||||
double link_bps = link_speed_bps(iface);
|
||||
|
||||
Display* display = XOpenDisplay(nullptr);
|
||||
if (!display) return 1;
|
||||
int screen = DefaultScreen(display);
|
||||
Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100,
|
||||
kWinW, kWinH, 1, BlackPixel(display, screen),
|
||||
WhitePixel(display, screen));
|
||||
XStoreName(display, window, kTitle);
|
||||
XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask |
|
||||
ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
|
||||
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
|
||||
XMapWindow(display, window);
|
||||
GC gc = DefaultGC(display, screen);
|
||||
SStyle_Manager styles(display, window, screen, gc);
|
||||
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60},
|
||||
Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*");
|
||||
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200},
|
||||
Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*");
|
||||
styles.set_active_style_byName("default");
|
||||
|
||||
XNetGraph net(display, screen, window, gc, &styles, 12, 12, 1000, 300);
|
||||
net.set_text("Network - " + iface);
|
||||
XSysBox down_box(display, screen, window, gc, &styles, 12, 324, 492, 180);
|
||||
down_box.set_text("Download - " + iface);
|
||||
down_box.set_unit(" KiB/s");
|
||||
XSysBox up_box(display, screen, window, gc, &styles, 520, 324, 492, 180);
|
||||
up_box.set_text("Upload - " + iface);
|
||||
up_box.set_unit(" KiB/s");
|
||||
up_box.set_graph_color(Srgb{220, 80, 40});
|
||||
XHistoryGraph hist(display, screen, window, gc, &styles, 12, 516, 1000, 172);
|
||||
hist.set_text("Rate history (KiB/s)");
|
||||
XUsageBar usage(display, screen, window, gc, &styles, 12, 700, 1000, 44);
|
||||
{
|
||||
char buf[96];
|
||||
std::snprintf(buf, sizeof(buf), "Link use - %s (%.0f Gb/s)", iface.c_str(), link_bps / 1e9);
|
||||
usage.set_text(buf);
|
||||
}
|
||||
usage.set_max(100.0);
|
||||
|
||||
auto redraw = [&]() {
|
||||
XClearWindow(display, window);
|
||||
net.draw();
|
||||
down_box.draw();
|
||||
up_box.draw();
|
||||
hist.draw();
|
||||
usage.draw();
|
||||
XFlush(display);
|
||||
};
|
||||
|
||||
// First live rate (two reads 500 ms apart), then flush the widgets'
|
||||
// seeded demo data with it so the screen is live from the start.
|
||||
unsigned long long rx0 = 0, tx0 = 0, rx1 = 0, tx1 = 0;
|
||||
read_counters(iface, rx0, tx0);
|
||||
usleep(500000);
|
||||
read_counters(iface, rx1, tx1);
|
||||
double down_bps = (rx1 >= rx0) ? (double)(rx1 - rx0) / 0.5 : 0.0;
|
||||
double up_bps = (tx1 >= tx0) ? (double)(tx1 - tx0) / 0.5 : 0.0;
|
||||
double down_kb = down_bps / 1024.0;
|
||||
double up_kb = up_bps / 1024.0;
|
||||
double peak_down = std::max(100.0, down_kb * 1.15);
|
||||
double peak_up = std::max(100.0, up_kb * 1.15);
|
||||
|
||||
for (int i = 0; i < 120; ++i) net.push(down_bps, up_bps);
|
||||
net.reset_totals(); // totals restart at session start, not demo-inflated
|
||||
hist.clear_series();
|
||||
hist.add_series("Down KiB/s", Srgb{0, 170, 70});
|
||||
hist.add_series("Up KiB/s", Srgb{220, 80, 40});
|
||||
hist.push(0, down_kb);
|
||||
hist.push(1, up_kb);
|
||||
down_box.set_max(peak_down);
|
||||
up_box.set_max(peak_up);
|
||||
for (int i = 0; i < 60; ++i) { down_box.set_value(down_kb); up_box.set_value(up_kb); }
|
||||
usage.set_value(std::min(100.0, (down_bps + up_bps) / link_bps * 100.0));
|
||||
|
||||
unsigned long long prev_rx = rx1, prev_tx = tx1;
|
||||
auto prev_time = std::chrono::steady_clock::now();
|
||||
redraw();
|
||||
|
||||
int fd = ConnectionNumber(display);
|
||||
bool running = true;
|
||||
auto last_tick = std::chrono::steady_clock::now();
|
||||
while (running)
|
||||
{
|
||||
while (XPending(display))
|
||||
{
|
||||
XEvent event;
|
||||
XNextEvent(display, &event);
|
||||
bool handled = false;
|
||||
switch (event.type)
|
||||
{
|
||||
case Expose:
|
||||
handled = true;
|
||||
break;
|
||||
case ButtonPress:
|
||||
handled = net.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
down_box.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
up_box.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
hist.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
usage.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button);
|
||||
break;
|
||||
case ButtonRelease:
|
||||
handled = net.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
down_box.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
up_box.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
hist.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
usage.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button);
|
||||
break;
|
||||
case MotionNotify:
|
||||
handled = net.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
down_box.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
up_box.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
hist.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
usage.on_motion(event.xmotion.x, event.xmotion.y);
|
||||
break;
|
||||
case KeyPress:
|
||||
{
|
||||
char buffer[32];
|
||||
KeySym key = 0;
|
||||
int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr);
|
||||
buffer[n] = 0;
|
||||
if (key == XK_Escape || (n == 1 && (buffer[0] == 'q' || buffer[0] == 'Q')))
|
||||
running = false;
|
||||
handled = net.on_key(key, buffer) || down_box.on_key(key, buffer) ||
|
||||
up_box.on_key(key, buffer) || hist.on_key(key, buffer) ||
|
||||
usage.on_key(key, buffer);
|
||||
break;
|
||||
}
|
||||
case ClientMessage:
|
||||
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) running = false;
|
||||
break;
|
||||
}
|
||||
if (handled) redraw();
|
||||
if (!running) break;
|
||||
}
|
||||
if (!running) break;
|
||||
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
long elapsed_ms = (long)std::chrono::duration_cast<std::chrono::milliseconds>(now - last_tick).count();
|
||||
if (elapsed_ms < 500)
|
||||
{
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(fd, &fds);
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = (500 - elapsed_ms) * 1000;
|
||||
select(fd + 1, &fds, nullptr, nullptr, &tv);
|
||||
continue;
|
||||
}
|
||||
last_tick = now;
|
||||
|
||||
// Live tick: difference /proc/net/dev over the real elapsed time.
|
||||
unsigned long long rx = 0, tx = 0;
|
||||
if (!read_counters(iface, rx, tx)) continue;
|
||||
double dt = std::chrono::duration<double>(now - prev_time).count();
|
||||
prev_time = now;
|
||||
if (dt <= 0.0) continue;
|
||||
down_bps = (rx >= prev_rx) ? (double)(rx - prev_rx) / dt : 0.0;
|
||||
up_bps = (tx >= prev_tx) ? (double)(tx - prev_tx) / dt : 0.0;
|
||||
prev_rx = rx;
|
||||
prev_tx = tx;
|
||||
down_kb = down_bps / 1024.0;
|
||||
up_kb = up_bps / 1024.0;
|
||||
peak_down = std::max(peak_down, down_kb * 1.15);
|
||||
peak_up = std::max(peak_up, up_kb * 1.15);
|
||||
|
||||
net.push(down_bps, up_bps);
|
||||
hist.push(0, down_kb);
|
||||
hist.push(1, up_kb);
|
||||
down_box.set_max(peak_down);
|
||||
up_box.set_max(peak_up);
|
||||
down_box.set_value(down_kb);
|
||||
up_box.set_value(up_kb);
|
||||
usage.set_value(std::min(100.0, (down_bps + up_bps) / link_bps * 100.0));
|
||||
redraw();
|
||||
}
|
||||
|
||||
XCloseDisplay(display);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 3.8 KiB |
Reference in new issue
Block a user