Files
linuxSmartie/src/sys.c
2026-05-23 18:59:29 +00:00

91 lines
3.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/statvfs.h>
#include <time.h>
#include "log.h"
#include "sys.h"
#include "utils.h"
void sys_get_time(struct time_date_t *td) {
const char months[][4] = {"sty", "lut", "mar", "kwi", "maj", "cze", "lip", "sie", "wrz", "paz", "lis", "gru"};
const char wdays[][4] = {"Nie", "Pon", "Wto", "Sro", "Czw", "Pia", "Sob"};
const time_t t = time(NULL);
const struct tm *tm = localtime(&t);
snprintf(td->time, sizeof(td->time), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
snprintf(td->date, sizeof(td->date), "%s, %02d %s", wdays[tm->tm_wday], tm->tm_mday, months[tm->tm_mon]);
}
void sys_get_load_avg(double *load_avg) {
getloadavg(load_avg, 3);
}
void sys_get_ram(struct memory_t *ram) {
char buf[64];
memset(ram, 0, sizeof(struct memory_t)); // zero out all values so we dont break out of the loop early
FILE *f = fopen("/proc/meminfo", "r");
if (f == NULL)
return;
while (fgets(buf, sizeof(buf), f)) {
sscanf(buf, "MemTotal: %lu kB", &ram->total); // NOLINT we live dangerously
sscanf(buf, "MemAvailable: %lu kB", &ram->free); // NOLINT (its not really that dangerous)
if (ram->total && ram->free)
break;
}
fclose(f);
ram->used = ram->total - ram->free;
}
void sys_get_disk(struct disk_t *disk) {
struct statvfs diskData;
statvfs(disk->mountpoint, &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_get_network(struct network_t *network) {
struct _net_t {
struct timespec ts;
uint64_t rx;
uint64_t tx;
};
static char path[128];
static char buf[32];
static struct _net_t prev;
struct _net_t current = {};
snprintf(path, sizeof(path), "/sys/class/net/%s/statistics/", network->device);
clock_gettime(CLOCK_MONOTONIC, &current.ts);
fast_read_num_u64(path, "rx_bytes", buf, &current.rx, 1);
fast_read_num_u64(path, "tx_bytes", buf, &current.tx, 1);
const double diff_s = (double)timespec_diff_us(&current.ts, &prev.ts) / 1000000.0;
network->download = (uint32_t)((double)(current.rx - prev.rx) / diff_s);
network->upload = (uint32_t)((double)(current.tx - prev.tx) / diff_s);
prev = current;
}
void sys_detect(struct syssensors_t *s, const char *platform) {
snprintf(s->hwmon, sizeof(s->hwmon), "/sys/devices/platform/%s/hwmon", platform);
const int ret = get_hwmon_dir(s->hwmon, sizeof(s->hwmon));
if (ret != 0) {
s->hwmon[0] = '\0';
applog(LOG_WRN, "Can't open system hwmon. Kernel params added? Module loaded? See README");
return;
}
applog(LOG_INF, "Detected system sensors in %s", s->hwmon);
}
void sys_update_sensors(struct syssensors_t *s) {
if (!s->hwmon[0])
return;
static char buf[8];
// fast_read_num_u16(s->hwmon, "/fan2_input", buf, &s->fan_speed[0], 1); // cpu fan, aka aio pump now
// fast_read_num_u16(s->hwmon, "/fan7_input", buf, &s->fan_speed[1], 1); // cpu opt
fast_read_num_i16(s->hwmon, "/temp5_input", buf, &s->t_sensor, 1000);
}