/** * Cmon (c'mon) is a simple system resource monitor * Copyright (C) 2022 Aaron Ball * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "status.h" int status_init(struct status* s) { int status = 0; char buf[128]; 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) { 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); return 1; } 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, "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; }