diff options
author | Aaron Ball <nullspoon@oper.io> | 2022-06-22 10:49:51 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2022-06-22 10:49:51 -0600 |
commit | 9738d0b23605c86d7ad69735ba17695cdf19532c (patch) | |
tree | 7bddcc2be92eea7321d30b58137320c7d295634e /src | |
parent | 2f875b8c71fcacea805bba947b3ecf9524279bbf (diff) | |
download | cmon-9738d0b23605c86d7ad69735ba17695cdf19532c.tar.gz cmon-9738d0b23605c86d7ad69735ba17695cdf19532c.tar.xz |
Implement disk stats
This adds the `diskreadkb` and `diskwritekb` values to the status file
and updates relevant main code to pull this data.
Note that the default disk to pull metrics for is `sda`, but this can be
overridden at runtime with the environment variable `CMON_DISKDEV`.
Another useful value might be `nvme0n1`. For available options, check
`/sys/block/`.
Diffstat (limited to 'src')
-rw-r--r-- | src/diskinfo.c | 28 | ||||
-rw-r--r-- | src/diskinfo.h | 4 | ||||
-rw-r--r-- | src/main.c | 7 | ||||
-rw-r--r-- | src/status.c | 66 | ||||
-rw-r--r-- | src/status.h | 2 |
5 files changed, 77 insertions, 30 deletions
diff --git a/src/diskinfo.c b/src/diskinfo.c new file mode 100644 index 0000000..815f7df --- /dev/null +++ b/src/diskinfo.c @@ -0,0 +1,28 @@ +#include "diskinfo.h" + +int disk_io_kb(char* disk, + unsigned long long* read, + unsigned long long* write) { + char syspath[512] = {'\0'}; + unsigned long long stats[8] = {-1}; + FILE* fd = NULL; + + sprintf(syspath, "/sys/block/%s/stat", disk); + + fd = fopen(syspath, "r"); + if(!fd) + return -1; + + // read io read merge read sectors read ticks + // write io write merge write sectors write ticks + fscanf(fd, "%lld %lld %lld %lld %lld %lld %lld %lld ", + &stats[0], &stats[1], &stats[2], &stats[3], + &stats[4], &stats[5], &stats[6], &stats[7]); + + fclose(fd); + + *read = stats[2] / 2; + *write = stats[6] / 2; + + return 1; +} diff --git a/src/diskinfo.h b/src/diskinfo.h new file mode 100644 index 0000000..9a4265f --- /dev/null +++ b/src/diskinfo.h @@ -0,0 +1,4 @@ +#include <stdio.h> +#include <stdlib.h> + +int disk_io_kb(char*, unsigned long long*, unsigned long long*); @@ -22,6 +22,7 @@ #include "meminfo.h" #include "netinfo.h" #include "cpuinfo.h" +#include "diskinfo.h" #include "proc.h" int main(int argc, char* argv[]) { @@ -52,6 +53,12 @@ int main(int argc, char* argv[]) { // Store number of processors to make load values more useful status.nprocs = get_nprocs(); + // Store disk io metrics + char* diskdev = "sda"; + if(getenv("CMON_DISKDEV") != NULL) + diskdev = getenv("CMON_DISKDEV"); + disk_io_kb(diskdev, &status.diskreadkb, &status.diskwritekb); + // Store number of processors to make load values more useful char* netdev = "wlan0"; if(getenv("CMON_NETDEV") != NULL) diff --git a/src/status.c b/src/status.c index f7148f9..3422887 100644 --- a/src/status.c +++ b/src/status.c @@ -20,29 +20,33 @@ int status_init(struct status* s) { int status = 0; - s->memtotal = 0; - s->memusedmax = 0; - s->memusedavg = 0; - s->loadavg = 0; - s->loadmax = 0; - s->nettx = 0; - s->netrx = 0; - s->count = 0; - s->uptime = 0; + 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; 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, "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 != 10) { + 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; } @@ -54,16 +58,18 @@ int status_init(struct status* s) { int status_write(struct status* s) { FILE* fd = fopen("/tmp/cmon.status", "w"); - fprintf(fd, "memtotal:%ld\n", s->memtotal); - fprintf(fd, "memusedmax:%ld\n", s->memusedmax); - fprintf(fd, "memusedavg:%ld\n", s->memusedavg); - fprintf(fd, "loadmax:%lf\n", s->loadmax); - fprintf(fd, "loadavg:%lf\n", s->loadavg); - fprintf(fd, "nprocs:%d\n", s->nprocs); - fprintf(fd, "nettx:%lld\n", s->nettx); - fprintf(fd, "netrx:%lld\n", s->netrx); - fprintf(fd, "uptime:%ld\n", s->uptime); - fprintf(fd, "count:%ld\n", s->count); + fprintf(fd, "memtotal:%ld\n", s->memtotal); + fprintf(fd, "memusedmax:%ld\n", s->memusedmax); + fprintf(fd, "memusedavg:%ld\n", s->memusedavg); + fprintf(fd, "loadmax:%lf\n", s->loadmax); + fprintf(fd, "loadavg:%lf\n", s->loadavg); + fprintf(fd, "nprocs:%d\n", s->nprocs); + fprintf(fd, "diskreadkb:%lld\n", s->diskreadkb); + fprintf(fd, "diskwritekb:%lld\n", s->diskwritekb); + fprintf(fd, "nettx:%lld\n", s->nettx); + fprintf(fd, "netrx:%lld\n", s->netrx); + fprintf(fd, "uptime:%ld\n", s->uptime); + fprintf(fd, "count:%ld\n", s->count); fclose(fd); return 1; diff --git a/src/status.h b/src/status.h index 89d0102..af3b6c8 100644 --- a/src/status.h +++ b/src/status.h @@ -27,6 +27,8 @@ struct status { int nprocs; unsigned long long nettx; unsigned long long netrx; + unsigned long long diskreadkb; + unsigned long long diskwritekb; long uptime; long count; }; |