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