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

Modbus Plugin: Panic due to bad configuration #13482

Closed
Jayclifford345 opened this issue Jun 23, 2023 · 0 comments · Fixed by #13485
Closed

Modbus Plugin: Panic due to bad configuration #13482

Jayclifford345 opened this issue Jun 23, 2023 · 0 comments · Fixed by #13485
Labels
bug unexpected problem or unintended behavior

Comments

@Jayclifford345
Copy link

Relevant telegraf.conf

# Telegraf Configuration
#
# Telegraf is entirely plugin driven. All metrics are gathered from the
# declared inputs, and sent to the declared outputs.
#
# Plugins must be declared in here to be active.
# To deactivate a plugin, comment out the name and any variables.
#
# Use 'telegraf -config telegraf.conf -test' to see what metrics a config
# file would generate.
#
# Environment variables can be used anywhere in this config file, simply surround
# them with ${}. For strings the variable must be within quotes (ie, "${STR_VAR}"),
# for numbers and booleans they should be plain (ie, ${INT_VAR}, ${BOOL_VAR})


# Global tags can be specified here in key="value" format.
[global_tags]
  # dc = "us-east-1" # will tag all metrics with dc=us-east-1
  # rack = "1a"
  ## Environment variables can be used as tags, and throughout the config file
  # user = "$USER"


# Configuration for telegraf agent
[agent]
  ## Default data collection interval for all inputs
  interval = "5s"
  ## Rounds collection interval to 'interval'
  ## ie, if interval="10s" then always collect on :00, :10, :20, etc.
  round_interval = true

  ## Telegraf will send metrics to outputs in batches of at most
  ## metric_batch_size metrics.
  ## This controls the size of writes that Telegraf sends to output plugins.
  metric_batch_size = 1000

  ## Maximum number of unwritten metrics per output.  Increasing this value
  ## allows for longer periods of output downtime without dropping metrics at the
  ## cost of higher maximum memory usage.
  metric_buffer_limit = 10000

  ## Collection jitter is used to jitter the collection by a random amount.
  ## Each plugin will sleep for a random time within jitter before collecting.
  ## This can be used to avoid many plugins querying things like sysfs at the
  ## same time, which can have a measurable effect on the system.
  collection_jitter = "5s"

  ## Default flushing interval for all outputs. Maximum flush_interval will be
  ## flush_interval + flush_jitter
  flush_interval = "10s"
  ## Jitter the flush interval by a random amount. This is primarily to avoid
  ## large write spikes for users running a large number of telegraf instances.
  ## ie, a jitter of 5s and interval 10s means flushes will happen every 10-15s
  flush_jitter = "5s"

  ## By default or when set to "0s", precision will be set to the same
  ## timestamp order as the collection interval, with the maximum being 1s.
  ##   ie, when interval = "10s", precision will be "1s"
  ##       when interval = "250ms", precision will be "1ms"
  ## Precision will NOT be used for service inputs. It is up to each individual
  ## service input to set the timestamp at the appropriate precision.
  ## Valid time units are "ns", "us" (or "µs"), "ms", "s".
  precision = ""


  ## Override default hostname, if empty use os.Hostname()
  hostname = ""
  ## If set to true, do no set the "host" tag in the telegraf agent.
  omit_hostname = false

  debug = true

  quiet = false


###############################################################################
#                            OUTPUT PLUGINS                                   #
###############################################################################

# Configuration for sending metrics to InfluxDB 2.0
[[outputs.influxdb_v2]]
alias = "opc-ua-db"
  ## The URLs of the InfluxDB cluster nodes.
  ##
  ## Multiple URLs can be specified for a single cluster, only ONE of the
  ## urls will be written to each interval.
  ##   ex: urls = ["https://us-west-2-1.aws.cloud2.influxdata.com"]
  urls = ["${INFLUX_HOST}"]

  ## Token for authentication.
  token = "${INFLUX_TOKEN}"

  ## Organization is the name of the organization you wish to write to.
  organization = "${INFLUX_ORG}"

  ## Destination bucket to write into.
  bucket = "${INFLUX_BUCKET}"



