#include #include #include #include #include "parser.h" #include "process.h" #include "string.h" #include "debug.h" #define STATFILE "/proc/stat" int cpu_parser(char *data) { char buf[1024]; char *str = buf; FILE *file = fopen(STATFILE, "r"); int user, nice, sys, idle, wait, irq, softirq; if (file == NULL) { pr_err("Failed to open file %s\n", STATFILE); return -1; } if (!fgets(buf, 1024, file)) { pr_err("Failed to read file %s\n", STATFILE); fclose(file); return -1; } user = dec_to_int(str, &str); nice = dec_to_int(str, &str); sys = dec_to_int(str, &str); idle = dec_to_int(str, &str); wait = dec_to_int(str, &str); irq = dec_to_int(str, &str); softirq = dec_to_int(str, &str); sprintf(data, "%d:%d:%d:%d:%d:%d:%d", user, nice, sys, idle, wait, irq, softirq); fclose(file); return 0; } #define MEMFILE "/proc/meminfo" int mem_parser(char *data) { char buf[1024], word[1024]; int free = 0, buffered = 0, cache = 0, active = 0, inactive = 0, swapfree = 0, anon = 0, slab = 0, tables = 0, swaptotal = 0; FILE *file = fopen(MEMFILE, "r"); if (file == NULL) { pr_err("Failed to open file %s\n", MEMFILE); return -1; } while (fgets(buf, 1024, file)) { get_word(buf, 0, word, 1024); if (!strcmp(word, "MemFree:")) { free = dec_to_int(buf, 0); } else if (!strcmp(word, "Buffers:")) { buffered = dec_to_int(buf, 0); } else if (!strcmp(word, "Cached:")) { cache = dec_to_int(buf, 0); } else if (!strcmp(word, "Active:")) { active = dec_to_int(buf, 0); } else if (!strcmp(word, "Inactive:")) { inactive = dec_to_int(buf, 0); } else if (!strcmp(word, "SwapFree:")) { swapfree = dec_to_int(buf, 0); } else if (!strcmp(word, "AnonPages:")) { anon = dec_to_int(buf, 0); } else if (!strcmp(word, "Slab:")) { slab = dec_to_int(buf, 0); } else if (!strcmp(word, "PageTables:")) { tables = dec_to_int(buf, 0); } else if (!strcmp(word, "SwapTotal:")) { swaptotal = dec_to_int(buf, 0); } } fclose(file); sprintf(data, "%f:%f:%f:%f:%f:%f:%f:%f:%f:%f", free / 1024.0, buffered / 1024.0, cache / 1024.0, active / 1024.0, inactive / 1024.0, swapfree / 1024.0, anon / 1024.0, slab / 1024.0, tables / 1024.0, (swaptotal - swapfree) / 1024.0); return 0; } int cpu_mem_parser(char *data) { char cpu[1024], mem[1024]; cpu_parser(cpu); mem_parser(mem); sprintf(data, "%s:%s", cpu, mem); return 0; } int digitemp_parser(char *data) { const char digitemp_cmd[] = "/usr/bin/digitemp"; char *const digitemp_args[] = { "", "-o2", "-a", "-q", 0 }; FILE *readf; int pid, ret, error; float t1 = 0, t2 = 0, t3 = 0; char buf[1024]; pid = run_piped_stream(digitemp_cmd, digitemp_args, 0, &readf, 0); if (pid < 0) { pr_err("Failed to parse digitemp\n"); sprintf(data, "U:U"); return -1; } ret = fscanf(readf, "%f %f %f", &t1, &t2, &t3); if (ret != 3) { error = errno; pr_err("Failed to parse digitemp output: %s\n", strerror(error)); sprintf(data, "U:U"); return 1; } t2 += 2.16; t3 += -0.44; /* Read whatever the process might be still printing out */ while (fgets(buf, 1024, readf)); harvest_zombies(pid); sprintf(data, "%.2f:%.2f", t3, t2); return 0; } int digitemp_parser_mod(char *data) { char buf[1024]; int ret; ret = digitemp_parser(buf); sprintf(data, "U:%s", buf); return ret; }