]> git.itanic.dy.fi Git - rrdd/blobdiff - parser.c
onewire_parser.c: Fix compiler warnings about string lengths
[rrdd] / parser.c
index bfdf04b6b82dd646cf7520ee9b82363d9af32a2e..0437096de547d6665cff20a6ad2ac52b63d468d4 100644 (file)
--- a/parser.c
+++ b/parser.c
-#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;
-
-       while(((*src < '0') || (*src > '9')) && *src)
-               src++;
+static struct parser_info *parser_list;
 
-       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 == '\n')) && *src)
-               src++;
-
-       while(((*src != ' ') && (*src != '\t') && (*src != '\n')) && *src && (ret < (size - 1))) {
-               *word = *src;
-               word++;
-               src++;
-               ret++;
-       }
-       *word = 0;
+       struct parser_info *parser;
 
-       if (dst)
-               *dst = src;
-
-       return ret;
-}
-
-#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];
-
-       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, *writef;
-       int pid;
-       char buf[1024], *bptr = buf;
-       float t1, t2, t3;
-
-       pid = run_piped_stream(digitemp_cmd, digitemp_args, &readf, &writef);
-       if (pid < 0) {
-               fprintf(stderr, "Failed to parse digitemp");
-               sprintf(data, "U:U");
-               return -1;
-       }
+       struct parser_info *parser;
+       int ret;
 
-       fgets(buf, 1024, readf);
-
-/*
-       get_word(bptr, &bptr, t1, 16);
-       *bptr++ = 0;
-       get_word(bptr, &bptr, t2, 16);
-       *bptr++ = 0;
-       get_word(bptr, &bptr, t3, 16);
-       *bptr++ = 0;
-*/
-       t1 = dec_to_float(bptr, &bptr);
-       t2 = dec_to_float(bptr, &bptr);
-       t3 = dec_to_float(bptr, &bptr);
-
-       t2 += 2.16;
-       t3 += -0.44;
-
-       while (fgets(buf, 1024, readf));
-       harvest_zombies(pid);
-       sprintf(data, "%.2f:%.2f", t3, t2);
-       return 0;
-}
+       parser = _str_to_parser(str);
 
-int digitemp_parser_mod(char *data)
-{
-       char buf[1024];
-       int ret;
+       if (parser)
+               return parser;
 
-       ret = digitemp_parser(buf);
-       sprintf(data, "U:%s", buf);
+       /* No parser found? Try loading a plugin */
+       ret = load_parser_plugin(str);
+       if (ret)
+               return NULL;
 
-       return ret;
+       /* Try finding the parser again, maybe we got lucky and have it now */
+       return _str_to_parser(str);
 }