From 5e42c45e28ac6a678efd504acec05c6633daf49a Mon Sep 17 00:00:00 2001 From: Drath Vader Date: Fri, 24 Oct 2025 14:04:27 +0200 Subject: [PATCH] add network monitoring --- include/sys.hpp | 9 ++++++++- src/sys.cpp | 26 ++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/include/sys.hpp b/include/sys.hpp index d32f579..c93128a 100644 --- a/include/sys.hpp +++ b/include/sys.hpp @@ -1,6 +1,7 @@ #ifndef SYS_HPP #define SYS_HPP +#include #include #include @@ -30,12 +31,18 @@ struct TimeDate { char date[13]; }; +struct Network { + uint32_t download = 0; + uint32_t upload = 0; +}; + void getTimeDate(TimeDate* td); void getDisk(Memory* disk, std::string mount); void getRam(Memory* mem); +void getNetwork(Network* net, std::string_view device); void detectHwmon(sysSensors* s, std::string platform); void updateSensors(sysSensors* s); } // namespace sys -#endif \ No newline at end of file +#endif diff --git a/src/sys.cpp b/src/sys.cpp index 4b110fe..ec889a1 100644 --- a/src/sys.cpp +++ b/src/sys.cpp @@ -1,3 +1,5 @@ +#include +#include #include 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 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(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)); -} \ No newline at end of file +}