文档文档

OPC UA 客户端读取器输入插件

此插件通过订阅配置的节点,从 OPC UA 服务器收集数据。

引入于: Telegraf v1.16.0 标签: iot 操作系统支持: all

全局配置选项

插件支持其他全局和插件配置设置,用于修改指标、标签和字段,创建别名以及配置插件顺序等任务。更多详情请参阅 CONFIGURATION.md

Secret-store 支持

此插件支持从 secret-stores 获取 usernamepassword 选项的密钥。有关如何使用它们的更多详细信息,请参阅 secret-store 文档

配置

# Retrieve data from OPCUA devices
[[inputs.opcua]]
  ## Metric name
  # name = "opcua"

  ## OPC UA Endpoint URL
  # endpoint = "opc.tcp://:4840"

  ## Maximum time allowed to establish a connect to the endpoint.
  # connect_timeout = "10s"

  ## Maximum time allowed for a request over the established connection.
  # request_timeout = "5s"

  ## Maximum time that a session shall remain open without activity.
  # session_timeout = "20m"

  ## Retry options for failing reads e.g. due to invalid sessions
  ## If the retry count is zero, the read will fail after the initial attempt.
  # read_retry_timeout = "100ms"
  # read_retry_count = 0

  ## Number of consecutive errors before forcing a reconnection
  ## If set to 1 (default), the client will reconnect after a single failed read
  # reconnect_error_threshold = 1

  ## Security policy, one of "None", "Basic128Rsa15", "Basic256",
  ## "Basic256Sha256", or "auto"
  # security_policy = "auto"

  ## Security mode, one of "None", "Sign", "SignAndEncrypt", or "auto"
  # security_mode = "auto"

  ## Path to client certificate and private key files, must be specified together.
  ## If none of the options are specified, a temporary self-signed certificate
  ## will be created. If the options are specified but the files do not exist, a
  ## self-signed certificate will be created and stored permanently at the
  ## given locations.
  # certificate = "/etc/telegraf/cert.pem"
  # private_key = "/etc/telegraf/key.pem"

  ## Path to additional, explicitly trusted certificate for the remote endpoint
  # remote_certificate = "/etc/telegraf/opcua_server_cert.pem"

  ## Authentication Method, one of "Certificate", "UserName", or "Anonymous".  To
  ## authenticate using a specific ID, select 'Certificate' or 'UserName'
  # auth_method = "Anonymous"

  ## Username and password required for auth_method = "UserName"
  # username = ""
  # password = ""

  ## Option to select the metric timestamp to use. Valid options are:
  ##     "gather" -- uses the time of receiving the data in telegraf
  ##     "server" -- uses the timestamp provided by the server
  ##     "source" -- uses the timestamp provided by the source
  # timestamp = "gather"

  ## Client trace messages
  ## When set to true, and debug mode enabled in the agent settings, the OPCUA
  ## client's messages are included in telegraf logs. These messages are very
  ## noisey, but essential for debugging issues.
  # client_trace = false

  ## Include additional Fields in each metric
  ## Available options are:
  ##   DataType -- OPC-UA Data Type (string)
  # optional_fields = []

  ## Node ID configuration
  ## name              - field name to use in the output
  ## namespace         - OPC UA namespace of the node (integer value 0 thru 3)
  ## namespace_uri     - OPC UA namespace URI (alternative to namespace for stable references)
  ## identifier_type   - OPC UA ID type (s=string, i=numeric, g=guid, b=opaque)
  ## identifier        - OPC UA ID (tag as shown in opcua browser)
  ## default_tags      - extra tags to be added to the output metric (optional)
  ##
  ## Note: Specify either 'namespace' or 'namespace_uri', not both.
  ## Use either the inline notation or the bracketed notation, not both.

  ## Inline notation (default_tags not supported yet)
  # nodes = [
  #   {name="", namespace="", identifier_type="", identifier=""},
  # ]

  ## Bracketed notation
  # [[inputs.opcua.nodes]]
  #   name = "node1"
  #   namespace = ""
  #   identifier_type = ""
  #   identifier = ""
  #   default_tags = { tag1 = "value1", tag2 = "value2" }
  #
  # [[inputs.opcua.nodes]]
  #   name = "node2"
  #   namespace = ""
  #   identifier_type = ""
  #   identifier = ""
  #
  # [[inputs.opcua.nodes]]
  #   name = "node3"
  #   namespace_uri = "http://opcfoundation.org/UA/"
  #   identifier_type = ""
  #   identifier = ""

  ## Node Group
  ## Sets defaults so they aren't required in every node.
  ## Default values can be set for:
  ## * Metric name
  ## * OPC UA namespace
  ## * Identifier
  ## * Default tags
  ##
  ## Multiple node groups are allowed
  #[[inputs.opcua.group]]
  ## Group Metric name. Overrides the top level name.  If unset, the
  ## top level name is used.
  # name =

  ## Group default namespace. If a node in the group doesn't set its
  ## namespace, this is used.
  # namespace =

  ## Group default namespace URI. Alternative to namespace for stable references.
  ## If a node in the group doesn't set its namespace_uri, this is used.
  # namespace_uri =

  ## Group default identifier type. If a node in the group doesn't set its
  ## identifier_type, this is used.
  # identifier_type =

  ## Default tags that are applied to every node in this group. Can be
  ## overwritten in a node by setting a different value for the tag name.
  ##   example: default_tags = { tag1 = "value1" }
  # default_tags = {}

  ## Node ID Configuration. Array of nodes with the same settings as above.
  ## Use either the inline notation or the bracketed notation, not both.

  ## Inline notation (default_tags not supported yet)
  # nodes = [
  #  {name="node1", namespace="", identifier_type="", identifier=""},
  #  {name="node2", namespace="", identifier_type="", identifier=""},
  #]

  ## Bracketed notation
  # [[inputs.opcua.group.nodes]]
  #   name = "node1"
  #   namespace = ""
  #   identifier_type = ""
  #   identifier = ""
  #   default_tags = { tag1 = "override1", tag2 = "value2" }
  #
  # [[inputs.opcua.group.nodes]]
  #   name = "node2"
  #   namespace = ""
  #   identifier_type = ""
  #   identifier = ""

  ## Enable workarounds required by some devices to work correctly
  # [inputs.opcua.workarounds]
  #   ## Set additional valid status codes, StatusOK (0x0) is always considered valid
  #   # additional_valid_status_codes = ["0xC0"]

  # [inputs.opcua.request_workarounds]
  #   ## Use unregistered reads instead of registered reads
  #   # use_unregistered_reads = false

