6e3f8da6817cfe667aaf17f7bff2afaa23bd945f
1 /**
2 * collectd - src/onewire.c
3 * Copyright (C) 2008 noris network AG
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 { /* DS18S20 Precision Thermometer and DS1920 ibutton */
48 /* family = */ "10.",
49 {
50 {
51 /* filename = */ "temperature",
52 /* type = */ "temperature",
53 /* type_instance = */ ""
54 }
55 },
56 /* features_num = */ 1
57 },
58 { /* DS1822 Econo Thermometer */
59 /* family = */ "22.",
60 {
61 {
62 /* filename = */ "temperature",
63 /* type = */ "temperature",
64 /* type_instance = */ ""
65 }
66 },
67 /* features_num = */ 1
68 },
69 { /* DS18B20 Programmable Resolution Thermometer */
70 /* family = */ "28.",
71 {
72 {
73 /* filename = */ "temperature",
74 /* type = */ "temperature",
75 /* type_instance = */ ""
76 }
77 },
78 /* features_num = */ 1
79 },
80 { /* DS2436 Volts/Temp */
81 /* family = */ "1B.",
82 {
83 {
84 /* filename = */ "temperature",
85 /* type = */ "temperature",
86 /* type_instance = */ ""
87 }
88 },
89 /* features_num = */ 1
90 },
91 { /* DS2438 Volts/Temp */
92 /* family = */ "26.",
93 {
94 {
95 /* filename = */ "temperature",
96 /* type = */ "temperature",
97 /* type_instance = */ ""
98 }
99 },
100 /* features_num = */ 1
101 }
102 };
103 static int ow_family_features_num = STATIC_ARRAY_SIZE (ow_family_features);
105 static char *device_g = NULL;
106 static cdtime_t ow_interval = 0;
108 static const char *config_keys[] =
109 {
110 "Device",
111 "IgnoreSelected",
112 "Sensor",
113 "Interval"
114 };
115 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
117 static ignorelist_t *sensor_list;
119 static int cow_load_config (const char *key, const char *value)
120 {
121 if (sensor_list == NULL)
122 sensor_list = ignorelist_create (1);
124 if (strcasecmp (key, "Sensor") == 0)
125 {
126 if (ignorelist_add (sensor_list, value))
127 {
128 ERROR ("sensors plugin: "
129 "Cannot add value to ignorelist.");
130 return (1);
131 }
132 }
133 else if (strcasecmp (key, "IgnoreSelected") == 0)
134 {
135 ignorelist_set_invert (sensor_list, 1);
136 if (IS_TRUE (value))
137 ignorelist_set_invert (sensor_list, 0);
138 }
139 else if (strcasecmp (key, "Device") == 0)
140 {
141 char *temp;
142 temp = strdup (value);
143 if (temp == NULL)
144 {
145 ERROR ("onewire plugin: strdup failed.");
146 return (1);
147 }
148 sfree (device_g);
149 device_g = temp;
150 }
151 else if (strcasecmp ("Interval", key) == 0)
152 {
153 double tmp;
154 tmp = atof (value);
155 if (tmp > 0.0)
156 ow_interval = DOUBLE_TO_CDTIME_T (tmp);
157 else
158 ERROR ("onewire plugin: Invalid `Interval' setting: %s", value);
159 }
160 else
161 {
162 return (-1);
163 }
165 return (0);
166 }
168 static int cow_read_values (const char *path, const char *name,
169 const ow_family_features_t *family_info)
170 {
171 value_t values[1];
172 value_list_t vl = VALUE_LIST_INIT;
173 int success = 0;
174 size_t i;
176 if (sensor_list != NULL)
177 {
178 DEBUG ("onewire plugin: Checking ignorelist for `%s'", name);
179 if (ignorelist_match (sensor_list, name) != 0)
180 return 0;
181 }
183 vl.values = values;
184 vl.values_len = 1;
186 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
187 sstrncpy (vl.plugin, "onewire", sizeof (vl.plugin));
188 sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance));
190 for (i = 0; i < family_info->features_num; i++)
191 {
192 char *buffer;
193 size_t buffer_size;
194 int status;
196 char file[4096];
197 char *endptr;
199 snprintf (file, sizeof (file), "%s/%s",
200 path, family_info->features[i].filename);
201 file[sizeof (file) - 1] = 0;
203 buffer = NULL;
204 buffer_size = 0;
205 status = OW_get (file, &buffer, &buffer_size);
206 if (status < 0)
207 {
208 ERROR ("onewire plugin: OW_get (%s/%s) failed. status = %#x;",
209 path, family_info->features[i].filename, status);
210 return (-1);
211 }
213 endptr = NULL;
214 values[0].gauge = strtod (buffer, &endptr);
215 if (endptr == NULL)
216 {
217 ERROR ("onewire plugin: Buffer is not a number: %s", buffer);
218 status = -1;
219 continue;
220 }
222 sstrncpy (vl.type, family_info->features[i].type, sizeof (vl.type));
223 sstrncpy (vl.type_instance, family_info->features[i].type_instance,
224 sizeof (vl.type_instance));
226 plugin_dispatch_values (&vl);
227 success++;
229 free (buffer);
230 } /* for (i = 0; i < features_num; i++) */
232 return ((success > 0) ? 0 : -1);
233 } /* int cow_read_values */
235 /* Forward declaration so the recursion below works */
236 static int cow_read_bus (const char *path);
238 /*
239 * cow_read_ds2409
240 *
241 * Handles:
242 * - DS2409 - MicroLAN Coupler
243 */
244 static int cow_read_ds2409 (const char *path)
245 {
246 char subpath[4096];
247 int status;
249 status = ssnprintf (subpath, sizeof (subpath), "%s/main", path);
250 if ((status > 0) && (status < sizeof (subpath)))
251 cow_read_bus (subpath);
253 status = ssnprintf (subpath, sizeof (subpath), "%s/aux", path);
254 if ((status > 0) && (status < sizeof (subpath)))
255 cow_read_bus (subpath);
257 return (0);
258 } /* int cow_read_ds2409 */
260 static int cow_read_bus (const char *path)
261 {
262 char *buffer;
263 size_t buffer_size;
264 int status;
266 char *buffer_ptr;
267 char *dummy;
268 char *saveptr;
269 char subpath[4096];
271 status = OW_get (path, &buffer, &buffer_size);
272 if (status < 0)
273 {
274 ERROR ("onewire plugin: OW_get (%s) failed. status = %#x;",
275 path, status);
276 return (-1);
277 }
278 DEBUG ("onewire plugin: OW_get (%s) returned: %s",
279 path, buffer);
281 dummy = buffer;
282 saveptr = NULL;
283 while ((buffer_ptr = strtok_r (dummy, ",/", &saveptr)) != NULL)
284 {
285 int i;
287 dummy = NULL;
289 if (strcmp ("/", path) == 0)
290 status = ssnprintf (subpath, sizeof (subpath), "/%s", buffer_ptr);
291 else
292 status = ssnprintf (subpath, sizeof (subpath), "%s/%s",
293 path, buffer_ptr);
294 if ((status <= 0) || (status >= sizeof (subpath)))
295 continue;
297 for (i = 0; i < ow_family_features_num; i++)
298 {
299 if (strncmp (ow_family_features[i].family, buffer_ptr,
300 strlen (ow_family_features[i].family)) != 0)
301 continue;
303 cow_read_values (subpath,
304 buffer_ptr + strlen (ow_family_features[i].family),
305 ow_family_features + i);
306 break;
307 }
308 if (i < ow_family_features_num)
309 continue;
311 /* DS2409 */
312 if (strncmp ("1F.", buffer_ptr, strlen ("1F.")) == 0)
313 {
314 cow_read_ds2409 (subpath);
315 continue;
316 }
317 } /* while (strtok_r) */
319 free (buffer);
320 return (0);
321 } /* int cow_read_bus */
323 static int cow_read (user_data_t *ud __attribute__((unused)))
324 {
325 return (cow_read_bus ("/"));
326 } /* int cow_read */
328 static int cow_shutdown (void)
329 {
330 OW_finish ();
331 ignorelist_free (sensor_list);
332 return (0);
333 } /* int cow_shutdown */
335 static int cow_init (void)
336 {
337 int status;
338 struct timespec cb_interval;
340 if (device_g == NULL)
341 {
342 ERROR ("onewire plugin: cow_init: No device configured.");
343 return (-1);
344 }
346 status = (int) OW_init (device_g);
347 if (status != 0)
348 {
349 ERROR ("onewire plugin: OW_init(%s) failed: %i.", device_g, status);
350 return (1);
351 }
353 CDTIME_T_TO_TIMESPEC (ow_interval, &cb_interval);
355 plugin_register_complex_read (/* group = */ NULL, "onewire", cow_read,
356 (ow_interval != 0) ? &cb_interval : NULL,
357 /* user data = */ NULL);
358 plugin_register_shutdown ("onewire", cow_shutdown);
360 return (0);
361 } /* int cow_init */
363 void module_register (void)
364 {
365 plugin_register_init ("onewire", cow_init);
366 plugin_register_config ("onewire", cow_load_config,
367 config_keys, config_keys_num);
368 }
370 /* vim: set sw=2 sts=2 ts=8 et fdm=marker cindent : */