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);
284 /* We'll do the error handling ourselves. */
285 modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
287 if ((host->port < 1) || (host->port > 65535))
288 host->port = MODBUS_TCP_DEFAULT_PORT;
290 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
291 host->node, host->port);
293 modbus_init_tcp (&host->connection,
294 /* host = */ host->node,
295 /* port = */ host->port);
297 status = modbus_connect (&host->connection);
300 ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
301 host->node, host->port, status);
305 host->is_connected = 1;
306 host->have_reconnected = 1;
308 } /* }}} int mb_init_connection */
310 #define CAST_TO_VALUE_T(ds,vt,raw) do { \
311 if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
312 (vt).counter = (counter_t) (raw); \
313 else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
314 (vt).gauge = (gauge_t) (raw); \
315 else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
316 (vt).derive = (derive_t) (raw); \
317 else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
318 (vt).absolute = (absolute_t) (raw); \
321 static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
326 const data_set_t *ds;
330 if ((host == NULL) || (slave == NULL) || (data == NULL))
333 ds = plugin_get_ds (data->type);
336 ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
342 ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
343 "I can only handle data sets with only one data source.",
344 data->type, ds->ds_num);
348 if ((ds->ds[0].type != DS_TYPE_GAUGE)
349 && (data->register_type != REG_TYPE_UINT32))
351 NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
352 "This will most likely result in problems, because the register type "
353 "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
356 memset (values, 0, sizeof (values));
357 if ((data->register_type == REG_TYPE_UINT32)
358 || (data->register_type == REG_TYPE_FLOAT))
363 for (i = 0; i < 2; i++)
365 status = read_holding_registers (&host->connection,
366 /* slave = */ slave->id, /* start_addr = */ data->register_base,
367 /* num_registers = */ values_num, /* buffer = */ values);
371 if (host->is_connected)
372 modbus_close (&host->connection);
373 host->is_connected = 0;
375 /* If we already tried reconnecting this round, give up. */
376 if (host->have_reconnected)
378 ERROR ("Modbus plugin: read_holding_registers (%s) failed. "
379 "Reconnecting has already been tried. Giving up.", host->host);
383 /* Maybe the device closed the connection during the waiting interval.
384 * Try re-establishing the connection. */
385 status = mb_init_connection (host);
388 ERROR ("Modbus plugin: read_holding_registers (%s) failed. "
389 "While trying to reconnect, connecting to \"%s\" failed. "
391 host->host, host->node);
395 DEBUG ("Modbus plugin: Re-established connection to %s", host->host);
399 } /* for (i = 0, 1) */
401 DEBUG ("Modbus plugin: mb_read_data: Success! "
402 "read_holding_registers returned with status %i.", status);
404 if (data->register_type == REG_TYPE_FLOAT)
409 float_value = mb_register_to_float (values[0], values[1]);
410 DEBUG ("Modbus plugin: mb_read_data: "
411 "Returned float value is %g", (double) float_value);
413 CAST_TO_VALUE_T (ds, vt, float_value);
414 mb_submit (host, slave, data, vt);
416 else if (data->register_type == REG_TYPE_UINT32)
421 v32 = (values[0] << 16) | values[1];
422 DEBUG ("Modbus plugin: mb_read_data: "
423 "Returned uint32 value is %"PRIu32, v32);
425 CAST_TO_VALUE_T (ds, vt, v32);
426 mb_submit (host, slave, data, vt);
428 else /* if (data->register_type == REG_TYPE_UINT16) */
432 DEBUG ("Modbus plugin: mb_read_data: "
433 "Returned uint16 value is %"PRIu16, values[0]);
435 CAST_TO_VALUE_T (ds, vt, values[0]);
436 mb_submit (host, slave, data, vt);
440 } /* }}} int mb_read_data */
442 static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
448 if ((host == NULL) || (slave == NULL))
452 for (data = slave->collect; data != NULL; data = data->next)
454 status = mb_read_data (host, slave, data);
463 } /* }}} int mb_read_slave */
465 static int mb_read (user_data_t *user_data) /* {{{ */
472 if ((user_data == NULL) || (user_data->data == NULL))
475 host = user_data->data;
477 /* Clear the reconnect flag. */
478 host->have_reconnected = 0;
481 for (i = 0; i < host->slaves_num; i++)
483 status = mb_read_slave (host, host->slaves + i);
492 } /* }}} int mb_read */
496 static void data_free_one (mb_data_t *data) /* {{{ */
503 } /* }}} void data_free_one */
505 static void data_free_all (mb_data_t *data) /* {{{ */
513 data_free_one (data);
515 data_free_all (next);
516 } /* }}} void data_free_all */
518 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
525 for (i = 0; i < slaves_num; i++)
526 data_free_all (slaves[i].collect);
528 } /* }}} void slaves_free_all */
530 static void host_free (void *void_host) /* {{{ */
532 mb_host_t *host = void_host;
537 slaves_free_all (host->slaves, host->slaves_num);
539 } /* }}} void host_free */
541 /* Config functions */
543 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
549 memset (&data, 0, sizeof (data));
551 data.register_type = REG_TYPE_UINT16;
554 status = cf_util_get_string (ci, &data.name);
558 for (i = 0; i < ci->children_num; i++)
560 oconfig_item_t *child = ci->children + i;
563 if (strcasecmp ("Type", child->key) == 0)
564 status = cf_util_get_string_buffer (child,
565 data.type, sizeof (data.type));
566 else if (strcasecmp ("Instance", child->key) == 0)
567 status = cf_util_get_string_buffer (child,
568 data.instance, sizeof (data.instance));
569 else if (strcasecmp ("RegisterBase", child->key) == 0)
570 status = cf_util_get_int (child, &data.register_base);
571 else if (strcasecmp ("RegisterType", child->key) == 0)
574 status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
577 else if (strcasecmp ("Uint16", tmp) == 0)
578 data.register_type = REG_TYPE_UINT16;
579 else if (strcasecmp ("Uint32", tmp) == 0)
580 data.register_type = REG_TYPE_UINT32;
581 else if (strcasecmp ("Float", tmp) == 0)
582 data.register_type = REG_TYPE_FLOAT;
585 ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
591 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
597 } /* for (i = 0; i < ci->children_num; i++) */
599 assert (data.name != NULL);
600 if (data.type[0] == 0)
602 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
608 data_copy (&data_definitions, &data);
613 } /* }}} int mb_config_add_data */
615 static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
618 struct addrinfo *ai_list;
619 struct addrinfo *ai_ptr;
620 struct addrinfo ai_hints;
623 if ((host == NULL) || (address == NULL))
626 memset (&ai_hints, 0, sizeof (ai_hints));
628 ai_hints.ai_flags |= AI_ADDRCONFIG;
630 /* XXX: libmodbus can only handle IPv4 addresses. */
631 ai_hints.ai_family = AF_INET;
632 ai_hints.ai_addr = NULL;
633 ai_hints.ai_canonname = NULL;
634 ai_hints.ai_next = NULL;
637 status = getaddrinfo (address, /* service = */ NULL,
638 &ai_hints, &ai_list);
642 ERROR ("Modbus plugin: getaddrinfo failed: %s",
643 (status == EAI_SYSTEM)
644 ? sstrerror (errno, errbuf, sizeof (errbuf))
645 : gai_strerror (status));
649 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
651 status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
652 host->node, sizeof (host->node),
653 /* service = */ NULL, /* length = */ 0,
654 /* flags = */ NI_NUMERICHOST);
659 freeaddrinfo (ai_list);
662 ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
663 else /* if (status == 0) */
665 DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
666 address, host->node);
670 } /* }}} int mb_config_set_host_address */
672 static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
678 if ((host == NULL) || (ci == NULL))
681 slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
684 host->slaves = slave;
685 slave = host->slaves + host->slaves_num;
686 memset (slave, 0, sizeof (*slave));
687 slave->collect = NULL;
689 status = cf_util_get_int (ci, &slave->id);
693 for (i = 0; i < ci->children_num; i++)
695 oconfig_item_t *child = ci->children + i;
698 if (strcasecmp ("Instance", child->key) == 0)
699 status = cf_util_get_string_buffer (child,
700 slave->instance, sizeof (slave->instance));
701 else if (strcasecmp ("Collect", child->key) == 0)
704 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
706 data_copy_by_name (&slave->collect, data_definitions, buffer);
707 status = 0; /* continue after failure. */
711 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
719 if ((status == 0) && (slave->collect == NULL))
727 else /* if (status != 0) */
728 data_free_all (slave->collect);
731 } /* }}} int mb_config_add_slave */
733 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
739 host = malloc (sizeof (*host));
742 memset (host, 0, sizeof (*host));
745 status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
748 if (host->host[0] == 0)
751 for (i = 0; i < ci->children_num; i++)
753 oconfig_item_t *child = ci->children + i;
756 if (strcasecmp ("Address", child->key) == 0)
758 char buffer[NI_MAXHOST];
759 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
761 status = mb_config_set_host_address (host, buffer);
763 else if (strcasecmp ("Port", child->key) == 0)
765 host->port = cf_util_get_port_number (child);
769 else if (strcasecmp ("Interval", child->key) == 0)
770 status = cf_util_get_int (child, &host->interval);
771 else if (strcasecmp ("Slave", child->key) == 0)
772 /* Don't set status: Gracefully continue if a slave fails. */
773 mb_config_add_slave (host, child);
776 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
782 } /* for (i = 0; i < ci->children_num; i++) */
784 assert (host->host[0] != 0);
785 if (host->host[0] == 0)
787 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
796 struct timespec interval;
799 ud.free_func = host_free;
801 ssnprintf (name, sizeof (name), "modbus-%s", host->host);
803 interval.tv_nsec = 0;
804 if (host->interval > 0)
805 interval.tv_sec = host->interval;
809 plugin_register_complex_read (/* group = */ NULL, name,
810 mb_read, (interval.tv_sec > 0) ? &interval : NULL, &ud);
818 } /* }}} int mb_config_add_host */
820 static int mb_config (oconfig_item_t *ci) /* {{{ */
827 for (i = 0; i < ci->children_num; i++)
829 oconfig_item_t *child = ci->children + i;
831 if (strcasecmp ("Data", child->key) == 0)
832 mb_config_add_data (child);
833 else if (strcasecmp ("Host", child->key) == 0)
834 mb_config_add_host (child);
836 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
840 } /* }}} int mb_config */
844 static int mb_shutdown (void) /* {{{ */
846 data_free_all (data_definitions);
847 data_definitions = NULL;
850 } /* }}} int mb_shutdown */
852 void module_register (void)
854 plugin_register_complex_config ("modbus", mb_config);
855 plugin_register_shutdown ("modbus", mb_shutdown);
856 } /* void module_register */
858 /* vim: set sw=2 sts=2 et fdm=marker : */