|
/* retoor <retoor@molodetz.nl> */
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/utsname.h>
|
|
#include "system.h"
|
|
|
|
int system_info_init(SystemInfo *info) {
|
|
if (!info) return -1;
|
|
memset(info, 0, sizeof(SystemInfo));
|
|
return system_info_update(info);
|
|
}
|
|
|
|
int system_info_update(SystemInfo *info) {
|
|
if (!info) return -1;
|
|
|
|
struct utsname uts;
|
|
if (uname(&uts) == 0) {
|
|
strncpy(info->hostname, uts.nodename, MAX_HOSTNAME_LEN - 1);
|
|
info->hostname[MAX_HOSTNAME_LEN - 1] = '\0';
|
|
strncpy(info->kernel, uts.release, MAX_KERNEL_LEN - 1);
|
|
info->kernel[MAX_KERNEL_LEN - 1] = '\0';
|
|
strncpy(info->arch, uts.machine, MAX_ARCH_LEN - 1);
|
|
info->arch[MAX_ARCH_LEN - 1] = '\0';
|
|
}
|
|
|
|
FILE *fp = fopen("/etc/os-release", "r");
|
|
if (fp) {
|
|
char line[512];
|
|
while (fgets(line, sizeof(line), fp)) {
|
|
if (strncmp(line, "PRETTY_NAME=", 12) == 0) {
|
|
char *start = strchr(line, '"');
|
|
if (start) {
|
|
start++;
|
|
char *end = strchr(start, '"');
|
|
if (end) {
|
|
*end = '\0';
|
|
strncpy(info->os_name, start, MAX_OS_LEN - 1);
|
|
info->os_name[MAX_OS_LEN - 1] = '\0';
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
fclose(fp);
|
|
}
|
|
|
|
fp = fopen("/proc/uptime", "r");
|
|
if (fp) {
|
|
double uptime;
|
|
if (fscanf(fp, "%lf", &uptime) == 1) {
|
|
info->uptime_seconds = (long)uptime;
|
|
info->uptime_days = info->uptime_seconds / 86400;
|
|
info->uptime_hours = (info->uptime_seconds % 86400) / 3600;
|
|
info->uptime_minutes = (info->uptime_seconds % 3600) / 60;
|
|
}
|
|
fclose(fp);
|
|
}
|
|
|
|
fp = fopen("/proc/loadavg", "r");
|
|
if (fp) {
|
|
if (fscanf(fp, "%lf %lf %lf", &info->load_1, &info->load_5, &info->load_15) != 3) {
|
|
info->load_1 = info->load_5 = info->load_15 = 0.0;
|
|
}
|
|
fclose(fp);
|
|
}
|
|
|
|
return 0;
|
|
}
|