-#include <stdlib.h>
-#include <stdio.h>
#include <string.h>
-#include <error.h>
#include "parser.h"
+#include "debug.h"
+#include "plugin_manager.h"
-static int str_to_dec(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 int get_word(char *src, char **dst, char *word)
-{
- int ret = 0;
- while(((*src == ' ') || (*src == '\t')) && *src)
- src++;
-
- while(((*src != ' ') && (*src != '\t')) && *src) {
- *word = *src;
- word++;
- src++;
- ret++;
- }
- *word = 0;
-
- if (dst)
- *dst = src;
-
- return ret;
-}
-
-#define STATFILE "/proc/stat"
+static struct parser_info *parser_list;
-int cpu_parser(char *data)
+int register_parser(struct parser_info *info)
{
- char buf[1024];
- char *str = buf;
- FILE *file = fopen(STATFILE, "r");
- int user, nice, sys, idle, wait, irq, softirq;
+ struct parser_info *parser;
- 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 = str_to_dec(str, &str);
- nice = str_to_dec(str, &str);
- sys = str_to_dec(str, &str);
- idle = str_to_dec(str, &str);
- wait = str_to_dec(str, &str);
- irq = str_to_dec(str, &str);
- softirq = str_to_dec(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);
-
- if (!strcmp(word, "MemFree:")) {
- free = str_to_dec(buf, 0);
- } else if (!strcmp(word, "Buffers:")) {
- buffered = str_to_dec(buf, 0);
- } else if (!strcmp(word, "Cached:")) {
- cache = str_to_dec(buf, 0);
- } else if (!strcmp(word, "Active:")) {
- active = str_to_dec(buf, 0);
- } else if (!strcmp(word, "Inactive:")) {
- inactive = str_to_dec(buf, 0);
- } else if (!strcmp(word, "SwapFree:")) {
- swapfree = str_to_dec(buf, 0);
- } else if (!strcmp(word, "AnonPages:")) {
- anon = str_to_dec(buf, 0);
- } else if (!strcmp(word, "Slab:")) {
- slab = str_to_dec(buf, 0);
- } else if (!strcmp(word, "PageTables:")) {
- tables = str_to_dec(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;
+
+ /* No parser found? Try loading a plugin */
+ ret = load_parser_plugin(str);
+ if (ret)
+ return NULL;
+
+ /* Try finding the parser again, maybe we got lucky and have it now */
+ return _str_to_parser(str);
}