Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Modbus connection settings (serial) #9256

Merged
merged 9 commits into from
Oct 19, 2021

Conversation

srebhan
Copy link
Member

@srebhan srebhan commented May 11, 2021

Required for all PRs:

  • Updated associated README.md.
  • Wrote appropriate unit tests.

resolves #9253
resolves #8225

With this PR we add options to enable debugging of modbus connections and to configure the connection handling more fine-grained.
With close_connection_after_gather you can force the plugin to close and reopen the connection in each gather cycle. This is especially important for long intervals and/or modbus devices with a restricted number of simultaneous connections.
The second option pause_between_requests allows to add a pause between two requests (e.g. when querying non-consecutive registers). This is required by some serial devices (see Issue #9253).

@telegraf-tiger telegraf-tiger bot added the feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin label May 11, 2021
@binueda
Copy link

binueda commented May 11, 2021

Tested with pause_between_requests = "100ms" with non-consecutive registers and found working perfect 👍

@binueda
Copy link

binueda commented May 11, 2021

Tested with pause_between_requests = "100ms" with non-consecutive registers and found working perfect 👍

Configuration

trace_connection = true
close_connection = false
name = "BM"
slave_id = 10
timeout = "4s"
pause_between_requests = "100ms"
#   ## Serial (RS485; RS232)
   controller = "file:///dev/ttyAMA0"
   baud_rate = 9600
   data_bits = 8
   parity = "E"
   stop_bits = 1
   transmission_mode = "RTU"
holding_registers = [
        { name = "Watts Total-CH1",     byte_order = "CDAB",   data_type = "FLOAT32-IEEE", scale=1.0,address = [4100,4101]},
        { name = "Total Current-2",       byte_order = "CDAB",   data_type = "FLOAT32-IEEE", scale=1.0,address = [4202,4203]},
   ]

Result

2021-05-11T11:31:49Z D! [agent] Starting service inputs
2021-05-11T11:31:50Z D! [inputs.modbus] modbus: send 0a 03 10 04 00 02 80 71
2021-05-11T11:31:50Z D! [inputs.modbus] modbus: recv 0a 03 04 00 00 00 00 40 f3
2021-05-11T11:31:50Z D! [inputs.modbus] got holding@4100[2]: [0 0 0 0]
2021-05-11T11:31:50Z D! [inputs.modbus]   field Watts Total-CH1 with offset 0 with len 4: [0 0 0 0] --> 0
2021-05-11T11:31:50Z D! [inputs.modbus] modbus: send 0a 03 10 6a 00 02 e1 ac
2021-05-11T11:31:50Z D! [inputs.modbus] modbus: recv 0a 03 04 00 00 00 00 40 f3
2021-05-11T11:31:50Z D! [inputs.modbus] got holding@4202[2]: [0 0 0 0]
2021-05-11T11:31:50Z D! [inputs.modbus]   field Total Current-2 with offset 0 with len 4: [0 0 0 0] --> 0
2021-05-11T11:31:55Z D! [inputs.modbus] modbus: send 0a 03 10 04 00 02 80 71
2021-05-11T11:31:55Z D! [inputs.modbus] modbus: recv 0a 03 04 00 00 00 00 40 f3
2021-05-11T11:31:55Z D! [inputs.modbus] got holding@4100[2]: [0 0 0 0]
2021-05-11T11:31:55Z D! [inputs.modbus]   field Watts Total-CH1 with offset 0 with len 4: [0 0 0 0] --> 0
2021-05-11T11:31:55Z D! [inputs.modbus] modbus: send 0a 03 10 6a 00 02 e1 ac
2021-05-11T11:31:55Z D! [inputs.modbus] modbus: recv 0a 03 04 00 00 00 00 40 f3
2021-05-11T11:31:55Z D! [inputs.modbus] got holding@4202[2]: [0 0 0 0]
2021-05-11T11:31:55Z D! [inputs.modbus]   field Total Current-2 with offset 0 with len 4: [0 0 0 0] --> 0
2021-05-11T11:31:59Z D! [outputs.file] Wrote batch of 2 metrics in 615.843µs
2021-05-11T11:31:59Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2021-05-11T11:32:00Z D! [inputs.modbus] modbus: send 0a 03 10 04 00 02 80 71
2021-05-11T11:32:00Z D! [inputs.modbus] modbus: recv 0a 03 04 00 00 00 00 40 f3
2021-05-11T11:32:00Z D! [inputs.modbus] got holding@4100[2]: [0 0 0 0]
2021-05-11T11:32:00Z D! [inputs.modbus]   field Watts Total-CH1 with offset 0 with len 4: [0 0 0 0] --> 0
2021-05-11T11:32:00Z D! [inputs.modbus] modbus: send 0a 03 10 6a 00 02 e1 ac
2021-05-11T11:32:00Z D! [inputs.modbus] modbus: recv 0a 03 04 00 00 00 00 40 f3
2021-05-11T11:32:00Z D! [inputs.modbus] got holding@4202[2]: [0 0 0 0]
2021-05-11T11:32:00Z D! [inputs.modbus]   field Total Current-2 with offset 0 with len 4: [0 0 0 0] --> 0
2021-05-11T11:32:05Z D! [inputs.modbus] modbus: send 0a 03 10 04 00 02 80 71
2021-05-11T11:32:05Z D! [inputs.modbus] modbus: recv 0a 03 04 00 00 00 00 40 f3
2021-05-11T11:32:05Z D! [inputs.modbus] got holding@4100[2]: [0 0 0 0]
2021-05-11T11:32:05Z D! [inputs.modbus]   field Watts Total-CH1 with offset 0 with len 4: [0 0 0 0] --> 0
2021-05-11T11:32:05Z D! [inputs.modbus] modbus: send 0a 03 10 6a 00 02 e1 ac
2021-05-11T11:32:05Z D! [inputs.modbus] modbus: recv 0a 03 04 00 00 00 00 40 f3
2021-05-11T11:32:05Z D! [inputs.modbus] got holding@4202[2]: [0 0 0 0]
2021-05-11T11:32:05Z D! [inputs.modbus]   field Total Current-2 with offset 0 with len 4: [0 0 0 0] --> 0

@rwalli
Copy link

rwalli commented Jun 17, 2021

workarounds = { close_connection_after_gather = true }

is working for me.

@srebhan
Copy link
Member Author

srebhan commented Jun 18, 2021

@rwalli btw using workarounds.close_connection_after_gather = true should also work according to the TOML syntax or

[inputs.modbus.workarounds]
    close_connection_after_gather = true

:-)

