2 * collectd - src/onewire.c
3 * Copyright (C) 2008 noris network AG
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at noris.net>
26 #include "utils_ignorelist.h"
29 #include <sys/types.h>
33 #define OW_FAMILY_LENGTH 8
34 #define OW_FAMILY_MAX_FEATURES 2
35 struct ow_family_features_s
37 char family[OW_FAMILY_LENGTH];
40 char filename[DATA_MAX_NAME_LEN];
41 char type[DATA_MAX_NAME_LEN];
42 char type_instance[DATA_MAX_NAME_LEN];
43 } features[OW_FAMILY_MAX_FEATURES];
46 typedef struct ow_family_features_s ow_family_features_t;
48 /* internal timing info collected in debug version only */
50 static struct timeval tv_begin, tv_end, tv_diff;
51 #endif /* COLLECT_DEBUG */
53 /* regexp to extract address (without family) and file from the owfs path */
54 static const char *regexp_to_match = "[A-Fa-f0-9]{2}\\.([A-Fa-f0-9]{12})/([[:alnum:]]+)$";
56 /* see http://owfs.sourceforge.net/ow_table.html for a list of families */
57 static ow_family_features_t ow_family_features[] =
59 { /* DS18S20 Precision Thermometer and DS1920 ibutton */
63 /* filename = */ "temperature",
64 /* type = */ "temperature",
65 /* type_instance = */ ""
68 /* features_num = */ 1
70 { /* DS1822 Econo Thermometer */
74 /* filename = */ "temperature",
75 /* type = */ "temperature",
76 /* type_instance = */ ""
79 /* features_num = */ 1
81 { /* DS18B20 Programmable Resolution Thermometer */
85 /* filename = */ "temperature",
86 /* type = */ "temperature",
87 /* type_instance = */ ""
90 /* features_num = */ 1
92 { /* DS2436 Volts/Temp */
96 /* filename = */ "temperature",
97 /* type = */ "temperature",
98 /* type_instance = */ ""
101 /* features_num = */ 1
103 { /* DS2438 Volts/Temp */
104 /* family = */ "26.",
107 /* filename = */ "temperature",
108 /* type = */ "temperature",
109 /* type_instance = */ ""
112 /* features_num = */ 1
115 static int ow_family_features_num = STATIC_ARRAY_SIZE (ow_family_features);
117 static char *device_g = NULL;
118 static cdtime_t ow_interval = 0;
119 static _Bool direct_access = 0;
121 static const char *config_keys[] =
128 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
130 static ignorelist_t *sensor_list;
132 static _Bool regex_direct_initialized = 0;
133 static regex_t regex_direct;
136 * List of onewire owfs "files" to be directly read
138 typedef struct direct_access_element_s
140 char *path; /**< The whole owfs path */
141 char *address; /**< 1-wire address without family */
142 char *file; /**< owfs file - e.g. temperature */
143 struct direct_access_element_s *next; /**< Next in the list */
144 } direct_access_element_t;
146 static direct_access_element_t * direct_list = NULL;
148 /* =================================================================================== */
151 /* Return 1 if the difference is negative, otherwise 0. */
152 static int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1)
154 long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
155 result->tv_sec = diff / 1000000;
156 result->tv_usec = diff % 1000000;
160 #endif /* COLLECT_DEBUG */
162 /* =================================================================================== */
164 static void direct_list_element_free(direct_access_element_t *el)
168 DEBUG ("onewire plugin: direct_list_element_free - deleting <%s>", el->path);
176 static int direct_list_insert(const char * config)
178 regmatch_t pmatch[3];
180 direct_access_element_t *element;
182 DEBUG ("onewire plugin: direct_list_insert <%s>", config);
184 element = malloc (sizeof (*element));
187 ERROR ("onewire plugin: direct_list_insert - cannot allocate element");
190 element->path = NULL;
191 element->address = NULL;
192 element->file = NULL;
194 element->path = strdup (config);
195 if (element->path == NULL)
197 ERROR ("onewire plugin: direct_list_insert - cannot allocate path");
198 direct_list_element_free (element);
202 DEBUG ("onewire plugin: direct_list_insert - about to match %s", config);
204 if (!regex_direct_initialized)
206 if (regcomp (®ex_direct, regexp_to_match, REG_EXTENDED))
208 ERROR ("onewire plugin: Cannot compile regex");
209 direct_list_element_free (element);
212 regex_direct_initialized = 1;
213 DEBUG ("onewire plugin: Compiled regex!!");
216 if (regexec (®ex_direct, config, nmatch, pmatch, 0))
218 ERROR ("onewire plugin: direct_list_insert - no regex match");
219 direct_list_element_free (element);
223 if (pmatch[1].rm_so<0)
225 ERROR ("onewire plugin: direct_list_insert - no address regex match");
226 direct_list_element_free (element);
229 element->address = strndup (config+pmatch[1].rm_so,
230 pmatch[1].rm_eo - pmatch[1].rm_so);
231 if (element->address == NULL)
233 ERROR ("onewire plugin: direct_list_insert - cannot allocate address");
234 direct_list_element_free (element);
237 DEBUG ("onewire plugin: direct_list_insert - found address <%s>",
240 if (pmatch[2].rm_so<0)
242 ERROR ("onewire plugin: direct_list_insert - no file regex match");
243 direct_list_element_free (element);
246 element->file = strndup (config+pmatch[2].rm_so,
247 pmatch[2].rm_eo - pmatch[2].rm_so);
248 if (element->file == NULL)
250 ERROR ("onewire plugin: direct_list_insert - cannot allocate file");
251 direct_list_element_free (element);
254 DEBUG ("onewire plugin: direct_list_insert - found file <%s>", element->file);
256 element->next = direct_list;
257 direct_list = element;
262 static void direct_list_free(void)
264 direct_access_element_t *traverse = direct_list;
265 direct_access_element_t *tmp = NULL;;
267 while(traverse != NULL)
270 traverse = traverse->next;
271 direct_list_element_free (tmp);
276 /* =================================================================================== */
278 static int cow_load_config (const char *key, const char *value)
280 if (sensor_list == NULL)
281 sensor_list = ignorelist_create (1);
283 if (strcasecmp (key, "Sensor") == 0)
285 if (direct_list_insert (value))
287 DEBUG ("onewire plugin: Cannot add %s to direct_list_insert.", value);
289 if (ignorelist_add (sensor_list, value))
291 ERROR ("onewire plugin: Cannot add value to ignorelist.");
297 DEBUG ("onewire plugin: %s is a direct access", value);
301 else if (strcasecmp (key, "IgnoreSelected") == 0)
303 ignorelist_set_invert (sensor_list, 1);
305 ignorelist_set_invert (sensor_list, 0);
307 else if (strcasecmp (key, "Device") == 0)
310 temp = strdup (value);
313 ERROR ("onewire plugin: strdup failed.");
319 else if (strcasecmp ("Interval", key) == 0)
324 ow_interval = DOUBLE_TO_CDTIME_T (tmp);
326 ERROR ("onewire plugin: Invalid `Interval' setting: %s", value);
336 static int cow_read_values (const char *path, const char *name,
337 const ow_family_features_t *family_info)
339 value_list_t vl = VALUE_LIST_INIT;
342 if (sensor_list != NULL)
344 DEBUG ("onewire plugin: Checking ignorelist for `%s'", name);
345 if (ignorelist_match (sensor_list, name) != 0)
349 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
350 sstrncpy (vl.plugin, "onewire", sizeof (vl.plugin));
351 sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance));
353 for (size_t i = 0; i < family_info->features_num; i++)
363 snprintf (file, sizeof (file), "%s/%s",
364 path, family_info->features[i].filename);
365 file[sizeof (file) - 1] = 0;
369 DEBUG ("Start reading onewire device %s", file);
370 status = OW_get (file, &buffer, &buffer_size);
373 ERROR ("onewire plugin: OW_get (%s/%s) failed. error = %s;",
374 path, family_info->features[i].filename, sstrerror(errno, errbuf, sizeof (errbuf)));
377 DEBUG ("Read onewire device %s as %s", file, buffer);
380 gauge_t g = strtod (buffer, &endptr);
383 ERROR ("onewire plugin: Buffer is not a number: %s", buffer);
387 sstrncpy (vl.type, family_info->features[i].type, sizeof (vl.type));
388 sstrncpy (vl.type_instance, family_info->features[i].type_instance,
389 sizeof (vl.type_instance));
391 vl.values = &(value_t) { .gauge = g };
394 plugin_dispatch_values (&vl);
398 } /* for (i = 0; i < features_num; i++) */
400 return ((success > 0) ? 0 : -1);
401 } /* int cow_read_values */
403 /* Forward declaration so the recursion below works */
404 static int cow_read_bus (const char *path);
410 * - DS2409 - MicroLAN Coupler
412 static int cow_read_ds2409 (const char *path)
417 status = ssnprintf (subpath, sizeof (subpath), "%s/main", path);
418 if ((status > 0) && (status < (int) sizeof (subpath)))
419 cow_read_bus (subpath);
421 status = ssnprintf (subpath, sizeof (subpath), "%s/aux", path);
422 if ((status > 0) && (status < (int) sizeof (subpath)))
423 cow_read_bus (subpath);
426 } /* int cow_read_ds2409 */
428 static int cow_read_bus (const char *path)
440 status = OW_get (path, &buffer, &buffer_size);
443 ERROR ("onewire plugin: OW_get (%s) failed. error = %s;",
444 path, sstrerror(errno, errbuf, sizeof (errbuf)));
447 DEBUG ("onewire plugin: OW_get (%s) returned: %s",
452 while ((buffer_ptr = strtok_r (dummy, ",/", &saveptr)) != NULL)
458 if (strcmp ("/", path) == 0)
459 status = ssnprintf (subpath, sizeof (subpath), "/%s", buffer_ptr);
461 status = ssnprintf (subpath, sizeof (subpath), "%s/%s",
463 if ((status <= 0) || (status >= (int) sizeof (subpath)))
466 for (i = 0; i < ow_family_features_num; i++)
468 if (strncmp (ow_family_features[i].family, buffer_ptr,
469 strlen (ow_family_features[i].family)) != 0)
472 cow_read_values (subpath,
473 buffer_ptr + strlen (ow_family_features[i].family),
474 ow_family_features + i);
477 if (i < ow_family_features_num)
481 if (strncmp ("1F.", buffer_ptr, strlen ("1F.")) == 0)
483 cow_read_ds2409 (subpath);
486 } /* while (strtok_r) */
490 } /* int cow_read_bus */
493 /* =================================================================================== */
495 static int cow_simple_read (void)
497 value_list_t vl = VALUE_LIST_INIT;
503 direct_access_element_t *traverse;
505 /* traverse list and check entries */
506 for (traverse = direct_list; traverse != NULL; traverse = traverse->next)
508 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
509 sstrncpy (vl.plugin, "onewire", sizeof (vl.plugin));
510 sstrncpy (vl.plugin_instance, traverse->address, sizeof (vl.plugin_instance));
512 status = OW_get (traverse->path, &buffer, &buffer_size);
515 ERROR ("onewire plugin: OW_get (%s) failed. status = %s;",
517 sstrerror(errno, errbuf, sizeof (errbuf)));
520 DEBUG ("onewire plugin: Read onewire device %s as %s", traverse->path, buffer);
523 gauge_t g = strtod (buffer, &endptr);
526 ERROR ("onewire plugin: Buffer is not a number: %s", buffer);
530 sstrncpy (vl.type, traverse->file, sizeof (vl.type));
531 sstrncpy (vl.type_instance, "", sizeof (""));
533 vl.values = &(value_t) { .gauge = g };
536 plugin_dispatch_values (&vl);
538 } /* for (traverse) */
541 } /* int cow_simple_read */
543 /* =================================================================================== */
545 static int cow_read (user_data_t *ud __attribute__((unused)))
550 gettimeofday (&tv_begin, NULL);
551 #endif /* COLLECT_DEBUG */
555 DEBUG ("onewire plugin: Direct access read");
556 result = cow_simple_read ();
560 DEBUG ("onewire plugin: Standard access read");
561 result = cow_read_bus ("/");
565 gettimeofday (&tv_end, NULL);
566 timeval_subtract (&tv_diff, &tv_end, &tv_begin);
567 DEBUG ("onewire plugin: Onewire read took us %ld.%06ld s",
570 #endif /* COLLECT_DEBUG */
575 static int cow_shutdown (void)
578 ignorelist_free (sensor_list);
582 if (regex_direct_initialized)
584 regfree(®ex_direct);
588 } /* int cow_shutdown */
590 static int cow_init (void)
595 if (device_g == NULL)
597 ERROR ("onewire plugin: cow_init: No device configured.");
601 DEBUG ("onewire plugin: about to init device <%s>.", device_g);
602 status = (int) OW_init (device_g);
605 ERROR ("onewire plugin: OW_init(%s) failed: %s.", device_g, sstrerror(errno, errbuf, sizeof (errbuf)));
609 plugin_register_complex_read (/* group = */ NULL, "onewire", cow_read,
610 ow_interval, /* user data = */ NULL);
611 plugin_register_shutdown ("onewire", cow_shutdown);
616 void module_register (void)
618 plugin_register_init ("onewire", cow_init);
619 plugin_register_config ("onewire", cow_load_config,
620 config_keys, config_keys_num);
623 /* vim: set sw=2 sts=2 ts=8 et fdm=marker cindent : */