From a7120784acd93382199be4fc37d8c15076181336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20B=C3=A9rub=C3=A9?= Date: Fri, 5 Apr 2024 08:26:08 -0400 Subject: [PATCH] Preparing for POST request handling (image upload), adding CPU/memory statistics --- osd/common.h | 1 + osd/main.c | 33 ++++++++++++++++++++++++++++++++- osd/net.h | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/osd/common.h b/osd/common.h index 8923eea..7f1d836 100644 --- a/osd/common.h +++ b/osd/common.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #ifdef __cplusplus diff --git a/osd/main.c b/osd/main.c index f9024c1..7917c15 100644 --- a/osd/main.c +++ b/osd/main.c @@ -10,7 +10,7 @@ struct osd *osds; static void fill(char* str) { - unsigned int rxb_l, txb_l; + unsigned int rxb_l, txb_l, cpu_l[6]; char out[80] = ""; char param = 0; int i = 0, j = 0; @@ -47,6 +47,37 @@ static void fill(char* str) freeifaddrs(ifaddr); } + else if (str[i + 1] == 'C') + { + i++; + char tmp[6]; + unsigned int cpu[6]; + FILE *stat = fopen("/proc/stat", "r"); + fscanf(stat, "%s %u %u %u %u %u %u", + tmp, &cpu[0], &cpu[1], &cpu[2], &cpu[3], &cpu[4], &cpu[5]); + fclose(stat); + + char c[5]; + char avg = 100 - (cpu[3] - cpu_l[3]) / sysconf(_SC_NPROCESSORS_ONLN); + sprintf(c, "%d%%", avg); + strcat(out, c); + j += strlen(c); + for (int i = 0; i < sizeof(cpu) / sizeof(cpu[0]); i++) + cpu_l[i] = cpu[i]; + } + else if (str[i + 1] == 'M') + { + i++; + struct sysinfo si; + sysinfo(&si); + + char m[16]; + short used = (si.freeram + si.bufferram) / 1024 / 1024; + short total = si.totalram / 1024 / 1024; + sprintf(m, "%d/%dMB", used, total); + strcat(out, m); + j += strlen(m); + } else if (str[i + 1] == 'T') { i++; diff --git a/osd/net.h b/osd/net.h index 82c3f34..afd1492 100644 --- a/osd/net.h +++ b/osd/net.h @@ -15,6 +15,7 @@ extern "C" *query, // "a=1&b=2" things after '?' *prot, // "HTTP/1.1" *payload; // for POST + extern int payload_size; void serve_forever(); char *extract_key(char **pair);