Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
89 lines (75 loc) · 3.5 KB
/
Copy pathmain.cpp
File metadata and controls
89 lines (75 loc) · 3.5 KB
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
// qiftop-ndjson — a ~100-line example consumer of libqiftop that turns the
// qiftop-agent's live DBus stream into NDJSON on stdout. One JSON object
// (a full interface snapshot) per line, so it pipes straight into jq,
// a log shipper, or any line-oriented tool:
//
// qiftop-ndjson | jq -c '.interfaces[] | select(.rxRate > 1e6)'
//
// Flags:
// --session talk to the agent on the SESSION bus (dev) instead of system
// --once emit a single snapshot (batch mode) and exit
//
// This is intentionally minimal — it only wires the DBus *interface* monitor
// through an InterfaceAggregator. The same pattern (DBus*Monitor ->
// aggregator -> serialise) extends to ConnectionAggregator for per-flow data,
// and is the seed of the planned Prometheus exporter / alerting daemon: none
// of them need Qt Widgets, only this Core-only library.
#include <qiftop/aggregate/InterfaceAggregator.h>
#include <qiftop/backend/dbus/DBusNetworkMonitor.h>
#include <qiftop/dbus/Types.h>
#include <QCoreApplication>
#include <QDateTime>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <cstdio>
using qiftop::aggregate::InterfaceAggregator;
using qiftop::backend::dbus_client::DBusNetworkMonitor;
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
// QList<InterfaceStats> crosses the monitor's signal/slot edge; register
// it so queued (cross-thread) delivery works regardless of backend.
qRegisterMetaType<InterfaceStats>();
qRegisterMetaType<QList<InterfaceStats>>();
// Register the DBus wire DTO metatypes with QtDBus, or the agent's
// StatsChanged reply fails to unmarshal ("type
// QList<qiftop::dbus::InterfaceStatsDto> is not registered with QtDBus").
qiftop::dbus::registerTypes();
const QStringList args = app.arguments();
const bool sessionBus = args.contains(QStringLiteral("--session"));
const bool once = args.contains(QStringLiteral("--once"));
auto *monitor = new DBusNetworkMonitor(sessionBus, &app);
auto *agg = new InterfaceAggregator(&app);
QObject::connect(monitor, &NetworkMonitor::statsUpdated,
agg, &InterfaceAggregator::updateStats);
const auto emitSnapshot = [agg, once, &app] {
QJsonArray ifaces;
for (const InterfaceAggregator::Row &r : agg->rows()) {
ifaces.append(QJsonObject{
{QStringLiteral("iface"), r.current.name},
{QStringLiteral("up"), r.current.isUp},
{QStringLiteral("rxRate"), r.rxRate},
{QStringLiteral("txRate"), r.txRate},
{QStringLiteral("rxBytes"), static_cast<double>(r.current.rxBytes)},
{QStringLiteral("txBytes"), static_cast<double>(r.current.txBytes)},
});
}
const QJsonObject line{
{QStringLiteral("t"), QDateTime::currentMSecsSinceEpoch()},
{QStringLiteral("interfaces"), ifaces},
};
const QByteArray json = QJsonDocument(line).toJson(QJsonDocument::Compact);
std::fwrite(json.constData(), 1, static_cast<size_t>(json.size()), stdout);
std::fputc('\n', stdout);
std::fflush(stdout);
if (once)
app.quit();
};
QObject::connect(agg, &InterfaceAggregator::didReset, &app, emitSnapshot);
QObject::connect(agg, &InterfaceAggregator::rowsChanged, &app,
[emitSnapshot](int, int) { emitSnapshot(); });
monitor->setPollIntervalMs(1000);
monitor->start();
return app.exec();
}