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 char buf[128];
23
24 s->memtotal = -1;
25 s->memusedmax = -1;
26 s->memusedavg = -1;
27 s->loadavg = -1;
28 s->loadmax = -1;
29 s->nettx = -1;
30 s->netrx = -1;
31 s->diskreadkb = -1;
32 s->diskwritekb = -1;
33 s->nprocs = -1;
34 s->uptime = -1;
35 s->count = -1;
36
37 FILE* fd = fopen("/tmp/cmon.status", "r");
38 if(!fd) {
39 fprintf(stderr, "Could not open status file\n");
40 return 0;
41 }
42
43 while(fgets(buf, 128, fd) != NULL) {
44 if(s->memtotal == -1 && strncmp(buf, "memtotal:", 9) == 0) {
45 status += sscanf(buf, "memtotal:%ld\n", &s->memtotal);
46 } else if(s->memusedmax == -1 && strncmp(buf, "memusedmax:", 11) == 0) {
47 status += sscanf(buf, "memusedmax:%ld\n", &s->memusedmax);
48 } else if(s->memusedavg == -1 && strncmp(buf, "memusedavg:", 11) == 0) {
49 status += sscanf(buf, "memusedavg:%ld\n", &s->memusedavg);
50
51 } else if(s->loadmax == -1 && strncmp(buf, "loadmax:", 8) == 0) {
52 status += sscanf(buf, "loadmax:%lf\n", &s->loadmax);
53 } else if(s->loadavg == -1 && strncmp(buf, "loadavg:", 8) == 0) {
54 status += sscanf(buf, "loadavg:%lf\n", &s->loadavg);
55
56 // Disk
57 } else if(s->diskreadkb == -1 && strncmp(buf, "diskreadkb:", 11) == 0) {
58 status += sscanf(buf, "diskreadkb:%lld\n", &s->diskreadkb);
59 } else if(s->diskwritekb == -1 && strncmp(buf, "diskwritekb:", 12) == 0) {
60 status += sscanf(buf, "diskwritekb:%lld\n", &s->diskwritekb);
61
62 // Network
63 } else if(s->nettx == -1 && strncmp(buf, "nettx:", 6) == 0) {
64 status += sscanf(buf, "nettx:%lld\n", &s->nettx);
65 } else if(s->netrx == -1 && strncmp(buf, "netrx:", 6) == 0) {
66 status += sscanf(buf, "netrx:%lld\n", &s->netrx);
67
68 // Misc
69 } else if(s->nprocs == -1 && strncmp(buf, "nprocs:", 7) == 0) {
70 status += sscanf(buf, "nprocs:%d\n", &s->nprocs);
71 } else if(s->uptime == -1 && strncmp(buf, "uptime:", 7) == 0) {
72 status += sscanf(buf, "uptime:%ld\n", &s->uptime);
73 } else if(s->count == -1 && strncmp(buf, "count:", 6) == 0) {
74 status += sscanf(buf, "count:%ld\n", &s->count);
75 } else {
76 fprintf(stderr, "Unknown status field: %s", buf);
77 }
78 }
79 fclose(fd);
80 return 1;
81 }
82
83 int status_write(struct status* s) {
84 FILE* fd = fopen("/tmp/cmon.status", "w");
85
86 fprintf(fd, "memtotal:%ld\n", s->memtotal);
87 fprintf(fd, "memusedmax:%ld\n", s->memusedmax);
88 fprintf(fd, "memusedavg:%ld\n", s->memusedavg);
89 fprintf(fd, "loadmax:%lf\n", s->loadmax);
90 fprintf(fd, "loadavg:%lf\n", s->loadavg);
91 fprintf(fd, "nprocs:%d\n", s->nprocs);
92 fprintf(fd, "diskreadkb:%lld\n", s->diskreadkb);
93 fprintf(fd, "diskwritekb:%lld\n", s->diskwritekb);
94 fprintf(fd, "nettx:%lld\n", s->nettx);
95 fprintf(fd, "netrx:%lld\n", s->netrx);
96 fprintf(fd, "uptime:%ld\n", s->uptime);
97 fprintf(fd, "count:%ld\n", s->count);
98
99 fclose(fd);
100 return 1;
101 }
|