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->count = 0;
31 s->uptime = 0;
32
33 FILE* fd = fopen("/tmp/cmon.status", "r");
34 if(fd) {
35 status += fscanf(fd, "memtotal:%ld\n", &s->memtotal);
36 status += fscanf(fd, "memusedmax:%ld\n", &s->memusedmax);
37 status += fscanf(fd, "memusedavg:%ld\n", &s->memusedavg);
38 status += fscanf(fd, "loadmax:%lf\n", &s->loadmax);
39 status += fscanf(fd, "loadavg:%lf\n", &s->loadavg);
40 status += fscanf(fd, "nprocs:%d\n", &s->nprocs);
41 status += fscanf(fd, "nettx:%lld\n", &s->nettx);
42 status += fscanf(fd, "netrx:%lld\n", &s->netrx);
43 status += fscanf(fd, "uptime:%ld\n", &s->uptime);
44 status += fscanf(fd, "count:%ld\n", &s->count);
45 if(status != 10) {
46 fprintf(stderr, "ERROR reading status file\n");
47 return 0;
48 }
49 fclose(fd);
50 }
51 return 1;
52 }
53
54 int status_write(struct status* s) {
55 FILE* fd = fopen("/tmp/cmon.status", "w");
56
57 fprintf(fd, "memtotal:%ld\n", s->memtotal);
58 fprintf(fd, "memusedmax:%ld\n", s->memusedmax);
59 fprintf(fd, "memusedavg:%ld\n", s->memusedavg);
60 fprintf(fd, "loadmax:%lf\n", s->loadmax);
61 fprintf(fd, "loadavg:%lf\n", s->loadavg);
62 fprintf(fd, "nprocs:%d\n", s->nprocs);
63 fprintf(fd, "nettx:%lld\n", s->nettx);
64 fprintf(fd, "netrx:%lld\n", s->netrx);
65 fprintf(fd, "uptime:%ld\n", s->uptime);
66 fprintf(fd, "count:%ld\n", s->count);
67
68 fclose(fd);
69 return 1;
70 }
|