b65371f9fbee746caf89c6f669557f816a800396
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 * Function 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_function_e /* {{{ */
80 {
81 FCN_HOLDING,
82 FCN_INPUT
83 }; /* }}} */
84 typedef enum mb_register_type_e mb_register_type_t;
85 typedef enum mb_function_e mb_function_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_function_t function;
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->function == FCN_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_registers (%s/%s) failed. status = %i, values_num = %i "
491 "Giving up.", host->host, host->node, status, values_num);
492 #if LEGACY_LIBMODBUS
493 modbus_close (&host->connection);
494 #else
495 modbus_close (host->connection);
496 modbus_free (host->connection);
497 #endif
498 host->connection = NULL;
499 return (-1);
500 }
502 DEBUG ("Modbus plugin: mb_read_data: Success! "
503 "modbus_read_registers returned with status %i.", status);
505 if (data->register_type == REG_TYPE_FLOAT)
506 {
507 float float_value;
508 value_t vt;
510 float_value = mb_register_to_float (values[0], values[1]);
511 DEBUG ("Modbus plugin: mb_read_data: "
512 "Returned float value is %g", (double) float_value);
514 CAST_TO_VALUE_T (ds, vt, float_value);
515 mb_submit (host, slave, data, vt);
516 }
517 else if (data->register_type == REG_TYPE_INT32)
518 {
519 union
520 {
521 uint32_t u32;
522 int32_t i32;
523 } v;
524 value_t vt;
526 v.u32 = (((uint32_t) values[0]) << 16)
527 | ((uint32_t) values[1]);
528 DEBUG ("Modbus plugin: mb_read_data: "
529 "Returned int32 value is %"PRIi32, v.i32);
531 CAST_TO_VALUE_T (ds, vt, v.i32);
532 mb_submit (host, slave, data, vt);
533 }
534 else if (data->register_type == REG_TYPE_INT16)
535 {
536 union
537 {
538 uint16_t u16;
539 int16_t i16;
540 } v;
541 value_t vt;
543 v.u16 = values[0];
545 DEBUG ("Modbus plugin: mb_read_data: "
546 "Returned int16 value is %"PRIi16, v.i16);
548 CAST_TO_VALUE_T (ds, vt, v.i16);
549 mb_submit (host, slave, data, vt);
550 }
551 else if (data->register_type == REG_TYPE_UINT32)
552 {
553 uint32_t v32;
554 value_t vt;
556 v32 = (((uint32_t) values[0]) << 16)
557 | ((uint32_t) values[1]);
558 DEBUG ("Modbus plugin: mb_read_data: "
559 "Returned uint32 value is %"PRIu32, v32);
561 CAST_TO_VALUE_T (ds, vt, v32);
562 mb_submit (host, slave, data, vt);
563 }
564 else /* if (data->register_type == REG_TYPE_UINT16) */
565 {
566 value_t vt;
568 DEBUG ("Modbus plugin: mb_read_data: "
569 "Returned uint16 value is %"PRIu16, values[0]);
571 CAST_TO_VALUE_T (ds, vt, values[0]);
572 mb_submit (host, slave, data, vt);
573 }
575 return (0);
576 } /* }}} int mb_read_data */
578 static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
579 {
580 mb_data_t *data;
581 int success;
582 int status;
584 if ((host == NULL) || (slave == NULL))
585 return (EINVAL);
587 success = 0;
588 for (data = slave->collect; data != NULL; data = data->next)
589 {
590 status = mb_read_data (host, slave, data);
591 if (status == 0)
592 success++;
593 }
595 if (success == 0)
596 return (-1);
597 else
598 return (0);
599 } /* }}} int mb_read_slave */
601 static int mb_read (user_data_t *user_data) /* {{{ */
602 {
603 mb_host_t *host;
604 size_t i;
605 int success;
606 int status;
608 if ((user_data == NULL) || (user_data->data == NULL))
609 return (EINVAL);
611 host = user_data->data;
613 success = 0;
614 for (i = 0; i < host->slaves_num; i++)
615 {
616 status = mb_read_slave (host, host->slaves + i);
617 if (status == 0)
618 success++;
619 }
621 if (success == 0)
622 return (-1);
623 else
624 return (0);
625 } /* }}} int mb_read */
627 /* Free functions */
629 static void data_free_one (mb_data_t *data) /* {{{ */
630 {
631 if (data == NULL)
632 return;
634 sfree (data->name);
635 sfree (data);
636 } /* }}} void data_free_one */
638 static void data_free_all (mb_data_t *data) /* {{{ */
639 {
640 mb_data_t *next;
642 if (data == NULL)
643 return;
645 next = data->next;
646 data_free_one (data);
648 data_free_all (next);
649 } /* }}} void data_free_all */
651 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
652 {
653 size_t i;
655 if (slaves == NULL)
656 return;
658 for (i = 0; i < slaves_num; i++)
659 data_free_all (slaves[i].collect);
660 sfree (slaves);
661 } /* }}} void slaves_free_all */
663 static void host_free (void *void_host) /* {{{ */
664 {
665 mb_host_t *host = void_host;
667 if (host == NULL)
668 return;
670 slaves_free_all (host->slaves, host->slaves_num);
671 sfree (host);
672 } /* }}} void host_free */
674 /* Config functions */
676 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
677 {
678 mb_data_t data;
679 int status;
680 int i;
682 memset (&data, 0, sizeof (data));
683 data.name = NULL;
684 data.register_type = REG_TYPE_UINT16;
685 data.next = NULL;
687 status = cf_util_get_string (ci, &data.name);
688 if (status != 0)
689 return (status);
691 for (i = 0; i < ci->children_num; i++)
692 {
693 oconfig_item_t *child = ci->children + i;
694 status = 0;
696 if (strcasecmp ("Type", child->key) == 0)
697 status = cf_util_get_string_buffer (child,
698 data.type, sizeof (data.type));
699 else if (strcasecmp ("Instance", child->key) == 0)
700 status = cf_util_get_string_buffer (child,
701 data.instance, sizeof (data.instance));
702 else if (strcasecmp ("RegisterBase", child->key) == 0)
703 status = cf_util_get_int (child, &data.register_base);
704 else if (strcasecmp ("RegisterType", child->key) == 0)
705 {
706 char tmp[16];
707 status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
708 if (status != 0)
709 /* do nothing */;
710 else if (strcasecmp ("Int16", tmp) == 0)
711 data.register_type = REG_TYPE_INT16;
712 else if (strcasecmp ("Int32", tmp) == 0)
713 data.register_type = REG_TYPE_INT32;
714 else if (strcasecmp ("Uint16", tmp) == 0)
715 data.register_type = REG_TYPE_UINT16;
716 else if (strcasecmp ("Uint32", tmp) == 0)
717 data.register_type = REG_TYPE_UINT32;
718 else if (strcasecmp ("Float", tmp) == 0)
719 data.register_type = REG_TYPE_FLOAT;
720 else
721 {
722 ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
723 status = -1;
724 }
725 }
726 else if (strcasecmp ("Function", child->key) == 0)
727 {
728 #if LEGACY_LIBMODBUS
729 ERROR("Modbus plugin: Function parameter can not be used with your libmodbus version");
730 #else
731 char tmp[16];
732 status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
733 if (status != 0)
734 /* do nothing */;
735 else if (strcasecmp ("holding", tmp) == 0)
736 data.function = FCN_HOLDING;
737 else if (strcasecmp ("input", tmp) == 0)
738 data.function = FCN_INPUT;
739 else
740 {
741 ERROR ("Modbus plugin: The function type \"%s\" is unknown.", tmp);
742 status = -1;
743 }
744 #endif
745 }
746 else
747 {
748 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
749 status = -1;
750 }
752 if (status != 0)
753 break;
754 } /* for (i = 0; i < ci->children_num; i++) */
756 assert (data.name != NULL);
757 if (data.type[0] == 0)
758 {
759 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
760 data.name);
761 status = -1;
762 }
764 if (status == 0)
765 data_copy (&data_definitions, &data);
767 sfree (data.name);
769 return (status);
770 } /* }}} int mb_config_add_data */
772 static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
773 const char *address)
774 {
775 struct addrinfo *ai_list;
776 struct addrinfo *ai_ptr;
777 struct addrinfo ai_hints;
778 int status;
780 if ((host == NULL) || (address == NULL))
781 return (EINVAL);
783 memset (&ai_hints, 0, sizeof (ai_hints));
784 #if AI_ADDRCONFIG
785 ai_hints.ai_flags |= AI_ADDRCONFIG;
786 #endif
787 /* XXX: libmodbus can only handle IPv4 addresses. */
788 ai_hints.ai_family = AF_INET;
789 ai_hints.ai_addr = NULL;
790 ai_hints.ai_canonname = NULL;
791 ai_hints.ai_next = NULL;
793 ai_list = NULL;
794 status = getaddrinfo (address, /* service = */ NULL,
795 &ai_hints, &ai_list);
796 if (status != 0)
797 {
798 char errbuf[1024];
799 ERROR ("Modbus plugin: getaddrinfo failed: %s",
800 (status == EAI_SYSTEM)
801 ? sstrerror (errno, errbuf, sizeof (errbuf))
802 : gai_strerror (status));
803 return (status);
804 }
806 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
807 {
808 status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
809 host->node, sizeof (host->node),
810 /* service = */ NULL, /* length = */ 0,
811 /* flags = */ NI_NUMERICHOST);
812 if (status == 0)
813 break;
814 } /* for (ai_ptr) */
816 freeaddrinfo (ai_list);
818 if (status != 0)
819 ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
820 else /* if (status == 0) */
821 {
822 DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
823 address, host->node);
824 }
826 return (status);
827 } /* }}} int mb_config_set_host_address */
829 static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
830 {
831 mb_slave_t *slave;
832 int status;
833 int i;
835 if ((host == NULL) || (ci == NULL))
836 return (EINVAL);
838 slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
839 if (slave == NULL)
840 return (ENOMEM);
841 host->slaves = slave;
842 slave = host->slaves + host->slaves_num;
843 memset (slave, 0, sizeof (*slave));
844 slave->collect = NULL;
846 status = cf_util_get_int (ci, &slave->id);
847 if (status != 0)
848 return (status);
850 for (i = 0; i < ci->children_num; i++)
851 {
852 oconfig_item_t *child = ci->children + i;
853 status = 0;
855 if (strcasecmp ("Instance", child->key) == 0)
856 status = cf_util_get_string_buffer (child,
857 slave->instance, sizeof (slave->instance));
858 else if (strcasecmp ("Collect", child->key) == 0)
859 {
860 char buffer[1024];
861 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
862 if (status == 0)
863 data_copy_by_name (&slave->collect, data_definitions, buffer);
864 status = 0; /* continue after failure. */
865 }
866 else
867 {
868 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
869 status = -1;
870 }
872 if (status != 0)
873 break;
874 }
876 if ((status == 0) && (slave->collect == NULL))
877 status = EINVAL;
879 if (slave->id < 0)
880 status = EINVAL;
882 if (status == 0)
883 host->slaves_num++;
884 else /* if (status != 0) */
885 data_free_all (slave->collect);
887 return (status);
888 } /* }}} int mb_config_add_slave */
890 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
891 {
892 mb_host_t *host;
893 int status;
894 int i;
896 host = malloc (sizeof (*host));
897 if (host == NULL)
898 return (ENOMEM);
899 memset (host, 0, sizeof (*host));
900 host->slaves = NULL;
902 status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
903 if (status != 0)
904 return (status);
905 if (host->host[0] == 0)
906 return (EINVAL);
908 for (i = 0; i < ci->children_num; i++)
909 {
910 oconfig_item_t *child = ci->children + i;
911 status = 0;
913 if (strcasecmp ("Address", child->key) == 0)
914 {
915 char buffer[NI_MAXHOST];
916 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
917 if (status == 0)
918 status = mb_config_set_host_address (host, buffer);
919 }
920 else if (strcasecmp ("Port", child->key) == 0)
921 {
922 host->port = cf_util_get_port_number (child);
923 if (host->port <= 0)
924 status = -1;
925 }
926 else if (strcasecmp ("Interval", child->key) == 0)
927 status = cf_util_get_cdtime (child, &host->interval);
928 else if (strcasecmp ("Slave", child->key) == 0)
929 /* Don't set status: Gracefully continue if a slave fails. */
930 mb_config_add_slave (host, child);
931 else
932 {
933 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
934 status = -1;
935 }
937 if (status != 0)
938 break;
939 } /* for (i = 0; i < ci->children_num; i++) */
941 assert (host->host[0] != 0);
942 if (host->host[0] == 0)
943 {
944 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
945 host->host);
946 status = -1;
947 }
949 if (status == 0)
950 {
951 user_data_t ud;
952 char name[1024];
953 struct timespec interval = { 0, 0 };
955 ud.data = host;
956 ud.free_func = host_free;
958 ssnprintf (name, sizeof (name), "modbus-%s", host->host);
960 CDTIME_T_TO_TIMESPEC (host->interval, &interval);
962 plugin_register_complex_read (/* group = */ NULL, name,
963 /* callback = */ mb_read,
964 /* interval = */ (host->interval > 0) ? &interval : NULL,
965 &ud);
966 }
967 else
968 {
969 host_free (host);
970 }
972 return (status);
973 } /* }}} int mb_config_add_host */
975 static int mb_config (oconfig_item_t *ci) /* {{{ */
976 {
977 int i;
979 if (ci == NULL)
980 return (EINVAL);
982 for (i = 0; i < ci->children_num; i++)
983 {
984 oconfig_item_t *child = ci->children + i;
986 if (strcasecmp ("Data", child->key) == 0)
987 mb_config_add_data (child);
988 else if (strcasecmp ("Host", child->key) == 0)
989 mb_config_add_host (child);
990 else
991 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
992 }
994 return (0);
995 } /* }}} int mb_config */
997 /* ========= */
999 static int mb_shutdown (void) /* {{{ */
1000 {
1001 data_free_all (data_definitions);
1002 data_definitions = NULL;
1004 return (0);
1005 } /* }}} int mb_shutdown */
1007 void module_register (void)
1008 {
1009 plugin_register_complex_config ("modbus", mb_config);
1010 plugin_register_shutdown ("modbus", mb_shutdown);
1011 } /* void module_register */
1013 /* vim: set sw=2 sts=2 et fdm=marker : */