###############################################################################
#                            INPUT PLUGINS                                    #
###############################################################################
[[inputs.modbus]]
  ## Connection Configuration
  ##
  ## The plugin supports connections to PLCs via MODBUS/TCP, RTU over TCP, ASCII over TCP or
  ## via serial line communication in binary (RTU) or readable (ASCII) encoding
  ##
  ## Device name
  name = "Device"
   name_prefix = "egx150warehouse_"

  ## Slave ID - addresses a MODBUS device on the bus
  ## Range: 0 - 255 [0 = broadcast; 248 - 255 = reserved]
  slave_id = 1

  ## Timeout for each request
  timeout = "1s"

  ## Maximum number of retries and the time to wait between retries
  ## when a slave-device is busy.
  # busy_retries = 0
  # busy_retries_wait = "100ms"

  # TCP - connect via Modbus/TCP
  controller = "tcp://localhost:502"

  ## Serial (RS485; RS232)
  # controller = "file:///dev/ttyUSB0"
  # baud_rate = 9600
  # data_bits = 8
  # parity = "N"
  # stop_bits = 1

  ## For Modbus over TCP you can choose between "TCP", "RTUoverTCP" and "ASCIIoverTCP"
  ## default behaviour is "TCP" if the controller is TCP
  ## For Serial you can choose between "RTU" and "ASCII"
  # transmission_mode = "RTU"

  ## Trace the connection to the modbus device as debug messages
  ## Note: You have to enable telegraf's debug mode to see those messages!
   #debug_connection = true

  ## Measurements
  ##

  ## Digital Variables, Discrete Inputs and Coils
  ## measurement - the (optional) measurement name, defaults to "modbus"
  ## name        - the variable name
  ## address     - variable address

  #discrete_inputs = [
   # { name = "start",          address = [4]}

  #]
 # coils = [
   # { name = "motor1_run",     address = [4]}
  #]

  ## Analog Variables, Input Registers and Holding Registers
  ## measurement - the (optional) measurement name, defaults to "modbus"
  ## name        - the variable name
  ## byte_order  - the ordering of bytes
  ##  |---AB, ABCD   - Big Endian
  ##  |---BA, DCBA   - Little Endian
  ##  |---BADC       - Mid-Big Endian
  ##  |---CDAB       - Mid-Little Endian
  ## data_type  - INT16, UINT16, INT32, UINT32, INT64, UINT64, FLOAT32-IEEE, FLOAT64-IEEE (the IEEE 754 binary representation)
  ##              FLOAT32 (deprecated), FIXED, UFIXED (fixed-point representation on input)
  ## scale      - the final numeric variable representation
  ## address    - variable address

  holding_registers = [
    { name = "power_factor", byte_order = "DCBA",   data_type = "UINT64", scale=0.01,  address = [0, 1]}
  ]
  #input_registers = [
    #{ name = "tank_level",   byte_order = "BA",   data_type = "FIXED",   scale=0.01,     address = [1]},
  #]

  ## Enable workarounds required by some devices to work correctly
  # [inputs.modbus.workarounds]
    ## 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
    ## idle-timeout, however, if you query a device with limited simultaneous connectivity (e.g. serial devices)
    ## from multiple instances you might want to only stay connected during gather and disconnect afterwards.
    # close_connection_after_gather = false

Logs from Telegraf

