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
6 Adding a Scale and Shift parameter to the modbus plugin in order to correct amplifed data
8 src/collectd.conf.pod | 10 ++++++++++
9 src/modbus.c | 18 ++++++++++++++----
10 2 files changed, 24 insertions(+), 4 deletions(-)
12 --- a/src/collectd.conf.pod
13 +++ b/src/collectd.conf.pod
14 @@ -4169,6 +4169,16 @@ supported.
15 Sets the type instance to use when dispatching the value to I<collectd>. If
16 unset, an empty string (no type instance) is used.
18 +=item B<Scale> I<Value>
20 +The values taken from collectd are multiplied by I<Value>. The field is optional
21 +and the default is B<1.0>.
23 +=item B<Shift> I<Value>
25 +I<Value> is added to values from collectd after they have been multiplied by
26 +B<Scale> value. The field is optional and the default value is B<0.0>.
30 =item E<lt>B<Host> I<Name>E<gt> blocks
33 @@ -105,6 +105,8 @@ struct mb_data_s /* {{{ */
34 mb_mreg_type_t modbus_register_type;
35 char type[DATA_MAX_NAME_LEN];
36 char instance[DATA_MAX_NAME_LEN];
42 @@ -395,13 +397,13 @@ static int mb_init_connection(mb_host_t
43 #define CAST_TO_VALUE_T(ds, vt, raw) \
45 if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
46 - (vt).counter = (counter_t)(raw); \
47 + (vt).counter = (((counter_t)(raw) * ds[0].scale) + ds[0].shift); \
48 else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
49 - (vt).gauge = (gauge_t)(raw); \
50 + (vt).gauge = (((gauge_t)(raw) * ds[0].scale) + ds[0].shift); \
51 else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
52 - (vt).derive = (derive_t)(raw); \
53 + (vt).derive = (((derive_t)(raw) * ds[0].scale) + ds[0].shift); \
54 else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
55 - (vt).absolute = (absolute_t)(raw); \
56 + (vt).absolute = (((absolute_t)(raw) * ds[0].scale) + ds[0].shift); \
59 static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
60 @@ -723,6 +725,8 @@ static int mb_config_add_data(oconfig_it
62 data.register_type = REG_TYPE_UINT16;
67 status = cf_util_get_string(ci, &data.name);
69 @@ -736,6 +740,12 @@ static int mb_config_add_data(oconfig_it
70 else if (strcasecmp("Instance", child->key) == 0)
71 status = cf_util_get_string_buffer(child, data.instance,
72 sizeof(data.instance));
73 + else if (strcasecmp("Scale", child->key) == 0)
74 + status = cf_util_get_string_buffer(child, data.scale,
75 + sizeof(data.scale));
76 + else if (strcasecmp("Shift", child->key) == 0)
77 + status = cf_util_get_string_buffer(child, data.shift,
78 + sizeof(data.shift));
79 else if (strcasecmp("RegisterBase", child->key) == 0)
80 status = cf_util_get_int(child, &data.register_base);
81 else if (strcasecmp("RegisterType", child->key) == 0) {