// Data model: server config, rules, autopilot policy, parsed metrics, formatting. #pragma once #include "Platform.h" #include #include #include #include #include namespace Fmt { QString bytes(double b, int prec = 1); inline QString rate(double b) { return bytes(b) + "/s"; } inline QString pct(double p, int prec = 1) { return QString::number(p, 'f', prec) + "%"; } inline QString temp(double c) { return QString::number(c, 'f', 0) + " °C"; } QString count(double v); QString duration(double s); } enum class Transport { Local, SSH }; inline QString transportName(Transport t) { return t == Transport::Local ? "Local" : "SSH"; } const QVector &ruleMetrics(); QString defLabel(const QVector &d, const QString &k, bool unit = false); // "When for s -> ()" struct Rule { QString id, metric = "cpu", op = ">", action = "notify", arg, severity = "warning"; bool enabled = true; double value = 90; int duration = 60, cooldown = 300; QString conditionText() const; QString actionText() const; }; // History-driven automatic control (evaluated by the Analyzer every minute). struct AutopilotConfig { bool enabled = true, dryRun = false, restartFailed = true, diskCleanup = false, anomalyAlerts = true; int maxRestartsPerHour = 3, runawayMinutes = 10, diskForecastDays = 3; double runawayCpu = 90, diskCleanupAt = 92, leakMemPressure = 90, anomalyZ = 3; QString runawayMode = "renice", leakMode = "notify"; // none|renice|terminate, none|notify|terminate QString diskCleanupCommand = P::cleanup; }; struct ServerConfig { QString id, name, group, host, user, keyFile, password, notes; Transport transport = Transport::SSH; int port = 22, interval = P::interval; bool useSudo = false, enabled = true; QVector rules = defaultRules(); AutopilotConfig autopilot; QString address() const; QJsonObject toJson() const; static ServerConfig fromJson(const QJsonObject &o); static QVector defaultRules(); static QString newId(); }; // ---- one parsed sample ------------------------------------------------------ struct CoreStat { double usage = 0, mhz = 0; }; struct MountInfo { QString device, type, mount; double total = 0, used = 0, avail = 0, pct = 0; }; struct DiskIO { QString name; double readBps = 0, writeBps = 0, readIops = 0, writeIops = 0, util = 0, totalRead = 0, totalWritten = 0; }; struct NetIf { QString name, state; int speed = -1; bool virt = false; double rxBps = 0, txBps = 0, rxPps = 0, txPps = 0, rxBytes = 0, txBytes = 0, errors = 0, drops = 0; }; struct ProcInfo { int pid = 0, ppid = 0, threads = 0, nice = 0; QString name, user; QChar state; double cpu = 0, memPct = 0, rss = 0, vsz = 0, age = 0; // cpu: % of one core }; struct ServiceInfo { QString unit, load, active, sub, description; }; struct TempSensor { QString name; double celsius = 0; }; struct GpuInfo { int index = 0; QString name; double util = 0, memUsed = 0, memTotal = 0, temp = 0, power = 0, fan = 0; }; struct Metrics { bool valid = false; qint64 timestamp = 0; QString hostname, os, kernel, arch, cpuModel, virt; int cpuCount = 0, loggedUsers = 0, procsRunning = 0, procsBlocked = 0, threadsTotal = 0, socketsUsed = 0; int servicesFailed = 0, servicesRunning = 0; double uptime = 0, load1 = 0, load5 = 0, load15 = 0; double cpuUsage = 0, cpuUser = 0, cpuSystem = 0, cpuIowait = 0, cpuSteal = 0, cpuIrq = 0; double ctxtPerSec = 0, intrPerSec = 0, forksPerSec = 0, avgMhz = 0; double memTotal = 0, memFree = 0, memAvailable = 0, memUsed = 0, buffers = 0, cached = 0, shared = 0, slab = 0, dirty = 0; double swapTotal = 0, swapUsed = 0, memPct = 0, swapPct = 0; double diskReadBps = 0, diskWriteBps = 0, maxDiskPct = 0, maxDiskUtil = 0, netRxBps = 0, netTxBps = 0, maxTemp = 0; QString fullestMount, busiestDisk; QVector cores; QVector mounts; QVector disks; QVector nets; QMap tcp; // ESTABLISHED -> n QVector procs; QVector services; QVector temps; QVector gpus; double gpuMax(bool temp) const; }; // Turns raw collector frames into Metrics; keeps counters for rates. process() is in Platform.cpp. class MetricsEngine { public: Metrics process(const QByteArray &raw, qint64 nowMs); void reset() { *this = MetricsEngine(); } private: double m_upt = 0; QVector> m_cpu; QHash> m_ctr; // misc counters: "stat", "disk:sda", "net:eth0" QHash m_ticks; QHash m_info; QHash m_users; QVector m_services; QVector m_gpus; };