2 * collectd - src/owfs.c
3 * Copyright (C) 2008 Florian octo Forster
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>
25 #include "utils_ignorelist.h"
29 #define OW_FAMILY_LENGTH 8
30 #define OW_FAMILY_MAX_FEATURES 2
31 struct ow_family_features_s
33 char family[OW_FAMILY_LENGTH];
36 char filename[DATA_MAX_NAME_LEN];
37 char type[DATA_MAX_NAME_LEN];
38 char type_instance[DATA_MAX_NAME_LEN];
39 } features[OW_FAMILY_MAX_FEATURES];
42 typedef struct ow_family_features_s ow_family_features_t;
44 /* see http://owfs.sourceforge.net/ow_table.html for a list of families */
45 static ow_family_features_t ow_family_features[] =
51 /* filename = */ "temperature",
52 /* type = */ "temperature",
53 /* type_instance = */ ""
56 /* features_num = */ 1
59 static int ow_family_features_num = STATIC_ARRAY_SIZE (ow_family_features);
61 static char *device_g = NULL;
63 static const char *config_keys[] =
69 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
71 static ignorelist_t *sensor_list;
73 static int cow_load_config (const char *key, const char *value)
75 if (sensor_list == NULL)
76 sensor_list = ignorelist_create (1);
78 if (strcasecmp (key, "Sensor") == 0)
80 if (ignorelist_add (sensor_list, value))
82 ERROR ("sensors plugin: "
83 "Cannot add value to ignorelist.");
87 else if (strcasecmp (key, "IgnoreSelected") == 0)
89 ignorelist_set_invert (sensor_list, 1);
90 if ((strcasecmp (value, "True") == 0)
91 || (strcasecmp (value, "Yes") == 0)
92 || (strcasecmp (value, "On") == 0))
93 ignorelist_set_invert (sensor_list, 0);
95 else if (strcasecmp (key, "Device") == 0)
98 temp = strdup (value);
101 ERROR ("onewire plugin: strdup failed.");
115 static int cow_init (void)
119 if (device_g == NULL)
121 ERROR ("onewire plugin: cow_init: No device configured.");
125 status = (int) OW_init (device_g);
128 ERROR ("onewire plugin: OW_init(%s) failed: %i.", device_g, status);
135 static int cow_read_values (const char *path, const char *name,
136 const ow_family_features_t *family_info)
139 value_list_t vl = VALUE_LIST_INIT;
143 if (sensor_list != NULL)
145 DEBUG ("onewire plugin: Checking ignorelist for `%s'", name);
146 if (ignorelist_match (sensor_list, name) != 0)
152 vl.time = time (NULL);
154 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
155 sstrncpy (vl.plugin, "onewire", sizeof (vl.plugin));
156 sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance));
158 for (i = 0; i < family_info->features_num; i++)
167 snprintf (file, sizeof (file), "%s/%s",
168 path, family_info->features[i].filename);
169 file[sizeof (file) - 1] = 0;
173 status = OW_get (file, &buffer, &buffer_size);
176 ERROR ("onewire plugin: OW_get (%s/%s) failed. status = %#x;",
177 path, family_info->features[i].filename, status);
182 values[0].gauge = strtod (buffer, &endptr);
185 ERROR ("onewire plugin: Buffer is not a number: %s", buffer);
190 sstrncpy (vl.type, family_info->features[i].type, sizeof (vl.type));
191 sstrncpy (vl.type_instance, family_info->features[i].type_instance,
192 sizeof (vl.type_instance));
194 plugin_dispatch_values (&vl);
198 } /* for (i = 0; i < features_num; i++) */
200 return ((success > 0) ? 0 : -1);
201 } /* int cow_read_values */
203 /* Forward declaration so the recursion below works */
204 static int cow_read_bus (const char *path);
210 * - DS2409 - MicroLAN Coupler
212 static int cow_read_ds2409 (const char *path)
217 status = ssnprintf (subpath, sizeof (subpath), "%s/main", path);
218 if ((status > 0) && (status < sizeof (subpath)))
219 cow_read_bus (subpath);
221 status = ssnprintf (subpath, sizeof (subpath), "%s/aux", path);
222 if ((status > 0) && (status < sizeof (subpath)))
223 cow_read_bus (subpath);
226 } /* int cow_read_ds2409 */
228 static int cow_read_bus (const char *path)
239 status = OW_get (path, &buffer, &buffer_size);
242 ERROR ("onewire plugin: OW_get (%s) failed. status = %#x;",
246 DEBUG ("onewire plugin: OW_get (%s) returned: %s",
251 while ((buffer_ptr = strtok_r (dummy, ",/", &saveptr)) != NULL)
257 if (strcmp ("/", path) == 0)
258 status = ssnprintf (subpath, sizeof (subpath), "/%s", buffer_ptr);
260 status = ssnprintf (subpath, sizeof (subpath), "%s/%s",
262 if ((status <= 0) || (status >= sizeof (subpath)))
265 for (i = 0; i < ow_family_features_num; i++)
267 if (strncmp (ow_family_features[i].family, buffer_ptr,
268 strlen (ow_family_features[i].family)) != 0)
271 cow_read_values (subpath,
272 buffer_ptr + strlen (ow_family_features[i].family),
273 ow_family_features + i);
276 if (i < ow_family_features_num)
280 if (strncmp ("1F.", buffer_ptr, strlen ("1F.")) == 0)
282 cow_read_ds2409 (subpath);
285 } /* while (strtok_r) */
289 } /* int cow_read_bus */
291 static int cow_read (void)
293 return (cow_read_bus ("/"));
296 static int cow_shutdown (void)
299 ignorelist_free (sensor_list);
301 } /* int cow_shutdown */
303 void module_register (void)
305 plugin_register_init ("onewire", cow_init);
306 plugin_register_read ("onewire", cow_read);
307 plugin_register_shutdown ("onewire", cow_shutdown);
308 plugin_register_config ("onewire", cow_load_config,
309 config_keys, config_keys_num);
312 /* vim: set sw=2 sts=2 ts=8 et fdm=marker cindent : */