795465f041d2a1962ea270ff40cd456771089394
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/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 * ModbusRegisterType holding
53 * Instance "..."
54 * </Data>
55 *
56 * <Host "name">
57 * Address "addr"
58 * Port "1234"
59 * Interval 60
60 *
61 * <Slave 1>
62 * Instance "foobar" # optional
63 * Collect "data_name"
64 * </Slave>
65 * </Host>
66 */
68 /*
69 * Data structures
70 */
71 enum mb_register_type_e /* {{{ */
72 {
73 REG_TYPE_INT16,
74 REG_TYPE_INT32,
75 REG_TYPE_UINT16,
76 REG_TYPE_UINT32,
77 REG_TYPE_FLOAT
78 }; /* }}} */
79 enum mb_mreg_type_e /* {{{ */
80 {
81 MREG_HOLDING,
82 MREG_INPUT
83 }; /* }}} */
84 typedef enum mb_register_type_e mb_register_type_t;
85 typedef enum mb_mreg_type_e mb_mreg_type_t;
87 struct mb_data_s;
88 typedef struct mb_data_s mb_data_t;
89 struct mb_data_s /* {{{ */
90 {
91 char *name;
92 int register_base;
93 mb_register_type_t register_type;
94 mb_mreg_type_t modbus_register_type;
95 char type[DATA_MAX_NAME_LEN];
96 char instance[DATA_MAX_NAME_LEN];
98 mb_data_t *next;
99 }; /* }}} */
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;
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;
117 mb_slave_t *slaves;
118 size_t slaves_num;
120 #if LEGACY_LIBMODBUS
121 modbus_param_t connection;
122 #else
123 modbus_t *connection;
124 #endif
125 _Bool is_connected;
126 }; /* }}} */
127 typedef struct mb_host_s mb_host_t;
129 struct mb_data_group_s;
130 typedef struct mb_data_group_s mb_data_group_t;
131 struct mb_data_group_s /* {{{ */
132 {
133 mb_data_t *registers;
134 size_t registers_num;
136 mb_data_group_t *next;
137 }; /* }}} */
139 /*
140 * Global variables
141 */
142 static mb_data_t *data_definitions = NULL;
144 /*
145 * Functions
146 */
147 static mb_data_t *data_get_by_name (mb_data_t *src, /* {{{ */
148 const char *name)
149 {
150 mb_data_t *ptr;
152 if (name == NULL)
153 return (NULL);
155 for (ptr = src; ptr != NULL; ptr = ptr->next)
156 if (strcasecmp (ptr->name, name) == 0)
157 return (ptr);
159 return (NULL);
160 } /* }}} mb_data_t *data_get_by_name */
162 static int data_append (mb_data_t **dst, mb_data_t *src) /* {{{ */
163 {
164 mb_data_t *ptr;
166 if ((dst == NULL) || (src == NULL))
167 return (EINVAL);
169 ptr = *dst;
171 if (ptr == NULL)
172 {
173 *dst = src;
174 return (0);
175 }
177 while (ptr->next != NULL)
178 ptr = ptr->next;
180 ptr->next = src;
182 return (0);
183 } /* }}} int data_append */
185 /* Copy a single mb_data_t and append it to another list. */
186 static int data_copy (mb_data_t **dst, const mb_data_t *src) /* {{{ */
187 {
188 mb_data_t *tmp;
189 int status;
191 if ((dst == NULL) || (src == NULL))
192 return (EINVAL);
194 tmp = malloc (sizeof (*tmp));
195 if (tmp == NULL)
196 return (ENOMEM);
197 memcpy (tmp, src, sizeof (*tmp));
198 tmp->name = NULL;
199 tmp->next = NULL;
201 tmp->name = strdup (src->name);
202 if (tmp->name == NULL)
203 {
204 sfree (tmp);
205 return (ENOMEM);
206 }
208 status = data_append (dst, tmp);
209 if (status != 0)
210 {
211 sfree (tmp->name);
212 sfree (tmp);
213 return (status);
214 }
216 return (0);
217 } /* }}} int data_copy */
219 /* Lookup a single mb_data_t instance, copy it and append the copy to another
220 * list. */
221 static int data_copy_by_name (mb_data_t **dst, mb_data_t *src, /* {{{ */
222 const char *name)
223 {
224 mb_data_t *ptr;
226 if ((dst == NULL) || (src == NULL) || (name == NULL))
227 return (EINVAL);
229 ptr = data_get_by_name (src, name);
230 if (ptr == NULL)
231 return (ENOENT);
233 return (data_copy (dst, ptr));
234 } /* }}} int data_copy_by_name */
236 /* Read functions */
238 static int mb_submit (mb_host_t *host, mb_slave_t *slave, /* {{{ */
239 mb_data_t *data, value_t value)
240 {
241 value_list_t vl = VALUE_LIST_INIT;
243 if ((host == NULL) || (slave == NULL) || (data == NULL))
244 return (EINVAL);
246 if (host->interval <= 0)
247 host->interval = plugin_get_interval ();
249 if (slave->instance[0] == 0)
250 ssnprintf (slave->instance, sizeof (slave->instance), "slave_%i",
251 slave->id);
253 vl.values = &value;
254 vl.values_len = 1;
255 vl.interval = host->interval;
256 sstrncpy (vl.host, host->host, sizeof (vl.host));
257 sstrncpy (vl.plugin, "modbus", sizeof (vl.plugin));
258 sstrncpy (vl.plugin_instance, slave->instance, sizeof (vl.plugin_instance));
259 sstrncpy (vl.type, data->type, sizeof (vl.type));
260 sstrncpy (vl.type_instance, data->instance, sizeof (vl.type_instance));
262 return (plugin_dispatch_values (&vl));
263 } /* }}} int mb_submit */
265 static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */
266 {
267 union
268 {
269 uint8_t b[4];
270 uint16_t s[2];
271 float f;
272 } conv;
274 #if BYTE_ORDER == LITTLE_ENDIAN
275 /* little endian */
276 conv.b[0] = lo & 0x00ff;
277 conv.b[1] = (lo >> 8) & 0x00ff;
278 conv.b[2] = hi & 0x00ff;
279 conv.b[3] = (hi >> 8) & 0x00ff;
280 #else
281 conv.b[3] = lo & 0x00ff;
282 conv.b[2] = (lo >> 8) & 0x00ff;
283 conv.b[1] = hi & 0x00ff;
284 conv.b[0] = (hi >> 8) & 0x00ff;
285 #endif
287 return (conv.f);
288 } /* }}} float mb_register_to_float */
290 #if LEGACY_LIBMODBUS
291 /* Version 2.0.3 */
292 static int mb_init_connection (mb_host_t *host) /* {{{ */
293 {
294 int status;
296 if (host == NULL)
297 return (EINVAL);
299 modbus_set_debug (&host->connection, 1);
301 /* We'll do the error handling ourselves. */
302 modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
304 if ((host->port < 1) || (host->port > 65535))
305 host->port = MODBUS_TCP_DEFAULT_PORT;
307 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
308 host->node, host->port);
310 modbus_init_tcp (&host->connection,
311 /* host = */ host->node,
312 /* port = */ host->port);
314 status = modbus_connect (&host->connection);
315 if (status != 0)
316 {
317 ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
318 host->node, host->port, status);
319 return (status);
320 }
322 host->is_connected = 1;
323 return (0);
324 } /* }}} int mb_init_connection */
325 /* #endif LEGACY_LIBMODBUS */
327 #else /* if !LEGACY_LIBMODBUS */
328 /* Version 2.9.2 */
329 static int mb_init_connection (mb_host_t *host) /* {{{ */
330 {
331 int status;
333 if (host == NULL)
334 return (EINVAL);
336 if (host->connection != NULL)
337 return (0);
339 if ((host->port < 1) || (host->port > 65535))
340 host->port = MODBUS_TCP_DEFAULT_PORT;
342 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
343 host->node, host->port);
345 host->connection = modbus_new_tcp (host->node, host->port);
346 if (host->connection == NULL)
347 {
348 ERROR ("Modbus plugin: Creating new Modbus/TCP object failed.");
349 return (-1);
350 }
352 modbus_set_debug (host->connection, 1);
354 /* We'll do the error handling ourselves. */
355 modbus_set_error_recovery (host->connection, 0);
357 status = modbus_connect (host->connection);
358 if (status != 0)
359 {
360 ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
361 host->node, host->port, status);
362 modbus_free (host->connection);
363 host->connection = NULL;
364 return (status);
365 }
367 return (0);
368 } /* }}} int mb_init_connection */
369 #endif /* !LEGACY_LIBMODBUS */
371 #define CAST_TO_VALUE_T(ds,vt,raw) do { \
372 if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
373 (vt).counter = (counter_t) (raw); \
374 else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
375 (vt).gauge = (gauge_t) (raw); \
376 else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
377 (vt).derive = (derive_t) (raw); \
378 else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
379 (vt).absolute = (absolute_t) (raw); \
380 } while (0)
382 static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
383 mb_data_t *data)
384 {
385 uint16_t values[2];
386 int values_num;
387 const data_set_t *ds;
388 int status;
390 if ((host == NULL) || (slave == NULL) || (data == NULL))
391 return (EINVAL);
393 ds = plugin_get_ds (data->type);
394 if (ds == NULL)
395 {
396 ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
397 return (-1);
398 }
400 if (ds->ds_num != 1)
401 {
402 ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
403 "I can only handle data sets with only one data source.",
404 data->type, ds->ds_num);
405 return (-1);
406 }
408 if ((ds->ds[0].type != DS_TYPE_GAUGE)
409 && (data->register_type != REG_TYPE_INT32)
410 && (data->register_type != REG_TYPE_UINT32))
411 {
412 NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
413 "This will most likely result in problems, because the register type "
414 "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
415 }
417 memset (values, 0, sizeof (values));
418 if ((data->register_type == REG_TYPE_INT32)
419 || (data->register_type == REG_TYPE_UINT32)
420 || (data->register_type == REG_TYPE_FLOAT))
421 values_num = 2;
422 else
423 values_num = 1;
425 status = 0;
426 if (host->connection == NULL)
427 {
428 status = EBADF;
429 }
430 else
431 {
432 struct sockaddr sockaddr;
433 socklen_t saddrlen = sizeof (sockaddr);
435 status = getpeername (modbus_get_socket (host->connection),
436 &sockaddr, &saddrlen);
437 if (status != 0)
438 status = errno;
439 }
441 if ((status == EBADF) || (status == ENOTSOCK) || (status == ENOTCONN))
442 {
443 status = mb_init_connection (host);
444 if (status != 0)
445 {
446 ERROR ("Modbus plugin: mb_init_connection (%s/%s) failed. ",
447 host->host, host->node);
448 host->is_connected = 0;
449 host->connection = NULL;
450 return (-1);
451 }
452 }
453 else if (status != 0)
454 {
455 #if LEGACY_LIBMODBUS
456 modbus_close (&host->connection);
457 #else
458 modbus_close (host->connection);
459 modbus_free (host->connection);
460 #endif
461 }
463 #if LEGACY_LIBMODBUS
464 /* Version 2.0.3: Pass the connection struct as a pointer and pass the slave
465 * id to each call of "read_holding_registers". */
466 # define modbus_read_registers(ctx, addr, nb, dest) \
467 read_holding_registers (&(ctx), slave->id, (addr), (nb), (dest))
468 #else /* if !LEGACY_LIBMODBUS */
469 /* Version 2.9.2: Set the slave id once before querying the registers. */
470 status = modbus_set_slave (host->connection, slave->id);
471 if (status != 0)
472 {
473 ERROR ("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
474 slave->id, status);
475 return (-1);
476 }
477 #endif
478 if (data->modbus_register_type == MREG_INPUT){
479 status = modbus_read_input_registers (host->connection,
480 /* start_addr = */ data->register_base,
481 /* num_registers = */ values_num, /* buffer = */ values);
482 }
483 else{
484 status = modbus_read_registers (host->connection,
485 /* start_addr = */ data->register_base,
486 /* num_registers = */ values_num, /* buffer = */ values);
487 }
488 if (status != values_num)
489 {
490 ERROR ("Modbus plugin: modbus read function (%s/%s) failed. "
491 " status = %i, values_num = %i. Giving up.",
492 host->host, host->node, status, values_num);
493 #if LEGACY_LIBMODBUS
494 modbus_close (&host->connection);
495 #else
496 modbus_close (host->connection);
497 modbus_free (host->connection);
498 #endif
499 host->connection = NULL;
500 return (-1);
501 }
503 DEBUG ("Modbus plugin: mb_read_data: Success! "
504 "modbus_read_registers returned with status %i.", status);
506 if (data->register_type == REG_TYPE_FLOAT)
507 {
508 float float_value;
509 value_t vt;
511 float_value = mb_register_to_float (values[0], values[1]);
512 DEBUG ("Modbus plugin: mb_read_data: "
513 "Returned float value is %g", (double) float_value);
515 CAST_TO_VALUE_T (ds, vt, float_value);
516 mb_submit (host, slave, data, vt);
517 }
518 else if (data->register_type == REG_TYPE_INT32)
519 {
520 union
521 {
522 uint32_t u32;
523 int32_t i32;
524 } v;
525 value_t vt;
527 v.u32 = (((uint32_t) values[0]) << 16)
528 | ((uint32_t) values[1]);
529 DEBUG ("Modbus plugin: mb_read_data: "
530 "Returned int32 value is %"PRIi32, v.i32);
532 CAST_TO_VALUE_T (ds, vt, v.i32);
533 mb_submit (host, slave, data, vt);
534 }
535 else if (data->register_type == REG_TYPE_INT16)
536 {
537 union
538 {
539 uint16_t u16;
540 int16_t i16;
541 } v;
542 value_t vt;
544 v.u16 = values[0];
546 DEBUG ("Modbus plugin: mb_read_data: "
547 "Returned int16 value is %"PRIi16, v.i16);
549 CAST_TO_VALUE_T (ds, vt, v.i16);
550 mb_submit (host, slave, data, vt);
551 }
552 else if (data->register_type == REG_TYPE_UINT32)
553 {
554 uint32_t v32;
555 value_t vt;
557 v32 = (((uint32_t) values[0]) << 16)
558 | ((uint32_t) values[1]);
559 DEBUG ("Modbus plugin: mb_read_data: "
560 "Returned uint32 value is %"PRIu32, v32);
562 CAST_TO_VALUE_T (ds, vt, v32);
563 mb_submit (host, slave, data, vt);
564 }
565 else /* if (data->register_type == REG_TYPE_UINT16) */
566 {
567 value_t vt;
569 DEBUG ("Modbus plugin: mb_read_data: "
570 "Returned uint16 value is %"PRIu16, values[0]);
572 CAST_TO_VALUE_T (ds, vt, values[0]);
573 mb_submit (host, slave, data, vt);
574 }
576 return (0);
577 } /* }}} int mb_read_data */
579 static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
580 {
581 mb_data_t *data;
582 int success;
583 int status;
585 if ((host == NULL) || (slave == NULL))
586 return (EINVAL);
588 success = 0;
589 for (data = slave->collect; data != NULL; data = data->next)
590 {
591 status = mb_read_data (host, slave, data);
592 if (status == 0)
593 success++;
594 }
596 if (success == 0)
597 return (-1);
598 else
599 return (0);
600 } /* }}} int mb_read_slave */
602 static int mb_read (user_data_t *user_data) /* {{{ */
603 {
604 mb_host_t *host;
605 size_t i;
606 int success;
607 int status;
609 if ((user_data == NULL) || (user_data->data == NULL))
610 return (EINVAL);
612 host = user_data->data;
614 success = 0;
615 for (i = 0; i < host->slaves_num; i++)
616 {
617 status = mb_read_slave (host, host->slaves + i);
618 if (status == 0)
619 success++;
620 }
622 if (success == 0)
623 return (-1);
624 else
625 return (0);
626 } /* }}} int mb_read */
628 /* Free functions */
630 static void data_free_one (mb_data_t *data) /* {{{ */
631 {
632 if (data == NULL)
633 return;
635 sfree (data->name);
636 sfree (data);
637 } /* }}} void data_free_one */
639 static void data_free_all (mb_data_t *data) /* {{{ */
640 {
641 mb_data_t *next;
643 if (data == NULL)
644 return;
646 next = data->next;
647 data_free_one (data);
649 data_free_all (next);
650 } /* }}} void data_free_all */
652 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
653 {
654 size_t i;
656 if (slaves == NULL)
657 return;
659 for (i = 0; i < slaves_num; i++)
660 data_free_all (slaves[i].collect);
661 sfree (slaves);
662 } /* }}} void slaves_free_all */
664 static void host_free (void *void_host) /* {{{ */
665 {
666 mb_host_t *host = void_host;
668 if (host == NULL)
669 return;
671 slaves_free_all (host->slaves, host->slaves_num);
672 sfree (host);
673 } /* }}} void host_free */
675 /* Config functions */
677 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
678 {
679 mb_data_t data;
680 int status;
681 int i;
683 memset (&data, 0, sizeof (data));
684 data.name = NULL;
685 data.register_type = REG_TYPE_UINT16;
686 data.next = NULL;
688 status = cf_util_get_string (ci, &data.name);
689 if (status != 0)
690 return (status);
692 for (i = 0; i < ci->children_num; i++)
693 {
694 oconfig_item_t *child = ci->children + i;
695 status = 0;
697 if (strcasecmp ("Type", child->key) == 0)
698 status = cf_util_get_string_buffer (child,
699 data.type, sizeof (data.type));
700 else if (strcasecmp ("Instance", child->key) == 0)
701 status = cf_util_get_string_buffer (child,
702 data.instance, sizeof (data.instance));
703 else if (strcasecmp ("RegisterBase", child->key) == 0)
704 status = cf_util_get_int (child, &data.register_base);
705 else if (strcasecmp ("RegisterType", child->key) == 0)
706 {
707 char tmp[16];
708 status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
709 if (status != 0)
710 /* do nothing */;
711 else if (strcasecmp ("Int16", tmp) == 0)
712 data.register_type = REG_TYPE_INT16;
713 else if (strcasecmp ("Int32", tmp) == 0)
714 data.register_type = REG_TYPE_INT32;
715 else if (strcasecmp ("Uint16", tmp) == 0)
716 data.register_type = REG_TYPE_UINT16;
717 else if (strcasecmp ("Uint32", tmp) == 0)
718 data.register_type = REG_TYPE_UINT32;
719 else if (strcasecmp ("Float", tmp) == 0)
720 data.register_type = REG_TYPE_FLOAT;
721 else
722 {
723 ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
724 status = -1;
725 }
726 }
727 else if (strcasecmp ("ModbusRegisterType", child->key) == 0)
728 {
729 #if LEGACY_LIBMODBUS
730 ERROR("Modbus plugin: ModbusRegisterType parameter can not be used "
731 "with your libmodbus version");
732 #else
733 char tmp[16];
734 status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
735 if (status != 0)
736 /* do nothing */;
737 else if (strcasecmp ("holding", tmp) == 0)
738 data.modbus_register_type = MREG_HOLDING;
739 else if (strcasecmp ("input", tmp) == 0)
740 data.modbus_register_type = MREG_INPUT;
741 else
742 {
743 ERROR ("Modbus plugin: The modbus_register_type \"%s\" is unknown.",
744 tmp);
745 status = -1;
746 }
747 #endif
748 }
749 else
750 {
751 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
752 status = -1;
753 }
755 if (status != 0)
756 break;
757 } /* for (i = 0; i < ci->children_num; i++) */
759 assert (data.name != NULL);
760 if (data.type[0] == 0)
761 {
762 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
763 data.name);
764 status = -1;
765 }
767 if (status == 0)
768 data_copy (&data_definitions, &data);
770 sfree (data.name);
772 return (status);
773 } /* }}} int mb_config_add_data */
775 static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
776 const char *address)
777 {
778 struct addrinfo *ai_list;
779 struct addrinfo *ai_ptr;
780 struct addrinfo ai_hints;
781 int status;
783 if ((host == NULL) || (address == NULL))
784 return (EINVAL);
786 memset (&ai_hints, 0, sizeof (ai_hints));
787 #if AI_ADDRCONFIG
788 ai_hints.ai_flags |= AI_ADDRCONFIG;
789 #endif
790 /* XXX: libmodbus can only handle IPv4 addresses. */
791 ai_hints.ai_family = AF_INET;
792 ai_hints.ai_addr = NULL;
793 ai_hints.ai_canonname = NULL;
794 ai_hints.ai_next = NULL;
796 ai_list = NULL;
797 status = getaddrinfo (address, /* service = */ NULL,
798 &ai_hints, &ai_list);
799 if (status != 0)
800 {
801 char errbuf[1024];
802 ERROR ("Modbus plugin: getaddrinfo failed: %s",
803 (status == EAI_SYSTEM)
804 ? sstrerror (errno, errbuf, sizeof (errbuf))
805 : gai_strerror (status));
806 return (status);
807 }
809 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
810 {
811 status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
812 host->node, sizeof (host->node),
813 /* service = */ NULL, /* length = */ 0,
814 /* flags = */ NI_NUMERICHOST);
815 if (status == 0)
816 break;
817 } /* for (ai_ptr) */
819 freeaddrinfo (ai_list);
821 if (status != 0)
822 ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
823 else /* if (status == 0) */
824 {
825 DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
826 address, host->node);
827 }
829 return (status);
830 } /* }}} int mb_config_set_host_address */
832 static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
833 {
834 mb_slave_t *slave;
835 int status;
836 int i;
838 if ((host == NULL) || (ci == NULL))
839 return (EINVAL);
841 slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
842 if (slave == NULL)
843 return (ENOMEM);
844 host->slaves = slave;
845 slave = host->slaves + host->slaves_num;
846 memset (slave, 0, sizeof (*slave));
847 slave->collect = NULL;
849 status = cf_util_get_int (ci, &slave->id);
850 if (status != 0)
851 return (status);
853 for (i = 0; i < ci->children_num; i++)
854 {
855 oconfig_item_t *child = ci->children + i;
856 status = 0;
858 if (strcasecmp ("Instance", child->key) == 0)
859 status = cf_util_get_string_buffer (child,
860 slave->instance, sizeof (slave->instance));
861 else if (strcasecmp ("Collect", child->key) == 0)
862 {
863 char buffer[1024];
864 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
865 if (status == 0)
866 data_copy_by_name (&slave->collect, data_definitions, buffer);
867 status = 0; /* continue after failure. */
868 }
869 else
870 {
871 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
872 status = -1;
873 }
875 if (status != 0)
876 break;
877 }
879 if ((status == 0) && (slave->collect == NULL))
880 status = EINVAL;
882 if (slave->id < 0)
883 status = EINVAL;
885 if (status == 0)
886 host->slaves_num++;
887 else /* if (status != 0) */
888 data_free_all (slave->collect);
890 return (status);
891 } /* }}} int mb_config_add_slave */
893 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
894 {
895 mb_host_t *host;
896 int status;
897 int i;
899 host = malloc (sizeof (*host));
900 if (host == NULL)
901 return (ENOMEM);
902 memset (host, 0, sizeof (*host));
903 host->slaves = NULL;
905 status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
906 if (status != 0)
907 return (status);
908 if (host->host[0] == 0)
909 return (EINVAL);
911 for (i = 0; i < ci->children_num; i++)
912 {
913 oconfig_item_t *child = ci->children + i;
914 status = 0;
916 if (strcasecmp ("Address", child->key) == 0)
917 {
918 char buffer[NI_MAXHOST];
919 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
920 if (status == 0)
921 status = mb_config_set_host_address (host, buffer);
922 }
923 else if (strcasecmp ("Port", child->key) == 0)
924 {
925 host->port = cf_util_get_port_number (child);
926 if (host->port <= 0)
927 status = -1;
928 }
929 else if (strcasecmp ("Interval", child->key) == 0)
930 status = cf_util_get_cdtime (child, &host->interval);
931 else if (strcasecmp ("Slave", child->key) == 0)
932 /* Don't set status: Gracefully continue if a slave fails. */
933 mb_config_add_slave (host, child);
934 else
935 {
936 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
937 status = -1;
938 }
940 if (status != 0)
941 break;
942 } /* for (i = 0; i < ci->children_num; i++) */
944 assert (host->host[0] != 0);
945 if (host->host[0] == 0)
946 {
947 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
948 host->host);
949 status = -1;
950 }
952 if (status == 0)
953 {
954 user_data_t ud;
955 char name[1024];
956 struct timespec interval = { 0, 0 };
958 ud.data = host;
959 ud.free_func = host_free;
961 ssnprintf (name, sizeof (name), "modbus-%s", host->host);
963 CDTIME_T_TO_TIMESPEC (host->interval, &interval);
965 plugin_register_complex_read (/* group = */ NULL, name,
966 /* callback = */ mb_read,
967 /* interval = */ (host->interval > 0) ? &interval : NULL,
968 &ud);
969 }
970 else
971 {
972 host_free (host);
973 }
975 return (status);
976 } /* }}} int mb_config_add_host */
978 static int mb_config (oconfig_item_t *ci) /* {{{ */
979 {
980 int i;
982 if (ci == NULL)
983 return (EINVAL);
985 for (i = 0; i < ci->children_num; i++)
986 {
987 oconfig_item_t *child = ci->children + i;
989 if (strcasecmp ("Data", child->key) == 0)
990 mb_config_add_data (child);
991 else if (strcasecmp ("Host", child->key) == 0)
992 mb_config_add_host (child);
993 else
994 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
995 }
997 return (0);
998 } /* }}} int mb_config */
1000 /* ========= */
1002 static int mb_shutdown (void) /* {{{ */
1003 {
1004 data_free_all (data_definitions);
1005 data_definitions = NULL;
1007 return (0);
1008 } /* }}} int mb_shutdown */
1010 void module_register (void)
1011 {
1012 plugin_register_complex_config ("modbus", mb_config);
1013 plugin_register_shutdown ("modbus", mb_shutdown);
1014 } /* void module_register */
1016 /* vim: set sw=2 sts=2 et fdm=marker : */