1 /**
2 * collectd - src/owfs.c
3 * Copyright (C) 2008 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at noris.net>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_ignorelist.h"
27 #include <owcapi.h>
29 #define OW_FAMILY_LENGTH 8
30 #define OW_FAMILY_MAX_FEATURES 2
31 struct ow_family_features_s
32 {
33 char family[OW_FAMILY_LENGTH];
34 struct
35 {
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];
40 size_t features_num;
41 };
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[] =
46 {
47 {
48 /* family = */ "10.",
49 {
50 {
51 /* filename = */ "temperature",
52 /* type = */ "temperature",
53 /* type_instance = */ ""
54 }
55 },
56 /* features_num = */ 1
57 }
58 };
59 static int ow_family_features_num = STATIC_ARRAY_SIZE (ow_family_features);
61 static char *device_g = NULL;
62 static int ow_interval = 0;
64 static const char *config_keys[] =
65 {
66 "Device",
67 "IgnoreSelected",
68 "Sensor",
69 "Interval"
70 };
71 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
73 static ignorelist_t *sensor_list;
75 static int cow_load_config (const char *key, const char *value)
76 {
77 if (sensor_list == NULL)
78 sensor_list = ignorelist_create (1);
80 if (strcasecmp (key, "Sensor") == 0)
81 {
82 if (ignorelist_add (sensor_list, value))
83 {
84 ERROR ("sensors plugin: "
85 "Cannot add value to ignorelist.");
86 return (1);
87 }
88 }
89 else if (strcasecmp (key, "IgnoreSelected") == 0)
90 {
91 ignorelist_set_invert (sensor_list, 1);
92 if ((strcasecmp (value, "True") == 0)
93 || (strcasecmp (value, "Yes") == 0)
94 || (strcasecmp (value, "On") == 0))
95 ignorelist_set_invert (sensor_list, 0);
96 }
97 else if (strcasecmp (key, "Device") == 0)
98 {
99 char *temp;
100 temp = strdup (value);
101 if (temp == NULL)
102 {
103 ERROR ("onewire plugin: strdup failed.");
104 return (1);
105 }
106 sfree (device_g);
107 device_g = temp;
108 }
109 else if (strcasecmp ("Interval", key) == 0)
110 {
111 int tmp;
112 tmp = atoi (value);
113 if (tmp > 0)
114 ow_interval = tmp;
115 else
116 ERROR ("onewire plugin: Invalid `Interval' setting: %s", value);
117 }
118 else
119 {
120 return (-1);
121 }
123 return (0);
124 }
126 static int cow_read_values (const char *path, const char *name,
127 const ow_family_features_t *family_info)
128 {
129 value_t values[1];
130 value_list_t vl = VALUE_LIST_INIT;
131 int success = 0;
132 size_t i;
134 if (sensor_list != NULL)
135 {
136 DEBUG ("onewire plugin: Checking ignorelist for `%s'", name);
137 if (ignorelist_match (sensor_list, name) != 0)
138 return 0;
139 }
141 vl.values = values;
142 vl.values_len = 1;
144 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
145 sstrncpy (vl.plugin, "onewire", sizeof (vl.plugin));
146 sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance));
148 for (i = 0; i < family_info->features_num; i++)
149 {
150 char *buffer;
151 size_t buffer_size;
152 int status;
154 char file[4096];
155 char *endptr;
157 snprintf (file, sizeof (file), "%s/%s",
158 path, family_info->features[i].filename);
159 file[sizeof (file) - 1] = 0;
161 buffer = NULL;
162 buffer_size = 0;
163 status = OW_get (file, &buffer, &buffer_size);
164 if (status < 0)
165 {
166 ERROR ("onewire plugin: OW_get (%s/%s) failed. status = %#x;",
167 path, family_info->features[i].filename, status);
168 return (-1);
169 }
171 endptr = NULL;
172 values[0].gauge = strtod (buffer, &endptr);
173 if (endptr == NULL)
174 {
175 ERROR ("onewire plugin: Buffer is not a number: %s", buffer);
176 status = -1;
177 continue;
178 }
180 sstrncpy (vl.type, family_info->features[i].type, sizeof (vl.type));
181 sstrncpy (vl.type_instance, family_info->features[i].type_instance,
182 sizeof (vl.type_instance));
184 plugin_dispatch_values (&vl);
185 success++;
187 free (buffer);
188 } /* for (i = 0; i < features_num; i++) */
190 return ((success > 0) ? 0 : -1);
191 } /* int cow_read_values */
193 /* Forward declaration so the recursion below works */
194 static int cow_read_bus (const char *path);
196 /*
197 * cow_read_ds2409
198 *
199 * Handles:
200 * - DS2409 - MicroLAN Coupler
201 */
202 static int cow_read_ds2409 (const char *path)
203 {
204 char subpath[4096];
205 int status;
207 status = ssnprintf (subpath, sizeof (subpath), "%s/main", path);
208 if ((status > 0) && (status < sizeof (subpath)))
209 cow_read_bus (subpath);
211 status = ssnprintf (subpath, sizeof (subpath), "%s/aux", path);
212 if ((status > 0) && (status < sizeof (subpath)))
213 cow_read_bus (subpath);
215 return (0);
216 } /* int cow_read_ds2409 */
218 static int cow_read_bus (const char *path)
219 {
220 char *buffer;
221 size_t buffer_size;
222 int status;
224 char *buffer_ptr;
225 char *dummy;
226 char *saveptr;
227 char subpath[4096];
229 status = OW_get (path, &buffer, &buffer_size);
230 if (status < 0)
231 {
232 ERROR ("onewire plugin: OW_get (%s) failed. status = %#x;",
233 path, status);
234 return (-1);
235 }
236 DEBUG ("onewire plugin: OW_get (%s) returned: %s",
237 path, buffer);
239 dummy = buffer;
240 saveptr = NULL;
241 while ((buffer_ptr = strtok_r (dummy, ",/", &saveptr)) != NULL)
242 {
243 int i;
245 dummy = NULL;
247 if (strcmp ("/", path) == 0)
248 status = ssnprintf (subpath, sizeof (subpath), "/%s", buffer_ptr);
249 else
250 status = ssnprintf (subpath, sizeof (subpath), "%s/%s",
251 path, buffer_ptr);
252 if ((status <= 0) || (status >= sizeof (subpath)))
253 continue;
255 for (i = 0; i < ow_family_features_num; i++)
256 {
257 if (strncmp (ow_family_features[i].family, buffer_ptr,
258 strlen (ow_family_features[i].family)) != 0)
259 continue;
261 cow_read_values (subpath,
262 buffer_ptr + strlen (ow_family_features[i].family),
263 ow_family_features + i);
264 break;
265 }
266 if (i < ow_family_features_num)
267 continue;
269 /* DS2409 */
270 if (strncmp ("1F.", buffer_ptr, strlen ("1F.")) == 0)
271 {
272 cow_read_ds2409 (subpath);
273 continue;
274 }
275 } /* while (strtok_r) */
277 free (buffer);
278 return (0);
279 } /* int cow_read_bus */
281 static int cow_read (user_data_t *ud __attribute__((unused)))
282 {
283 return (cow_read_bus ("/"));
284 } /* int cow_read */
286 static int cow_shutdown (void)
287 {
288 OW_finish ();
289 ignorelist_free (sensor_list);
290 return (0);
291 } /* int cow_shutdown */
293 static int cow_init (void)
294 {
295 int status;
296 struct timespec cb_interval;
298 if (device_g == NULL)
299 {
300 ERROR ("onewire plugin: cow_init: No device configured.");
301 return (-1);
302 }
304 status = (int) OW_init (device_g);
305 if (status != 0)
306 {
307 ERROR ("onewire plugin: OW_init(%s) failed: %i.", device_g, status);
308 return (1);
309 }
311 memset (&cb_interval, 0, sizeof (cb_interval));
312 if (ow_interval > 0)
313 cb_interval.tv_sec = (time_t) ow_interval;
315 plugin_register_complex_read ("onewire", cow_read,
316 &cb_interval, /* user data = */ NULL);
317 plugin_register_shutdown ("onewire", cow_shutdown);
319 return (0);
320 } /* int cow_init */
322 void module_register (void)
323 {
324 plugin_register_init ("onewire", cow_init);
325 plugin_register_config ("onewire", cow_load_config,
326 config_keys, config_keys_num);
327 }
329 /* vim: set sw=2 sts=2 ts=8 et fdm=marker cindent : */