Thanks for testsing!

Copy link
Contributor

@Hipska Hipska left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to go for the workarounds = { } notation instead of [inputs.modbus.workarounds]?

plugins/inputs/modbus/README.md Outdated Show resolved Hide resolved
plugins/inputs/modbus/modbus.go Outdated Show resolved Hide resolved
@srebhan
Copy link
Member Author

srebhan commented Jul 12, 2021

Any reason to go for the workarounds = { } notation instead of [inputs.modbus.workarounds]?

No special intention behind it. It just matches the register definition a bit better, but if you insist on the [inputs.modbus.workarounds] notation for the sample it's also fine with me. However, @rwalli is right, you cannot configure other options afterwards as everything is then assigned to the workarounds table. That's the downside of having no grouping in this part of the TOML syntax.

@Hipska
Copy link
Contributor

Hipska commented Jul 12, 2021

It seems like the [] notation seems more preferred by telegraf/InfluxDB?

@srebhan
Copy link
Member Author

srebhan commented Jul 12, 2021

@Hipska I agree, however using this notation will probably lead to problems for less-experienced users as this

[[inputs.modbus]]
    ...
    holding_registers = [
        { name = "power_factor", byte_order = "AB",   data_type = "FIXED", scale=0.01,  address = [8]},
        { name = "voltage",      byte_order = "AB",   data_type = "FIXED", scale=0.1,   address = [0]},
        { name = "energy",       byte_order = "ABCD", data_type = "FIXED", scale=0.001, address = [5,6]}
    ]

    [inputs.modbus.workarounds]
        pause_between_requests = "100ms"

will work just fine. However, this

[[inputs.modbus]]
    ...
    [inputs.modbus.workarounds]
        pause_between_requests = "100ms"

    holding_registers = [
        { name = "power_factor", byte_order = "AB",   data_type = "FIXED", scale=0.01,  address = [8]},
        { name = "voltage",      byte_order = "AB",   data_type = "FIXED", scale=0.1,   address = [0]},
        { name = "energy",       byte_order = "ABCD", data_type = "FIXED", scale=0.001, address = [5,6]}
    ]

will fail as now holding_registers actually becomes inputs.modbus.workarounds.holding_registers instead of inputs.modbus.holding_registers.

In contrast

