]> git.itanic.dy.fi Git - rrdd/commitdiff
Add onewire parser
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 24 Jun 2012 08:47:25 +0000 (11:47 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 24 Jun 2012 08:56:04 +0000 (11:56 +0300)
This parser will parse data from onewire server. It will use the
network protocol to query the data, thus it is capable of fetching the
data over any network location supported by the ownet library.

The first parser_data string must containt the server address:ip. The
rest of the strings are used to query data entries from the server. If
one wishes to leave blank entries to some of the data, a "U" string is
passed as is to the rrd_data.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
Makefile
config.c
onewire_parser.c [new file with mode: 0644]
parser.h

index 6da318051baf212cf7fa11581fd29614518db22f..edfeea460730fca3ee9587d4394111a7f1288b44 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ LD=ld
 CFLAGS=-Wall -O2 -g
 
 RRDD_OBJS= main.o process.o rrdtool.o parser.o scheduler.o string.o \
-               debug.o config.o
+               debug.o config.o onewire_parser.o
 
 ALL_OBJS = $(RRDD_OBJS)
 ALL_DEBS = $(patsubst %.o,.%.o.d,$(ALL_OBJS))
@@ -21,7 +21,7 @@ endif
 all: rrdd
 
 rrdd: $(RRDD_OBJS)
-       $(QUIET_LINK)$(CC) -o rrdd $(RRDD_OBJS) -lconfig
+       $(QUIET_LINK)$(CC) -o rrdd $(RRDD_OBJS) -lconfig -lownet
 
 clean:
        rm -vf rrdd *~ *.o .*.d
index b10ac7e87dbd3dbb74908d3a31739b86dbe42188..bfb4f07a775d482831fb1e675384ddbef04d409d 100644 (file)
--- a/config.c
+++ b/config.c
@@ -161,6 +161,9 @@ static int (*str_to_parser(const char *str))(char *rrd_data, const char **parser
        if (!strcmp(str, "netstats"))
                return netstats_parser;
 
+       if (!strcmp(str, "onewire"))
+               return onewire_parser;
+
        return NULL;
 }
 
@@ -431,6 +434,9 @@ static char *parser_to_str(int (*parser)(char *rrd_data, const char **parser_dat
        if (parser == netstats_parser)
                return "netstats";
 
+       if (parser == onewire_parser)
+               return "onewire";
+
        return NULL;
 }
 
diff --git a/onewire_parser.c b/onewire_parser.c
new file mode 100644 (file)
index 0000000..fa927c8
--- /dev/null
@@ -0,0 +1,71 @@
+#include <ownetapi.h>
+
+#include "parser.h"
+#include "debug.h"
+#include "string.h"
+
+int onewire_parser(char *rrd_data, const char **parser_data)
+{
+       OWNET_HANDLE h;
+       const char *server_addr;
+       char buf[24], *tmp;
+       int i = 1, ret;
+       int max_str = RRD_DATA_MAX_LEN;
+
+       if (!parser_data) {
+               pr_err("No parser data available\n");
+               return -1;
+       }
+
+       server_addr = parser_data[0];
+
+       if (!server_addr) {
+               pr_err("Server address not specified\n");
+               return -1;
+       }
+
+       h = OWNET_init(server_addr);
+       if (h < 0) {
+               pr_err("Failed to connect to server %s\n", server_addr);
+               return -1;
+       }
+
+       while (parser_data[i]) {
+               if (!strcmp("U", parser_data[i])) {
+undefined:
+                       ret = snprintf(rrd_data, max_str, "U");
+                       max_str -= ret;
+                       rrd_data += ret;
+                       goto next;
+               }
+
+               pr_info("Reading data for entry %s\n", parser_data[i]);
+               ret = OWNET_read(h, parser_data[i], &tmp);
+               if (ret < 0) {
+                       pr_err("Failed to read entry %s\n", parser_data[i]);
+                       goto undefined;
+               }
+
+               /* The data from OWNET_read appears to not be NULL terminated */
+               memcpy(buf, tmp, MIN(ret, sizeof(buf) -1));
+               free(tmp);
+               buf[ret] = 0;
+
+               ret = snprintf(rrd_data, max_str, "%s", buf);
+               max_str -= ret;
+               rrd_data += ret;
+
+next:
+               i++;
+               if (!parser_data[i])
+                       break;
+
+               ret = snprintf(rrd_data, max_str, ":");
+               max_str -= ret;
+               rrd_data += ret;
+       }
+       rrd_data = 0;
+
+       OWNET_finish();
+       return 0;
+}
index 64f119a5890723ea6d0f429827981c3f36174662..5ec6a8e26340b6d510c6cc8dc2304803fe72ae1b 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -10,6 +10,7 @@ int digitemp_parser(char *data, const char **p);
 int digitemp_parser_mod(char *data, const char **p);
 int script_parser(char *rrd_data, const char **parser_data);
 int netstats_parser(char *rrd_data, const char **parser_data);
+int onewire_parser(char *rrd_data, const char **parser_data);
 
 #define MIN(a, b) ((a) < (b) ? (a) : (b))