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