equal
deleted
inserted
replaced
|
1 #define _DEFAULT_SOURCE |
|
2 #include <stdio.h> |
|
3 #include <stdlib.h> |
|
4 #include <unistd.h> |
|
5 #include <string.h> |
|
6 #include <errno.h> |
|
7 |
|
8 #include "config.h" |
|
9 |
|
10 int |
|
11 main(void) |
|
12 { |
|
13 while (1) { |
|
14 char line[513]; |
|
15 FILE *fd; |
|
16 |
|
17 fd = fopen("/proc/meminfo", "r"); |
|
18 if (fd == NULL) { |
|
19 perror("fopen"); |
|
20 exit(1); |
|
21 } |
|
22 |
|
23 int free = 0; |
|
24 int total = 0; |
|
25 int sub; |
|
26 while (!feof(fd)) { |
|
27 if (fgets(line, sizeof(line)-1, fd) == NULL) |
|
28 break; |
|
29 |
|
30 if (!strncmp(line, "MemTotal", 8)) |
|
31 sscanf(line+9, "%*[ ]%d%*[^\n]", &total); |
|
32 |
|
33 if (!strncmp(line, "MemFree", 7)) |
|
34 sscanf(line+8, "%*[ ]%d%*[^\n]", &free); |
|
35 |
|
36 if (!strncmp(line, "Buffers", 7)) { |
|
37 sscanf(line+8, "%*[ ]%d%*[^\n]", &sub); |
|
38 free += sub; |
|
39 } |
|
40 if (!strncmp(line, "Cached", 6)) { |
|
41 sscanf(line+7, "%*[ ]%d%*[^\n]", &sub); |
|
42 free += sub; |
|
43 } |
|
44 } |
|
45 fclose(fd); |
|
46 |
|
47 int used = total - free; |
|
48 |
|
49 char *color; |
|
50 if (used*1.0/total < GREEN) color = "color='green'"; |
|
51 else if (used*1.0/total > RED) color = "color='red'"; |
|
52 else if (used*1.0/total > ORANGE) color = "color='orange'"; |
|
53 else color = ""; |
|
54 printf("<span %s>%d/%d</span>\n", color, used/1024, total/1024); |
|
55 fflush(stdout); |
|
56 sleep(UPDATE_PERIOD); |
|
57 } |
|
58 } |