summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 42d2b5ece9f62279572c1c37afb9e85d9837886a (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 <stdio.h>
   19 #include <stdlib.h>
   20 
   21 #include "status.h"
   22 #include "meminfo.h"
   23 #include "netinfo.h"
   24 #include "cpuinfo.h"
   25 #include "proc.h"
   26 
   27 int main(int argc, char* argv[]) {
   28   struct meminfo minfo; // Struct for relevant data from /proc/meminfo
   29   struct status status; // Struct for tracking measurement statuses
   30 
   31   if(! status_init(&status))
   32     return 1;
   33   status.count += 1;
   34 
   35   if(! meminfo_init(&minfo)) {
   36     fprintf(stderr, "ERROR: Could not read meminfo\n");
   37     return 1;
   38   }
   39 
   40   // Calculate max memory usage and average memory usage
   41   if(status.memmax < (minfo.used))
   42     status.memmax = minfo.used;
   43   status.memavg = (((status.count - 1) * status.memavg) + minfo.used) / status.count;
   44 
   45   // Calculate load max and incremental load average
   46   double load = cpuinfo_load1m();
   47   if(status.loadmax < load)
   48     status.loadmax = load;
   49   status.loadavg = (((status.count - 1) * status.loadavg) + load) / status.count;
   50 
   51   // Store number of processors to make load values more useful
   52   status.nprocs = get_nprocs();
   53 
   54   // Store number of processors to make load values more useful
   55   char* netdev = "wlan0";
   56   if(getenv("CMON_NETDEV") != NULL)
   57     netdev = getenv("CMON_NETDEV");
   58   status.nettx = net_x_bytes(netdev, 't');
   59   status.netrx = net_x_bytes(netdev, 'r');
   60 
   61   // Store uptime
   62   status.uptime = proc_uptime();
   63 
   64   // Cleanup!
   65   status_write(&status);
   66   return 0;
   67 }

Generated by cgit