From 589447abafcc719a15b3904d90a87f83470bf2e7 Mon Sep 17 00:00:00 2001 From: Joerg Epping <56630628+joerg-git@users.noreply.github.com> Date: Tue, 18 Oct 2022 20:42:18 +0200 Subject: [PATCH] feat(inputs.modbus): add config option to pause after connect (#11983) --- etc/telegraf.conf | 2 ++ etc/telegraf_windows.conf | 2 ++ plugins/inputs/modbus/README.md | 8 +++++--- plugins/inputs/modbus/modbus.go | 9 +++++++-- plugins/inputs/modbus/sample.conf | 2 ++ plugins/inputs/modbus/sample_general_end.conf | 2 ++ 6 files changed, 20 insertions(+), 5 deletions(-) diff --git a/etc/telegraf.conf b/etc/telegraf.conf index 89ee352a5..e52c5fb06 100644 --- a/etc/telegraf.conf +++ b/etc/telegraf.conf @@ -5970,6 +5970,8 @@ # # ## Enable workarounds required by some devices to work correctly # # [inputs.modbus.workarounds] +# ## Pause after connect delays the first request by the specified time. This might be necessary for (slow) devices. +# # pause_after_connect = "0ms" # ## Pause between read requests sent to the device. This might be necessary for (slow) serial devices. # # pause_between_requests = "0ms" # ## Close the connection after every gather cycle. Usually the plugin closes the connection after a certain diff --git a/etc/telegraf_windows.conf b/etc/telegraf_windows.conf index 8e9791aa9..e0ff4e028 100644 --- a/etc/telegraf_windows.conf +++ b/etc/telegraf_windows.conf @@ -5764,6 +5764,8 @@ # # ## Enable workarounds required by some devices to work correctly # # [inputs.modbus.workarounds] +# ## Pause after connect delays the first request by the specified time. This might be necessary for (slow) devices. +# # pause_after_connect = "0ms" # ## Pause between read requests sent to the device. This might be necessary for (slow) serial devices. # # pause_between_requests = "0ms" # ## Close the connection after every gather cycle. Usually the plugin closes the connection after a certain diff --git a/plugins/inputs/modbus/README.md b/plugins/inputs/modbus/README.md index 432036506..256bdf45a 100644 --- a/plugins/inputs/modbus/README.md +++ b/plugins/inputs/modbus/README.md @@ -196,6 +196,8 @@ Registers via Modbus TCP or Modbus RTU/ASCII. ## Enable workarounds required by some devices to work correctly # [inputs.modbus.workarounds] + ## Pause after connect delays the first request by the specified time. This might be necessary for (slow) devices. + # pause_after_connect = "0ms" ## Pause between read requests sent to the device. This might be necessary for (slow) serial devices. # pause_between_requests = "0ms" ## Close the connection after every gather cycle. Usually the plugin closes the connection after a certain @@ -211,9 +213,9 @@ those debug messages, Telegraf has to be started with debugging enabled (i.e. with the `--debug` option). Please be aware that connection tracing will produce a lot of messages and should __NOT__ be used in production environments. -Please use `pause_between_requests` with care. Ensure the total gather time, -including the pause(s), does not exceed the configured collection interval. Note -that pauses add up if multiple requests are sent! +Please use `pause_after_connect` / `pause_between_requests` with care. Ensure +the total gather time, including the pause(s), does not exceed the configured +collection interval. Note that pauses add up if multiple requests are sent! ## Configuration styles diff --git a/plugins/inputs/modbus/modbus.go b/plugins/inputs/modbus/modbus.go index 26e42e882..7aeb25b47 100644 --- a/plugins/inputs/modbus/modbus.go +++ b/plugins/inputs/modbus/modbus.go @@ -24,8 +24,9 @@ var sampleConfigStart string var sampleConfigEnd string type ModbusWorkarounds struct { - PollPause config.Duration `toml:"pause_between_requests"` - CloseAfterGather bool `toml:"close_connection_after_gather"` + AfterConnectPause config.Duration `toml:"pause_after_connect"` + PollPause config.Duration `toml:"pause_between_requests"` + CloseAfterGather bool `toml:"close_connection_after_gather"` } // Modbus holds all data relevant to the plugin @@ -265,6 +266,10 @@ func (m *Modbus) initClient() error { func (m *Modbus) connect() error { err := m.handler.Connect() m.isConnected = err == nil + if m.isConnected && m.Workarounds.AfterConnectPause != 0 { + nextRequest := time.Now().Add(time.Duration(m.Workarounds.AfterConnectPause)) + time.Sleep(time.Until(nextRequest)) + } return err } diff --git a/plugins/inputs/modbus/sample.conf b/plugins/inputs/modbus/sample.conf index 8fe908ed0..cf1216be2 100644 --- a/plugins/inputs/modbus/sample.conf +++ b/plugins/inputs/modbus/sample.conf @@ -188,6 +188,8 @@ ## Enable workarounds required by some devices to work correctly # [inputs.modbus.workarounds] + ## Pause after connect delays the first request by the specified time. This might be necessary for (slow) devices. + # pause_after_connect = "0ms" ## Pause between read requests sent to the device. This might be necessary for (slow) serial devices. # pause_between_requests = "0ms" ## Close the connection after every gather cycle. Usually the plugin closes the connection after a certain diff --git a/plugins/inputs/modbus/sample_general_end.conf b/plugins/inputs/modbus/sample_general_end.conf index f6245caed..3f58b336b 100644 --- a/plugins/inputs/modbus/sample_general_end.conf +++ b/plugins/inputs/modbus/sample_general_end.conf @@ -1,5 +1,7 @@ ## Enable workarounds required by some devices to work correctly # [inputs.modbus.workarounds] + ## Pause after connect delays the first request by the specified time. This might be necessary for (slow) devices. + # pause_after_connect = "0ms" ## Pause between read requests sent to the device. This might be necessary for (slow) serial devices. # pause_between_requests = "0ms" ## Close the connection after every gather cycle. Usually the plugin closes the connection after a certain