You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
3.5 KiB

  1. From eeabc41e703f39cae0ad7eb8a596045a5a2f25b4 Mon Sep 17 00:00:00 2001
  2. From: cekstam <christian.ekstam@gmail.com>
  3. Date: Tue, 27 Mar 2018 13:15:28 +0200
  4. Subject: [PATCH 1/3] Add scale and shift to modbus plugin
  5. Adding a Scale and Shift parameter to the modbus plugin in order to correct amplifed data
  6. ---
  7. src/collectd.conf.pod | 10 ++++++++++
  8. src/modbus.c | 18 ++++++++++++++----
  9. 2 files changed, 24 insertions(+), 4 deletions(-)
  10. --- a/src/collectd.conf.pod
  11. +++ b/src/collectd.conf.pod
  12. @@ -4169,6 +4169,16 @@ supported.
  13. Sets the type instance to use when dispatching the value to I<collectd>. If
  14. unset, an empty string (no type instance) is used.
  15. +=item B<Scale> I<Value>
  16. +
  17. +The values taken from collectd are multiplied by I<Value>. The field is optional
  18. +and the default is B<1.0>.
  19. +
  20. +=item B<Shift> I<Value>
  21. +
  22. +I<Value> is added to values from collectd after they have been multiplied by
  23. +B<Scale> value. The field is optional and the default value is B<0.0>.
  24. +
  25. =back
  26. =item E<lt>B<Host> I<Name>E<gt> blocks
  27. --- a/src/modbus.c
  28. +++ b/src/modbus.c
  29. @@ -105,6 +105,8 @@ struct mb_data_s /* {{{ */
  30. mb_mreg_type_t modbus_register_type;
  31. char type[DATA_MAX_NAME_LEN];
  32. char instance[DATA_MAX_NAME_LEN];
  33. + double scale;
  34. + double shift;
  35. mb_data_t *next;
  36. }; /* }}} */
  37. @@ -395,13 +397,13 @@ static int mb_init_connection(mb_host_t
  38. #define CAST_TO_VALUE_T(ds, vt, raw) \
  39. do { \
  40. if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
  41. - (vt).counter = (counter_t)(raw); \
  42. + (vt).counter = (((counter_t)(raw) * ds[0].scale) + ds[0].shift); \
  43. else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
  44. - (vt).gauge = (gauge_t)(raw); \
  45. + (vt).gauge = (((gauge_t)(raw) * ds[0].scale) + ds[0].shift); \
  46. else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
  47. - (vt).derive = (derive_t)(raw); \
  48. + (vt).derive = (((derive_t)(raw) * ds[0].scale) + ds[0].shift); \
  49. else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
  50. - (vt).absolute = (absolute_t)(raw); \
  51. + (vt).absolute = (((absolute_t)(raw) * ds[0].scale) + ds[0].shift); \
  52. } while (0)
  53. static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
  54. @@ -723,6 +725,8 @@ static int mb_config_add_data(oconfig_it
  55. data.name = NULL;
  56. data.register_type = REG_TYPE_UINT16;
  57. data.next = NULL;
  58. + data.scale = 1;
  59. + data.shift = 0;
  60. status = cf_util_get_string(ci, &data.name);
  61. if (status != 0)
  62. @@ -736,6 +740,12 @@ static int mb_config_add_data(oconfig_it
  63. else if (strcasecmp("Instance", child->key) == 0)
  64. status = cf_util_get_string_buffer(child, data.instance,
  65. sizeof(data.instance));
  66. + else if (strcasecmp("Scale", child->key) == 0)
  67. + status = cf_util_get_string_buffer(child, data.scale,
  68. + sizeof(data.scale));
  69. + else if (strcasecmp("Shift", child->key) == 0)
  70. + status = cf_util_get_string_buffer(child, data.shift,
  71. + sizeof(data.shift));
  72. else if (strcasecmp("RegisterBase", child->key) == 0)
  73. status = cf_util_get_int(child, &data.register_base);
  74. else if (strcasecmp("RegisterType", child->key) == 0) {