summaryrefslogtreecommitdiff
path: root/src/status.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/status.c')
-rw-r--r--src/status.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/status.c b/src/status.c
new file mode 100644
index 0000000..00b9047
--- /dev/null
+++ b/src/status.c
@@ -0,0 +1,72 @@
+/**
+ * Cmon (c'mon) is a simple system resource monitor
+ * Copyright (C) 2022 Aaron Ball <nullspoon@oper.io>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+#include "status.h"
+
+struct status* status_init() {
+ struct status* s = malloc(sizeof(struct status));
+ int status = 0;
+
+ s->memmax = 0;
+ s->memavg = 0;
+ s->loadavg = 0;
+ s->loadmax = 0;
+ s->nettx = 0;
+ s->netrx = 0;
+ s->count = 0;
+ s->uptime = 0;
+
+ FILE* fd = fopen("/tmp/cmon.status", "r");
+ if(fd) {
+ status += fscanf(fd, "memmax:%ld\n", &s->memmax);
+ status += fscanf(fd, "memavg:%ld\n", &s->memavg);
+ status += fscanf(fd, "loadmax:%lf\n", &s->loadmax);
+ status += fscanf(fd, "loadavg:%lf\n", &s->loadavg);
+ status += fscanf(fd, "nprocs:%d\n", &s->nprocs);
+ status += fscanf(fd, "nettx:%lld\n", &s->nettx);
+ status += fscanf(fd, "netrx:%lld\n", &s->netrx);
+ status += fscanf(fd, "uptime:%ld\n", &s->uptime);
+ status += fscanf(fd, "count:%ld\n", &s->count);
+ if(status != 9) {
+ fprintf(stderr, "ERROR reading status file\n");
+ return NULL;
+ }
+ fclose(fd);
+ }
+ return s;
+}
+
+int status_write(struct status* s) {
+ FILE* fd = fopen("/tmp/cmon.status", "w");
+
+ fprintf(fd, "memmax:%ld\n", s->memmax);
+ fprintf(fd, "memavg:%ld\n", s->memavg);
+ fprintf(fd, "loadmax:%lf\n", s->loadmax);
+ fprintf(fd, "loadavg:%lf\n", s->loadavg);
+ fprintf(fd, "nprocs:%d\n", s->nprocs);
+ fprintf(fd, "nettx:%lld\n", s->nettx);
+ fprintf(fd, "netrx:%lld\n", s->netrx);
+ fprintf(fd, "uptime:%ld\n", s->uptime);
+ fprintf(fd, "count:%ld\n", s->count);
+
+ fclose(fd);
+ return 1;
+}
+
+void status_free(struct status* s) {
+ free(s);
+}

Generated by cgit