author | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Tue, 31 Mar 2009 07:45:33 +0000 (09:45 +0200) | ||
committer | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Tue, 31 Mar 2009 07:45:33 +0000 (09:45 +0200) |
README | patch | blob | history | |
configure.in | patch | blob | history | |
src/Makefile.am | patch | blob | history | |
src/collectd.conf.pod | patch | blob | history | |
src/ted.c | [new file with mode: 0644] | patch | blob |
index a0ce7640f8aba48b0c5eb1c89afb7472b08cfd40..c556c750ce9eeca1d457f8b8d9dc648ff6bc9901 100644 (file)
--- a/README
+++ b/README
- teamspeak2
TeamSpeak2 server statistics.
+ - ted
+ Plugin to read values from `The Energy Detective' (TED).
+
- thermal
Linux ACPI thermal zone information.
diff --git a/configure.in b/configure.in
index 95b2755e4fac4743f5185dcdcb033f2b0b4c8d2a..08bd8a2e721eaea5296bb76dca79f21955b3f1c8 100644 (file)
--- a/configure.in
+++ b/configure.in
plugin_swap="no"
plugin_tape="no"
plugin_tcpconns="no"
+plugin_ted="no"
plugin_thermal="no"
plugin_users="no"
plugin_uptime="no"
if test "x$have_termios_h" = "xyes"
then
plugin_multimeter="yes"
+ plugin_ted="yes"
fi
if test "x$have_thread_info" = "xyes"
AC_PLUGIN([target_set], [yes], [The set target])
AC_PLUGIN([tcpconns], [$plugin_tcpconns], [TCP connection statistics])
AC_PLUGIN([teamspeak2], [yes], [TeamSpeak2 server statistics])
+AC_PLUGIN([ted], [$plugin_ted], [Read The Energy Detective values])
AC_PLUGIN([thermal], [$plugin_thermal], [Linux ACPI thermal zone statistics])
AC_PLUGIN([unixsock], [yes], [Unixsock communication plugin])
AC_PLUGIN([uptime], [$plugin_uptime], [Uptime statistics])
target_set . . . . . $enable_target_set
tcpconns . . . . . . $enable_tcpconns
teamspeak2 . . . . . $enable_teamspeak2
+ ted . . . . . . . . . $enable_ted
thermal . . . . . . . $enable_thermal
unixsock . . . . . . $enable_unixsock
uptime . . . . . . . $enable_uptime
diff --git a/src/Makefile.am b/src/Makefile.am
index a8c8c03dd5e7785ddeb9336ff19de84fe0522555..fee47b17272a1414629bd8f32769f6b16d666064 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
collectd_DEPENDENCIES += teamspeak2.la
endif
+if BUILD_PLUGIN_TED
+pkglib_LTLIBRARIES += ted.la
+ted_la_SOURCES = ted.c
+ted_la_LDFLAGS = -module -avoid-version
+collectd_LDADD += "-dlopen" ted.la
+collectd_DEPENDENCIES += ted.la
+endif
+
if BUILD_PLUGIN_THERMAL
pkglib_LTLIBRARIES += thermal.la
thermal_la_SOURCES = thermal.c
diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod
index 7feb198e9cd4e68546b96b122fe4e636d07d6944..7c9c6cb5f9b0d5b0b55b8e80a64a91dc5b0874e4 100644 (file)
--- a/src/collectd.conf.pod
+++ b/src/collectd.conf.pod
=back
+=head2 Plugin C<ted>
+
+The I<TED> plugin connects to a device of "The Energy Detective", a device to
+measure power consumption. These devices are usually connected to a serial
+(RS232) or USB port. The plugin opens a configured device and tries to read the
+current energy readings. For more information on TED, visit
+L<http://www.theenergydetective.com/>.
+
+Available configuration options:
+
+=over 4
+
+=item B<Device> I<Path>
+
+Path to the device on which TED is connected. collectd will need read and write
+permissions on that file.
+
+Default: B</dev/ttyUSB0>
+
+=item B<Retries> I<Num>
+
+Apparently reading from TED is not that reliable. You can therefore configure a
+number of retries here. You only configure the I<retries> here, to if you
+specify zero, one reading will be performed (but no retries if that fails); if
+you specify three, a maximum of four readings are performed. Negative values
+are illegal.
+
+Default: B<0>
+
+=back
+
=head2 Plugin C<tcpconns>
The C<tcpconns plugin> counts the number of currently established TCP
diff --git a/src/ted.c b/src/ted.c
--- /dev/null
+++ b/src/ted.c
@@ -0,0 +1,360 @@
+/**
+ * collectd - src/ted.c
+ * Copyright (C) 2009 Eric Reed
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors:
+ * Eric Reed <ericr at reedhome.net>
+ *
+ * This is a collectd module for The Energy Detective: A low-cost whole
+ * house energy monitoring system. For more information on TED, see
+ * http://theenergydetective.com
+ *
+ * This module was not created by Energy, Inc. nor is it supported by
+ * them in any way. It was created using information from two sources:
+ * David Satterfield's TED module for Misterhouse, and Micah Dowty's TED
+ * Python Module.
+ *
+ * This has only tested with the model 1001 RDU, with
+ * firmware version 9.01U. The USB port is uses the very common FTDI
+ * USB-to-serial chip, so the RDU will show up as a serial device on
+ * Windows, Mac OS, or Linux.
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+#include "configfile.h"
+
+#if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H && HAVE_MATH_H
+# include <termios.h>
+# include <sys/ioctl.h>
+# include <math.h>
+#else
+# error "No applicable input method."
+#endif
+
+#define EXPECTED_PACKAGE_LENGTH 278
+#define ESCAPE 0x10
+#define PKT_BEGIN 0x04
+#define PKT_END 0x03
+
+#define DEFAULT_DEVICE "/dev/ttyUSB0"
+
+static char *conf_device = NULL;
+static int conf_retries = 0;
+
+static int fd = -1;
+
+static const char *config_keys[] =
+{
+ "Device",
+ "Retries"
+};
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
+
+static int ted_read_value(double *ret_power, double *ret_voltage)
+{
+ unsigned char receive_buffer[300];
+ unsigned char package_buffer[300];
+ char pkt_request[1] = {0xAA};
+ int package_buffer_pos;
+
+ fd_set input;
+ struct timeval timeout;
+
+ int end_flag;
+ int escape_flag;
+
+ int status;
+
+ assert (fd >= 0);
+
+ /* Initialize the input set*/
+ FD_ZERO (&input);
+ FD_SET (fd, &input);
+
+ /* Initialize timeout structure, set to 2 seconds */
+ memset (&timeout, 0, sizeof (timeout));
+ timeout.tv_sec = 2;
+ timeout.tv_usec = 0;
+
+ /* clear out anything in the buffer */
+ tcflush (fd, TCIFLUSH);
+
+ status = write (fd, pkt_request, sizeof(pkt_request));
+ if (status <= 0)
+ {
+ ERROR ("ted plugin: swrite failed.");
+ return (-1);
+ }
+
+ /* Loop until we find the end of the package */
+ end_flag = 0;
+ escape_flag = 0;
+ package_buffer_pos = 0;
+ while (end_flag == 0)
+ {
+ ssize_t receive_buffer_length;
+ ssize_t i;
+
+ /* check for timeout or input error*/
+ status = select (fd + 1, &input, NULL, NULL, &timeout);
+ if (status == 0) /* Timeout */
+ {
+ WARNING ("ted plugin: Timeout while waiting for file descriptor "
+ "to become ready.");
+ return (-1);
+ }
+ else if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
+ {
+ /* Some signal or something. Start over.. */
+ continue;
+ }
+ else if (status < 0)
+ {
+ char errbuf[1024];
+ ERROR ("ted plugin: select failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return (-1);
+ }
+
+ receive_buffer_length = read (fd, receive_buffer, sizeof (receive_buffer));
+ if (receive_buffer_length < 0)
+ {
+ char errbuf[1024];
+ if ((errno == EAGAIN) || (errno == EINTR))
+ continue;
+ ERROR ("ted plugin: read(2) failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return (-1);
+ }
+ else if (receive_buffer_length == 0)
+ {
+ /* Should we close the FD in this case? */
+ WARNING ("ted plugin: Received EOF from file descriptor.");
+ return (-1);
+ }
+ else if (receive_buffer_length > sizeof (receive_buffer))
+ {
+ ERROR ("ted plugin: read(2) returned invalid value %zi.",
+ receive_buffer_length);
+ return (-1);
+ }
+
+ /*
+ * packet filter loop
+ *
+ * Handle escape sequences in `receive_buffer' and put the
+ * result in `package_buffer'.
+ */
+ /* We need to see the begin sequence first. When we receive `ESCAPE
+ * PKT_BEGIN', we set `package_buffer_pos' to zero to signal that
+ * the beginning of the package has been found. */
+
+ escape_flag = 0;
+ for (i = 0; i < receive_buffer_length; i++)
+ {
+ /* Check if previous byte was the escape byte. */
+ if (escape_flag == 1)
+ {
+ escape_flag = 0;
+ /* escape escape = single escape */
+ if ((receive_buffer[i] == ESCAPE)
+ && (package_buffer_pos >= 0))
+ {
+ package_buffer[package_buffer_pos] = ESCAPE;
+ package_buffer_pos++;
+ }
+ else if (receive_buffer[i] == PKT_BEGIN)
+ {
+ package_buffer_pos = 0;
+ }
+ else if (receive_buffer[i] == PKT_END)
+ {
+ end_flag = 1;
+ break;
+ }
+ else
+ {
+ DEBUG ("ted plugin: Unknown escaped byte: %#x",
+ (unsigned int) receive_buffer[i]);
+ }
+ }
+ else if (receive_buffer[i] == ESCAPE)
+ {
+ escape_flag = 1;
+ }
+ /* if we are in a package add byte to buffer
+ * otherwise throw away */
+ else if (package_buffer_pos >= 0)
+ {
+ package_buffer[package_buffer_pos] = receive_buffer[i];
+ package_buffer_pos++;
+ }
+ } /* for (i = 0; i < receive_buffer_length; i++) */
+ } /* while (end_flag == 0) */
+
+ /* Check for errors inside the loop. */
+ if ((end_flag == 0) || (package_buffer_pos != EXPECTED_PACKAGE_LENGTH))
+ return (-1);
+
+ /*
+ * Power is at positions 247 and 248 (LSB first) in [10kW].
+ * Voltage is at positions 251 and 252 (LSB first) in [.1V].
+ *
+ * Power is in 10 Watt steps
+ * Voltage is in volts
+ */
+ *ret_power = 10.0 * (double) ((((int) package_buffer[248]) * 256)
+ + ((int) package_buffer[247]));
+ *ret_voltage = 0.1 * (double) ((((int) package_buffer[252]) * 256)
+ + ((int) package_buffer[251]));
+
+ /* success */
+ return (0);
+} /* int ted_read_value */
+
+static int ted_open_device (void)
+{
+ const char *dev;
+ struct termios options;
+
+ if (fd >= 0)
+ return (0);
+
+ dev = DEFAULT_DEVICE;
+ if (conf_device != NULL)
+ dev = conf_device;
+
+ fd = open (dev, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
+ if (fd < 0)
+ {
+ ERROR ("ted plugin: Unable to open device %s.", dev);
+ return (-1);
+ }
+
+ /* Get the current options for the port... */
+ tcgetattr(fd, &options);
+ options.c_cflag = B19200 | CS8 | CSTOPB | CREAD | CLOCAL;
+ options.c_iflag = IGNBRK | IGNPAR;
+ options.c_oflag = 0;
+ options.c_lflag = 0;
+ options.c_cc[VTIME] = 20;
+ options.c_cc[VMIN] = 250;
+
+ /* Set the new options for the port... */
+ tcflush(fd, TCIFLUSH);
+ tcsetattr(fd, TCSANOW, &options);
+
+ INFO ("ted plugin: Successfully opened %s.", dev);
+ return (0);
+} /* int ted_open_device */
+
+static void ted_submit (char *type, double value)
+{
+ value_t values[1];
+ value_list_t vl = VALUE_LIST_INIT;
+
+ values[0].gauge = value;
+
+ vl.time = time (NULL);
+ vl.values = values;
+ vl.values_len = 1;
+ sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+ sstrncpy (vl.plugin, "ted", sizeof (vl.plugin));
+ sstrncpy (vl.type, type, sizeof (vl.type));
+
+ plugin_dispatch_values (&vl);
+}
+
+static int ted_config (const char *key, const char *value)
+{
+ if (strcasecmp ("Device", key) == 0)
+ {
+ sfree (conf_device);
+ conf_device = sstrdup (value);
+ }
+ else if (strcasecmp ("Retries", key) == 0)
+ {
+ int tmp;
+
+ tmp = atoi (value);
+ if (tmp < 0)
+ {
+ WARNING ("ted plugin: Invalid retry count: %i", tmp);
+ return (1);
+ }
+ conf_retries = tmp;
+ }
+ else
+ {
+ ERROR ("ted plugin: Unknown config option: %s", key);
+ return (-1);
+ }
+
+ return (0);
+} /* int ted_config */
+
+static int ted_read (void)
+{
+ double power;
+ double voltage;
+ int status;
+ int i;
+
+ status = ted_open_device ();
+ if (status != 0)
+ return (-1);
+
+ power = NAN;
+ voltage = NAN;
+ for (i = 0; i <= conf_retries; i++)
+ {
+ status = ted_read_value (&power, &voltage);
+ if (status == 0)
+ break;
+ }
+
+ if (status != 0)
+ return (-1);
+
+ ted_submit ("power", power);
+ ted_submit ("voltage", voltage);
+
+ return (0);
+} /* int ted_read */
+
+static int ted_shutdown (void)
+{
+ if (fd >= 0)
+ {
+ close (fd);
+ fd = -1;
+ }
+
+ return (0);
+} /* int ted_shutdown */
+
+void module_register (void)
+{
+ plugin_register_config ("ted", ted_config,
+ config_keys, config_keys_num);
+ plugin_register_read ("ted", ted_read);
+ plugin_register_shutdown ("ted", ted_shutdown);
+} /* void module_register */
+
+/* vim: set sw=4 et : */