status-mem/mem.c
changeset 1 3e9290bf7249
child 3 dae770da6416
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/status-mem/mem.c	Mon Oct 20 22:59:08 2014 +0400
@@ -0,0 +1,58 @@
+#define _DEFAULT_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+#include "config.h"
+
+int
+main(void)
+{
+    while (1) {
+        char line[513];
+        FILE *fd;
+
+        fd = fopen("/proc/meminfo", "r");
+        if (fd == NULL) {
+            perror("fopen");
+            exit(1);
+        }
+
+        int free = 0;
+        int total = 0;
+        int sub;
+        while (!feof(fd)) {
+            if (fgets(line, sizeof(line)-1, fd) == NULL)
+                break;
+
+            if (!strncmp(line, "MemTotal", 8))
+                sscanf(line+9, "%*[ ]%d%*[^\n]", &total);
+
+            if (!strncmp(line, "MemFree", 7))
+                sscanf(line+8, "%*[ ]%d%*[^\n]", &free);
+
+            if (!strncmp(line, "Buffers", 7)) {
+                sscanf(line+8, "%*[ ]%d%*[^\n]", &sub);
+                free += sub;
+            }
+            if (!strncmp(line, "Cached", 6)) {
+                sscanf(line+7, "%*[ ]%d%*[^\n]", &sub);
+                free += sub;
+            }
+        }
+        fclose(fd);
+
+        int used = total - free;
+
+        char *color;
+        if (used*1.0/total < GREEN) color = "color='green'";
+        else if (used*1.0/total > RED) color = "color='red'";
+        else if (used*1.0/total > ORANGE) color = "color='orange'";
+        else color = "";
+        printf("<span %s>%d/%d</span>\n", color, used/1024, total/1024);
+        fflush(stdout);
+	sleep(UPDATE_PERIOD);
+    }
+}