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