summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 5415a1c)
raw | patch | inline | side by side (parent: 5415a1c)
author | Bert Vermeulen <bert@biot.com> | |
Fri, 26 Jul 2013 12:00:23 +0000 (14:00 +0200) | ||
committer | Bert Vermeulen <bert@biot.com> | |
Fri, 26 Jul 2013 18:34:17 +0000 (20:34 +0200) |
src/collectd.conf.in | patch | blob | history | |
src/collectd.conf.pod | patch | blob | history | |
src/sigrok.c | [new file with mode: 0644] | patch | blob |
diff --git a/src/collectd.conf.in b/src/collectd.conf.in
index 80aba6a46fbc2e84cc341d3ef33234015de6fa40..542be6745b0e9ea3398623534695dd027cf27c64 100644 (file)
--- a/src/collectd.conf.in
+++ b/src/collectd.conf.in
@LOAD_PLUGIN_RRDTOOL@LoadPlugin rrdtool
#@BUILD_PLUGIN_SENSORS_TRUE@LoadPlugin sensors
#@BUILD_PLUGIN_SERIAL_TRUE@LoadPlugin serial
+#@BUILD_PLUGIN_SIGROK_TRUE@LoadPlugin sigrok
#@BUILD_PLUGIN_SNMP_TRUE@LoadPlugin snmp
#@BUILD_PLUGIN_SWAP_TRUE@LoadPlugin swap
#@BUILD_PLUGIN_TABLE_TRUE@LoadPlugin table
# IgnoreSelected false
#</Plugin>
+#<Plugin sigrok>
+# LogLevel 3
+# <Device "AC Voltage">
+# Driver "fluke-dmm"
+# Interval 10
+# Conn "/dev/ttyUSB2"
+# </Device>
+# <Device "Sound Level">
+# Driver "cem-dt-885x"
+# Conn "/dev/ttyUSB1"
+# </Device>
+#</Plugin>
+
#<Plugin snmp>
# <Data "powerplus_voltge_input">
# Type "voltage"
diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod
index 11db1ccdc3d1e07c49f7c6efc93761ba43c0e21b..fe92aac8b3b5a04a323e6b21ab2cf266f0477d09 100644 (file)
--- a/src/collectd.conf.pod
+++ b/src/collectd.conf.pod
=back
+=head2 Plugin "sigrok"
+
+The I<sigrok> plugin uses libsigrok to retrieve measurements from any device
+supported by the L<sigrok|http://sigrok.org/> project.
+
+B<Synopsis>
+
+ <Plugin sigrok>
+ LogLevel 3
+ <Device "AC Voltage">
+ Driver "fluke-dmm"
+ Interval 10
+ Conn "/dev/ttyUSB2"
+ </Device>
+ <Device "Sound Level">
+ Driver "cem-dt-885x"
+ Conn "/dev/ttyUSB1"
+ </Device>
+ </Plugin>
+
+=over 4
+
+=item B<LogLevel> B<0-5>
+
+The sigrok logging level to pass on to the collectd log, as a number 0-5.
+These levels correspond to None, Errors, Warnings, Informational, Debug
+and Spew, respectively. The default is 2 (Warnings). The sigrok log messages,
+regardless of their level, are always submitted to collectd at its INFO
+log level.
+
+=item E<lt>B<Device> I<name>E<gt>
+
+A sigrok-supported device, uniquely identified by this section's options. The
+I<name> is passed to collectd as the I<plugin instance>.
+
+=item B<Driver>
+
+The sigrok driver to use for this device.
+
+=item B<Conn>
+
+If the device cannot be auto-discovered, or more than one might be discovered
+by the driver, I<Conn> specifies the connection string to the device. It can
+be of the form of a serial port (I</dev/ttyUSB2>), or, in case of a non-serial
+USB-connected device, the USB VendorID/ProductID separated by a period
+(I<0403.6001>). A USB device can also be specified as bus.address
+(I<1.41>).
+
+=item B<SerialComm>
+
+For serial devices with non-standard port settings, this option can be used
+to specify them in the form I<9600/8n1>. This should not be necessary; drivers
+know how to communicate with devices they support.
+
+=item B<Interval>
+
+Specifies the minimum time between measurement dispatches to collectd, in
+seconds. Since some sigrok-supported devices can acquire measurements many
+times per second, it may be necessary to throttle these. For example, the
+RRD plugin cannot process writes more than once per second.
+
+The default (and minimum) interval is 1 second. Unused measurements are
+discarded.
+
+=back
+
=head2 Plugin C<snmp>
Since the configuration of the C<snmp plugin> is a little more complicated than
diff --git a/src/sigrok.c b/src/sigrok.c
--- /dev/null
+++ b/src/sigrok.c
@@ -0,0 +1,361 @@
+/*
+ * collectd - src/sigrok.c
+ * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <pthread.h>
+
+#include <glib.h>
+#include <libsigrok/libsigrok.h>
+
+/* Minimum interval between dispatches coming from this plugin. The RRD
+ * plugin, at least, complains when written to with sub-second intervals.*/
+#define DEFAULT_MIN_DISPATCH_INTERVAL TIME_T_TO_CDTIME_T(1)
+
+static pthread_t sr_thread;
+static int sr_thread_running = FALSE;
+GSList *config_devices;
+static struct sr_session *session = NULL;
+static int num_devices;
+static int loglevel = SR_LOG_WARN;
+static struct sr_context *sr_ctx;
+
+struct config_device {
+ char *name;
+ char *driver;
+ char *conn;
+ char *serialcomm;
+ struct sr_dev_inst *sdi;
+ cdtime_t min_dispatch_interval;
+ cdtime_t last_dispatch;
+};
+
+
+static int cd_logger(void *cb_data, int msg_loglevel, const char *format,
+ va_list args)
+{
+ char s[512];
+
+ if (msg_loglevel <= loglevel) {
+ vsnprintf(s, 512, format, args);
+ plugin_log(LOG_INFO, "sigrok: %s", s);
+ }
+
+ return 0;
+}
+
+static int sigrok_config_device(oconfig_item_t *ci)
+{
+ oconfig_item_t *item;
+ struct config_device *cfdev;
+ int ret, i;
+
+ if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) {
+ ERROR("Invalid device name.");
+ return 1;
+ }
+
+ if (!(cfdev = malloc(sizeof(struct config_device)))) {
+ ERROR("malloc() failed.");
+ return 1;
+ }
+ memset(cfdev, 0, sizeof(struct config_device));
+ cf_util_get_string(ci, &cfdev->name);
+ cfdev->min_dispatch_interval = DEFAULT_MIN_DISPATCH_INTERVAL;
+
+ for (i = 0; i < ci->children_num; i++) {
+ item = ci->children + i;
+ if (item->values_num != 1) {
+ ERROR("Missing value for '%s'.", item->key);
+ return 1;
+ }
+ if (!strcasecmp(item->key, "driver"))
+ ret = cf_util_get_string(item, &cfdev->driver);
+ else if (!strcasecmp(item->key, "conn"))
+ ret = cf_util_get_string(item, &cfdev->conn);
+ else if (!strcasecmp(item->key, "serialcomm"))
+ ret = cf_util_get_string(item, &cfdev->serialcomm);
+ else if (!strcasecmp(item->key, "interval"))
+ ret = cf_util_get_cdtime(item, &cfdev->min_dispatch_interval);
+ if (ret) {
+ ERROR("Invalid keyword '%s'.", item->key);
+ return 1;
+ }
+ }
+
+ config_devices = g_slist_append(config_devices, cfdev);
+
+ return 0;
+}
+
+static int sigrok_config(oconfig_item_t *ci)
+{
+ oconfig_item_t *item;
+ int tmp, i;
+
+ for (i = 0; i < ci->children_num; i++) {
+ item = ci->children + i;
+ if (!strcasecmp(item->key, "loglevel")) {
+ if (cf_util_get_int(item, &tmp) || tmp < 0 || tmp > 5) {
+ ERROR("Invalid loglevel");
+ return 1;
+ }
+ loglevel = tmp;
+ } else if (!strcasecmp(item->key, "Device")) {
+ if (sigrok_config_device(item) != 0)
+ return 1;
+ } else {
+ ERROR("Invalid keyword '%s'.", item->key);
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+static void free_drvopts(struct sr_config *src)
+{
+ g_variant_unref(src->data);
+ g_free(src);
+}
+
+static void sigrok_feed_callback(const struct sr_dev_inst *sdi,
+ const struct sr_datafeed_packet *packet, void *cb_data)
+{
+ const struct sr_datafeed_analog *analog;
+ struct config_device *cfdev;
+ GSList *l;
+ value_t *values;
+ value_list_t vl = VALUE_LIST_INIT;
+ int num_probes, s, p;
+
+ if (packet->type == SR_DF_END) {
+ /* TODO: try to restart acquisition after a delay? */
+ INFO("oops! ended");
+ return;
+ }
+
+ /* Find this device's configuration. */
+ cfdev = NULL;
+ for (l = config_devices; l; l = l->next) {
+ cfdev = l->data;
+ if (cfdev->sdi == sdi) {
+ /* Found it. */
+ break;
+ }
+ cfdev = NULL;
+ }
+ if (!cfdev) {
+ ERROR("Unknown device instance in sigrok driver %s.", sdi->driver->name);
+ return;
+ }
+
+ if (packet->type == SR_DF_ANALOG) {
+ if (cdtime() - cfdev->last_dispatch < cfdev->min_dispatch_interval)
+ return;
+
+ analog = packet->payload;
+ num_probes = g_slist_length(analog->probes);
+ if (!(values = malloc(sizeof(value_t) * num_probes))) {
+ ERROR("malloc() failed.");
+ return;
+ }
+ for (s = 0; s < analog->num_samples; s++) {
+ for (p = 0; p < num_probes; p++) {
+ values[s + p].gauge = analog->data[s + p];
+ }
+ }
+ vl.values = values;
+ vl.values_len = num_probes;
+ sstrncpy(vl.host, hostname_g, sizeof(vl.host));
+ sstrncpy(vl.plugin, "sigrok", sizeof(vl.plugin));
+ ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance),
+ "%s", cfdev->name);
+ sstrncpy(vl.type, "gauge", sizeof(vl.type));
+ plugin_dispatch_values(&vl);
+
+ cfdev->last_dispatch = cdtime();
+ }
+
+}
+
+static void *thread_init(void *arg)
+{
+ struct sr_dev_driver *drv, **drvlist;
+ struct sr_config *src;
+ GSList *devlist, *drvopts, *l;
+ struct config_device *cfdev;
+ int ret, i;
+ char hwident[512];
+
+ (void)arg;
+
+ sr_log_callback_set(cd_logger, NULL);
+ sr_log_loglevel_set(loglevel);
+
+ if ((ret = sr_init(&sr_ctx)) != SR_OK) {
+ ERROR("Failed to initialize libsigrok: %s.", sr_strerror(ret));
+ return NULL;
+ }
+
+ if (!(session = sr_session_new()))
+ return NULL;
+
+ num_devices = 0;
+ drvlist = sr_driver_list();
+ for (l = config_devices; l; l = l->next) {
+ cfdev = l->data;
+ drv = NULL;
+ for (i = 0; drvlist[i]; i++) {
+ if (!strcmp(drvlist[i]->name, cfdev->driver)) {
+ drv = drvlist[i];
+ break;
+ }
+ }
+ if (!drv) {
+ ERROR("sigrok: Unknown driver '%s'.", cfdev->driver);
+ return NULL;
+ }
+
+ if (sr_driver_init(sr_ctx, drv) != SR_OK)
+ return NULL;
+
+ drvopts = NULL;
+ if (cfdev->conn) {
+ if (!(src = malloc(sizeof(struct sr_config))))
+ return NULL;
+ src->key = SR_CONF_CONN;
+ src->data = g_variant_new_string(cfdev->conn);
+ drvopts = g_slist_append(drvopts, src);
+ }
+ if (cfdev->serialcomm) {
+ if (!(src = malloc(sizeof(struct sr_config))))
+ return NULL;
+ src->key = SR_CONF_SERIALCOMM;
+ src->data = g_variant_new_string(cfdev->serialcomm);
+ drvopts = g_slist_append(drvopts, src);
+ }
+ devlist = sr_driver_scan(drv, drvopts);
+ g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
+ if (!devlist)
+ /* No devices found for this driver. */
+ continue;
+
+ if (g_slist_length(devlist) > 1) {
+ INFO("sigrok: %d sigrok devices for device entry '%s': must be 1.",
+ g_slist_length(devlist), cfdev->name);
+ return NULL;
+ }
+ cfdev->sdi = devlist->data;
+ g_slist_free(devlist);
+ ssnprintf(hwident, sizeof(hwident), "%s %s %s",
+ cfdev->sdi->vendor ? cfdev->sdi->vendor : "",
+ cfdev->sdi->model ? cfdev->sdi->model : "",
+ cfdev->sdi->version ? cfdev->sdi->version : "");
+ INFO("sigrok: Device '%s' is a %s.", cfdev->name, hwident);
+
+ if (sr_dev_open(cfdev->sdi) != SR_OK)
+ return NULL;
+
+ if (sr_session_dev_add(cfdev->sdi) != SR_OK)
+ return NULL;
+
+ num_devices++;
+ }
+
+ if (num_devices > 0) {
+ /* Do this only when we're sure there's hardware to talk to. */
+ if (sr_session_datafeed_callback_add(sigrok_feed_callback, NULL) != SR_OK)
+ return NULL;
+
+ /* Start acquisition on all devices. */
+ if (sr_session_start() != SR_OK)
+ return NULL;
+
+ /* Main loop, runs forever. */
+ sr_session_run();
+
+ sr_session_stop();
+ sr_session_dev_remove_all();
+ }
+
+ sr_session_destroy();
+
+ sr_exit(sr_ctx);
+
+ pthread_exit(NULL);
+ sr_thread_running = FALSE;
+
+ return NULL;
+}
+
+static int sigrok_init(void)
+{
+ int status;
+
+ if (sr_thread_running) {
+ ERROR("sigrok: Thread already running.");
+ return -1;
+ }
+
+ if ((status = plugin_thread_create(&sr_thread, NULL, thread_init, NULL)) != 0) {
+ ERROR("sigrok: Failed to create thread: %s.", strerror(status));
+ return -1;
+ }
+ sr_thread_running = TRUE;
+
+ return 0;
+}
+
+static int sigrok_shutdown(void)
+{
+ struct config_device *cfdev;
+ GSList *l;
+
+ if (sr_thread_running) {
+ pthread_cancel(sr_thread);
+ pthread_join(sr_thread, NULL);
+ }
+
+ for (l = config_devices; l; l = l->next) {
+ cfdev = l->data;
+ free(cfdev->name);
+ free(cfdev->driver);
+ free(cfdev->conn);
+ free(cfdev->serialcomm);
+ free(cfdev);
+ }
+ g_slist_free(config_devices);
+
+ return 0;
+}
+
+void module_register(void)
+{
+
+ plugin_register_complex_config("sigrok", sigrok_config);
+ plugin_register_init("sigrok", sigrok_init);
+ plugin_register_shutdown("sigrok", sigrok_shutdown);
+
+}