add network monitoring

This commit is contained in:
Drath Vader
2025-10-24 14:04:27 +02:00
parent 6e4e23e097
commit 5e42c45e28
2 changed files with 32 additions and 3 deletions

View File

@@ -1,3 +1,5 @@
#include <chrono>
#include <string>
#include <sys.hpp>
void sys::getTimeDate(TimeDate* td) {
@@ -22,12 +24,32 @@ void sys::getDisk(Memory* disk, std::string mount) {
disk->total = diskData.f_blocks * diskData.f_frsize;
}
void sys::getNetwork(Network* net, std::string_view device) {
struct _net {
std::chrono::time_point<std::chrono::steady_clock> ts;
uint64_t rx = 0;
uint64_t tx = 0;
};
static _net prev;
_net current;
current.ts = std::chrono::steady_clock::now();
current.rx = std::stoull(readFile("/sys/class/net/" + std::string(device) + "/statistics/rx_bytes", 32));
current.tx = std::stoull(readFile("/sys/class/net/" + std::string(device) + "/statistics/tx_bytes", 32));
float ts_diff = std::chrono::duration_cast<std::chrono::microseconds>(current.ts - prev.ts).count() / 1000000.0f;
net->download = (current.rx - prev.rx) / ts_diff;
net->upload = (current.tx - prev.tx) / ts_diff;
prev = current;
}
void sys::detectHwmon(sysSensors* s, std::string platform) {
s->hwmon = getFirstDir("/sys/devices/platform/" + platform + "/hwmon"); // we hardcode the chip path, should be sane
s->hwmon = getFirstDir("/sys/devices/platform/" + platform + "/hwmon");
}
void sys::updateSensors(sysSensors* s) {
s->fan_speed[0] = std::stol(readFile(s->hwmon + "/fan2_input", 4));
s->fan_speed[1] = std::stol(readFile(s->hwmon + "/fan7_input", 4));;
s->t_sensor = std::stol(readFile(s->hwmon + "/temp5_input", 2));
}
}