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 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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
27 #include <netdb.h>
29 #include <modbus/modbus.h>
31 #ifndef LIBMODBUS_VERSION_CHECK
32 /* Assume version 2.0.3 */
33 # define LEGACY_LIBMODBUS 1
34 #else
35 /* Assume version 2.9.2 */
36 #endif
38 #ifndef MODBUS_TCP_DEFAULT_PORT
39 # ifdef MODBUS_TCP_PORT
40 # define MODBUS_TCP_DEFAULT_PORT MODBUS_TCP_PORT
41 # else
42 # define MODBUS_TCP_DEFAULT_PORT 502
43 # endif
44 #endif
46 /*
47 * <Data "data_name">
48 * RegisterBase 1234
49 * RegisterType float
50 * Type gauge
51 * Instance "..."
52 * </Data>
53 *
54 * <Host "name">
55 * Address "addr"
56 * Port "1234"
57 * Interval 60
58 *
59 * <Slave 1>
60 * Instance "foobar" # optional
61 * Collect "data_name"
62 * </Slave>
63 * </Host>
64 */
66 /*
67 * Data structures
68 */
69 enum mb_register_type_e /* {{{ */
70 {
71 REG_TYPE_INT16,
72 REG_TYPE_INT32,
73 REG_TYPE_UINT16,
74 REG_TYPE_UINT32,
75 REG_TYPE_FLOAT
76 }; /* }}} */
77 typedef enum mb_register_type_e mb_register_type_t;
79 struct mb_data_s;
80 typedef struct mb_data_s mb_data_t;
81 struct mb_data_s /* {{{ */
82 {
83 char *name;
84 int register_base;
85 mb_register_type_t register_type;
86 char type[DATA_MAX_NAME_LEN];
87 char instance[DATA_MAX_NAME_LEN];
89 mb_data_t *next;
90 }; /* }}} */
92 struct mb_slave_s /* {{{ */
93 {
94 int id;
95 char instance[DATA_MAX_NAME_LEN];
96 mb_data_t *collect;
97 }; /* }}} */
98 typedef struct mb_slave_s mb_slave_t;
100 struct mb_host_s /* {{{ */
101 {
102 char host[DATA_MAX_NAME_LEN];
103 char node[NI_MAXHOST];
104 /* char service[NI_MAXSERV]; */
105 int port;
106 cdtime_t interval;
108 mb_slave_t *slaves;
109 size_t slaves_num;
111 #if LEGACY_LIBMODBUS
112 modbus_param_t connection;
113 #else
114 modbus_t *connection;
115 #endif
116 _Bool is_connected;
117 _Bool have_reconnected;
118 }; /* }}} */
119 typedef struct mb_host_s mb_host_t;
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;
128 mb_data_group_t *next;
129 }; /* }}} */
131 /*
132 * Global variables
133 */
134 static mb_data_t *data_definitions = NULL;
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;
144 if (name == NULL)
145 return (NULL);
147 for (ptr = src; ptr != NULL; ptr = ptr->next)
148 if (strcasecmp (ptr->name, name) == 0)
149 return (ptr);
151 return (NULL);
152 } /* }}} mb_data_t *data_get_by_name */
154 static int data_append (mb_data_t **dst, mb_data_t *src) /* {{{ */
155 {
156 mb_data_t *ptr;
158 if ((dst == NULL) || (src == NULL))
159 return (EINVAL);
161 ptr = *dst;
163 if (ptr == NULL)
164 {
165 *dst = src;
166 return (0);
167 }
169 while (ptr->next != NULL)
170 ptr = ptr->next;
172 ptr->next = src;
174 return (0);
175 } /* }}} int data_append */
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;
183 if ((dst == NULL) || (src == NULL))
184 return (EINVAL);
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;
193 tmp->name = strdup (src->name);
194 if (tmp->name == NULL)
195 {
196 sfree (tmp);
197 return (ENOMEM);
198 }
200 status = data_append (dst, tmp);
201 if (status != 0)
202 {
203 sfree (tmp->name);
204 sfree (tmp);
205 return (status);
206 }
208 return (0);
209 } /* }}} int data_copy */
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;
218 if ((dst == NULL) || (src == NULL) || (name == NULL))
219 return (EINVAL);
221 ptr = data_get_by_name (src, name);
222 if (ptr == NULL)
223 return (ENOENT);
225 return (data_copy (dst, ptr));
226 } /* }}} int data_copy_by_name */
228 /* Read functions */
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;
235 if ((host == NULL) || (slave == NULL) || (data == NULL))
236 return (EINVAL);
238 if (host->interval <= 0)
239 host->interval = interval_g;
241 if (slave->instance[0] == 0)
242 ssnprintf (slave->instance, sizeof (slave->instance), "slave_%i",
243 slave->id);
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));
254 return (plugin_dispatch_values (&vl));
255 } /* }}} int mb_submit */
257 static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */
258 {
259 union
260 {
261 uint8_t b[4];
262 float f;
263 } conv;
265 #if BYTE_ORDER == LITTLE_ENDIAN
266 /* little endian */
267 conv.b[0] = lo & 0x00ff;
268 conv.b[1] = (lo >> 8) & 0x00ff;
269 conv.b[2] = hi & 0x00ff;
270 conv.b[3] = (hi >> 8) & 0x00ff;
271 #else
272 conv.b[3] = lo & 0x00ff;
273 conv.b[2] = (lo >> 8) & 0x00ff;
274 conv.b[1] = hi & 0x00ff;
275 conv.b[0] = (hi >> 8) & 0x00ff;
276 #endif
278 return (conv.f);
279 } /* }}} float mb_register_to_float */
281 #if LEGACY_LIBMODBUS
282 /* Version 2.0.3 */
283 static int mb_init_connection (mb_host_t *host) /* {{{ */
284 {
285 int status;
287 if (host == NULL)
288 return (EINVAL);
290 if (host->is_connected)
291 return (0);
293 /* Only reconnect once per interval. */
294 if (host->have_reconnected)
295 return (-1);
297 modbus_set_debug (&host->connection, 1);
299 /* We'll do the error handling ourselves. */
300 modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
302 if ((host->port < 1) || (host->port > 65535))
303 host->port = MODBUS_TCP_DEFAULT_PORT;
305 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
306 host->node, host->port);
308 modbus_init_tcp (&host->connection,
309 /* host = */ host->node,
310 /* port = */ host->port);
312 status = modbus_connect (&host->connection);
313 if (status != 0)
314 {
315 ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
316 host->node, host->port, status);
317 return (status);
318 }
320 host->is_connected = 1;
321 host->have_reconnected = 1;
322 return (0);
323 } /* }}} int mb_init_connection */
324 /* #endif LEGACY_LIBMODBUS */
326 #else /* if !LEGACY_LIBMODBUS */
327 /* Version 2.9.2 */
328 static int mb_init_connection (mb_host_t *host) /* {{{ */
329 {
330 int status;
332 if (host == NULL)
333 return (EINVAL);
335 if (host->connection != NULL)
336 return (0);
338 /* Only reconnect once per interval. */
339 if (host->have_reconnected)
340 return (-1);
342 if ((host->port < 1) || (host->port > 65535))
343 host->port = MODBUS_TCP_DEFAULT_PORT;
345 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
346 host->node, host->port);
348 host->connection = modbus_new_tcp (host->node, host->port);
349 if (host->connection == NULL)
350 {
351 host->have_reconnected = 1;
352 ERROR ("Modbus plugin: Creating new Modbus/TCP object failed.");
353 return (-1);
354 }
356 modbus_set_debug (host->connection, 1);
358 /* We'll do the error handling ourselves. */
359 modbus_set_error_recovery (host->connection, 0);
361 status = modbus_connect (host->connection);
362 if (status != 0)
363 {
364 ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
365 host->node, host->port, status);
366 modbus_free (host->connection);
367 host->connection = NULL;
368 return (status);
369 }
371 host->have_reconnected = 1;
372 return (0);
373 } /* }}} int mb_init_connection */
374 #endif /* !LEGACY_LIBMODBUS */
376 #define CAST_TO_VALUE_T(ds,vt,raw) do { \
377 if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
378 (vt).counter = (counter_t) (raw); \
379 else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
380 (vt).gauge = (gauge_t) (raw); \
381 else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
382 (vt).derive = (derive_t) (raw); \
383 else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
384 (vt).absolute = (absolute_t) (raw); \
385 } while (0)
387 static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
388 mb_data_t *data)
389 {
390 uint16_t values[2];
391 int values_num;
392 const data_set_t *ds;
393 int status;
394 int i;
396 if ((host == NULL) || (slave == NULL) || (data == NULL))
397 return (EINVAL);
399 ds = plugin_get_ds (data->type);
400 if (ds == NULL)
401 {
402 ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
403 return (-1);
404 }
406 if (ds->ds_num != 1)
407 {
408 ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
409 "I can only handle data sets with only one data source.",
410 data->type, ds->ds_num);
411 return (-1);
412 }
414 if ((ds->ds[0].type != DS_TYPE_GAUGE)
415 && (data->register_type != REG_TYPE_INT32)
416 && (data->register_type != REG_TYPE_UINT32))
417 {
418 NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
419 "This will most likely result in problems, because the register type "
420 "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
421 }
423 memset (values, 0, sizeof (values));
424 if ((data->register_type == REG_TYPE_INT32)
425 || (data->register_type == REG_TYPE_UINT32)
426 || (data->register_type == REG_TYPE_FLOAT))
427 values_num = 2;
428 else
429 values_num = 1;
431 #if LEGACY_LIBMODBUS
432 /* Version 2.0.3: Pass the connection struct as a pointer and pass the slave
433 * id to each call of "read_holding_registers". */
434 # define modbus_read_registers(ctx, addr, nb, dest) \
435 read_holding_registers (&(ctx), slave->id, (addr), (nb), (dest))
436 #else /* if !LEGACY_LIBMODBUS */
437 /* Version 2.9.2: Set the slave id once before querying the registers. */
438 status = modbus_set_slave (host->connection, slave->id);
439 if (status != 0)
440 {
441 ERROR ("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
442 slave->id, status);
443 return (-1);
444 }
445 #endif
447 for (i = 0; i < 2; i++)
448 {
449 status = modbus_read_registers (host->connection,
450 /* start_addr = */ data->register_base,
451 /* num_registers = */ values_num, /* buffer = */ values);
452 if (status > 0)
453 break;
455 if (host->is_connected)
456 {
457 #if LEGACY_LIBMODBUS
458 modbus_close (&host->connection);
459 host->is_connected = 0;
460 #else
461 modbus_close (host->connection);
462 modbus_free (host->connection);
463 host->connection = NULL;
464 #endif
465 }
467 /* If we already tried reconnecting this round, give up. */
468 if (host->have_reconnected)
469 {
470 ERROR ("Modbus plugin: modbus_read_registers (%s) failed. "
471 "Reconnecting has already been tried. Giving up.", host->host);
472 return (-1);
473 }
475 /* Maybe the device closed the connection during the waiting interval.
476 * Try re-establishing the connection. */
477 status = mb_init_connection (host);
478 if (status != 0)
479 {
480 ERROR ("Modbus plugin: modbus_read_registers (%s) failed. "
481 "While trying to reconnect, connecting to \"%s\" failed. "
482 "Giving up.",
483 host->host, host->node);
484 return (-1);
485 }
487 DEBUG ("Modbus plugin: Re-established connection to %s", host->host);
489 /* try again */
490 continue;
491 } /* for (i = 0, 1) */
493 DEBUG ("Modbus plugin: mb_read_data: Success! "
494 "modbus_read_registers returned with status %i.", status);
496 if (data->register_type == REG_TYPE_FLOAT)
497 {
498 float float_value;
499 value_t vt;
501 float_value = mb_register_to_float (values[0], values[1]);
502 DEBUG ("Modbus plugin: mb_read_data: "
503 "Returned float value is %g", (double) float_value);
505 CAST_TO_VALUE_T (ds, vt, float_value);
506 mb_submit (host, slave, data, vt);
507 }
508 else if (data->register_type == REG_TYPE_INT32)
509 {
510 union
511 {
512 uint32_t u32;
513 int32_t i32;
514 } v;
515 value_t vt;
517 v.u32 = (((uint32_t) values[0]) << 16)
518 | ((uint32_t) values[1]);
519 DEBUG ("Modbus plugin: mb_read_data: "
520 "Returned int32 value is %"PRIi32, v.i32);
522 CAST_TO_VALUE_T (ds, vt, v.i32);
523 mb_submit (host, slave, data, vt);
524 }
525 else if (data->register_type == REG_TYPE_INT16)
526 {
527 union
528 {
529 uint16_t u16;
530 int16_t i16;
531 } v;
532 value_t vt;
534 v.u16 = values[0];
536 DEBUG ("Modbus plugin: mb_read_data: "
537 "Returned int16 value is %"PRIi16, v.i16);
539 CAST_TO_VALUE_T (ds, vt, v.i16);
540 mb_submit (host, slave, data, vt);
541 }
542 else if (data->register_type == REG_TYPE_UINT32)
543 {
544 uint32_t v32;
545 value_t vt;
547 v32 = (((uint32_t) values[0]) << 16)
548 | ((uint32_t) values[1]);
549 DEBUG ("Modbus plugin: mb_read_data: "
550 "Returned uint32 value is %"PRIu32, v32);
552 CAST_TO_VALUE_T (ds, vt, v32);
553 mb_submit (host, slave, data, vt);
554 }
555 else /* if (data->register_type == REG_TYPE_UINT16) */
556 {
557 value_t vt;
559 DEBUG ("Modbus plugin: mb_read_data: "
560 "Returned uint16 value is %"PRIu16, values[0]);
562 CAST_TO_VALUE_T (ds, vt, values[0]);
563 mb_submit (host, slave, data, vt);
564 }
566 return (0);
567 } /* }}} int mb_read_data */
569 static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
570 {
571 mb_data_t *data;
572 int success;
573 int status;
575 if ((host == NULL) || (slave == NULL))
576 return (EINVAL);
578 success = 0;
579 for (data = slave->collect; data != NULL; data = data->next)
580 {
581 status = mb_read_data (host, slave, data);
582 if (status == 0)
583 success++;
584 }
586 if (success == 0)
587 return (-1);
588 else
589 return (0);
590 } /* }}} int mb_read_slave */
592 static int mb_read (user_data_t *user_data) /* {{{ */
593 {
594 mb_host_t *host;
595 size_t i;
596 int success;
597 int status;
599 if ((user_data == NULL) || (user_data->data == NULL))
600 return (EINVAL);
602 host = user_data->data;
604 /* Clear the reconnect flag. */
605 host->have_reconnected = 0;
607 success = 0;
608 for (i = 0; i < host->slaves_num; i++)
609 {
610 status = mb_read_slave (host, host->slaves + i);
611 if (status == 0)
612 success++;
613 }
615 if (success == 0)
616 return (-1);
617 else
618 return (0);
619 } /* }}} int mb_read */
621 /* Free functions */
623 static void data_free_one (mb_data_t *data) /* {{{ */
624 {
625 if (data == NULL)
626 return;
628 sfree (data->name);
629 sfree (data);
630 } /* }}} void data_free_one */
632 static void data_free_all (mb_data_t *data) /* {{{ */
633 {
634 mb_data_t *next;
636 if (data == NULL)
637 return;
639 next = data->next;
640 data_free_one (data);
642 data_free_all (next);
643 } /* }}} void data_free_all */
645 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
646 {
647 size_t i;
649 if (slaves == NULL)
650 return;
652 for (i = 0; i < slaves_num; i++)
653 data_free_all (slaves[i].collect);
654 sfree (slaves);
655 } /* }}} void slaves_free_all */
657 static void host_free (void *void_host) /* {{{ */
658 {
659 mb_host_t *host = void_host;
661 if (host == NULL)
662 return;
664 slaves_free_all (host->slaves, host->slaves_num);
665 sfree (host);
666 } /* }}} void host_free */
668 /* Config functions */
670 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
671 {
672 mb_data_t data;
673 int status;
674 int i;
676 memset (&data, 0, sizeof (data));
677 data.name = NULL;
678 data.register_type = REG_TYPE_UINT16;
679 data.next = NULL;
681 status = cf_util_get_string (ci, &data.name);
682 if (status != 0)
683 return (status);
685 for (i = 0; i < ci->children_num; i++)
686 {
687 oconfig_item_t *child = ci->children + i;
688 status = 0;
690 if (strcasecmp ("Type", child->key) == 0)
691 status = cf_util_get_string_buffer (child,
692 data.type, sizeof (data.type));
693 else if (strcasecmp ("Instance", child->key) == 0)
694 status = cf_util_get_string_buffer (child,
695 data.instance, sizeof (data.instance));
696 else if (strcasecmp ("RegisterBase", child->key) == 0)
697 status = cf_util_get_int (child, &data.register_base);
698 else if (strcasecmp ("RegisterType", child->key) == 0)
699 {
700 char tmp[16];
701 status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
702 if (status != 0)
703 /* do nothing */;
704 else if (strcasecmp ("Int16", tmp) == 0)
705 data.register_type = REG_TYPE_INT16;
706 else if (strcasecmp ("Int32", tmp) == 0)
707 data.register_type = REG_TYPE_INT32;
708 else if (strcasecmp ("Uint16", tmp) == 0)
709 data.register_type = REG_TYPE_UINT16;
710 else if (strcasecmp ("Uint32", tmp) == 0)
711 data.register_type = REG_TYPE_UINT32;
712 else if (strcasecmp ("Float", tmp) == 0)
713 data.register_type = REG_TYPE_FLOAT;
714 else
715 {
716 ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
717 status = -1;
718 }
719 }
720 else
721 {
722 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
723 status = -1;
724 }
726 if (status != 0)
727 break;
728 } /* for (i = 0; i < ci->children_num; i++) */
730 assert (data.name != NULL);
731 if (data.type[0] == 0)
732 {
733 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
734 data.name);
735 status = -1;
736 }
738 if (status == 0)
739 data_copy (&data_definitions, &data);
741 sfree (data.name);
743 return (status);
744 } /* }}} int mb_config_add_data */
746 static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
747 const char *address)
748 {
749 struct addrinfo *ai_list;
750 struct addrinfo *ai_ptr;
751 struct addrinfo ai_hints;
752 int status;
754 if ((host == NULL) || (address == NULL))
755 return (EINVAL);
757 memset (&ai_hints, 0, sizeof (ai_hints));
758 #if AI_ADDRCONFIG
759 ai_hints.ai_flags |= AI_ADDRCONFIG;
760 #endif
761 /* XXX: libmodbus can only handle IPv4 addresses. */
762 ai_hints.ai_family = AF_INET;
763 ai_hints.ai_addr = NULL;
764 ai_hints.ai_canonname = NULL;
765 ai_hints.ai_next = NULL;
767 ai_list = NULL;
768 status = getaddrinfo (address, /* service = */ NULL,
769 &ai_hints, &ai_list);
770 if (status != 0)
771 {
772 char errbuf[1024];
773 ERROR ("Modbus plugin: getaddrinfo failed: %s",
774 (status == EAI_SYSTEM)
775 ? sstrerror (errno, errbuf, sizeof (errbuf))
776 : gai_strerror (status));
777 return (status);
778 }
780 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
781 {
782 status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
783 host->node, sizeof (host->node),
784 /* service = */ NULL, /* length = */ 0,
785 /* flags = */ NI_NUMERICHOST);
786 if (status == 0)
787 break;
788 } /* for (ai_ptr) */
790 freeaddrinfo (ai_list);
792 if (status != 0)
793 ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
794 else /* if (status == 0) */
795 {
796 DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
797 address, host->node);
798 }
800 return (status);
801 } /* }}} int mb_config_set_host_address */
803 static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
804 {
805 mb_slave_t *slave;
806 int status;
807 int i;
809 if ((host == NULL) || (ci == NULL))
810 return (EINVAL);
812 slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
813 if (slave == NULL)
814 return (ENOMEM);
815 host->slaves = slave;
816 slave = host->slaves + host->slaves_num;
817 memset (slave, 0, sizeof (*slave));
818 slave->collect = NULL;
820 status = cf_util_get_int (ci, &slave->id);
821 if (status != 0)
822 return (status);
824 for (i = 0; i < ci->children_num; i++)
825 {
826 oconfig_item_t *child = ci->children + i;
827 status = 0;
829 if (strcasecmp ("Instance", child->key) == 0)
830 status = cf_util_get_string_buffer (child,
831 slave->instance, sizeof (slave->instance));
832 else if (strcasecmp ("Collect", child->key) == 0)
833 {
834 char buffer[1024];
835 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
836 if (status == 0)
837 data_copy_by_name (&slave->collect, data_definitions, buffer);
838 status = 0; /* continue after failure. */
839 }
840 else
841 {
842 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
843 status = -1;
844 }
846 if (status != 0)
847 break;
848 }
850 if ((status == 0) && (slave->collect == NULL))
851 status = EINVAL;
853 if (slave->id < 0)
854 status = EINVAL;
856 if (status == 0)
857 host->slaves_num++;
858 else /* if (status != 0) */
859 data_free_all (slave->collect);
861 return (status);
862 } /* }}} int mb_config_add_slave */
864 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
865 {
866 mb_host_t *host;
867 int status;
868 int i;
870 host = malloc (sizeof (*host));
871 if (host == NULL)
872 return (ENOMEM);
873 memset (host, 0, sizeof (*host));
874 host->slaves = NULL;
876 status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
877 if (status != 0)
878 return (status);
879 if (host->host[0] == 0)
880 return (EINVAL);
882 for (i = 0; i < ci->children_num; i++)
883 {
884 oconfig_item_t *child = ci->children + i;
885 status = 0;
887 if (strcasecmp ("Address", child->key) == 0)
888 {
889 char buffer[NI_MAXHOST];
890 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
891 if (status == 0)
892 status = mb_config_set_host_address (host, buffer);
893 }
894 else if (strcasecmp ("Port", child->key) == 0)
895 {
896 host->port = cf_util_get_port_number (child);
897 if (host->port <= 0)
898 status = -1;
899 }
900 else if (strcasecmp ("Interval", child->key) == 0)
901 status = cf_util_get_cdtime (child, &host->interval);
902 else if (strcasecmp ("Slave", child->key) == 0)
903 /* Don't set status: Gracefully continue if a slave fails. */
904 mb_config_add_slave (host, child);
905 else
906 {
907 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
908 status = -1;
909 }
911 if (status != 0)
912 break;
913 } /* for (i = 0; i < ci->children_num; i++) */
915 assert (host->host[0] != 0);
916 if (host->host[0] == 0)
917 {
918 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
919 host->host);
920 status = -1;
921 }
923 if (status == 0)
924 {
925 user_data_t ud;
926 char name[1024];
927 struct timespec interval = { 0, 0 };
929 ud.data = host;
930 ud.free_func = host_free;
932 ssnprintf (name, sizeof (name), "modbus-%s", host->host);
934 CDTIME_T_TO_TIMESPEC (host->interval, &interval);
936 plugin_register_complex_read (/* group = */ NULL, name,
937 /* callback = */ mb_read,
938 /* interval = */ (host->interval > 0) ? &interval : NULL,
939 &ud);
940 }
941 else
942 {
943 host_free (host);
944 }
946 return (status);
947 } /* }}} int mb_config_add_host */
949 static int mb_config (oconfig_item_t *ci) /* {{{ */
950 {
951 int i;
953 if (ci == NULL)
954 return (EINVAL);
956 for (i = 0; i < ci->children_num; i++)
957 {
958 oconfig_item_t *child = ci->children + i;
960 if (strcasecmp ("Data", child->key) == 0)
961 mb_config_add_data (child);
962 else if (strcasecmp ("Host", child->key) == 0)
963 mb_config_add_host (child);
964 else
965 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
966 }
968 return (0);
969 } /* }}} int mb_config */
971 /* ========= */
973 static int mb_shutdown (void) /* {{{ */
974 {
975 data_free_all (data_definitions);
976 data_definitions = NULL;
978 return (0);
979 } /* }}} int mb_shutdown */
981 void module_register (void)
982 {
983 plugin_register_complex_config ("modbus", mb_config);
984 plugin_register_shutdown ("modbus", mb_shutdown);
985 } /* void module_register */
987 /* vim: set sw=2 sts=2 et fdm=marker : */