diff options
author | Aaron Ball <nullspoon@oper.io> | 2022-06-22 16:52:11 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2022-06-22 16:52:11 -0600 |
commit | 116319dec4f522388a21b218a1251ffba8289c6c (patch) | |
tree | 494c7653baf8ac59fb4890ac58818577a0766953 | |
parent | 304800656ccd61c400ab62ce8b3a21f26be11b4e (diff) | |
parent | cac7ed0e3f8ba1cca23fb24071c583cd092c0ec6 (diff) | |
download | cmon-116319dec4f522388a21b218a1251ffba8289c6c.tar.gz cmon-116319dec4f522388a21b218a1251ffba8289c6c.tar.xz |
-rw-r--r-- | src/status.c | 81 | ||||
-rw-r--r-- | src/status.h | 1 |
2 files changed, 54 insertions, 28 deletions
diff --git a/src/status.c b/src/status.c index 3422887..5a02a2a 100644 --- a/src/status.c +++ b/src/status.c @@ -19,39 +19,64 @@ int status_init(struct status* s) { int status = 0; + char buf[128]; - s->memtotal = 0; - s->memusedmax = 0; - s->memusedavg = 0; - s->loadavg = 0; - s->loadmax = 0; - s->nettx = 0; - s->netrx = 0; - s->diskreadkb = 0; - s->diskwritekb = 0; - s->count = 0; - s->uptime = 0; + s->memtotal = -1; + s->memusedmax = -1; + s->memusedavg = -1; + s->loadavg = -1; + s->loadmax = -1; + s->nettx = -1; + s->netrx = -1; + s->diskreadkb = -1; + s->diskwritekb = -1; + s->nprocs = -1; + s->uptime = -1; + s->count = -1; FILE* fd = fopen("/tmp/cmon.status", "r"); - if(fd) { - status += fscanf(fd, "memtotal:%ld\n", &s->memtotal); - status += fscanf(fd, "memusedmax:%ld\n", &s->memusedmax); - status += fscanf(fd, "memusedavg:%ld\n", &s->memusedavg); - status += fscanf(fd, "loadmax:%lf\n", &s->loadmax); - status += fscanf(fd, "loadavg:%lf\n", &s->loadavg); - status += fscanf(fd, "nprocs:%d\n", &s->nprocs); - status += fscanf(fd, "diskreadkb:%lld\n", &s->diskreadkb); - status += fscanf(fd, "diskwritekb:%lld\n", &s->diskwritekb); - status += fscanf(fd, "nettx:%lld\n", &s->nettx); - status += fscanf(fd, "netrx:%lld\n", &s->netrx); - status += fscanf(fd, "uptime:%ld\n", &s->uptime); - status += fscanf(fd, "count:%ld\n", &s->count); - if(status != 12) { - fprintf(stderr, "ERROR reading status file\n"); - return 0; + if(!fd) { + fprintf(stderr, "Could not open status file\n"); + return 0; + } + + while(fgets(buf, 128, fd) != NULL) { + if(s->memtotal == -1 && strncmp(buf, "memtotal:", 9) == 0) { + status += sscanf(buf, "memtotal:%ld\n", &s->memtotal); + } else if(s->memusedmax == -1 && strncmp(buf, "memusedmax:", 11) == 0) { + status += sscanf(buf, "memusedmax:%ld\n", &s->memusedmax); + } else if(s->memusedavg == -1 && strncmp(buf, "memusedavg:", 11) == 0) { + status += sscanf(buf, "memusedavg:%ld\n", &s->memusedavg); + + } else if(s->loadmax == -1 && strncmp(buf, "loadmax:", 8) == 0) { + status += sscanf(buf, "loadmax:%lf\n", &s->loadmax); + } else if(s->loadavg == -1 && strncmp(buf, "loadavg:", 8) == 0) { + status += sscanf(buf, "loadavg:%lf\n", &s->loadavg); + + // Disk + } else if(s->diskreadkb == -1 && strncmp(buf, "diskreadkb:", 11) == 0) { + status += sscanf(buf, "diskreadkb:%lld\n", &s->diskreadkb); + } else if(s->diskwritekb == -1 && strncmp(buf, "diskwritekb:", 12) == 0) { + status += sscanf(buf, "diskwritekb:%lld\n", &s->diskwritekb); + + // Network + } else if(s->nettx == -1 && strncmp(buf, "nettx:", 6) == 0) { + status += sscanf(buf, "nettx:%lld\n", &s->nettx); + } else if(s->netrx == -1 && strncmp(buf, "netrx:", 6) == 0) { + status += sscanf(buf, "netrx:%lld\n", &s->netrx); + + // Misc + } else if(s->nprocs == -1 && strncmp(buf, "nprocs:", 7) == 0) { + status += sscanf(buf, "nprocs:%d\n", &s->nprocs); + } else if(s->uptime == -1 && strncmp(buf, "uptime:", 7) == 0) { + status += sscanf(buf, "uptime:%ld\n", &s->uptime); + } else if(s->count == -1 && strncmp(buf, "count:", 6) == 0) { + status += sscanf(buf, "count:%ld\n", &s->count); + } else { + fprintf(stderr, "Unknown status field: %s", buf); } - fclose(fd); } + fclose(fd); return 1; } diff --git a/src/status.h b/src/status.h index af3b6c8..28de4eb 100644 --- a/src/status.h +++ b/src/status.h @@ -17,6 +17,7 @@ */ #include <stdio.h> #include <stdlib.h> +#include <string.h> struct status { long memtotal; |