write_redis: remove unused variable from wr_write()
[collectd.git] / src / onewire.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_ignorelist.h"
26
27 #include <owcapi.h>
28
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;
43
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);
60
61 static char *device_g = NULL;
62 static cdtime_t ow_interval = 0;
63
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);
72
73 static ignorelist_t *sensor_list;
74
75 static int cow_load_config (const char *key, const char *value)
76 {
77   if (sensor_list == NULL)
78     sensor_list = ignorelist_create (1);
79
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 (IS_TRUE (value))
93       ignorelist_set_invert (sensor_list, 0);
94   }
95   else if (strcasecmp (key, "Device") == 0)
96   {
97     char *temp;
98     temp = strdup (value);
99     if (temp == NULL)
100     {
101       ERROR ("onewire plugin: strdup failed.");
102       return (1);
103     }
104     sfree (device_g);
105     device_g = temp;
106   }
107   else if (strcasecmp ("Interval", key) == 0)
108   {
109     double tmp;
110     tmp = atof (value);
111     if (tmp > 0.0)
112       ow_interval = DOUBLE_TO_CDTIME_T (tmp);
113     else
114       ERROR ("onewire plugin: Invalid `Interval' setting: %s", value);
115   }
116   else
117   {
118     return (-1);
119   }
120
121   return (0);
122 }
123
124 static int cow_read_values (const char *path, const char *name,
125     const ow_family_features_t *family_info)
126 {
127   value_t values[1];
128   value_list_t vl = VALUE_LIST_INIT;
129   int success = 0;
130   size_t i;
131
132   if (sensor_list != NULL)
133   {
134     DEBUG ("onewire plugin: Checking ignorelist for `%s'", name);
135     if (ignorelist_match (sensor_list, name) != 0)
136       return 0;
137   }
138
139   vl.values = values;
140   vl.values_len = 1;
141
142   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
143   sstrncpy (vl.plugin, "onewire", sizeof (vl.plugin));
144   sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance));
145
146   for (i = 0; i < family_info->features_num; i++)
147   {
148     char *buffer;
149     size_t buffer_size;
150     int status;
151
152     char file[4096];
153     char *endptr;
154
155     snprintf (file, sizeof (file), "%s/%s",
156         path, family_info->features[i].filename);
157     file[sizeof (file) - 1] = 0;
158
159     buffer = NULL;
160     buffer_size = 0;
161     status = OW_get (file, &buffer, &buffer_size);
162     if (status < 0)
163     {
164       ERROR ("onewire plugin: OW_get (%s/%s) failed. status = %#x;",
165           path, family_info->features[i].filename, status);
166       return (-1);
167     }
168
169     endptr = NULL;
170     values[0].gauge = strtod (buffer, &endptr);
171     if (endptr == NULL)
172     {
173       ERROR ("onewire plugin: Buffer is not a number: %s", buffer);
174       continue;
175     }
176
177     sstrncpy (vl.type, family_info->features[i].type, sizeof (vl.type));
178     sstrncpy (vl.type_instance, family_info->features[i].type_instance,
179         sizeof (vl.type_instance));
180
181     plugin_dispatch_values (&vl);
182     success++;
183
184     free (buffer);
185   } /* for (i = 0; i < features_num; i++) */
186
187   return ((success > 0) ? 0 : -1);
188 } /* int cow_read_values */
189
190 /* Forward declaration so the recursion below works */
191 static int cow_read_bus (const char *path);
192
193 /*
194  * cow_read_ds2409
195  *
196  * Handles:
197  * - DS2409 - MicroLAN Coupler
198  */
199 static int cow_read_ds2409 (const char *path)
200 {
201   char subpath[4096];
202   int status;
203
204   status = ssnprintf (subpath, sizeof (subpath), "%s/main", path);
205   if ((status > 0) && (status < sizeof (subpath)))
206     cow_read_bus (subpath);
207
208   status = ssnprintf (subpath, sizeof (subpath), "%s/aux", path);
209   if ((status > 0) && (status < sizeof (subpath)))
210     cow_read_bus (subpath);
211
212   return (0);
213 } /* int cow_read_ds2409 */
214
215 static int cow_read_bus (const char *path)
216 {
217   char *buffer;
218   size_t buffer_size;
219   int status;
220
221   char *buffer_ptr;
222   char *dummy;
223   char *saveptr;
224   char subpath[4096];
225
226   status = OW_get (path, &buffer, &buffer_size);
227   if (status < 0)
228   {
229     ERROR ("onewire plugin: OW_get (%s) failed. status = %#x;",
230         path, status);
231     return (-1);
232   }
233   DEBUG ("onewire plugin: OW_get (%s) returned: %s",
234       path, buffer);
235
236   dummy = buffer;
237   saveptr = NULL;
238   while ((buffer_ptr = strtok_r (dummy, ",/", &saveptr)) != NULL)
239   {
240     int i;
241
242     dummy = NULL;
243
244     if (strcmp ("/", path) == 0)
245       status = ssnprintf (subpath, sizeof (subpath), "/%s", buffer_ptr);
246     else
247       status = ssnprintf (subpath, sizeof (subpath), "%s/%s",
248           path, buffer_ptr);
249     if ((status <= 0) || (status >= sizeof (subpath)))
250       continue;
251
252     for (i = 0; i < ow_family_features_num; i++)
253     {
254       if (strncmp (ow_family_features[i].family, buffer_ptr,
255             strlen (ow_family_features[i].family)) != 0)
256         continue;
257
258       cow_read_values (subpath,
259           buffer_ptr + strlen (ow_family_features[i].family),
260           ow_family_features + i);
261       break;
262     }
263     if (i < ow_family_features_num)
264       continue;
265
266     /* DS2409 */
267     if (strncmp ("1F.", buffer_ptr, strlen ("1F.")) == 0)
268     {
269       cow_read_ds2409 (subpath);
270       continue;
271     }
272   } /* while (strtok_r) */
273
274   free (buffer);
275   return (0);
276 } /* int cow_read_bus */
277
278 static int cow_read (user_data_t *ud __attribute__((unused)))
279 {
280   return (cow_read_bus ("/"));
281 } /* int cow_read */
282
283 static int cow_shutdown (void)
284 {
285   OW_finish ();
286   ignorelist_free (sensor_list);
287   return (0);
288 } /* int cow_shutdown */
289
290 static int cow_init (void)
291 {
292   int status;
293   struct timespec cb_interval;
294
295   if (device_g == NULL)
296   {
297     ERROR ("onewire plugin: cow_init: No device configured.");
298     return (-1);
299   }
300
301   status = (int) OW_init (device_g);
302   if (status != 0)
303   {
304     ERROR ("onewire plugin: OW_init(%s) failed: %i.", device_g, status);
305     return (1);
306   }
307
308   CDTIME_T_TO_TIMESPEC (ow_interval, &cb_interval);
309
310   plugin_register_complex_read (/* group = */ NULL, "onewire", cow_read,
311       (ow_interval != 0) ? &cb_interval : NULL,
312       /* user data = */ NULL);
313   plugin_register_shutdown ("onewire", cow_shutdown);
314
315   return (0);
316 } /* int cow_init */
317
318 void module_register (void)
319 {
320   plugin_register_init ("onewire", cow_init);
321   plugin_register_config ("onewire", cow_load_config,
322     config_keys, config_keys_num);
323 }
324
325 /* vim: set sw=2 sts=2 ts=8 et fdm=marker cindent : */