9 static int dec_to_int(char *src, char **dst)
13 while(((*src < '0') || (*src > '9')) && *src)
19 while(((*src >= '0') && (*src <= '9')) && *src)
27 static float dec_to_float(char *src, char **dst)
31 while(((*src < '0') || (*src > '9')) && *src)
37 while((((*src >= '0') && (*src <= '9')) || (*src =='.')) && *src)
46 * Copy white space separated stirng from *src to *word
49 * dst: place where store the end of word pointer from source string
50 * word: destination where to copy the string
51 * size: maximum size of bytes to copy to the output buffer
53 * return the number of bytes copied
56 static int get_word(char *src, char **dst, char *word, int size)
59 while(((*src == ' ') || (*src == '\t') || (*src == '\n')) && *src)
62 while(((*src != ' ') && (*src != '\t') && (*src != '\n')) && *src && (ret < (size - 1))) {
76 #define STATFILE "/proc/stat"
78 int cpu_parser(char *data)
82 FILE *file = fopen(STATFILE, "r");
83 int user, nice, sys, idle, wait, irq, softirq;
86 fprintf(stderr, "Failed to open file %s\n", STATFILE);
90 if (!fgets(buf, 1024, file)) {
91 fprintf(stderr, "Failed to read file %s\n", STATFILE);
96 user = dec_to_int(str, &str);
97 nice = dec_to_int(str, &str);
98 sys = dec_to_int(str, &str);
99 idle = dec_to_int(str, &str);
100 wait = dec_to_int(str, &str);
101 irq = dec_to_int(str, &str);
102 softirq = dec_to_int(str, &str);
104 sprintf(data, "%d:%d:%d:%d:%d:%d:%d",
105 user, nice, sys, idle, wait, irq, softirq);
111 #define MEMFILE "/proc/meminfo"
113 int mem_parser(char *data)
115 char buf[1024], word[1024];
116 int free = 0, buffered = 0, cache = 0, active = 0, inactive = 0,
117 swapfree = 0, anon = 0, slab = 0, tables = 0;
118 FILE *file = fopen(MEMFILE, "r");
121 fprintf(stderr, "Failed to open file %s\n", MEMFILE);
125 while (fgets(buf, 1024, file)) {
126 get_word(buf, 0, word, 1024);
128 if (!strcmp(word, "MemFree:")) {
129 free = dec_to_int(buf, 0);
130 } else if (!strcmp(word, "Buffers:")) {
131 buffered = dec_to_int(buf, 0);
132 } else if (!strcmp(word, "Cached:")) {
133 cache = dec_to_int(buf, 0);
134 } else if (!strcmp(word, "Active:")) {
135 active = dec_to_int(buf, 0);
136 } else if (!strcmp(word, "Inactive:")) {
137 inactive = dec_to_int(buf, 0);
138 } else if (!strcmp(word, "SwapFree:")) {
139 swapfree = dec_to_int(buf, 0);
140 } else if (!strcmp(word, "AnonPages:")) {
141 anon = dec_to_int(buf, 0);
142 } else if (!strcmp(word, "Slab:")) {
143 slab = dec_to_int(buf, 0);
144 } else if (!strcmp(word, "PageTables:")) {
145 tables = dec_to_int(buf, 0);
150 sprintf(data, "%f:%f:%f:%f:%f:%f:%f:%f:%f",
164 int cpu_mem_parser(char *data)
166 char cpu[1024], mem[1024];
170 sprintf(data, "%s:%s", cpu, mem);
175 int digitemp_parser(char *data)
177 const char digitemp_cmd[] = "/usr/bin/digitemp";
178 char *const digitemp_args[] = { "", "-o2", "-a", "-q", 0 };
179 FILE *readf, *writef;
181 char buf[1024], *bptr = buf;
184 pid = run_piped_stream(digitemp_cmd, digitemp_args, &readf, &writef);
186 fprintf(stderr, "Failed to parse digitemp");
187 sprintf(data, "U:U");
191 fgets(buf, 1024, readf);
194 get_word(bptr, &bptr, t1, 16);
196 get_word(bptr, &bptr, t2, 16);
198 get_word(bptr, &bptr, t3, 16);
201 t1 = dec_to_float(bptr, &bptr);
202 t2 = dec_to_float(bptr, &bptr);
203 t3 = dec_to_float(bptr, &bptr);
208 while (fgets(buf, 1024, readf));
209 harvest_zombies(pid);
210 sprintf(data, "%.2f:%.2f", t3, t2);
214 int digitemp_parser_mod(char *data)
219 ret = digitemp_parser(buf);
220 sprintf(data, "U:%s", buf);