84 lines
3.0 KiB
C++
84 lines
3.0 KiB
C++
#include <sys/statvfs.h>
|
|
|
|
#include <chrono>
|
|
#include <format>
|
|
#include <log.hpp>
|
|
#include <string>
|
|
#include <sys.hpp>
|
|
#include <utils.hpp>
|
|
|
|
int getVal(std::string_view target, std::string_view content) {
|
|
int result = -1;
|
|
std::size_t start = content.find(target);
|
|
if (start != std::string::npos) {
|
|
int begin = start + target.length();
|
|
std::size_t end = content.find("kB", start);
|
|
result = std::strtol(content.substr(begin, end - begin).data(), NULL, 10);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void sys::getTimeDate(TimeDate* td) {
|
|
auto now = std::chrono::system_clock::now();
|
|
auto local = std::chrono::current_zone()->to_local(now);
|
|
std::chrono::year_month_day ymd{floor<std::chrono::days>(local)};
|
|
std::chrono::weekday wd{floor<std::chrono::days>(local)};
|
|
td->time = std::format("{:%H:%M:%S}", floor<std::chrono::seconds>(local));
|
|
td->date = std::format("{}, {} {}", wdays[wd.c_encoding()], ymd.day(), months[(unsigned)ymd.month() - 1]);
|
|
}
|
|
|
|
void sys::getRam(Memory* mem) {
|
|
static char buf[128]; // file is longer but we only need first 128 chars
|
|
readFile("/proc/meminfo", buf, sizeof(buf));
|
|
mem->free = getVal("MemAvailable:", std::string_view(buf));
|
|
mem->total = getVal("MemTotal:", std::string_view(buf));
|
|
mem->used = mem->total - mem->free;
|
|
}
|
|
|
|
void sys::getDisk(Disk* disk) {
|
|
struct statvfs diskData;
|
|
statvfs(disk->mountpoint.c_str(), &diskData);
|
|
disk->usage.free = diskData.f_bfree * diskData.f_frsize;
|
|
disk->usage.used = (diskData.f_blocks - diskData.f_bfree) * diskData.f_frsize;
|
|
disk->usage.total = diskData.f_blocks * diskData.f_frsize;
|
|
}
|
|
|
|
void sys::getNetwork(Network* net, const std::string& device) {
|
|
struct _net {
|
|
std::chrono::time_point<std::chrono::steady_clock> ts;
|
|
uint64_t rx = 0;
|
|
uint64_t tx = 0;
|
|
};
|
|
|
|
static char buf[24];
|
|
static _net prev;
|
|
_net current;
|
|
|
|
current.ts = std::chrono::steady_clock::now();
|
|
fastReadNum("/sys/class/net/" + device + "/statistics/rx_bytes", current.rx);
|
|
fastReadNum("/sys/class/net/" + device + "/statistics/tx_bytes", current.tx);
|
|
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, const std::string& platform) {
|
|
// we dont really need getFirstDir since its a static folder, but its useful for try/catch
|
|
try {
|
|
s->hwmon = getFirstDir("/sys/devices/platform/" + platform + "/hwmon");
|
|
} catch (...) {
|
|
log(LOG::ERR, "Can't open sys hwmon. Module loaded? Kernel params added?");
|
|
return;
|
|
}
|
|
}
|
|
|
|
void sys::updateSensors(sysSensors* s) {
|
|
if (s->hwmon.length() < 1)
|
|
return;
|
|
static char buf[8];
|
|
fastReadNum(s->hwmon + "/fan2_input", s->fan_speed[0]);
|
|
fastReadNum(s->hwmon + "/fan7_input", s->fan_speed[1]);
|
|
fastReadNum(s->hwmon + "/temp5_input", s->t_sensor) / 1000;
|
|
}
|