File size: 4,873 Bytes
bad179a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Data model: server config, rules, autopilot policy, parsed metrics, formatting.
#pragma once
#include "Platform.h"
#include <QHash>
#include <QJsonObject>
#include <QMap>
#include <QStringList>
#include <QVector>

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<Def> &ruleMetrics();
QString defLabel(const QVector<Def> &d, const QString &k, bool unit = false);

// "When <metric> <op> <value> for <duration> s -> <action>(<arg>)"
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<Rule> rules = defaultRules();
    AutopilotConfig autopilot;
    QString address() const;
    QJsonObject toJson() const;
    static ServerConfig fromJson(const QJsonObject &o);
    static QVector<Rule> 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<CoreStat> cores;
    QVector<MountInfo> mounts;
    QVector<DiskIO> disks;
    QVector<NetIf> nets;
    QMap<QString, int> tcp; // ESTABLISHED -> n
    QVector<ProcInfo> procs;
    QVector<ServiceInfo> services;
    QVector<TempSensor> temps;
    QVector<GpuInfo> 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<QVector<double>> m_cpu;
    QHash<QString, QVector<double>> m_ctr; // misc counters: "stat", "disk:sda", "net:eth0"
    QHash<int, double> m_ticks;
    QHash<QString, QString> m_info;
    QHash<int, QString> m_users;
    QVector<ServiceInfo> m_services;
    QVector<GpuInfo> m_gpus;
};