-#include <stdlib.h>
-#include <stdio.h>
#include <string.h>
-#include <error.h>
#include "parser.h"
-#include "process.h"
+#include "debug.h"
+#include "plugin_manager.h"
-static int dec_to_int(char *src, char **dst)
-{
- int ret;
-
- while(((*src < '0') || (*src > '9')) && *src)
- src++;
-
- ret = atoi(src);
-
- if (dst) {
- while(((*src >= '0') && (*src <= '9')) && *src)
- src++;
- *dst = src;
- }
-
- return ret;
-}
-
-static float dec_to_float(char *src, char **dst)
-{
- float ret;
+static struct parser_info *parser_list;
- while(((*src < '0') || (*src > '9')) && *src)
- src++;
-
- ret = atof(src);
-
- if (dst) {
- while((((*src >= '0') && (*src <= '9')) || (*src =='.')) && *src)
- src++;
- *dst = src;
- }
-
- return ret;
-}
-
-/*
- * Copy white space separated stirng from *src to *word
- *
- * src: source
- * dst: place where store the end of word pointer from source string
- * word: destination where to copy the string
- * size: maximum size of bytes to copy to the output buffer
- *
- * return the number of bytes copied
- */
-
-static int get_word(char *src, char **dst, char *word, int size)
+int register_parser(struct parser_info *info)
{
- int ret = 0;
- while(((*src == ' ') || (*src == '\t')) && *src)
- src++;
-
- while(((*src != ' ') && (*src != '\t')) && *src && (ret < (size - 1))) {
- *word = *src;
- word++;
- src++;
- ret++;
- }
- *word = 0;
-
- if (dst)
- *dst = src;
-
- return ret;
-}
+ struct parser_info *parser;
-#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) {
- fprintf(stderr, "Failed to open file %s\n", STATFILE);
+ if (!info->name) {
+ pr_err("Unable to register parser without a name\n");
return -1;
}
- if (!fgets(buf, 1024, file)) {
- fprintf(stderr, "Failed to read file %s\n", STATFILE);
- fclose(file);
- return -1;
+ pr_info("Registering parser %s\n", info->name);
+
+ if (!parser_list) {
+ parser_list = info;
+ return 0;
}
- 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);
+ for (parser = parser_list; parser->next; parser = parser->next)
+ ;
- sprintf(data, "%d:%d:%d:%d:%d:%d:%d",
- user, nice, sys, idle, wait, irq, softirq);
+ parser->next = info;
+ info->next = NULL;
- fclose(file);
return 0;
}
-#define MEMFILE "/proc/meminfo"
-
-int mem_parser(char *data)
+static struct parser_info *_str_to_parser(const char *str)
{
- char buf[1024], word[1024];
- int free = 0, buffered = 0, cache = 0, active = 0, inactive = 0,
- swapfree = 0, anon = 0, slab = 0, tables = 0;
- FILE *file = fopen(MEMFILE, "r");
+ struct parser_info *parser;
- if (file == NULL) {
- fprintf(stderr, "Failed to open file %s\n", MEMFILE);
- return -1;
+ for (parser = parser_list; parser; parser = parser->next) {
+ if (!strcmp(str, parser->name))
+ return parser;
}
- 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);
- }
- }
- fclose(file);
-
- sprintf(data, "%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);
-
- return 0;
+ return NULL;
}
-int cpu_mem_parser(char *data)
+struct parser_info *str_to_parser(const char *str)
{
- char cpu[1024], mem[1024];
+ struct parser_info *parser;
+ int ret;
- cpu_parser(cpu);
- mem_parser(mem);
- sprintf(data, "%s:%s", cpu, mem);
+ parser = _str_to_parser(str);
- return 0;
-}
+ if (parser)
+ return parser;
-int digitemp_parser(char *data)
-{
- const char digitemp_cmd[] = "/usr/bin/digitemp";
- char *const digitemp_args[] = { "", "-o2", "-a", "-q", 0 };
- FILE *readf, *writef;
- int pid;
- char buf[1024], *bptr = buf;
- char t1[16], t2[16], t3[16];
-
- pid = run_piped_stream(digitemp_cmd, digitemp_args, &readf, &writef);
- if (pid < 0) {
- fprintf(stderr, "Failed to parse digitemp");
- sprintf(data, "U:U:U");
- return -1;
- }
-
- fgets(buf, 1024, readf);
+ /* No parser found? Try loading a plugin */
+ ret = load_parser_plugin(str);
+ if (ret)
+ return NULL;
- get_word(bptr, &bptr, t1, 16);
- *bptr++ = 0;
- get_word(bptr, &bptr, t2, 16);
- *bptr++ = 0;
- get_word(bptr, &bptr, t3, 16);
- *bptr++ = 0;
-
- while (fgets(buf, 1024, readf));
- harvest_zombies(pid);
- sprintf(data, "U:%s:%s", t2, t3);
- return 0;
+ /* Try finding the parser again, maybe we got lucky and have it now */
+ return _str_to_parser(str);
}