multimeter plugin: Converted to the new plugin interface.
authorFlorian Forster <sifnfors@faui02b.informatik.uni-erlangen.de>
Mon, 5 Feb 2007 11:06:04 +0000 (12:06 +0100)
committerFlorian Forster <sifnfors@faui02b.informatik.uni-erlangen.de>
Mon, 5 Feb 2007 11:06:04 +0000 (12:06 +0100)
configure.in
src/multimeter.c

index a9d8eff..ba936d8 100644 (file)
@@ -294,6 +294,10 @@ AC_CHECK_HEADERS(netinet/if_ether.h)
 AC_CHECK_HEADERS(netinet/tcp.h)
 AC_CHECK_HEADERS(netinet/udp.h)
 
+# For the multimeter plugin
+AC_CHECK_HEADERS(termios.h)
+AC_CHECK_HEADERS(sys/ioctl.h)
+
 dnl Checking for libraries
 AC_CHECK_LIB(m, ext)
 
index 317745b..d1cf190 100644 (file)
  *   Peter Holik <peter at holik.at>
  *
  * Used multimeter: Metex M-4650CR
- *
  **/
 
-#include <termios.h>
-#include <sys/ioctl.h>
-#include <math.h>
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
 
-#define MODULE_NAME "multimeter"
-
-static char *multimeter_file = "multimeter.rrd";
+#if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H && HAVE_MATH_H
+# include <termios.h>
+# include <sys/ioctl.h>
+# include <math.h>
+# define MULTIMETER_HAVE_READ 1
+#else
+# define MULTIMETER_HAVE_READ 0
+# error "multimeter cannot read!"
+#endif
+
+static data_source_t data_source[1] =
+{
+       {"value", DS_TYPE_GAUGE, NAN, NAN}
+};
 
-static char *ds_def[] =
+static data_set_t data_set =
 {
-       "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
-       NULL
+       "multimeter", 1, data_source
 };
-static int ds_num = 1;
 
+#if MULTIMETER_HAVE_READ
 static int fd = -1;
 
 static int multimeter_timeval_sub (struct timeval *tv1, struct timeval *tv2,
@@ -75,7 +81,7 @@ static int multimeter_read_value(double *value)
 
                if (gettimeofday (&time_end, NULL) < 0)
                {
-                       syslog (LOG_ERR, MODULE_NAME": gettimeofday failed: %s",
+                       syslog (LOG_ERR, "multimeter plugin: gettimeofday failed: %s",
                                 strerror (errno));
                        return (-1);
                }
@@ -97,8 +103,9 @@ static int multimeter_read_value(double *value)
 
                        if (gettimeofday (&time_now, NULL) < 0)
                        {
-                               syslog (LOG_ERR, MODULE_NAME": gettimeofday failed: %s",
-                                        strerror (errno));
+                               syslog (LOG_ERR, "multimeter plugin: "
+                                               "gettimeofday failed: %s",
+                                               strerror (errno));
                                return (-1);
                        }
                        if (multimeter_timeval_sub (&time_end, &time_now, &timeout) == -1)
@@ -150,17 +157,18 @@ static int multimeter_read_value(double *value)
                        }
                        else /* status == -1 */
                        {
-                               syslog (LOG_ERR, MODULE_NAME": select failed: %s",
-                                        strerror (errno));
+                               syslog (LOG_ERR, "multimeter plugin: "
+                                               "select failed: %s",
+                                               strerror (errno));
                                break;
                        }
                }
        } while (--retry);
 
        return (-2);  /* no value received */
-}
+} /* int multimeter_read_value */
 
-static void multimeter_init (void)
+static int multimeter_init (void)
 {
        int i;
        char device[] = "/dev/ttyS ";
@@ -186,49 +194,74 @@ static void multimeter_init (void)
                        tcsetattr(fd, TCSANOW, &tios);
                        ioctl(fd, TIOCMBIC, &rts);
                        
-                       if (multimeter_read_value(&value) < -1)
+                       if (multimeter_read_value (&value) < -1)
                        {
-                               close(fd);
+                               close (fd);
                                fd = -1;
                        }
                        else
                        {
-                               syslog (LOG_INFO, MODULE_NAME" found (%s)", device);
-                               return;
+                               syslog (LOG_INFO, "multimeter plugin: Device "
+                                               "found at %s", device);
+                               return (0);
                        }
                }
        }
-       syslog (LOG_ERR, MODULE_NAME" not found");
+
+       syslog (LOG_ERR, "multimeter plugin: No device found");
+       return (-1);
 }
 #undef LINE_LENGTH
 
-static void multimeter_write (char *host, char *inst, char *val)
-{
-       rrd_update_file (host, multimeter_file, val, ds_def, ds_num);
-}
-#define BUFSIZE 128
-static void multimeter_submit (double *value)
+static void multimeter_submit (double value)
 {
-       char buf[BUFSIZE];
+       value_t values[1];
+       value_list_t vl = VALUE_LIST_INIT;
 
-       if (snprintf (buf, BUFSIZE, "%u:%f", (unsigned int) curtime, *value) >= BUFSIZE)
-               return;
+       values[0].gauge = value;
 
-       plugin_submit (MODULE_NAME, NULL, buf);
+       vl.values = values;
+       vl.values_len = 1;
+       vl.time = time (NULL);
+       strcpy (vl.host, hostname);
+       strcpy (vl.plugin, "multimeter");
+
+       plugin_dispatch_values ("multimeter", &vl);
 }
-#undef BUFSIZE
 
-static void multimeter_read (void)
+static int multimeter_read (void)
 {
        double value;
 
-       if (fd > -1 && !(multimeter_read_value(&value)))
-               multimeter_submit (&value);
+       if (fd < 0)
+               return (-1);
+
+       if (multimeter_read_value (&value) != 0)
+               return (-1);
+
+       multimeter_submit (value);
+       return (0);
+} /* int multimeter_read */
+
+static int multimeter_shutdown (void)
+{
+       if (fd >= 0)
+       {
+               close (fd);
+               fd = -1;
+       }
+
+       return (0);
 }
+#endif /* MULTIMETER_HAVE_READ */
 
 void module_register (void)
 {
-       plugin_register (MODULE_NAME, multimeter_init, multimeter_read, multimeter_write);
-}
+       plugin_register_data_set (&data_set);
 
-#undef MODULE_NAME
+#if MULTIMETER_HAVE_READ
+       plugin_register_init ("multimeter", multimeter_init);
+       plugin_register_read ("multimeter", multimeter_read);
+       plugin_register_shutdown ("multimeter", multimeter_shutdown);
+#endif /* MULTIMETER_HAVE_READ */
+}