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

Generated by cgit