Live process table from /proc (sortable columns, in-cell CPU% bars, colour-coded rows, refresh()), load-average badge (1/5/15 min, threshold colours, uptime from /proc), monitor button bound to a process (/proc scan), systemd unit (systemctl is-active) or TCP port (connect to localhost) with poll()/update() and click-for-details, grid panel of monitor buttons with one shared poll_all(), composite resource box (title + big number + history + usage bar). Header-only C++, demo mains (polling loop for the live three), examples, Xvfb screenshots.
314 lines
9.2 KiB
C++
314 lines
9.2 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <dirent.h>
|
|
#include <string>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <unistd.h>
|
|
|
|
// region XMonitorButton
|
|
//
|
|
// A button that IS a live monitor: bound to a process running (by name),
|
|
// a systemd unit active (systemctl is-active), or a TCP port open.
|
|
// Glows green when up, red when down, dim when unknown; label + status
|
|
// text; polls on a timer via poll(); click polls now and shows details.
|
|
// Checks use stdlib / syscalls only. Raw X11, same as the XFrame set.
|
|
//
|
|
class XMonitorButton
|
|
{
|
|
public:
|
|
enum Kind
|
|
{
|
|
Process = 0, // target = process name (comm)
|
|
Unit = 1, // target = systemd unit, e.g. "sshd.service"
|
|
Port = 2 // target = TCP port number, e.g. "22"
|
|
};
|
|
enum MonitorStatus
|
|
{
|
|
MonUnknown = 0,
|
|
MonUp = 1,
|
|
MonDown = 2
|
|
};
|
|
|
|
private:
|
|
Display* display_;
|
|
int screen_;
|
|
Window window_;
|
|
GC gc_;
|
|
SStyle_Manager* styles_;
|
|
int x1_, y1_, x2_, y2_;
|
|
std::string name_;
|
|
std::string text_; // label
|
|
std::string style_name_;
|
|
bool visible_ = true;
|
|
bool enabled_ = true;
|
|
bool focused_ = false;
|
|
|
|
Kind kind_ = Port;
|
|
std::string target_ = "22";
|
|
MonitorStatus status_ = MonUnknown;
|
|
std::string details_;
|
|
bool show_details_ = false;
|
|
|
|
Srgb white_color_{255, 255, 255};
|
|
Srgb up_color_{0, 170, 70};
|
|
Srgb down_color_{210, 40, 30};
|
|
Srgb unknown_color_{150, 150, 150};
|
|
|
|
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
|
|
|
|
void draw_text_color(int x, int baseline_y, const std::string& s, Srgb color)
|
|
{
|
|
SStyle st = style();
|
|
if (!st.font_ || s.empty()) return;
|
|
XSetFont(display_, gc_, st.font_->fid);
|
|
XSetForeground(display_, gc_, col(color));
|
|
XDrawString(display_, window_, gc_, x, baseline_y, s.c_str(), (int)s.size());
|
|
}
|
|
|
|
bool check_process(const std::string& name, int& found_pid)
|
|
{
|
|
DIR* dir = opendir("/proc");
|
|
if (!dir) return false;
|
|
bool found = false;
|
|
struct dirent* ent;
|
|
while ((ent = readdir(dir)) != nullptr)
|
|
{
|
|
if (ent->d_name[0] < '0' || ent->d_name[0] > '9') continue;
|
|
std::string comm_path = std::string("/proc/") + ent->d_name + "/comm";
|
|
FILE* f = std::fopen(comm_path.c_str(), "r");
|
|
if (!f) continue;
|
|
char buf[256];
|
|
if (std::fgets(buf, sizeof(buf), f))
|
|
{
|
|
buf[std::strcspn(buf, "\n")] = 0;
|
|
if (name == buf)
|
|
{
|
|
found = true;
|
|
found_pid = std::atoi(ent->d_name);
|
|
std::fclose(f);
|
|
break;
|
|
}
|
|
}
|
|
std::fclose(f);
|
|
}
|
|
closedir(dir);
|
|
return found;
|
|
}
|
|
|
|
bool check_unit(const std::string& unit, std::string& out)
|
|
{
|
|
// Only safe unit-name characters reach the shell.
|
|
for (char c : unit)
|
|
{
|
|
bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
|
|
(c >= '0' && c <= '9') || c == '@' || c == '.' || c == '_' || c == '-';
|
|
if (!ok) return false;
|
|
}
|
|
std::string cmd = "systemctl is-active " + unit + " 2>/dev/null";
|
|
FILE* p = popen(cmd.c_str(), "r");
|
|
if (!p) return false;
|
|
char buf[128];
|
|
bool active = false;
|
|
if (std::fgets(buf, sizeof(buf), p))
|
|
{
|
|
buf[std::strcspn(buf, "\n")] = 0;
|
|
out = buf;
|
|
active = (out == "active");
|
|
}
|
|
pclose(p);
|
|
return active;
|
|
}
|
|
|
|
bool check_port(int port)
|
|
{
|
|
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (fd < 0) return false;
|
|
struct timeval tv;
|
|
tv.tv_sec = 1;
|
|
tv.tv_usec = 0;
|
|
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
|
struct sockaddr_in addr;
|
|
std::memset(&addr, 0, sizeof(addr));
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_port = htons((uint16_t)port);
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
bool open = connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0;
|
|
close(fd);
|
|
return open;
|
|
}
|
|
|
|
public:
|
|
XMonitorButton(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height)
|
|
{
|
|
display_ = display;
|
|
screen_ = screen;
|
|
window_ = window;
|
|
gc_ = gc;
|
|
styles_ = styles;
|
|
x1_ = x;
|
|
y1_ = y;
|
|
x2_ = x + width;
|
|
y2_ = y + height;
|
|
name_ = "XMonitorButton";
|
|
text_ = "sshd :22";
|
|
poll();
|
|
}
|
|
|
|
~XMonitorButton() {}
|
|
|
|
// region Standard plumbing
|
|
void set_bounds(int x, int y, int width, int height)
|
|
{
|
|
x1_ = x;
|
|
y1_ = y;
|
|
x2_ = x + width;
|
|
y2_ = y + height;
|
|
}
|
|
int width() { return x2_ - x1_; }
|
|
int height() { return y2_ - y1_; }
|
|
bool contains(int x, int y)
|
|
{
|
|
return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_;
|
|
}
|
|
void set_visible(bool v) { visible_ = v; }
|
|
bool get_visible() { return visible_; }
|
|
void set_enabled(bool e) { enabled_ = e; }
|
|
bool get_enabled() { return enabled_; }
|
|
void set_style_name(std::string s) { style_name_ = s; }
|
|
void set_text(std::string t) { text_ = t; }
|
|
std::string get_text() { return text_; }
|
|
SStyle style() { return styles_->resolve(style_name_); }
|
|
// endregion
|
|
|
|
// region Monitor API
|
|
void set_kind(Kind k) { kind_ = k; }
|
|
Kind get_kind() { return kind_; }
|
|
void set_target(const std::string& t) { target_ = t; }
|
|
std::string get_target() { return target_; }
|
|
MonitorStatus status() { return status_; }
|
|
std::string status_text()
|
|
{
|
|
switch (status_)
|
|
{
|
|
case MonUp: return "UP";
|
|
case MonDown: return "DOWN";
|
|
default: return "UNKNOWN";
|
|
}
|
|
}
|
|
std::string details() { return details_; }
|
|
|
|
// Run one check now; call on a timer for live monitoring.
|
|
void poll()
|
|
{
|
|
bool ok = false;
|
|
if (kind_ == Process)
|
|
{
|
|
int pid = 0;
|
|
ok = check_process(target_, pid);
|
|
details_ = ok ? ("pid " + std::to_string(pid)) : "not running";
|
|
}
|
|
else if (kind_ == Unit)
|
|
{
|
|
std::string out;
|
|
ok = check_unit(target_, out);
|
|
details_ = out.empty() ? "no result" : out;
|
|
}
|
|
else
|
|
{
|
|
int port = std::atoi(target_.c_str());
|
|
if (port <= 0 || port > 65535)
|
|
{
|
|
status_ = MonUnknown;
|
|
details_ = "bad port";
|
|
return;
|
|
}
|
|
ok = check_port(port);
|
|
details_ = ok ? "open" : "closed";
|
|
}
|
|
status_ = ok ? MonUp : MonDown;
|
|
}
|
|
// Backwards-compatible name: update() polls once.
|
|
void update() { poll(); }
|
|
// endregion
|
|
|
|
void draw()
|
|
{
|
|
if (!visible_) return;
|
|
SStyle st = style();
|
|
int w = width();
|
|
int h = height();
|
|
Srgb state = status_ == MonUp ? up_color_ : (status_ == MonDown ? down_color_ : unknown_color_);
|
|
|
|
// Body: white, thick status-coloured border = the glow.
|
|
XSetForeground(display_, gc_, col(white_color_));
|
|
XFillRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
|
XSetForeground(display_, gc_, col(state));
|
|
xf::set_line_width(display_, gc_, 3);
|
|
XDrawRectangle(display_, window_, gc_, x1_ + 1, y1_ + 1, w - 2, h - 2);
|
|
xf::set_line_width(display_, gc_, 1);
|
|
|
|
// Status dot.
|
|
int dot_r = 7;
|
|
int dot_cx = x1_ + 16;
|
|
int dot_cy = y1_ + h / 2 - (show_details_ ? 8 : 0);
|
|
XSetForeground(display_, gc_, col(state));
|
|
XFillArc(display_, window_, gc_, dot_cx - dot_r, dot_cy - dot_r, 2 * dot_r, 2 * dot_r, 0, 360 * 64);
|
|
|
|
// Label + status text.
|
|
xf::draw_text_at(display_, window_, gc_, st, x1_ + 30, dot_cy + 4, text_);
|
|
std::string stxt = status_text();
|
|
int sw = xf::text_width(st, stxt);
|
|
draw_text_color(x2_ - 10 - sw, dot_cy + 4, stxt, state);
|
|
draw_text_color(x2_ - 10 - sw + 1, dot_cy + 4, stxt, state);
|
|
|
|
if (show_details_)
|
|
{
|
|
std::string d = kind_ == Process ? ("process " + target_ + ": " + details_)
|
|
: kind_ == Unit ? ("unit " + target_ + ": " + details_)
|
|
: ("port " + target_ + ": " + details_);
|
|
xf::draw_text_at(display_, window_, gc_, st, x1_ + 30, y1_ + h - 8, d.substr(0, 48));
|
|
}
|
|
|
|
XFlush(display_);
|
|
}
|
|
|
|
bool on_button_press(int x, int y, unsigned int button)
|
|
{
|
|
if (button == 1 && contains(x, y))
|
|
{
|
|
show_details_ = !show_details_;
|
|
poll();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
bool on_button_release(int x, int y, unsigned int button)
|
|
{
|
|
(void)x;
|
|
(void)y;
|
|
(void)button;
|
|
return false;
|
|
}
|
|
bool on_motion(int x, int y)
|
|
{
|
|
(void)x;
|
|
(void)y;
|
|
return false;
|
|
}
|
|
bool on_key(KeySym key, const char* str)
|
|
{
|
|
(void)key;
|
|
(void)str;
|
|
return false;
|
|
}
|
|
};
|