1 /**
2 * collectd - src/modbus.c
3 * Copyright (C) 2010,2011 noris network AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; only version 2.1 of the License is
8 * applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian Forster <octo at noris.net>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
28 #include <netdb.h>
30 #include <modbus.h>
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
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
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 */
67 /*
68 * Data structures
69 */
70 enum mb_register_type_e /* {{{ */
71 {
72 REG_TYPE_INT16,
73 REG_TYPE_INT32,
74 REG_TYPE_UINT16,
75 REG_TYPE_UINT32,
76 REG_TYPE_FLOAT
77 }; /* }}} */
78 typedef enum mb_register_type_e mb_register_type_t;
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];
90 mb_data_t *next;
91 }; /* }}} */
93 struct mb_slave_s /* {{{ */
94 {
95 int id;
96 char instance[DATA_MAX_NAME_LEN];
97 mb_data_t *collect;
98 }; /* }}} */
99 typedef struct mb_slave_s mb_slave_t;
101 struct mb_host_s /* {{{ */
102 {
103 char host[DATA_MAX_NAME_LEN];
104 char node[NI_MAXHOST];
105 /* char service[NI_MAXSERV]; */
106 int port;
107 cdtime_t interval;
109 mb_slave_t *slaves;
110 size_t slaves_num;
112 #if LEGACY_LIBMODBUS
113 modbus_param_t connection;
114 #else
115 modbus_t *connection;
116 #endif
117 _Bool is_connected;
118 }; /* }}} */
119 typedef struct mb_host_s mb_host_t;
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 = plugin_get_interval ();
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 uint16_t s[2];
263 float f;
264 } conv;
266 #if BYTE_ORDER == LITTLE_ENDIAN
267 /* little endian */
268 conv.b[0] = lo & 0x00ff;
269 conv.b[1] = (lo >> 8) & 0x00ff;
270 conv.b[2] = hi & 0x00ff;
271 conv.b[3] = (hi >> 8) & 0x00ff;
272 #else
273 conv.b[3] = lo & 0x00ff;
274 conv.b[2] = (lo >> 8) & 0x00ff;
275 conv.b[1] = hi & 0x00ff;
276 conv.b[0] = (hi >> 8) & 0x00ff;
277 #endif
279 return (conv.f);
280 } /* }}} float mb_register_to_float */
282 #if LEGACY_LIBMODBUS
283 /* Version 2.0.3 */
284 static int mb_init_connection (mb_host_t *host) /* {{{ */
285 {
286 int status;
288 if (host == NULL)
289 return (EINVAL);
291 modbus_set_debug (&host->connection, 1);
293 /* We'll do the error handling ourselves. */
294 modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
296 if ((host->port < 1) || (host->port > 65535))
297 host->port = MODBUS_TCP_DEFAULT_PORT;
299 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
300 host->node, host->port);
302 modbus_init_tcp (&host->connection,
303 /* host = */ host->node,
304 /* port = */ host->port);
306 status = modbus_connect (&host->connection);
307 if (status != 0)
308 {
309 ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
310 host->node, host->port, status);
311 return (status);
312 }
314 host->is_connected = 1;
315 return (0);
316 } /* }}} int mb_init_connection */
317 /* #endif LEGACY_LIBMODBUS */
319 #else /* if !LEGACY_LIBMODBUS */
320 /* Version 2.9.2 */
321 static int mb_init_connection (mb_host_t *host) /* {{{ */
322 {
323 int status;
325 if (host == NULL)
326 return (EINVAL);
328 if (host->connection != NULL)
329 return (0);
331 if ((host->port < 1) || (host->port > 65535))
332 host->port = MODBUS_TCP_DEFAULT_PORT;
334 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
335 host->node, host->port);
337 host->connection = modbus_new_tcp (host->node, host->port);
338 if (host->connection == NULL)
339 {
340 ERROR ("Modbus plugin: Creating new Modbus/TCP object failed.");
341 return (-1);
342 }
344 modbus_set_debug (host->connection, 1);
346 /* We'll do the error handling ourselves. */
347 modbus_set_error_recovery (host->connection, 0);
349 status = modbus_connect (host->connection);
350 if (status != 0)
351 {
352 ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
353 host->node, host->port, status);
354 modbus_free (host->connection);
355 host->connection = NULL;
356 return (status);
357 }
359 return (0);
360 } /* }}} int mb_init_connection */
361 #endif /* !LEGACY_LIBMODBUS */
363 #define CAST_TO_VALUE_T(ds,vt,raw) do { \
364 if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
365 (vt).counter = (counter_t) (raw); \
366 else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
367 (vt).gauge = (gauge_t) (raw); \
368 else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
369 (vt).derive = (derive_t) (raw); \
370 else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
371 (vt).absolute = (absolute_t) (raw); \
372 } while (0)
374 static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
375 mb_data_t *data)
376 {
377 uint16_t values[2];
378 int values_num;
379 const data_set_t *ds;
380 int status = 0;
382 if ((host == NULL) || (slave == NULL) || (data == NULL))
383 return (EINVAL);
385 ds = plugin_get_ds (data->type);
386 if (ds == NULL)
387 {
388 ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
389 return (-1);
390 }
392 if (ds->ds_num != 1)
393 {
394 ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
395 "I can only handle data sets with only one data source.",
396 data->type, ds->ds_num);
397 return (-1);
398 }
400 if ((ds->ds[0].type != DS_TYPE_GAUGE)
401 && (data->register_type != REG_TYPE_INT32)
402 && (data->register_type != REG_TYPE_UINT32))
403 {
404 NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
405 "This will most likely result in problems, because the register type "
406 "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
407 }
409 memset (values, 0, sizeof (values));
410 if ((data->register_type == REG_TYPE_INT32)
411 || (data->register_type == REG_TYPE_UINT32)
412 || (data->register_type == REG_TYPE_FLOAT))
413 values_num = 2;
414 else
415 values_num = 1;
417 if (host->connection == NULL)
418 {
419 status = EBADF;
420 }
421 else
422 {
423 struct sockaddr sockaddr;
424 socklen_t saddrlen = sizeof (sockaddr);
426 status = getpeername (modbus_get_socket (host->connection),
427 &sockaddr, &saddrlen);
428 if (status != 0)
429 status = errno;
430 }
432 if ((status == EBADF) || (status == ENOTSOCK) || (status == ENOTCONN))
433 {
434 status = mb_init_connection (host);
435 if (status != 0)
436 {
437 ERROR ("Modbus plugin: mb_init_connection (%s/%s) failed. ",
438 host->host, host->node);
439 host->is_connected = 0;
440 host->connection = NULL;
441 return (-1);
442 }
443 }
444 else if (status != 0)
445 {
446 #if LEGACY_LIBMODBUS
447 modbus_close (&host->connection);
448 #else
449 modbus_close (host->connection);
450 modbus_free (host->connection);
451 #endif
452 }
454 #if LEGACY_LIBMODBUS
455 /* Version 2.0.3: Pass the connection struct as a pointer and pass the slave
456 * id to each call of "read_holding_registers". */
457 # define modbus_read_registers(ctx, addr, nb, dest) \
458 read_holding_registers (&(ctx), slave->id, (addr), (nb), (dest))
459 #else /* if !LEGACY_LIBMODBUS */
460 /* Version 2.9.2: Set the slave id once before querying the registers. */
461 status = modbus_set_slave (host->connection, slave->id);
462 if (status != 0)
463 {
464 ERROR ("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
465 slave->id, status);
466 return (-1);
467 }
468 #endif
470 status = modbus_read_registers (host->connection,
471 /* start_addr = */ data->register_base,
472 /* num_registers = */ values_num, /* buffer = */ values);
473 if (status != values_num)
474 {
475 ERROR ("Modbus plugin: modbus_read_registers (%s/%s) failed. status = %i, values_num = %i "
476 "Giving up.", host->host, host->node, status, values_num);
477 #if LEGACY_LIBMODBUS
478 modbus_close (&host->connection);
479 #else
480 modbus_close (host->connection);
481 modbus_free (host->connection);
482 #endif
483 host->connection = NULL;
484 return (-1);
485 }
487 DEBUG ("Modbus plugin: mb_read_data: Success! "
488 "modbus_read_registers returned with status %i.", status);
490 if (data->register_type == REG_TYPE_FLOAT)
491 {
492 float float_value;
493 value_t vt;
495 float_value = mb_register_to_float (values[0], values[1]);
496 DEBUG ("Modbus plugin: mb_read_data: "
497 "Returned float value is %g", (double) float_value);
499 CAST_TO_VALUE_T (ds, vt, float_value);
500 mb_submit (host, slave, data, vt);
501 }
502 else if (data->register_type == REG_TYPE_INT32)
503 {
504 union
505 {
506 uint32_t u32;
507 int32_t i32;
508 } v;
509 value_t vt;
511 v.u32 = (((uint32_t) values[0]) << 16)
512 | ((uint32_t) values[1]);
513 DEBUG ("Modbus plugin: mb_read_data: "
514 "Returned int32 value is %"PRIi32, v.i32);
516 CAST_TO_VALUE_T (ds, vt, v.i32);
517 mb_submit (host, slave, data, vt);
518 }
519 else if (data->register_type == REG_TYPE_INT16)
520 {
521 union
522 {
523 uint16_t u16;
524 int16_t i16;
525 } v;
526 value_t vt;
528 v.u16 = values[0];
530 DEBUG ("Modbus plugin: mb_read_data: "
531 "Returned int16 value is %"PRIi16, v.i16);
533 CAST_TO_VALUE_T (ds, vt, v.i16);
534 mb_submit (host, slave, data, vt);
535 }
536 else if (data->register_type == REG_TYPE_UINT32)
537 {
538 uint32_t v32;
539 value_t vt;
541 v32 = (((uint32_t) values[0]) << 16)
542 | ((uint32_t) values[1]);
543 DEBUG ("Modbus plugin: mb_read_data: "
544 "Returned uint32 value is %"PRIu32, v32);
546 CAST_TO_VALUE_T (ds, vt, v32);
547 mb_submit (host, slave, data, vt);
548 }
549 else /* if (data->register_type == REG_TYPE_UINT16) */
550 {
551 value_t vt;
553 DEBUG ("Modbus plugin: mb_read_data: "
554 "Returned uint16 value is %"PRIu16, values[0]);
556 CAST_TO_VALUE_T (ds, vt, values[0]);
557 mb_submit (host, slave, data, vt);
558 }
560 return (0);
561 } /* }}} int mb_read_data */
563 static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
564 {
565 mb_data_t *data;
566 int success;
567 int status;
569 if ((host == NULL) || (slave == NULL))
570 return (EINVAL);
572 success = 0;
573 for (data = slave->collect; data != NULL; data = data->next)
574 {
575 status = mb_read_data (host, slave, data);
576 if (status == 0)
577 success++;
578 }
580 if (success == 0)
581 return (-1);
582 else
583 return (0);
584 } /* }}} int mb_read_slave */
586 static int mb_read (user_data_t *user_data) /* {{{ */
587 {
588 mb_host_t *host;
589 size_t i;
590 int success;
591 int status;
593 if ((user_data == NULL) || (user_data->data == NULL))
594 return (EINVAL);
596 host = user_data->data;
598 success = 0;
599 for (i = 0; i < host->slaves_num; i++)
600 {
601 status = mb_read_slave (host, host->slaves + i);
602 if (status == 0)
603 success++;
604 }
606 if (success == 0)
607 return (-1);
608 else
609 return (0);
610 } /* }}} int mb_read */
612 /* Free functions */
614 static void data_free_one (mb_data_t *data) /* {{{ */
615 {
616 if (data == NULL)
617 return;
619 sfree (data->name);
620 sfree (data);
621 } /* }}} void data_free_one */
623 static void data_free_all (mb_data_t *data) /* {{{ */
624 {
625 mb_data_t *next;
627 if (data == NULL)
628 return;
630 next = data->next;
631 data_free_one (data);
633 data_free_all (next);
634 } /* }}} void data_free_all */
636 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
637 {
638 size_t i;
640 if (slaves == NULL)
641 return;
643 for (i = 0; i < slaves_num; i++)
644 data_free_all (slaves[i].collect);
645 sfree (slaves);
646 } /* }}} void slaves_free_all */
648 static void host_free (void *void_host) /* {{{ */
649 {
650 mb_host_t *host = void_host;
652 if (host == NULL)
653 return;
655 slaves_free_all (host->slaves, host->slaves_num);
656 sfree (host);
657 } /* }}} void host_free */
659 /* Config functions */
661 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
662 {
663 mb_data_t data;
664 int status;
665 int i;
667 memset (&data, 0, sizeof (data));
668 data.name = NULL;
669 data.register_type = REG_TYPE_UINT16;
670 data.next = NULL;
672 status = cf_util_get_string (ci, &data.name);
673 if (status != 0)
674 return (status);
676 for (i = 0; i < ci->children_num; i++)
677 {
678 oconfig_item_t *child = ci->children + i;
680 if (strcasecmp ("Type", child->key) == 0)
681 status = cf_util_get_string_buffer (child,
682 data.type, sizeof (data.type));
683 else if (strcasecmp ("Instance", child->key) == 0)
684 status = cf_util_get_string_buffer (child,
685 data.instance, sizeof (data.instance));
686 else if (strcasecmp ("RegisterBase", child->key) == 0)
687 status = cf_util_get_int (child, &data.register_base);
688 else if (strcasecmp ("RegisterType", child->key) == 0)
689 {
690 char tmp[16];
691 status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
692 if (status != 0)
693 /* do nothing */;
694 else if (strcasecmp ("Int16", tmp) == 0)
695 data.register_type = REG_TYPE_INT16;
696 else if (strcasecmp ("Int32", tmp) == 0)
697 data.register_type = REG_TYPE_INT32;
698 else if (strcasecmp ("Uint16", tmp) == 0)
699 data.register_type = REG_TYPE_UINT16;
700 else if (strcasecmp ("Uint32", tmp) == 0)
701 data.register_type = REG_TYPE_UINT32;
702 else if (strcasecmp ("Float", tmp) == 0)
703 data.register_type = REG_TYPE_FLOAT;
704 else
705 {
706 ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
707 status = -1;
708 }
709 }
710 else
711 {
712 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
713 status = -1;
714 }
716 if (status != 0)
717 break;
718 } /* for (i = 0; i < ci->children_num; i++) */
720 assert (data.name != NULL);
721 if (data.type[0] == 0)
722 {
723 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
724 data.name);
725 status = -1;
726 }
728 if (status == 0)
729 data_copy (&data_definitions, &data);
731 sfree (data.name);
733 return (status);
734 } /* }}} int mb_config_add_data */
736 static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
737 const char *address)
738 {
739 struct addrinfo *ai_list;
740 struct addrinfo *ai_ptr;
741 struct addrinfo ai_hints;
742 int status;
744 if ((host == NULL) || (address == NULL))
745 return (EINVAL);
747 memset (&ai_hints, 0, sizeof (ai_hints));
748 #if AI_ADDRCONFIG
749 ai_hints.ai_flags |= AI_ADDRCONFIG;
750 #endif
751 /* XXX: libmodbus can only handle IPv4 addresses. */
752 ai_hints.ai_family = AF_INET;
753 ai_hints.ai_addr = NULL;
754 ai_hints.ai_canonname = NULL;
755 ai_hints.ai_next = NULL;
757 ai_list = NULL;
758 status = getaddrinfo (address, /* service = */ NULL,
759 &ai_hints, &ai_list);
760 if (status != 0)
761 {
762 char errbuf[1024];
763 ERROR ("Modbus plugin: getaddrinfo failed: %s",
764 (status == EAI_SYSTEM)
765 ? sstrerror (errno, errbuf, sizeof (errbuf))
766 : gai_strerror (status));
767 return (status);
768 }
770 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
771 {
772 status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
773 host->node, sizeof (host->node),
774 /* service = */ NULL, /* length = */ 0,
775 /* flags = */ NI_NUMERICHOST);
776 if (status == 0)
777 break;
778 } /* for (ai_ptr) */
780 freeaddrinfo (ai_list);
782 if (status != 0)
783 ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
784 else /* if (status == 0) */
785 {
786 DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
787 address, host->node);
788 }
790 return (status);
791 } /* }}} int mb_config_set_host_address */
793 static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
794 {
795 mb_slave_t *slave;
796 int status;
797 int i;
799 if ((host == NULL) || (ci == NULL))
800 return (EINVAL);
802 slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
803 if (slave == NULL)
804 return (ENOMEM);
805 host->slaves = slave;
806 slave = host->slaves + host->slaves_num;
807 memset (slave, 0, sizeof (*slave));
808 slave->collect = NULL;
810 status = cf_util_get_int (ci, &slave->id);
811 if (status != 0)
812 return (status);
814 for (i = 0; i < ci->children_num; i++)
815 {
816 oconfig_item_t *child = ci->children + i;
818 if (strcasecmp ("Instance", child->key) == 0)
819 status = cf_util_get_string_buffer (child,
820 slave->instance, sizeof (slave->instance));
821 else if (strcasecmp ("Collect", child->key) == 0)
822 {
823 char buffer[1024];
824 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
825 if (status == 0)
826 data_copy_by_name (&slave->collect, data_definitions, buffer);
827 status = 0; /* continue after failure. */
828 }
829 else
830 {
831 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
832 status = -1;
833 }
835 if (status != 0)
836 break;
837 }
839 if ((status == 0) && (slave->collect == NULL))
840 status = EINVAL;
842 if (slave->id < 0)
843 status = EINVAL;
845 if (status == 0)
846 host->slaves_num++;
847 else /* if (status != 0) */
848 data_free_all (slave->collect);
850 return (status);
851 } /* }}} int mb_config_add_slave */
853 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
854 {
855 mb_host_t *host;
856 int status;
857 int i;
859 host = malloc (sizeof (*host));
860 if (host == NULL)
861 return (ENOMEM);
862 memset (host, 0, sizeof (*host));
863 host->slaves = NULL;
865 status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
866 if (status != 0)
867 {
868 sfree (host);
869 return (status);
870 }
871 if (host->host[0] == 0)
872 {
873 sfree (host);
874 return (EINVAL);
875 }
877 for (i = 0; i < ci->children_num; i++)
878 {
879 oconfig_item_t *child = ci->children + i;
880 status = 0;
882 if (strcasecmp ("Address", child->key) == 0)
883 {
884 char buffer[NI_MAXHOST];
885 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
886 if (status == 0)
887 status = mb_config_set_host_address (host, buffer);
888 }
889 else if (strcasecmp ("Port", child->key) == 0)
890 {
891 host->port = cf_util_get_port_number (child);
892 if (host->port <= 0)
893 status = -1;
894 }
895 else if (strcasecmp ("Interval", child->key) == 0)
896 status = cf_util_get_cdtime (child, &host->interval);
897 else if (strcasecmp ("Slave", child->key) == 0)
898 /* Don't set status: Gracefully continue if a slave fails. */
899 mb_config_add_slave (host, child);
900 else
901 {
902 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
903 status = -1;
904 }
906 if (status != 0)
907 break;
908 } /* for (i = 0; i < ci->children_num; i++) */
910 assert (host->host[0] != 0);
911 if (host->host[0] == 0)
912 {
913 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
914 host->host);
915 status = -1;
916 }
918 if (status == 0)
919 {
920 user_data_t ud;
921 char name[1024];
922 struct timespec interval = { 0, 0 };
924 ud.data = host;
925 ud.free_func = host_free;
927 ssnprintf (name, sizeof (name), "modbus-%s", host->host);
929 CDTIME_T_TO_TIMESPEC (host->interval, &interval);
931 plugin_register_complex_read (/* group = */ NULL, name,
932 /* callback = */ mb_read,
933 /* interval = */ (host->interval > 0) ? &interval : NULL,
934 &ud);
935 }
936 else
937 {
938 host_free (host);
939 }
941 return (status);
942 } /* }}} int mb_config_add_host */
944 static int mb_config (oconfig_item_t *ci) /* {{{ */
945 {
946 int i;
948 if (ci == NULL)
949 return (EINVAL);
951 for (i = 0; i < ci->children_num; i++)
952 {
953 oconfig_item_t *child = ci->children + i;
955 if (strcasecmp ("Data", child->key) == 0)
956 mb_config_add_data (child);
957 else if (strcasecmp ("Host", child->key) == 0)
958 mb_config_add_host (child);
959 else
960 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
961 }
963 return (0);
964 } /* }}} int mb_config */
966 /* ========= */
968 static int mb_shutdown (void) /* {{{ */
969 {
970 data_free_all (data_definitions);
971 data_definitions = NULL;
973 return (0);
974 } /* }}} int mb_shutdown */
976 void module_register (void)
977 {
978 plugin_register_complex_config ("modbus", mb_config);
979 plugin_register_shutdown ("modbus", mb_shutdown);
980 } /* void module_register */
982 /* vim: set sw=2 sts=2 et fdm=marker : */