jayclifford@Jays-MBP Telegraf-Community-Configs % telegraf --debug --test --config ./modbus/telegraf-modbus.conf
2023-06-23T09:55:24Z I! Loading config: ./modbus/telegraf-modbus.conf
2023-06-23T09:55:24Z I! Starting Telegraf 1.27.1
2023-06-23T09:55:24Z I! Available plugins: 237 inputs, 9 aggregators, 28 processors, 23 parsers, 59 outputs, 4 secret-stores
2023-06-23T09:55:24Z I! Loaded inputs: modbus
2023-06-23T09:55:24Z I! Loaded aggregators:
2023-06-23T09:55:24Z I! Loaded processors:
2023-06-23T09:55:24Z I! Loaded secretstores:
2023-06-23T09:55:24Z W! Outputs are not used in testing mode!
2023-06-23T09:55:24Z I! Tags enabled: host=Jays-MBP
2023-06-23T09:55:24Z D! [agent] Initializing plugins
2023-06-23T09:55:24Z I! [inputs.modbus] Got 1 request(s) touching 2 holding registers for 1 fields (slave 1)
2023-06-23T09:55:24Z I! [inputs.modbus] Got 0 request(s) touching 0 inputs registers for 0 fields (slave 1)
2023-06-23T09:55:24Z I! [inputs.modbus] Got 0 request(s) touching 0 discrete registers for 0 fields (slave 1)
2023-06-23T09:55:24Z I! [inputs.modbus] Got 0 request(s) touching 0 coil registers for 0 fields (slave 1)
2023-06-23T09:55:24Z D! [agent] Starting service inputs
2023-06-23T09:55:24Z D! [inputs.modbus] Reading slave 1 for tcp://localhost:502...
2023-06-23T09:55:24Z D! [inputs.modbus] trying to read holding@0[2]...
2023-06-23T09:55:24Z D! [inputs.modbus] got holding@0[2]: [0 20 0 30]
panic: runtime error: index out of range [7] with length 4

goroutine 69 [running]:
encoding/binary.littleEndian.Uint64(...)
	encoding/binary/binary.go:102
github.com/influxdata/telegraf/plugins/inputs/modbus.determineConverterU64Scale.func1({0xc000aa8009?, 0x106884fc8?, 0xfb?})
	github.com/influxdata/telegraf/plugins/inputs/modbus/type_conversions64.go:146 +0x31
github.com/influxdata/telegraf/plugins/inputs/modbus.(*Modbus).gatherRequestsHolding(0xc000882d00, {0xc000e58d50?, 0x1, 0xc000e89ba8?})
	github.com/influxdata/telegraf/plugins/inputs/modbus/modbus.go:464 +0x49c
github.com/influxdata/telegraf/plugins/inputs/modbus.(*Modbus).gatherFields(0x1068a8948?, {{0x0, 0x0, 0x0}, {0x0, 0x0, 0x0}, {0xc000e58d50, 0x1, 0x1}, ...})
	github.com/influxdata/telegraf/plugins/inputs/modbus/modbus.go:387 +0x6d
github.com/influxdata/telegraf/plugins/inputs/modbus.(*Modbus).readSlaveData(0xc000882d00, 0x0?, {{0x0, 0x0, 0x0}, {0x0, 0x0, 0x0}, {0xc000e58d50, 0x1, ...}, ...})
	github.com/influxdata/telegraf/plugins/inputs/modbus/modbus.go:377 +0x1fb
github.com/influxdata/telegraf/plugins/inputs/modbus.(*Modbus).Gather(0xc000882d00, {0x10749c600, 0xc000819b60})
	github.com/influxdata/telegraf/plugins/inputs/modbus/modbus.go:205 +0x205
github.com/influxdata/telegraf/agent.(*Agent).testRunInputs.func2(0xc000518d70)
	github.com/influxdata/telegraf/agent/agent.go:508 +0x2f8
created by github.com/influxdata/telegraf/agent.(*Agent).testRunInputs
	github.com/influxdata/telegraf/agent/agent.go:477 +0xc8

System info

Latest

Docker

No response

Steps to reproduce

Run Modbus simulator

Create two registers at address 0 and 1

Run config.

Expected behavior

Panic.

Actual behavior

Offical behaviour should be a config error.

Additional info

No response

@Jayclifford345 Jayclifford345 added the bug unexpected problem or unintended behavior label Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant