2 * collectd - src/modbus.c
3 * Copyright (C) 2010 noris network AG
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.
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.
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
19 * Florian Forster <octo at noris.net>
25 #include "configfile.h"
29 #include <modbus/modbus.h>
31 #ifndef MODBUS_TCP_DEFAULT_PORT
32 # ifdef MODBUS_TCP_PORT
33 # define MODBUS_TCP_DEFAULT_PORT MODBUS_TCP_PORT
35 # define MODBUS_TCP_DEFAULT_PORT 502
53 * Instance "foobar" # optional
62 enum mb_register_type_e /* {{{ */
68 typedef enum mb_register_type_e mb_register_type_t;
71 typedef struct mb_data_s mb_data_t;
72 struct mb_data_s /* {{{ */
76 mb_register_type_t register_type;
77 char type[DATA_MAX_NAME_LEN];
78 char instance[DATA_MAX_NAME_LEN];
83 struct mb_slave_s /* {{{ */
86 char instance[DATA_MAX_NAME_LEN];
89 typedef struct mb_slave_s mb_slave_t;
91 struct mb_host_s /* {{{ */
93 char host[DATA_MAX_NAME_LEN];
94 char node[NI_MAXHOST];
95 /* char service[NI_MAXSERV]; */
102 modbus_param_t connection;
104 _Bool have_reconnected;
106 typedef struct mb_host_s mb_host_t;
108 struct mb_data_group_s;
109 typedef struct mb_data_group_s mb_data_group_t;
110 struct mb_data_group_s /* {{{ */
112 mb_data_t *registers;
113 size_t registers_num;
115 mb_data_group_t *next;
121 static mb_data_t *data_definitions = NULL;
126 static mb_data_t *data_get_by_name (mb_data_t *src, /* {{{ */
134 for (ptr = src; ptr != NULL; ptr = ptr->next)
135 if (strcasecmp (ptr->name, name) == 0)
139 } /* }}} mb_data_t *data_get_by_name */
141 static int data_append (mb_data_t **dst, mb_data_t *src) /* {{{ */
145 if ((dst == NULL) || (src == NULL))
156 while (ptr->next != NULL)
162 } /* }}} int data_append */
164 /* Copy a single mb_data_t and append it to another list. */
165 static int data_copy (mb_data_t **dst, const mb_data_t *src) /* {{{ */
170 if ((dst == NULL) || (src == NULL))
173 tmp = malloc (sizeof (*tmp));
176 memcpy (tmp, src, sizeof (*tmp));
180 tmp->name = strdup (src->name);
181 if (tmp->name == NULL)
187 status = data_append (dst, tmp);
196 } /* }}} int data_copy */
198 /* Lookup a single mb_data_t instance, copy it and append the copy to another
200 static int data_copy_by_name (mb_data_t **dst, mb_data_t *src, /* {{{ */
205 if ((dst == NULL) || (src == NULL) || (name == NULL))
208 ptr = data_get_by_name (src, name);
212 return (data_copy (dst, ptr));
213 } /* }}} int data_copy_by_name */
217 static int mb_submit (mb_host_t *host, mb_slave_t *slave, /* {{{ */
218 mb_data_t *data, value_t value)
220 value_list_t vl = VALUE_LIST_INIT;
222 if ((host == NULL) || (slave == NULL) || (data == NULL))
225 if (host->interval <= 0)
226 host->interval = interval_g;
228 if (slave->instance[0] == 0)
229 ssnprintf (slave->instance, sizeof (slave->instance), "slave_%i",
234 vl.interval = host->interval;
235 sstrncpy (vl.host, host->host, sizeof (vl.host));
236 sstrncpy (vl.plugin, "modbus", sizeof (vl.plugin));
237 sstrncpy (vl.plugin_instance, slave->instance, sizeof (vl.plugin_instance));
238 sstrncpy (vl.type, data->type, sizeof (vl.type));
239 sstrncpy (vl.type_instance, data->instance, sizeof (vl.type_instance));
241 return (plugin_dispatch_values (&vl));
242 } /* }}} int mb_submit */
244 static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */
252 #if BYTE_ORDER == LITTLE_ENDIAN
254 conv.b[0] = lo & 0x00ff;
255 conv.b[1] = (lo >> 8) & 0x00ff;
256 conv.b[2] = hi & 0x00ff;
257 conv.b[3] = (hi >> 8) & 0x00ff;
259 conv.b[3] = lo & 0x00ff;
260 conv.b[2] = (lo >> 8) & 0x00ff;
261 conv.b[1] = hi & 0x00ff;
262 conv.b[0] = (hi >> 8) & 0x00ff;
266 } /* }}} float mb_register_to_float */
268 static int mb_init_connection (mb_host_t *host) /* {{{ */
275 if (host->is_connected)
278 /* Only reconnect once per interval. */
279 if (host->have_reconnected)
282 modbus_set_debug (&host->connection, 1);
285 /* We'll do the error handling ourselves. */
286 modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
289 if ((host->port < 1) || (host->port > 65535))
290 host->port = MODBUS_TCP_DEFAULT_PORT;
292 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
293 host->node, host->port);
295 modbus_init_tcp (&host->connection,
296 /* host = */ host->node);
298 /* port = */ host->port);
301 status = modbus_connect (&host->connection);
304 ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
305 host->node, host->port, status);
309 host->is_connected = 1;
310 host->have_reconnected = 1;
312 } /* }}} int mb_init_connection */
314 #define CAST_TO_VALUE_T(ds,vt,raw) do { \
315 if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
316 (vt).counter = (counter_t) (raw); \
317 else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
318 (vt).gauge = (gauge_t) (raw); \
319 else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
320 (vt).derive = (derive_t) (raw); \
321 else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
322 (vt).absolute = (absolute_t) (raw); \
325 static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
330 const data_set_t *ds;
334 if ((host == NULL) || (slave == NULL) || (data == NULL))
337 ds = plugin_get_ds (data->type);
340 ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
346 ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
347 "I can only handle data sets with only one data source.",
348 data->type, ds->ds_num);
352 if ((ds->ds[0].type != DS_TYPE_GAUGE)
353 && (data->register_type != REG_TYPE_UINT32))
355 NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
356 "This will most likely result in problems, because the register type "
357 "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
360 memset (values, 0, sizeof (values));
361 if ((data->register_type == REG_TYPE_UINT32)
362 || (data->register_type == REG_TYPE_FLOAT))
367 for (i = 0; i < 2; i++)
369 status = read_holding_registers (&host->connection,
370 /* slave = */ slave->id, /* start_addr = */ data->register_base,
371 /* num_registers = */ values_num, /* buffer = */ values);
375 if (host->is_connected)
376 modbus_close (&host->connection);
377 host->is_connected = 0;
379 /* If we already tried reconnecting this round, give up. */
380 if (host->have_reconnected)
382 ERROR ("Modbus plugin: read_holding_registers (%s) failed. "
383 "Reconnecting has already been tried. Giving up.", host->host);
387 /* Maybe the device closed the connection during the waiting interval.
388 * Try re-establishing the connection. */
389 status = mb_init_connection (host);
392 ERROR ("Modbus plugin: read_holding_registers (%s) failed. "
393 "While trying to reconnect, connecting to \"%s\" failed. "
395 host->host, host->node);
399 DEBUG ("Modbus plugin: Re-established connection to %s", host->host);
403 } /* for (i = 0, 1) */
405 DEBUG ("Modbus plugin: mb_read_data: Success! "
406 "read_holding_registers returned with status %i.", status);
408 if (data->register_type == REG_TYPE_FLOAT)
413 float_value = mb_register_to_float (values[0], values[1]);
414 DEBUG ("Modbus plugin: mb_read_data: "
415 "Returned float value is %g", (double) float_value);
417 CAST_TO_VALUE_T (ds, vt, float_value);
418 mb_submit (host, slave, data, vt);
420 else if (data->register_type == REG_TYPE_UINT32)
425 v32 = (values[0] << 16) | values[1];
426 DEBUG ("Modbus plugin: mb_read_data: "
427 "Returned uint32 value is %"PRIu32, v32);
429 CAST_TO_VALUE_T (ds, vt, v32);
430 mb_submit (host, slave, data, vt);
432 else /* if (data->register_type == REG_TYPE_UINT16) */
436 DEBUG ("Modbus plugin: mb_read_data: "
437 "Returned uint16 value is %"PRIu16, values[0]);
439 CAST_TO_VALUE_T (ds, vt, values[0]);
440 mb_submit (host, slave, data, vt);
444 } /* }}} int mb_read_data */
446 static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
452 if ((host == NULL) || (slave == NULL))
456 for (data = slave->collect; data != NULL; data = data->next)
458 status = mb_read_data (host, slave, data);
467 } /* }}} int mb_read_slave */
469 static int mb_read (user_data_t *user_data) /* {{{ */
476 if ((user_data == NULL) || (user_data->data == NULL))
479 host = user_data->data;
481 /* Clear the reconnect flag. */
482 host->have_reconnected = 0;
485 for (i = 0; i < host->slaves_num; i++)
487 status = mb_read_slave (host, host->slaves + i);
496 } /* }}} int mb_read */
500 static void data_free_one (mb_data_t *data) /* {{{ */
507 } /* }}} void data_free_one */
509 static void data_free_all (mb_data_t *data) /* {{{ */
517 data_free_one (data);
519 data_free_all (next);
520 } /* }}} void data_free_all */
522 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
529 for (i = 0; i < slaves_num; i++)
530 data_free_all (slaves[i].collect);
532 } /* }}} void slaves_free_all */
534 static void host_free (void *void_host) /* {{{ */
536 mb_host_t *host = void_host;
541 slaves_free_all (host->slaves, host->slaves_num);
543 } /* }}} void host_free */
545 /* Config functions */
547 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
553 memset (&data, 0, sizeof (data));
555 data.register_type = REG_TYPE_UINT16;
558 status = cf_util_get_string (ci, &data.name);
562 for (i = 0; i < ci->children_num; i++)
564 oconfig_item_t *child = ci->children + i;
567 if (strcasecmp ("Type", child->key) == 0)
568 status = cf_util_get_string_buffer (child,
569 data.type, sizeof (data.type));
570 else if (strcasecmp ("Instance", child->key) == 0)
571 status = cf_util_get_string_buffer (child,
572 data.instance, sizeof (data.instance));
573 else if (strcasecmp ("RegisterBase", child->key) == 0)
574 status = cf_util_get_int (child, &data.register_base);
575 else if (strcasecmp ("RegisterType", child->key) == 0)
578 status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
581 else if (strcasecmp ("Uint16", tmp) == 0)
582 data.register_type = REG_TYPE_UINT16;
583 else if (strcasecmp ("Uint32", tmp) == 0)
584 data.register_type = REG_TYPE_UINT32;
585 else if (strcasecmp ("Float", tmp) == 0)
586 data.register_type = REG_TYPE_FLOAT;
589 ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
595 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
601 } /* for (i = 0; i < ci->children_num; i++) */
603 assert (data.name != NULL);
604 if (data.type[0] == 0)
606 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
612 data_copy (&data_definitions, &data);
617 } /* }}} int mb_config_add_data */
619 static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
622 struct addrinfo *ai_list;
623 struct addrinfo *ai_ptr;
624 struct addrinfo ai_hints;
627 if ((host == NULL) || (address == NULL))
630 memset (&ai_hints, 0, sizeof (ai_hints));
632 ai_hints.ai_flags |= AI_ADDRCONFIG;
634 /* XXX: libmodbus can only handle IPv4 addresses. */
635 ai_hints.ai_family = AF_INET;
636 ai_hints.ai_addr = NULL;
637 ai_hints.ai_canonname = NULL;
638 ai_hints.ai_next = NULL;
641 status = getaddrinfo (address, /* service = */ NULL,
642 &ai_hints, &ai_list);
646 ERROR ("Modbus plugin: getaddrinfo failed: %s",
647 (status == EAI_SYSTEM)
648 ? sstrerror (errno, errbuf, sizeof (errbuf))
649 : gai_strerror (status));
653 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
655 status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
656 host->node, sizeof (host->node),
657 /* service = */ NULL, /* length = */ 0,
658 /* flags = */ NI_NUMERICHOST);
663 freeaddrinfo (ai_list);
666 ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
667 else /* if (status == 0) */
669 DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
670 address, host->node);
674 } /* }}} int mb_config_set_host_address */
676 static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
682 if ((host == NULL) || (ci == NULL))
685 slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
688 host->slaves = slave;
689 slave = host->slaves + host->slaves_num;
690 memset (slave, 0, sizeof (*slave));
691 slave->collect = NULL;
693 status = cf_util_get_int (ci, &slave->id);
697 for (i = 0; i < ci->children_num; i++)
699 oconfig_item_t *child = ci->children + i;
702 if (strcasecmp ("Instance", child->key) == 0)
703 status = cf_util_get_string_buffer (child,
704 slave->instance, sizeof (slave->instance));
705 else if (strcasecmp ("Collect", child->key) == 0)
708 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
710 data_copy_by_name (&slave->collect, data_definitions, buffer);
711 status = 0; /* continue after failure. */
715 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
723 if ((status == 0) && (slave->collect == NULL))
731 else /* if (status != 0) */
732 data_free_all (slave->collect);
735 } /* }}} int mb_config_add_slave */
737 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
743 host = malloc (sizeof (*host));
746 memset (host, 0, sizeof (*host));
749 status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
752 if (host->host[0] == 0)
755 for (i = 0; i < ci->children_num; i++)
757 oconfig_item_t *child = ci->children + i;
760 if (strcasecmp ("Address", child->key) == 0)
762 char buffer[NI_MAXHOST];
763 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
765 status = mb_config_set_host_address (host, buffer);
767 else if (strcasecmp ("Port", child->key) == 0)
769 host->port = cf_util_get_port_number (child);
773 else if (strcasecmp ("Interval", child->key) == 0)
774 status = cf_util_get_int (child, &host->interval);
775 else if (strcasecmp ("Slave", child->key) == 0)
776 /* Don't set status: Gracefully continue if a slave fails. */
777 mb_config_add_slave (host, child);
780 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
786 } /* for (i = 0; i < ci->children_num; i++) */
788 assert (host->host[0] != 0);
789 if (host->host[0] == 0)
791 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
800 struct timespec interval;
803 ud.free_func = host_free;
805 ssnprintf (name, sizeof (name), "modbus-%s", host->host);
807 interval.tv_nsec = 0;
808 if (host->interval > 0)
809 interval.tv_sec = host->interval;
813 plugin_register_complex_read (/* group = */ NULL, name,
814 mb_read, (interval.tv_sec > 0) ? &interval : NULL, &ud);
822 } /* }}} int mb_config_add_host */
824 static int mb_config (oconfig_item_t *ci) /* {{{ */
831 for (i = 0; i < ci->children_num; i++)
833 oconfig_item_t *child = ci->children + i;
835 if (strcasecmp ("Data", child->key) == 0)
836 mb_config_add_data (child);
837 else if (strcasecmp ("Host", child->key) == 0)
838 mb_config_add_host (child);
840 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
844 } /* }}} int mb_config */
848 static int mb_shutdown (void) /* {{{ */
850 data_free_all (data_definitions);
851 data_definitions = NULL;
854 } /* }}} int mb_shutdown */
856 void module_register (void)
858 plugin_register_complex_config ("modbus", mb_config);
859 plugin_register_shutdown ("modbus", mb_shutdown);
860 } /* void module_register */
862 /* vim: set sw=2 sts=2 et fdm=marker : */