summaryrefslogtreecommitdiff
path: root/src/status.c
blob: 3422887fc4f0c0fc2919044543601103f4ded29f (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 int status_init(struct status* s) {
   21   int status = 0;
   22 
   23   s->memtotal    = 0;
   24   s->memusedmax  = 0;
   25   s->memusedavg  = 0;
   26   s->loadavg     = 0;
   27   s->loadmax     = 0;
   28   s->nettx       = 0;
   29   s->netrx       = 0;
   30   s->diskreadkb  = 0;
   31   s->diskwritekb = 0;
   32   s->count       = 0;
   33   s->uptime      = 0;
   34 
   35   FILE* fd = fopen("/tmp/cmon.status", "r");
   36   if(fd) {
   37     status += fscanf(fd, "memtotal:%ld\n",     &s->memtotal);
   38     status += fscanf(fd, "memusedmax:%ld\n",   &s->memusedmax);
   39     status += fscanf(fd, "memusedavg:%ld\n",   &s->memusedavg);
   40     status += fscanf(fd, "loadmax:%lf\n",      &s->loadmax);
   41     status += fscanf(fd, "loadavg:%lf\n",      &s->loadavg);
   42     status += fscanf(fd, "nprocs:%d\n",        &s->nprocs);
   43     status += fscanf(fd, "diskreadkb:%lld\n",  &s->diskreadkb);
   44     status += fscanf(fd, "diskwritekb:%lld\n", &s->diskwritekb);
   45     status += fscanf(fd, "nettx:%lld\n",       &s->nettx);
   46     status += fscanf(fd, "netrx:%lld\n",       &s->netrx);
   47     status += fscanf(fd, "uptime:%ld\n",       &s->uptime);
   48     status += fscanf(fd, "count:%ld\n",        &s->count);
   49     if(status != 12) {
   50       fprintf(stderr, "ERROR reading status file\n");
   51       return 0;
   52     }
   53     fclose(fd);
   54   }
   55   return 1;
   56 }
   57 
   58 int status_write(struct status* s) {
   59   FILE* fd = fopen("/tmp/cmon.status", "w");
   60 
   61   fprintf(fd, "memtotal:%ld\n",     s->memtotal);
   62   fprintf(fd, "memusedmax:%ld\n",   s->memusedmax);
   63   fprintf(fd, "memusedavg:%ld\n",   s->memusedavg);
   64   fprintf(fd, "loadmax:%lf\n",      s->loadmax);
   65   fprintf(fd, "loadavg:%lf\n",      s->loadavg);
   66   fprintf(fd, "nprocs:%d\n",        s->nprocs);
   67   fprintf(fd, "diskreadkb:%lld\n",  s->diskreadkb);
   68   fprintf(fd, "diskwritekb:%lld\n", s->diskwritekb);
   69   fprintf(fd, "nettx:%lld\n",       s->nettx);
   70   fprintf(fd, "netrx:%lld\n",       s->netrx);
   71   fprintf(fd, "uptime:%ld\n",       s->uptime);
   72   fprintf(fd, "count:%ld\n",        s->count);
   73 
   74   fclose(fd);
   75   return 1;
   76 }

Generated by cgit