summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 41b300f45b4f161de1f141c3158da42c6511ea08 (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.memusedmax < (minfo.used))
   42     status.memusedmax = minfo.used;
   43   status.memusedavg = (((status.count - 1) * status.memusedavg) + minfo.used) / status.count;
   44   status.memtotal = minfo.total;
   45 
   46   // Calculate load max and incremental load average
   47   double load = cpuinfo_load1m();
   48   if(status.loadmax < load)
   49     status.loadmax = load;
   50   status.loadavg = (((status.count - 1) * status.loadavg) + load) / status.count;
   51 
   52   // Store number of processors to make load values more useful
   53   status.nprocs = get_nprocs();
   54 
   55   // Store number of processors to make load values more useful
   56   char* netdev = "wlan0";
   57   if(getenv("CMON_NETDEV") != NULL)
   58     netdev = getenv("CMON_NETDEV");
   59   status.nettx = net_x_bytes(netdev, 't');
   60   status.netrx = net_x_bytes(netdev, 'r');
   61 
   62   // Store uptime
   63   status.uptime = proc_uptime();
   64 
   65   // Cleanup!
   66   status_write(&status);
   67   return 0;
   68 }

Generated by cgit