File size: 5,018 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 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 <QDateTime>
#include <QElapsedTimer>
#include <QPointer>
#include <QProcess>
#include <QSet>
#include <QTimer>
#include <functional>

using Done = std::function<void(int code, QString output)>;

// 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<qint64> t;
    QVector<double> s[N];
    QHash<QString, QVector<double>> 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<Link> 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<int, Done> m_pending;
    HistoryStore m_store;
    Analysis m_an;
    QSet<QString> m_reported, m_dry;
    QHash<QString, qint64> m_cooldown;
    struct RuleState { qint64 since = 0, lastFire = 0; bool active = false; };
    QHash<QString, RuleState> m_rules;
    struct ProcTrack { QString name; qint64 highSince = 0, lastMinute = 0; QVector<double> rss; bool kernel = false; };
    QHash<int, ProcTrack> m_procs;
    struct SvcTrack { QString last; bool preexisting = false; QVector<qint64> fails, restarts; };
    QHash<QString, SvcTrack> 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<ServerSession *> &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<AlertEvent> &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<ServerSession *> m_s;
    QVector<AlertEvent> m_alerts;
};