Download Server Monitor(cqt-window)/src/core/Model.cpp from mchuca/ServerMonitor: direct link, hf CLI and curl.
- Browser
- Download file 5.72 kB
-
https://huggingface.co/mchuca/ServerMonitor/resolve/main/Server%20Monitor(cqt-window)/src/core/Model.cpp
- Command line
-
hf download 'hf://mchuca/ServerMonitor/Server Monitor(cqt-window)/src/core/Model.cpp'
-
curl -L -o Model.cpp 'https://huggingface.co/mchuca/ServerMonitor/resolve/main/Server%20Monitor(cqt-window)/src/core/Model.cpp'
5.72 kB
| // ---- formatting ------------------------------------------------------------- | |
| QString Fmt::bytes(double b, int prec) | |
| { | |
| static const char *u[] = {"B", "KB", "MB", "GB", "TB", "PB"}; | |
| int i = 0; | |
| for (; qAbs(b) >= 1024 && i < 5; ++i) b /= 1024; | |
| return QString::number(b, 'f', i ? prec : 0) + " " + u[i]; | |
| } | |
| QString Fmt::count(double v) | |
| { | |
| const double a = qAbs(v); | |
| return a >= 1e9 ? QString::number(v / 1e9, 'f', 1) + "G" : a >= 1e6 ? QString::number(v / 1e6, 'f', 1) + "M" : a >= 1e4 ? QString::number(v / 1e3, 'f', 1) + "K" : QString::number(v, 'f', 0); | |
| } | |
| QString Fmt::duration(double sec) | |
| { | |
| qint64 s = qint64(sec), d = s / 86400, h = s % 86400 / 3600, m = s % 3600 / 60; | |
| return d ? QString("%1d %2h %3m").arg(d).arg(h).arg(m) : h ? QString("%1h %2m").arg(h).arg(m) : m ? QString("%1m %2s").arg(m).arg(s % 60) : QString("%1s").arg(s); | |
| } | |
| // ---- catalogues ------------------------------------------------------------- | |
| const QVector<Def> &ruleMetrics() | |
| { | |
| static const QVector<Def> d = {{"cpu", "CPU usage", "%"}, {"iowait", "CPU I/O wait", "%"}, {"load", "Load (1 min) per core", "%"}, | |
| {"mem", "Memory usage", "%"}, {"swap", "Swap usage", "%"}, {"disk", "Fullest disk usage", "%"}, {"diskio", "Busiest disk utilisation", "%"}, | |
| {"temp", "Hottest sensor", "°C"}, {"rx", "Network download", "MB/s"}, {"tx", "Network upload", "MB/s"}, {"failed", "Failed services", ""}, | |
| {"procs", "Process count", ""}, {"gpu", "GPU usage (max)", "%"}, {"gputemp", "GPU temperature (max)", "°C"}}; | |
| static const QVector<Def> noWait = d.mid(0, 1) + d.mid(2); | |
| return P::posixCpu ? d : noWait; | |
| } | |
| QString defLabel(const QVector<Def> &d, const QString &k, bool unit) | |
| { | |
| for (const Def &x : d) | |
| if (x.key == k) return unit ? x.unit : x.label; | |
| return unit ? QString() : k; | |
| } | |
| QString Rule::conditionText() const | |
| { | |
| const QString u = defLabel(ruleMetrics(), metric, true); | |
| return QString("%1 %2 %3%4").arg(defLabel(ruleMetrics(), metric), op).arg(value).arg(u.isEmpty() ? "" : " " + u) + (duration ? QString(" for %1 s").arg(duration) : ""); | |
| } | |
| QString Rule::actionText() const { return defLabel(P::ruleActions(), action) + (arg.isEmpty() ? "" : ": " + arg); } | |
| // ---- JSON: one field list per struct drives both saving and loading --------------------------- | |
| template <class V> static void each(Rule &x, V v) { F(id) F(metric) F(op) F(action) F(arg) F(severity) F(enabled) F(value) F(duration) F(cooldown) } | |
| template <class V> static void each(ServerConfig &x, V v) { F(id) F(name) F(group) F(host) F(user) F(keyFile) F(notes) F(port) F(interval) F(useSudo) F(enabled) } | |
| template <class V> static void each(AutopilotConfig &x, V v) | |
| { | |
| F(enabled) F(dryRun) F(restartFailed) F(diskCleanup) F(anomalyAlerts) F(maxRestartsPerHour) F(runawayMinutes) F(diskForecastDays) | |
| F(runawayCpu) F(diskCleanupAt) F(leakMemPressure) F(anomalyZ) F(runawayMode) F(leakMode) F(diskCleanupCommand) | |
| } | |
| template <class T> static QJsonObject save(const T &c) | |
| { | |
| QJsonObject o; | |
| each(const_cast<T &>(c), [&](const char *k, const auto &f) { o[k] = QJsonValue::fromVariant(QVariant::fromValue(f)); }); | |
| return o; | |
| } | |
| template <class T> static T load(const QJsonObject &o) | |
| { | |
| T c; | |
| each(c, [&](const char *k, auto &f) { if (o.contains(k)) f = o[k].toVariant().value<std::decay_t<decltype(f)>>(); }); | |
| return c; | |
| } | |
| static QString xorB64(const QString &s, bool enc) // light obfuscation of secrets (NOT encryption) | |
| { | |
| static const QByteArray k = "ServerMonitor/obfuscation/v1"; | |
| QByteArray b = enc ? s.toUtf8() : QByteArray::fromBase64(s.toLatin1()); | |
| for (int i = 0; i < b.size(); ++i) b[i] = char(b[i] ^ k[i % k.size()]); | |
| return enc ? QString(b.toBase64()) : QString::fromUtf8(b); | |
| } | |
| QJsonObject ServerConfig::toJson() const | |
| { | |
| QJsonObject o = save(*this); | |
| QJsonArray rs; | |
| for (const Rule &r : rules) rs << save(r); | |
| o["transport"] = int(transport), o["password"] = xorB64(password, true), o["rules"] = rs, o["autopilot"] = save(autopilot); | |
| return o; | |
| } | |
| ServerConfig ServerConfig::fromJson(const QJsonObject &o) | |
| { | |
| ServerConfig c = load<ServerConfig>(o); | |
| c.autopilot = load<AutopilotConfig>(o["autopilot"].toObject()); | |
| if (o.contains("rules")) c.rules.clear(); | |
| for (const auto &v : o["rules"].toArray()) c.rules << load<Rule>(v.toObject()); | |
| for (Rule &r : c.rules) | |
| if (r.id.isEmpty()) r.id = newId(); | |
| c.transport = o["transport"].toInt(1) == 0 ? Transport::Local : Transport::SSH; | |
| c.password = xorB64(o["password"].toString(), false); | |
| c.interval = qBound(1, c.interval, 300); | |
| if (c.id.isEmpty()) c.id = newId(); | |
| return c; | |
| } | |
| QVector<Rule> ServerConfig::defaultRules() | |
| { | |
| QVector<Rule> rs; | |
| for (auto [m, v, d, s] : {std::tuple{"cpu", 90, 60, "warning"}, {"mem", 90, 60, "warning"}, {"swap", 80, 120, "warning"}, | |
| {"disk", 90, 0, "critical"}, {"temp", 85, 30, "critical"}, {"load", 150, 120, "warning"}, {"failed", 0, 0, "warning"}}) { | |
| Rule r; | |
| r.id = newId(), r.metric = m, r.value = v, r.duration = d, r.severity = s; | |
| rs << r; | |
| } | |
| return rs; | |
| } | |
| QString ServerConfig::newId() { return QUuid::createUuid().toString(QUuid::WithoutBraces).left(12); } | |
| QString ServerConfig::address() const | |
| { | |
| if (transport == Transport::Local) return "this machine"; | |
| return (user.isEmpty() ? "" : user + "@") + host + (port != 22 ? QString(":%1").arg(port) : ""); | |
| } | |
| double Metrics::gpuMax(bool t) const | |
| { | |
| double v = 0; | |
| for (const GpuInfo &g : gpus) v = qMax(v, t ? g.temp : g.util); | |
| return v; | |
| } | |