]> git.itanic.dy.fi Git - rrdd/commitdiff
Add digitemp parser
authorTimo Kokkonen <kaapeli@ee.oulu.fi>
Mon, 31 Mar 2008 17:50:31 +0000 (20:50 +0300)
committerTimo Kokkonen <kaapeli@ee.oulu.fi>
Mon, 31 Mar 2008 17:50:31 +0000 (20:50 +0300)
parser.c
parser.h

index 1a635ffbf47e125ae9581f6c18ddf64e7c939841..7febe07f8b4686b11c8e17db6c933fdbe967f23c 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -4,8 +4,9 @@
 #include <error.h>
 
 #include "parser.h"
+#include "process.h"
 
-static int str_to_dec(char *src, char **dst)
+static int dec_to_int(char *src, char **dst)
 {
        int ret;
 
@@ -23,13 +24,42 @@ static int str_to_dec(char *src, char **dst)
        return ret;
 }
 
-static int get_word(char *src, char **dst, char *word)
+static float dec_to_float(char *src, char **dst)
+{
+       float ret;
+
+       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 ret = 0;
        while(((*src == ' ') || (*src == '\t')) && *src)
                src++;
 
-       while(((*src != ' ') && (*src != '\t')) && *src) {
+       while(((*src != ' ') && (*src != '\t')) && *src && (ret < (size - 1))) {
                *word = *src;
                word++;
                src++;
@@ -63,13 +93,13 @@ int cpu_parser(char *data)
                return -1;
        }
 
-       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);
+       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);
@@ -93,26 +123,26 @@ int mem_parser(char *data)
        }
 
        while (fgets(buf, 1024, file)) {
-               get_word(buf, 0, word);
+               get_word(buf, 0, word, 1024);
                
                if (!strcmp(word, "MemFree:")) {
-                       free = str_to_dec(buf, 0);
+                       free = dec_to_int(buf, 0);
                } else if (!strcmp(word, "Buffers:")) {
-                       buffered = str_to_dec(buf, 0);
+                       buffered = dec_to_int(buf, 0);
                } else if (!strcmp(word, "Cached:")) {
-                       cache = str_to_dec(buf, 0);
+                       cache = dec_to_int(buf, 0);
                } else if (!strcmp(word, "Active:")) {
-                       active = str_to_dec(buf, 0);
+                       active = dec_to_int(buf, 0);
                } else if (!strcmp(word, "Inactive:")) {
-                       inactive = str_to_dec(buf, 0);
+                       inactive = dec_to_int(buf, 0);
                } else if (!strcmp(word, "SwapFree:")) {
-                       swapfree = str_to_dec(buf, 0);
+                       swapfree = dec_to_int(buf, 0);
                } else if (!strcmp(word, "AnonPages:")) {
-                               anon = str_to_dec(buf, 0);
+                               anon = dec_to_int(buf, 0);
                } else if (!strcmp(word, "Slab:")) {
-                       slab = str_to_dec(buf, 0);
+                       slab = dec_to_int(buf, 0);
                } else if (!strcmp(word, "PageTables:")) {
-                       tables = str_to_dec(buf, 0);
+                       tables = dec_to_int(buf, 0);
                }
        }
        fclose(file);
@@ -141,3 +171,34 @@ int cpu_mem_parser(char *data)
 
        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;
+       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);
+
+       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;
+}
index 9702a998041c4c9a37328210a4c77f4d3ed0ddf6..296ac18c0be4aece0da304d8299d763d21bafa03 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -4,5 +4,6 @@
 int cpu_parser(char *data);
 int mem_parser(char *data);
 int cpu_mem_parser(char *data);
+int digitemp_parser(char *data);
 
 #endif