Skip to content

Commit

Permalink
Preparing for POST request handling (image upload), adding CPU/memory…
Browse files Browse the repository at this point in the history
… statistics
  • Loading branch information
wberube committed Apr 5, 2024
1 parent 6d76a20 commit a712078
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions osd/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
#include <sys/types.h>

#ifdef __cplusplus
Expand Down
33 changes: 32 additions & 1 deletion osd/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++;
Expand Down
1 change: 1 addition & 0 deletions osd/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a712078

Please sign in to comment.