// Connection to one machine (local sh or system ssh), the per-server session // (metrics, live history, rules, autopilot) and the manager of all sessions. #pragma once #include "History.h" #include #include #include #include #include #include #include using Done = std::function; // Streams collector frames and runs one-shot commands. Deleting it kills everything. class Link : public QObject { Q_OBJECT public: Link(const ServerConfig &c, QObject *parent); ~Link() override; void start(); int exec(const QByteArray &input, int timeoutMs); signals: void snapshot(const QByteArray &frame); void disconnected(const QString &reason); void execFinished(int id, int code, const QString &out); private: QProcess *spawn(const QStringList &shell); // not started yet ServerConfig m_c; QByteArray m_buf, m_err; int m_next = 1; }; enum class Status { Disabled, Connecting, Online, Offline }; QString statusName(Status s); struct AlertEvent { QDateTime time; QString serverName, severity, message; }; // severity: info|warning|critical|resolved|action struct Live { // last hour in memory for the live charts enum { CPU, USER, SYS, IOWAIT, STEAL, MEM, CACHE, SWAP, RX, TX, RD, WR, LOAD, N }; QVector t; QVector s[N]; QHash> ifRx, ifTx; void push(const Metrics &m); }; class ServerSession : public QObject { Q_OBJECT public: ServerSession(const ServerConfig &c, QObject *parent); ~ServerSession() override; const ServerConfig &config() const { return m_cfg; } void setConfig(const ServerConfig &c); Status status() const { return m_status; } QString lastError() const { return m_err; } const Metrics &metrics() const { return m_m; } const Live &live() const { return m_live; } bool online() const { return m_status == Status::Online && m_m.valid; } bool isPaused() const { return m_paused; } void setPaused(bool p) { m_paused = p, emit updated(); } void start(); void stop(); void reconnect() { m_retries = 0, m_paused = false, start(); } // privileged + "useSudo" wraps the command in sudo -n void exec(const QString &cmd, bool privileged, Done done = {}, int timeoutMs = 60000); double metricValue(const QString &key) const; QStringList activeAlerts() const; const HistoryStore &store() const { return m_store; } const Analysis &analysis() const { return m_an; } void analyzeNow(); void runFinding(const Finding &f, bool automatic, Done done = {}); void deleteHistory(); signals: void updated(); void analysisUpdated(); void alertRaised(const AlertEvent &e); private: void setStatus(Status s, const QString &err = {}); void drop(const QString &why); // closes the link and fails the commands that wait for it void onFrame(const QByteArray &raw); void onLost(const QString &why); void record(bool up); void track(qint64 now); void evaluateRules(); void run(const QString &action, const QString &arg, const QString &what, Done done = {}); void raise(const QString &sev, const QString &msg); ServerConfig m_cfg; Status m_status = Status::Disabled; QString m_err; QPointer m_link; MetricsEngine m_engine; Metrics m_m; Live m_live; QTimer m_retry, m_watchdog, m_analysisTimer, m_offlineTick; int m_retries = 0; bool m_paused = false, m_offlineAlerted = false; QHash m_pending; HistoryStore m_store; Analysis m_an; QSet m_reported, m_dry; QHash m_cooldown; struct RuleState { qint64 since = 0, lastFire = 0; bool active = false; }; QHash m_rules; struct ProcTrack { QString name; qint64 highSince = 0, lastMinute = 0; QVector rss; bool kernel = false; }; QHash m_procs; struct SvcTrack { QString last; bool preexisting = false; QVector fails, restarts; }; QHash m_svcs; }; class ServerManager : public QObject { Q_OBJECT public: using QObject::QObject; ~ServerManager() override { qDeleteAll(m_s); } static QString configDir(); void load(); void save() const; const QVector &sessions() const { return m_s; } ServerSession *session(const QString &id) const; ServerSession *add(const ServerConfig &c); void update(const ServerConfig &c); void remove(const QString &id, bool deleteHistory); void move(const QString &id, int delta); const QVector &alerts() const { return m_alerts; } void clearAlerts() { m_alerts.clear(), emit alertsCleared(); } signals: void listChanged(); void alertRaised(const AlertEvent &e); void alertsCleared(); private: ServerSession *attach(const ServerConfig &c); QVector m_s; QVector m_alerts; };