客户端证书配置

当使用“None”以外的安全模式时,Telegraf 将充当 OPC UA 客户端,并需要一个客户端证书来向服务器进行身份验证。该插件支持三种证书管理方法。

临时自签名证书(默认)

如果 certificateprivate_key 选项均留空或被注释掉,Telegraf 将在每次启动时在临时目录中自动生成一个自签名证书。

这些证书会在每次 Telegraf 重启时重新创建,每次都需要 OPC UA 服务器重新授权。这适用于测试,但不推荐用于生产环境。

为了在重启后保持相同的客户端身份,请同时指定 certificateprivate_key 的路径。如果文件不存在,Telegraf 将在指定位置生成它们,并在后续重启时重复使用它们。

确保 Telegraf 对指定的路径具有写入权限。首次运行时,Telegraf 将生成证书并记录其位置。后续重启时,Telegraf 将重复使用现有证书,从而无需在服务器的信任存储中重新授权客户端。

手动证书管理(生产)

对于生产环境,请使用组织的 PKI 基础设施手动生成和部署证书。在启动 Telegraf 之前,将证书和私钥文件放置在配置好的路径。如果两个文件都存在,Telegraf 将在不进行修改的情况下使用它们。

证书验证规则

  • certificateprivate_key 必须一起指定,或者两者都留空。
  • 如果一个文件存在而另一个不存在,Telegraf 将返回错误。

节点配置

OPC UA 节点 ID 可能类似于:“ns=3;s=Temperature”。在此示例中:

  • ns=3 表示 namespace 是 3
  • s=Temperature 表示 identifier_type 是字符串,identifier 值为 ‘Temperature’
  • 这个示例温度节点的值为 79.0

要从该节点收集数据,请在上面的 'nodes' 属性中输入以下行:

{field_name="temp", namespace="3", identifier_type="s", identifier="Temperature"},

此节点配置会产生类似这样的指标:

opcua,id=ns\=3;s\=Temperature temp=79.0,Quality="OK (0x0)" 1597820490000000000

在附加指标中输入 'DataType',此节点配置会产生类似这样的指标:

opcua,id=ns\=3;s\=Temperature temp=79.0,Quality="OK (0x0)",DataType="Float" 1597820490000000000

如果值为数组,则使用索引键将每个元素解包到一个字段中。例如:

opcua,id=ns\=3;s\=Temperature temp[0]=79.0,temp[1]=38.9,Quality="OK (0x0)",DataType="Float" 1597820490000000000

命名空间索引与命名空间 URI

OPC UA 支持两种指定命名空间的方式:

  1. 命名空间索引 (namespace):一个整数(0-3 或更高),它引用服务器命名空间数组中的一个位置。这更简单,但如果服务器重启或重新配置,它可能会发生变化。

  2. 命名空间 URI (namespace_uri):一个字符串 URI,用于唯一标识命名空间。这在服务器重启后更稳定,但需要插件从服务器获取命名空间数组以将 URI 解析为索引。

何时使用命名空间索引

  • 对于标准的 OPC UA 命名空间(0 = OPC UA,1 = 本地服务器)
  • 当命名空间稳定性不是问题时
  • 为了更简单的配置

