configure.ac: fix path when testing for liboconfig/parser.c presence
[collectd.git] / src / modbus.c
1 /**
2  * collectd - src/modbus.c
3  * Copyright (C) 2010,2011  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 Lesser General Public License as published by
7  * the Free Software Foundation; only version 2.1 of the License is
8  * applicable.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors:
20  *   Florian Forster <octo at noris.net>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27
28 #include <netdb.h>
29
30 #include <modbus.h>
31
32 #ifndef LIBMODBUS_VERSION_CHECK
33 /* Assume version 2.0.3 */
34 # define LEGACY_LIBMODBUS 1
35 #else
36 /* Assume version 2.9.2 */
37 #endif
38
39 #ifndef MODBUS_TCP_DEFAULT_PORT
40 # ifdef MODBUS_TCP_PORT
41 #  define MODBUS_TCP_DEFAULT_PORT MODBUS_TCP_PORT
42 # else
43 #  define MODBUS_TCP_DEFAULT_PORT 502
44 # endif
45 #endif
46
47 /*
48  * <Data "data_name">
49  *   RegisterBase 1234
50  *   RegisterType float
51  *   Type gauge
52  *   Instance "..."
53  * </Data>
54  *
55  * <Host "name">
56  *   Address "addr"
57  *   Port "1234"
58  *   Interval 60
59  *
60  *   <Slave 1>
61  *     Instance "foobar" # optional
62  *     Collect "data_name"
63  *   </Slave>
64  * </Host>
65  */
66
67 /*
68  * Data structures
69  */
70 enum mb_register_type_e /* {{{ */
71 {
72   REG_TYPE_INT16,
73   REG_TYPE_INT32,
74   REG_TYPE_UINT16,
75   REG_TYPE_UINT32,
76   REG_TYPE_FLOAT
77 }; /* }}} */
78 typedef enum mb_register_type_e mb_register_type_t;
79
80 struct mb_data_s;
81 typedef struct mb_data_s mb_data_t;
82 struct mb_data_s /* {{{ */
83 {
84   char *name;
85   int register_base;
86   mb_register_type_t register_type;
87   char type[DATA_MAX_NAME_LEN];
88   char instance[DATA_MAX_NAME_LEN];
89
90   mb_data_t *next;
91 }; /* }}} */
92
93 struct mb_slave_s /* {{{ */
94 {
95   int id;
96   char instance[DATA_MAX_NAME_LEN];
97   mb_data_t *collect;
98 }; /* }}} */
99 typedef struct mb_slave_s mb_slave_t;
100
101 struct mb_host_s /* {{{ */
102 {
103   char host[DATA_MAX_NAME_LEN];
104   char node[NI_MAXHOST];
105   /* char service[NI_MAXSERV]; */
106   int port;
107   cdtime_t interval;
108
109   mb_slave_t *slaves;
110   size_t slaves_num;
111
112 #if LEGACY_LIBMODBUS
113   modbus_param_t connection;
114 #else
115   modbus_t *connection;
116 #endif
117   _Bool is_connected;
118 }; /* }}} */
119 typedef struct mb_host_s mb_host_t;
120
121 struct mb_data_group_s;
122 typedef struct mb_data_group_s mb_data_group_t;
123 struct mb_data_group_s /* {{{ */
124 {
125   mb_data_t *registers;
126   size_t registers_num;
127
128   mb_data_group_t *next;
129 }; /* }}} */
130
131 /*
132  * Global variables
133  */
134 static mb_data_t *data_definitions = NULL;
135
136 /*
137  * Functions
138  */
139 static mb_data_t *data_get_by_name (mb_data_t *src, /* {{{ */
140     const char *name)
141 {
142   mb_data_t *ptr;
143
144   if (name == NULL)
145     return (NULL);
146
147   for (ptr = src; ptr != NULL; ptr = ptr->next)
148     if (strcasecmp (ptr->name, name) == 0)
149       return (ptr);
150
151   return (NULL);
152 } /* }}} mb_data_t *data_get_by_name */
153
154 static int data_append (mb_data_t **dst, mb_data_t *src) /* {{{ */
155 {
156   mb_data_t *ptr;
157
158   if ((dst == NULL) || (src == NULL))
159     return (EINVAL);
160
161   ptr = *dst;
162
163   if (ptr == NULL)
164   {
165     *dst = src;
166     return (0);
167   }
168
169   while (ptr->next != NULL)
170     ptr = ptr->next;
171
172   ptr->next = src;
173
174   return (0);
175 } /* }}} int data_append */
176
177 /* Copy a single mb_data_t and append it to another list. */
178 static int data_copy (mb_data_t **dst, const mb_data_t *src) /* {{{ */
179 {
180   mb_data_t *tmp;
181   int status;
182
183   if ((dst == NULL) || (src == NULL))
184     return (EINVAL);
185
186   tmp = malloc (sizeof (*tmp));
187   if (tmp == NULL)
188     return (ENOMEM);
189   memcpy (tmp, src, sizeof (*tmp));
190   tmp->name = NULL;
191   tmp->next = NULL;
192
193   tmp->name = strdup (src->name);
194   if (tmp->name == NULL)
195   {
196     sfree (tmp);
197     return (ENOMEM);
198   }
199
200   status = data_append (dst, tmp);
201   if (status != 0)
202   {
203     sfree (tmp->name);
204     sfree (tmp);
205     return (status);
206   }
207
208   return (0);
209 } /* }}} int data_copy */
210
211 /* Lookup a single mb_data_t instance, copy it and append the copy to another
212  * list. */
213 static int data_copy_by_name (mb_data_t **dst, mb_data_t *src, /* {{{ */
214     const char *name)
215 {
216   mb_data_t *ptr;
217
218   if ((dst == NULL) || (src == NULL) || (name == NULL))
219     return (EINVAL);
220
221   ptr = data_get_by_name (src, name);
222   if (ptr == NULL)
223     return (ENOENT);
224
225   return (data_copy (dst, ptr));
226 } /* }}} int data_copy_by_name */
227
228 /* Read functions */
229
230 static int mb_submit (mb_host_t *host, mb_slave_t *slave, /* {{{ */
231     mb_data_t *data, value_t value)
232 {
233   value_list_t vl = VALUE_LIST_INIT;
234
235   if ((host == NULL) || (slave == NULL) || (data == NULL))
236     return (EINVAL);
237
238   if (host->interval <= 0)
239     host->interval = plugin_get_interval ();
240
241   if (slave->instance[0] == 0)
242     ssnprintf (slave->instance, sizeof (slave->instance), "slave_%i",
243         slave->id);
244
245   vl.values = &value;
246   vl.values_len = 1;
247   vl.interval = host->interval;
248   sstrncpy (vl.host, host->host, sizeof (vl.host));
249   sstrncpy (vl.plugin, "modbus", sizeof (vl.plugin));
250   sstrncpy (vl.plugin_instance, slave->instance, sizeof (vl.plugin_instance));
251   sstrncpy (vl.type, data->type, sizeof (vl.type));
252   sstrncpy (vl.type_instance, data->instance, sizeof (vl.type_instance));
253
254   return (plugin_dispatch_values (&vl));
255 } /* }}} int mb_submit */
256
257 static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */
258 {
259   union
260   {
261     uint8_t b[4];
262     uint16_t s[2];
263     float f;
264   } conv;
265
266 #if BYTE_ORDER == LITTLE_ENDIAN
267   /* little endian */
268   conv.b[0] = lo & 0x00ff;
269   conv.b[1] = (lo >> 8) & 0x00ff;
270   conv.b[2] = hi & 0x00ff;
271   conv.b[3] = (hi >> 8) & 0x00ff;
272 #else
273   conv.b[3] = lo & 0x00ff;
274   conv.b[2] = (lo >> 8) & 0x00ff;
275   conv.b[1] = hi & 0x00ff;
276   conv.b[0] = (hi >> 8) & 0x00ff;
277 #endif
278
279   return (conv.f);
280 } /* }}} float mb_register_to_float */
281
282 #if LEGACY_LIBMODBUS
283 /* Version 2.0.3 */
284 static int mb_init_connection (mb_host_t *host) /* {{{ */
285 {
286   int status;
287
288   if (host == NULL)
289     return (EINVAL);
290
291 #if COLLECT_DEBUG
292   modbus_set_debug (&host->connection, 1);
293 #endif
294
295   /* We'll do the error handling ourselves. */
296   modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
297
298   if ((host->port < 1) || (host->port > 65535))
299     host->port = MODBUS_TCP_DEFAULT_PORT;
300
301   DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
302       host->node, host->port);
303
304   modbus_init_tcp (&host->connection,
305       /* host = */ host->node,
306       /* port = */ host->port);
307
308   status = modbus_connect (&host->connection);
309   if (status != 0)
310   {
311     ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
312         host->node, host->port, status);
313     return (status);
314   }
315
316   host->is_connected = 1;
317   return (0);
318 } /* }}} int mb_init_connection */
319 /* #endif LEGACY_LIBMODBUS */
320
321 #else /* if !LEGACY_LIBMODBUS */
322 /* Version 2.9.2 */
323 static int mb_init_connection (mb_host_t *host) /* {{{ */
324 {
325   int status;
326
327   if (host == NULL)
328     return (EINVAL);
329
330   if (host->connection != NULL)
331     return (0);
332
333   if ((host->port < 1) || (host->port > 65535))
334     host->port = MODBUS_TCP_DEFAULT_PORT;
335
336   DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
337       host->node, host->port);
338
339   host->connection = modbus_new_tcp (host->node, host->port);
340   if (host->connection == NULL)
341   {
342     ERROR ("Modbus plugin: Creating new Modbus/TCP object failed.");
343     return (-1);
344   }
345
346 #if COLLECT_DEBUG
347   modbus_set_debug (host->connection, 1);
348 #endif
349
350   /* We'll do the error handling ourselves. */
351   modbus_set_error_recovery (host->connection, 0);
352
353   status = modbus_connect (host->connection);
354   if (status != 0)
355   {
356     ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
357         host->node, host->port, status);
358     modbus_free (host->connection);
359     host->connection = NULL;
360     return (status);
361   }
362
363   return (0);
364 } /* }}} int mb_init_connection */
365 #endif /* !LEGACY_LIBMODBUS */
366
367 #define CAST_TO_VALUE_T(ds,vt,raw) do { \
368   if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
369     (vt).counter = (counter_t) (raw); \
370   else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
371     (vt).gauge = (gauge_t) (raw); \
372   else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
373     (vt).derive = (derive_t) (raw); \
374   else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
375     (vt).absolute = (absolute_t) (raw); \
376 } while (0)
377
378 static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
379     mb_data_t *data)
380 {
381   uint16_t values[2];
382   int values_num;
383   const data_set_t *ds;
384   int status = 0;
385
386   if ((host == NULL) || (slave == NULL) || (data == NULL))
387     return (EINVAL);
388
389   ds = plugin_get_ds (data->type);
390   if (ds == NULL)
391   {
392     ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
393     return (-1);
394   }
395
396   if (ds->ds_num != 1)
397   {
398     ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
399         "I can only handle data sets with only one data source.",
400         data->type, ds->ds_num);
401     return (-1);
402   }
403
404   if ((ds->ds[0].type != DS_TYPE_GAUGE)
405       && (data->register_type != REG_TYPE_INT32)
406       && (data->register_type != REG_TYPE_UINT32))
407   {
408     NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
409         "This will most likely result in problems, because the register type "
410         "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
411   }
412
413   memset (values, 0, sizeof (values));
414   if ((data->register_type == REG_TYPE_INT32)
415       || (data->register_type == REG_TYPE_UINT32)
416       || (data->register_type == REG_TYPE_FLOAT))
417     values_num = 2;
418   else
419     values_num = 1;
420
421   if (host->connection == NULL)
422   {
423     status = EBADF;
424   }
425   else
426   {
427     struct sockaddr sockaddr;
428     socklen_t saddrlen = sizeof (sockaddr);
429
430     status = getpeername (modbus_get_socket (host->connection),
431         &sockaddr, &saddrlen);
432     if (status != 0)
433       status = errno;
434   }
435
436   if ((status == EBADF) || (status == ENOTSOCK) || (status == ENOTCONN))
437   {
438     status = mb_init_connection (host);
439     if (status != 0)
440     {
441       ERROR ("Modbus plugin: mb_init_connection (%s/%s) failed. ",
442           host->host, host->node);
443       host->is_connected = 0;
444       host->connection = NULL;
445       return (-1);
446     }
447   }
448   else if (status != 0)
449   {
450 #if LEGACY_LIBMODBUS
451     modbus_close (&host->connection);
452 #else
453     modbus_close (host->connection);
454     modbus_free (host->connection);
455 #endif
456   }
457  
458 #if LEGACY_LIBMODBUS
459   /* Version 2.0.3: Pass the connection struct as a pointer and pass the slave
460    * id to each call of "read_holding_registers". */
461 # define modbus_read_registers(ctx, addr, nb, dest) \
462   read_holding_registers (&(ctx), slave->id, (addr), (nb), (dest))
463 #else /* if !LEGACY_LIBMODBUS */
464   /* Version 2.9.2: Set the slave id once before querying the registers. */
465   status = modbus_set_slave (host->connection, slave->id);
466   if (status != 0)
467   {
468     ERROR ("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
469         slave->id, status);
470     return (-1);
471   }
472 #endif
473
474   status = modbus_read_registers (host->connection,
475         /* start_addr = */ data->register_base,
476         /* num_registers = */ values_num, /* buffer = */ values);
477   if (status != values_num)
478   {
479     ERROR ("Modbus plugin: modbus_read_registers (%s/%s) failed. status = %i, values_num = %i "
480         "Giving up.", host->host, host->node, status, values_num);
481 #if LEGACY_LIBMODBUS
482     modbus_close (&host->connection);
483 #else
484     modbus_close (host->connection);
485     modbus_free (host->connection);
486 #endif
487     host->connection = NULL;
488     return (-1);
489   }
490
491   DEBUG ("Modbus plugin: mb_read_data: Success! "
492       "modbus_read_registers returned with status %i.", status);
493
494   if (data->register_type == REG_TYPE_FLOAT)
495   {
496     float float_value;
497     value_t vt;
498
499     float_value = mb_register_to_float (values[0], values[1]);
500     DEBUG ("Modbus plugin: mb_read_data: "
501         "Returned float value is %g", (double) float_value);
502
503     CAST_TO_VALUE_T (ds, vt, float_value);
504     mb_submit (host, slave, data, vt);
505   }
506   else if (data->register_type == REG_TYPE_INT32)
507   {
508     union
509     {
510       uint32_t u32;
511       int32_t  i32;
512     } v;
513     value_t vt;
514
515     v.u32 = (((uint32_t) values[0]) << 16)
516       | ((uint32_t) values[1]);
517     DEBUG ("Modbus plugin: mb_read_data: "
518         "Returned int32 value is %"PRIi32, v.i32);
519
520     CAST_TO_VALUE_T (ds, vt, v.i32);
521     mb_submit (host, slave, data, vt);
522   }
523   else if (data->register_type == REG_TYPE_INT16)
524   {
525     union
526     {
527       uint16_t u16;
528       int16_t  i16;
529     } v;
530     value_t vt;
531
532     v.u16 = values[0];
533
534     DEBUG ("Modbus plugin: mb_read_data: "
535         "Returned int16 value is %"PRIi16, v.i16);
536
537     CAST_TO_VALUE_T (ds, vt, v.i16);
538     mb_submit (host, slave, data, vt);
539   }
540   else if (data->register_type == REG_TYPE_UINT32)
541   {
542     uint32_t v32;
543     value_t vt;
544
545     v32 = (((uint32_t) values[0]) << 16)
546       | ((uint32_t) values[1]);
547     DEBUG ("Modbus plugin: mb_read_data: "
548         "Returned uint32 value is %"PRIu32, v32);
549
550     CAST_TO_VALUE_T (ds, vt, v32);
551     mb_submit (host, slave, data, vt);
552   }
553   else /* if (data->register_type == REG_TYPE_UINT16) */
554   {
555     value_t vt;
556
557     DEBUG ("Modbus plugin: mb_read_data: "
558         "Returned uint16 value is %"PRIu16, values[0]);
559
560     CAST_TO_VALUE_T (ds, vt, values[0]);
561     mb_submit (host, slave, data, vt);
562   }
563
564   return (0);
565 } /* }}} int mb_read_data */
566
567 static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
568 {
569   mb_data_t *data;
570   int success;
571   int status;
572
573   if ((host == NULL) || (slave == NULL))
574     return (EINVAL);
575
576   success = 0;
577   for (data = slave->collect; data != NULL; data = data->next)
578   {
579     status = mb_read_data (host, slave, data);
580     if (status == 0)
581       success++;
582   }
583
584   if (success == 0)
585     return (-1);
586   else
587     return (0);
588 } /* }}} int mb_read_slave */
589
590 static int mb_read (user_data_t *user_data) /* {{{ */
591 {
592   mb_host_t *host;
593   size_t i;
594   int success;
595   int status;
596
597   if ((user_data == NULL) || (user_data->data == NULL))
598     return (EINVAL);
599
600   host = user_data->data;
601
602   success = 0;
603   for (i = 0; i < host->slaves_num; i++)
604   {
605     status = mb_read_slave (host, host->slaves + i);
606     if (status == 0)
607       success++;
608   }
609
610   if (success == 0)
611     return (-1);
612   else
613     return (0);
614 } /* }}} int mb_read */
615
616 /* Free functions */
617
618 static void data_free_one (mb_data_t *data) /* {{{ */
619 {
620   if (data == NULL)
621     return;
622
623   sfree (data->name);
624   sfree (data);
625 } /* }}} void data_free_one */
626
627 static void data_free_all (mb_data_t *data) /* {{{ */
628 {
629   mb_data_t *next;
630
631   if (data == NULL)
632     return;
633
634   next = data->next;
635   data_free_one (data);
636
637   data_free_all (next);
638 } /* }}} void data_free_all */
639
640 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
641 {
642   size_t i;
643
644   if (slaves == NULL)
645     return;
646
647   for (i = 0; i < slaves_num; i++)
648     data_free_all (slaves[i].collect);
649   sfree (slaves);
650 } /* }}} void slaves_free_all */
651
652 static void host_free (void *void_host) /* {{{ */
653 {
654   mb_host_t *host = void_host;
655
656   if (host == NULL)
657     return;
658
659   slaves_free_all (host->slaves, host->slaves_num);
660   sfree (host);
661 } /* }}} void host_free */
662
663 /* Config functions */
664
665 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
666 {
667   mb_data_t data;
668   int status;
669   int i;
670
671   memset (&data, 0, sizeof (data));
672   data.name = NULL;
673   data.register_type = REG_TYPE_UINT16;
674   data.next = NULL;
675
676   status = cf_util_get_string (ci, &data.name);
677   if (status != 0)
678     return (status);
679
680   for (i = 0; i < ci->children_num; i++)
681   {
682     oconfig_item_t *child = ci->children + i;
683
684     if (strcasecmp ("Type", child->key) == 0)
685       status = cf_util_get_string_buffer (child,
686           data.type, sizeof (data.type));
687     else if (strcasecmp ("Instance", child->key) == 0)
688       status = cf_util_get_string_buffer (child,
689           data.instance, sizeof (data.instance));
690     else if (strcasecmp ("RegisterBase", child->key) == 0)
691       status = cf_util_get_int (child, &data.register_base);
692     else if (strcasecmp ("RegisterType", child->key) == 0)
693     {
694       char tmp[16];
695       status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
696       if (status != 0)
697         /* do nothing */;
698       else if (strcasecmp ("Int16", tmp) == 0)
699         data.register_type = REG_TYPE_INT16;
700       else if (strcasecmp ("Int32", tmp) == 0)
701         data.register_type = REG_TYPE_INT32;
702       else if (strcasecmp ("Uint16", tmp) == 0)
703         data.register_type = REG_TYPE_UINT16;
704       else if (strcasecmp ("Uint32", tmp) == 0)
705         data.register_type = REG_TYPE_UINT32;
706       else if (strcasecmp ("Float", tmp) == 0)
707         data.register_type = REG_TYPE_FLOAT;
708       else
709       {
710         ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
711         status = -1;
712       }
713     }
714     else
715     {
716       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
717       status = -1;
718     }
719
720     if (status != 0)
721       break;
722   } /* for (i = 0; i < ci->children_num; i++) */
723
724   assert (data.name != NULL);
725   if (data.type[0] == 0)
726   {
727     ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
728         data.name);
729     status = -1;
730   }
731
732   if (status == 0)
733     data_copy (&data_definitions, &data);
734
735   sfree (data.name);
736
737   return (status);
738 } /* }}} int mb_config_add_data */
739
740 static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
741     const char *address)
742 {
743   struct addrinfo *ai_list;
744   struct addrinfo *ai_ptr;
745   struct addrinfo  ai_hints;
746   int status;
747
748   if ((host == NULL) || (address == NULL))
749     return (EINVAL);
750
751   memset (&ai_hints, 0, sizeof (ai_hints));
752 #if AI_ADDRCONFIG
753   ai_hints.ai_flags |= AI_ADDRCONFIG;
754 #endif
755   /* XXX: libmodbus can only handle IPv4 addresses. */
756   ai_hints.ai_family = AF_INET;
757   ai_hints.ai_addr = NULL;
758   ai_hints.ai_canonname = NULL;
759   ai_hints.ai_next = NULL;
760
761   ai_list = NULL;
762   status = getaddrinfo (address, /* service = */ NULL,
763       &ai_hints, &ai_list);
764   if (status != 0)
765   {
766     char errbuf[1024];
767     ERROR ("Modbus plugin: getaddrinfo failed: %s",
768         (status == EAI_SYSTEM)
769         ? sstrerror (errno, errbuf, sizeof (errbuf))
770         : gai_strerror (status));
771     return (status);
772   }
773
774   for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
775   {
776     status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
777         host->node, sizeof (host->node),
778         /* service = */ NULL, /* length = */ 0,
779         /* flags = */ NI_NUMERICHOST);
780     if (status == 0)
781       break;
782   } /* for (ai_ptr) */
783
784   freeaddrinfo (ai_list);
785
786   if (status != 0)
787     ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
788   else /* if (status == 0) */
789   {
790     DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
791         address, host->node);
792   }
793
794   return (status);
795 } /* }}} int mb_config_set_host_address */
796
797 static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
798 {
799   mb_slave_t *slave;
800   int status;
801   int i;
802
803   if ((host == NULL) || (ci == NULL))
804     return (EINVAL);
805
806   slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
807   if (slave == NULL)
808     return (ENOMEM);
809   host->slaves = slave;
810   slave = host->slaves + host->slaves_num;
811   memset (slave, 0, sizeof (*slave));
812   slave->collect = NULL;
813
814   status = cf_util_get_int (ci, &slave->id);
815   if (status != 0)
816     return (status);
817
818   for (i = 0; i < ci->children_num; i++)
819   {
820     oconfig_item_t *child = ci->children + i;
821
822     if (strcasecmp ("Instance", child->key) == 0)
823       status = cf_util_get_string_buffer (child,
824           slave->instance, sizeof (slave->instance));
825     else if (strcasecmp ("Collect", child->key) == 0)
826     {
827       char buffer[1024];
828       status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
829       if (status == 0)
830         data_copy_by_name (&slave->collect, data_definitions, buffer);
831       status = 0; /* continue after failure. */
832     }
833     else
834     {
835       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
836       status = -1;
837     }
838
839     if (status != 0)
840       break;
841   }
842
843   if ((status == 0) && (slave->collect == NULL))
844     status = EINVAL;
845
846   if (slave->id < 0)
847     status = EINVAL;
848
849   if (status == 0)
850     host->slaves_num++;
851   else /* if (status != 0) */
852     data_free_all (slave->collect);
853
854   return (status);
855 } /* }}} int mb_config_add_slave */
856
857 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
858 {
859   mb_host_t *host;
860   int status;
861   int i;
862
863   host = malloc (sizeof (*host));
864   if (host == NULL)
865     return (ENOMEM);
866   memset (host, 0, sizeof (*host));
867   host->slaves = NULL;
868
869   status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
870   if (status != 0)
871   {
872     sfree (host);
873     return (status);
874   }
875   if (host->host[0] == 0)
876   {
877     sfree (host);
878     return (EINVAL);
879   }
880
881   for (i = 0; i < ci->children_num; i++)
882   {
883     oconfig_item_t *child = ci->children + i;
884     status = 0;
885
886     if (strcasecmp ("Address", child->key) == 0)
887     {
888       char buffer[NI_MAXHOST];
889       status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
890       if (status == 0)
891         status = mb_config_set_host_address (host, buffer);
892     }
893     else if (strcasecmp ("Port", child->key) == 0)
894     {
895       host->port = cf_util_get_port_number (child);
896       if (host->port <= 0)
897         status = -1;
898     }
899     else if (strcasecmp ("Interval", child->key) == 0)
900       status = cf_util_get_cdtime (child, &host->interval);
901     else if (strcasecmp ("Slave", child->key) == 0)
902       /* Don't set status: Gracefully continue if a slave fails. */
903       mb_config_add_slave (host, child);
904     else
905     {
906       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
907       status = -1;
908     }
909
910     if (status != 0)
911       break;
912   } /* for (i = 0; i < ci->children_num; i++) */
913
914   assert (host->host[0] != 0);
915   if (host->host[0] == 0)
916   {
917     ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
918         host->host);
919     status = -1;
920   }
921
922   if (status == 0)
923   {
924     user_data_t ud;
925     char name[1024];
926     struct timespec interval = { 0, 0 };
927
928     ud.data = host;
929     ud.free_func = host_free;
930
931     ssnprintf (name, sizeof (name), "modbus-%s", host->host);
932
933     CDTIME_T_TO_TIMESPEC (host->interval, &interval);
934
935     plugin_register_complex_read (/* group = */ NULL, name,
936         /* callback = */ mb_read,
937         /* interval = */ (host->interval > 0) ? &interval : NULL,
938         &ud);
939   }
940   else
941   {
942     host_free (host);
943   }
944
945   return (status);
946 } /* }}} int mb_config_add_host */
947
948 static int mb_config (oconfig_item_t *ci) /* {{{ */
949 {
950   int i;
951
952   if (ci == NULL)
953     return (EINVAL);
954
955   for (i = 0; i < ci->children_num; i++)
956   {
957     oconfig_item_t *child = ci->children + i;
958
959     if (strcasecmp ("Data", child->key) == 0)
960       mb_config_add_data (child);
961     else if (strcasecmp ("Host", child->key) == 0)
962       mb_config_add_host (child);
963     else
964       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
965   }
966
967   return (0);
968 } /* }}} int mb_config */
969
970 /* ========= */
971
972 static int mb_shutdown (void) /* {{{ */
973 {
974   data_free_all (data_definitions);
975   data_definitions = NULL;
976
977   return (0);
978 } /* }}} int mb_shutdown */
979
980 void module_register (void)
981 {
982   plugin_register_complex_config ("modbus", mb_config);
983   plugin_register_shutdown ("modbus", mb_shutdown);
984 } /* void module_register */
985
986 /* vim: set sw=2 sts=2 et fdm=marker : */