summaryrefslogtreecommitdiff
path: root/src/status.c
blob: 00b9047f858c1622ebc5dade6f1d5e866d010776 (plain)
    1 /**
    2  * Cmon (c'mon) is a simple system resource monitor
    3  * Copyright (C) 2022  Aaron Ball <nullspoon@oper.io>
    4  * 
    5  * This program is free software: you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation, either version 3 of the License, or
    8  * (at your option) any later version.
    9  * 
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  * 
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
   17  */
   18 #include "status.h"
   19 
   20 struct status* status_init() {
   21   struct status* s = malloc(sizeof(struct status));
   22   int status = 0;
   23 
   24   s->memmax  = 0;
   25   s->memavg  = 0;
   26   s->loadavg = 0;
   27   s->loadmax = 0;
   28   s->nettx   = 0;
   29   s->netrx   = 0;
   30   s->count   = 0;
   31   s->uptime  = 0;
   32 
   33   FILE* fd = fopen("/tmp/cmon.status", "r");
   34   if(fd) {
   35     status += fscanf(fd, "memmax:%ld\n",  &s->memmax);
   36     status += fscanf(fd, "memavg:%ld\n",  &s->memavg);
   37     status += fscanf(fd, "loadmax:%lf\n", &s->loadmax);
   38     status += fscanf(fd, "loadavg:%lf\n", &s->loadavg);
   39     status += fscanf(fd, "nprocs:%d\n",   &s->nprocs);
   40     status += fscanf(fd, "nettx:%lld\n",  &s->nettx);
   41     status += fscanf(fd, "netrx:%lld\n",  &s->netrx);
   42     status += fscanf(fd, "uptime:%ld\n",  &s->uptime);
   43     status += fscanf(fd, "count:%ld\n",   &s->count);
   44     if(status != 9) {
   45       fprintf(stderr, "ERROR reading status file\n");
   46       return NULL;
   47     }
   48     fclose(fd);
   49   }
   50   return s;
   51 }
   52 
   53 int status_write(struct status* s) {
   54   FILE* fd = fopen("/tmp/cmon.status", "w");
   55 
   56   fprintf(fd, "memmax:%ld\n",  s->memmax);
   57   fprintf(fd, "memavg:%ld\n",  s->memavg);
   58   fprintf(fd, "loadmax:%lf\n", s->loadmax);
   59   fprintf(fd, "loadavg:%lf\n", s->loadavg);
   60   fprintf(fd, "nprocs:%d\n",   s->nprocs);
   61   fprintf(fd, "nettx:%lld\n",  s->nettx);
   62   fprintf(fd, "netrx:%lld\n",  s->netrx);
   63   fprintf(fd, "uptime:%ld\n",  s->uptime);
   64   fprintf(fd, "count:%ld\n",   s->count);
   65 
   66   fclose(fd);
   67   return 1;
   68 }
   69 
   70 void status_free(struct status* s) {
   71   free(s);
   72 }

Generated by cgit