[[inputs.modbus]]
    ...
    workarounds = {
        pause_between_requests = "100ms"
    }

    holding_registers = [
        { name = "power_factor", byte_order = "AB",   data_type = "FIXED", scale=0.01,  address = [8]},
        { name = "voltage",      byte_order = "AB",   data_type = "FIXED", scale=0.1,   address = [0]},
        { name = "energy",       byte_order = "ABCD", data_type = "FIXED", scale=0.001, address = [5,6]}
    ]

will work in both positions due to the explicit start { and end } markers.

Should we really put the fragile formatting in the example?

@Hipska
Copy link
Contributor

Hipska commented Jul 13, 2021

I agree, but just saying it has always been like this. See for example tag/field filtering documentation…

@srebhan srebhan changed the title Modbus connection settings (serial) feat: Modbus connection settings (serial) Aug 4, 2021
plugins/inputs/modbus/modbus.go Show resolved Hide resolved
plugins/inputs/modbus/modbus.go Show resolved Hide resolved
plugins/inputs/modbus/modbus.go Show resolved Hide resolved
@srebhan srebhan requested a review from reimda October 13, 2021 11:02
@Hipska Hipska added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Oct 13, 2021
@reimda reimda merged commit cf605b5 into influxdata:master Oct 19, 2021
phemmer added a commit to phemmer/telegraf that referenced this pull request Oct 26, 2021
* origin/master: (176 commits)
  fix: Linter fixes for plugins/inputs/[h-j]* (influxdata#9986)
  fix(inputs/kube_inventory): don't skip resources with zero s/ns timestamps (influxdata#9978)
  fix: update gjson to v1.10.2 (influxdata#9998)
  fix: procstat tags were not getting generated correctly (influxdata#9973)
  chore: create bug report form (influxdata#9976)
  fix: Allow for non x86 macs in Go install script (influxdata#9982)
  test: add sqlserver plugin integration tests (influxdata#9943)
  feat: plugins/common/tls/config.go: Filter client certificates by DNS names (influxdata#9910)
  feat: add option to skip table creation in azure data explorer output (influxdata#9942)
  docs: update nightlies links (influxdata#9989)
  fix: add s390x to nightlies (influxdata#9990)
  feat: Add more details to processors.ifname logmessages (influxdata#9984)
  docs: Create SECURITY.md (influxdata#9951)
  fix: set NIGHTLY=1 for correctly named nightly artifacts (influxdata#9987)
  feat: Kafka Add metadata full to config (influxdata#9833)
  chore: Update to AWS SDK v2 (influxdata#9647)
  chore: lint ignore fmt.Printf unhandled error (influxdata#9967)
  fix: starlark pop operation for non-existing keys (influxdata#9954)
  feat: update etc/telegraf.conf and etc/telegraf_windows.conf (influxdata#9876)
  fix: Check return code of zfs command for FreeBSD. (influxdata#9956)
  chore: update go to 1.17.2 (influxdata#9873)
  fix: Graylog plugin TLS support and message format (influxdata#9862)
  docs: update README with info on package repos (influxdata#9964)
  feat: Modbus connection settings (serial) (influxdata#9256)
  fix: segfault in ingress, persistentvolumeclaim, statefulset in kube_inventory (influxdata#9585)
  fix: add normalization of tags for ethtool input plugin (influxdata#9901)
  chore: remove empty build.py (influxdata#9958)
  fix: internet_speed input plugin not collecting/reporting latency (influxdata#9957)
  chore: reference db2 external plugin (influxdata#9952)
  chore: update readme go version from 1.14 to 1.17 (influxdata#9944)
  fix: decode Prometheus scrape path from Kuberentes labels (influxdata#9662)
  docs: fix broken link (influxdata#9812)
  fix: Correct conversion of int with specific bit size (influxdata#9933)
  fix: update golanci-lint to v1.42.1 (influxdata#9932)
  feat: Azure Event Hubs output plugin (influxdata#9346)
  feat: more fields for papertrail event webhook (influxdata#9940)
  fix: solve compatibility issue for mongodb inputs when using 5.x relicaset (influxdata#9892)
  docs: Add symlink to command documentation (influxdata#9926)
  docs: update contributing.md (influxdata#9914)
  chore: reference oracle external plugin (influxdata#9934)
  ...
phemmer added a commit to phemmer/telegraf that referenced this pull request Oct 26, 2021
* origin/master: (176 commits)
  fix: Linter fixes for plugins/inputs/[h-j]* (influxdata#9986)
  fix(inputs/kube_inventory): don't skip resources with zero s/ns timestamps (influxdata#9978)
  fix: update gjson to v1.10.2 (influxdata#9998)
  fix: procstat tags were not getting generated correctly (influxdata#9973)
  chore: create bug report form (influxdata#9976)
  fix: Allow for non x86 macs in Go install script (influxdata#9982)
  test: add sqlserver plugin integration tests (influxdata#9943)
  feat: plugins/common/tls/config.go: Filter client certificates by DNS names (influxdata#9910)
  feat: add option to skip table creation in azure data explorer output (influxdata#9942)
  docs: update nightlies links (influxdata#9989)
  fix: add s390x to nightlies (influxdata#9990)
  feat: Add more details to processors.ifname logmessages (influxdata#9984)
  docs: Create SECURITY.md (influxdata#9951)
  fix: set NIGHTLY=1 for correctly named nightly artifacts (influxdata#9987)
  feat: Kafka Add metadata full to config (influxdata#9833)
  chore: Update to AWS SDK v2 (influxdata#9647)
  chore: lint ignore fmt.Printf unhandled error (influxdata#9967)
  fix: starlark pop operation for non-existing keys (influxdata#9954)
  feat: update etc/telegraf.conf and etc/telegraf_windows.conf (influxdata#9876)
  fix: Check return code of zfs command for FreeBSD. (influxdata#9956)
  chore: update go to 1.17.2 (influxdata#9873)
  fix: Graylog plugin TLS support and message format (influxdata#9862)
  docs: update README with info on package repos (influxdata#9964)
  feat: Modbus connection settings (serial) (influxdata#9256)
  fix: segfault in ingress, persistentvolumeclaim, statefulset in kube_inventory (influxdata#9585)
  fix: add normalization of tags for ethtool input plugin (influxdata#9901)
  chore: remove empty build.py (influxdata#9958)
  fix: internet_speed input plugin not collecting/reporting latency (influxdata#9957)
  chore: reference db2 external plugin (influxdata#9952)
  chore: update readme go version from 1.14 to 1.17 (influxdata#9944)
  fix: decode Prometheus scrape path from Kuberentes labels (influxdata#9662)
  docs: fix broken link (influxdata#9812)
  fix: Correct conversion of int with specific bit size (influxdata#9933)
  fix: update golanci-lint to v1.42.1 (influxdata#9932)
  feat: Azure Event Hubs output plugin (influxdata#9346)
  feat: more fields for papertrail event webhook (influxdata#9940)
  fix: solve compatibility issue for mongodb inputs when using 5.x relicaset (influxdata#9892)
  docs: Add symlink to command documentation (influxdata#9926)
  docs: update contributing.md (influxdata#9914)
  chore: reference oracle external plugin (influxdata#9934)
  ...
VladislavSenkevich pushed a commit to gwos/telegraf that referenced this pull request Nov 23, 2021
phemmer added a commit to phemmer/telegraf that referenced this pull request Nov 30, 2021
* origin/master: (176 commits)
  fix: Linter fixes for plugins/inputs/[h-j]* (influxdata#9986)
  fix(inputs/kube_inventory): don't skip resources with zero s/ns timestamps (influxdata#9978)
  fix: update gjson to v1.10.2 (influxdata#9998)
  fix: procstat tags were not getting generated correctly (influxdata#9973)
  chore: create bug report form (influxdata#9976)
  fix: Allow for non x86 macs in Go install script (influxdata#9982)
  test: add sqlserver plugin integration tests (influxdata#9943)
  feat: plugins/common/tls/config.go: Filter client certificates by DNS names (influxdata#9910)
  feat: add option to skip table creation in azure data explorer output (influxdata#9942)
  docs: update nightlies links (influxdata#9989)
  fix: add s390x to nightlies (influxdata#9990)
  feat: Add more details to processors.ifname logmessages (influxdata#9984)
  docs: Create SECURITY.md (influxdata#9951)
  fix: set NIGHTLY=1 for correctly named nightly artifacts (influxdata#9987)
  feat: Kafka Add metadata full to config (influxdata#9833)
  chore: Update to AWS SDK v2 (influxdata#9647)
  chore: lint ignore fmt.Printf unhandled error (influxdata#9967)
  fix: starlark pop operation for non-existing keys (influxdata#9954)
  feat: update etc/telegraf.conf and etc/telegraf_windows.conf (influxdata#9876)
  fix: Check return code of zfs command for FreeBSD. (influxdata#9956)
  chore: update go to 1.17.2 (influxdata#9873)
  fix: Graylog plugin TLS support and message format (influxdata#9862)
  docs: update README with info on package repos (influxdata#9964)
  feat: Modbus connection settings (serial) (influxdata#9256)
  fix: segfault in ingress, persistentvolumeclaim, statefulset in kube_inventory (influxdata#9585)
  fix: add normalization of tags for ethtool input plugin (influxdata#9901)
  chore: remove empty build.py (influxdata#9958)
  fix: internet_speed input plugin not collecting/reporting latency (influxdata#9957)
  chore: reference db2 external plugin (influxdata#9952)
  chore: update readme go version from 1.14 to 1.17 (influxdata#9944)
  fix: decode Prometheus scrape path from Kuberentes labels (influxdata#9662)
  docs: fix broken link (influxdata#9812)
  fix: Correct conversion of int with specific bit size (influxdata#9933)
  fix: update golanci-lint to v1.42.1 (influxdata#9932)
  feat: Azure Event Hubs output plugin (influxdata#9346)
  feat: more fields for papertrail event webhook (influxdata#9940)
  fix: solve compatibility issue for mongodb inputs when using 5.x relicaset (influxdata#9892)
  docs: Add symlink to command documentation (influxdata#9926)
  docs: update contributing.md (influxdata#9914)
  chore: reference oracle external plugin (influxdata#9934)
  ...
phemmer added a commit to phemmer/telegraf that referenced this pull request Jan 3, 2022
* origin/master: (176 commits)
  fix: Linter fixes for plugins/inputs/[h-j]* (influxdata#9986)
  fix(inputs/kube_inventory): don't skip resources with zero s/ns timestamps (influxdata#9978)
  fix: update gjson to v1.10.2 (influxdata#9998)
  fix: procstat tags were not getting generated correctly (influxdata#9973)
  chore: create bug report form (influxdata#9976)
  fix: Allow for non x86 macs in Go install script (influxdata#9982)
  test: add sqlserver plugin integration tests (influxdata#9943)
  feat: plugins/common/tls/config.go: Filter client certificates by DNS names (influxdata#9910)
  feat: add option to skip table creation in azure data explorer output (influxdata#9942)
  docs: update nightlies links (influxdata#9989)
  fix: add s390x to nightlies (influxdata#9990)
  feat: Add more details to processors.ifname logmessages (influxdata#9984)
  docs: Create SECURITY.md (influxdata#9951)
  fix: set NIGHTLY=1 for correctly named nightly artifacts (influxdata#9987)
  feat: Kafka Add metadata full to config (influxdata#9833)
  chore: Update to AWS SDK v2 (influxdata#9647)
  chore: lint ignore fmt.Printf unhandled error (influxdata#9967)
  fix: starlark pop operation for non-existing keys (influxdata#9954)
  feat: update etc/telegraf.conf and etc/telegraf_windows.conf (influxdata#9876)
  fix: Check return code of zfs command for FreeBSD. (influxdata#9956)
  chore: update go to 1.17.2 (influxdata#9873)
  fix: Graylog plugin TLS support and message format (influxdata#9862)
  docs: update README with info on package repos (influxdata#9964)
  feat: Modbus connection settings (serial) (influxdata#9256)
  fix: segfault in ingress, persistentvolumeclaim, statefulset in kube_inventory (influxdata#9585)
  fix: add normalization of tags for ethtool input plugin (influxdata#9901)
  chore: remove empty build.py (influxdata#9958)
  fix: internet_speed input plugin not collecting/reporting latency (influxdata#9957)
  chore: reference db2 external plugin (influxdata#9952)
  chore: update readme go version from 1.14 to 1.17 (influxdata#9944)
  fix: decode Prometheus scrape path from Kuberentes labels (influxdata#9662)
  docs: fix broken link (influxdata#9812)
  fix: Correct conversion of int with specific bit size (influxdata#9933)
  fix: update golanci-lint to v1.42.1 (influxdata#9932)
  feat: Azure Event Hubs output plugin (influxdata#9346)
  feat: more fields for papertrail event webhook (influxdata#9940)
  fix: solve compatibility issue for mongodb inputs when using 5.x relicaset (influxdata#9892)
  docs: Add symlink to command documentation (influxdata#9926)
  docs: update contributing.md (influxdata#9914)
  chore: reference oracle external plugin (influxdata#9934)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/modbus feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modbus Serial port timeout for discontinued address poll input.modbus TCP connection handling
5 participants