Change named colors to hex values
[sysstatus.git] / src / status / memusage.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "common.h"
5 #include "tools.h"
6
7
8 void status_memusage(GlobalData *g)
9 {
10   StatusItem s;
11   char text[16] = { 0 };
12
13   char *stline = NULL;
14   size_t stlen;
15   FILE *stfile;
16
17   int memtotal = 0;
18   int memfree = 0;
19   int memused;
20   int membuffers = 0;
21   int memcached = 0;
22
23
24   statusitem_init(&s);
25   s.text = text;
26
27   stfile = fopen("/proc/meminfo", "r");
28   if (stfile != NULL) {
29     stlen = getline(&stline, &stlen, stfile);
30     memtotal = atoi(&stline[17]);
31
32     stlen = getline(&stline, &stlen, stfile);
33     memfree = atoi(&stline[17]);
34
35     stlen = getline(&stline, &stlen, stfile);
36     membuffers = atoi(&stline[17]);
37
38     stlen = getline(&stline, &stlen, stfile);
39     memcached = atoi(&stline[17]);
40     free(stline);
41
42     fclose(stfile);
43
44     memused = memtotal - memfree - memcached - membuffers;
45
46     memused /= 1024;    // Just show MBs used
47
48     /* Change color based on % of RAM used */
49     if ((float)memused / (float)memtotal < 0.85) {
50       s.color = "#22FF22";  // green
51     } else {
52       s.color = "#FF0000";  // red
53     }
54
55     snprintf(text, sizeof(text), "Mem: %d M", memused);
56
57     line_append_item(g, &s);
58   }
59 }