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