何时使用命名空间 URI

  • 当您需要在服务器重启之间保持一致的节点引用时
  • 在命名空间索引可能发生变化的生产环境中
  • 处理供应商特定的命名空间时

使用命名空间 URI 的示例

[[inputs.opcua.nodes]]
  name = "ServerStatus"
  namespace_uri = "http://opcfoundation.org/UA/"
  identifier_type = "i"
  identifier = "2256"

这会在内部产生与以下内容相同的节点 ID:

[[inputs.opcua.nodes]]
  name = "ServerStatus"
  namespace = "0"
  identifier_type = "i"
  identifier = "2256"

注意:您必须指定 namespacenamespace_uri,但不能同时指定两者。

组配置

组可以为命名空间(索引或 URI)、标识符类型和标签设置设置默认值。默认值适用于组中的所有节点。如果设置了默认值,节点可以完全省略该设置。这简化了节点配置,尤其是在许多节点共享相同的命名空间或标识符类型时。

输出指标将包括在组和节点中设置的标签。如果在两个地方设置了同名的标签,则使用来自节点的标签值。

此示例组配置包含三个组,每个组有两个节点:

  # Group 1
  [[inputs.opcua.group]]
    name = "group1_metric_name"
    namespace = "3"
    identifier_type = "i"
    default_tags = { group1_tag = "val1" }
    [[inputs.opcua.group.nodes]]
      name = "name"
      identifier = "1001"
      default_tags = { node1_tag = "val2" }
    [[inputs.opcua.group.nodes]]
      name = "name"
      identifier = "1002"
      default_tags = {node1_tag = "val3"}

  # Group 2
  [[inputs.opcua.group]]
    name = "group2_metric_name"
    namespace = "3"
    identifier_type = "i"
    default_tags = { group2_tag = "val3" }
    [[inputs.opcua.group.nodes]]
      name = "saw"
      identifier = "1003"
      default_tags = { node2_tag = "val4" }
    [[inputs.opcua.group.nodes]]
      name = "sin"
      identifier = "1004"

  # Group 3
  [[inputs.opcua.group]]
    name = "group3_metric_name"
    namespace = "3"
    identifier_type = "i"
    default_tags = { group3_tag = "val5" }
    nodes = [
      {name="name", identifier="1001"},
      {name="name", identifier="1002"},
    ]

服务器证书信任

当使用安全模式(Sign 或 SignAndEncrypt)连接到具有自签名证书的 OPC UA 服务器时,您需要明确信任服务器的证书。使用 remote_certificate 选项指定服务器证书文件的路径。

大多数 OPC UA 服务器通过其管理界面或配置目录提供其证书。请查阅您的 OPC UA 服务器文档以查找证书,通常可以在服务器的 PKI(公钥基础设施)目录中找到。或者,您可以使用 OPC UA 客户端工具导出证书。

连接服务

此插件主动读取以从 OPC 服务器检索数据。这是每 interval 执行一次的。

Metrics

此输入插件收集的指标将取决于配置的 nodesgroup

示例输出

group1_metric_name,group1_tag=val1,id=ns\=3;i\=1001,node1_tag=val2 name=0,Quality="OK (0x0)" 1606893246000000000
group1_metric_name,group1_tag=val1,id=ns\=3;i\=1002,node1_tag=val3 name=-1.389117,Quality="OK (0x0)" 1606893246000000000
group2_metric_name,group2_tag=val3,id=ns\=3;i\=1003,node2_tag=val4 Quality="OK (0x0)",saw=-1.6 1606893246000000000
group2_metric_name,group2_tag=val3,id=ns\=3;i\=1004 sin=1.902113,Quality="OK (0x0)" 1606893246000000000

此页面是否有帮助?

感谢您的反馈!


InfluxDB 3.8 新特性

InfluxDB 3.8 和 InfluxDB 3 Explorer 1.6 的主要增强功能。

查看博客文章

InfluxDB 3.8 现已适用于 Core 和 Enterprise 版本,同时发布了 InfluxDB 3 Explorer UI 的 1.6 版本。本次发布着重于操作成熟度,以及如何更轻松地部署、管理和可靠地运行 InfluxDB。

更多信息,请查看

InfluxDB Docker 的 latest 标签将指向 InfluxDB 3 Core

在 **2026 年 2 月 3 日**,InfluxDB Docker 镜像的 latest 标签将指向 InfluxDB 3 Core。为避免意外升级,请在您的 Docker 部署中使用特定的版本标签。

如果使用 Docker 来安装和运行 InfluxDB,latest 标签将指向 InfluxDB 3 Core。为避免意外升级,请在您的 Docker 部署中使用特定的版本标签。例如,如果使用 Docker 运行 InfluxDB v2,请将 latest 版本标签替换为 Docker pull 命令中的特定版本标签 — 例如

docker pull influxdb:2