简体   繁体   English

使用 Telegraf 解析 JSON 的问题

[英]Problems parsing JSON with Telegraf

I want to collect data from a swagger API using Telegraf我想使用 Telegraf 从 swagger API 收集数据

this is the DS definition in my telegraf.conf这是我的 Telegraf.conf 中的 DS 定义

[[inputs.httpjson]]
  ## NOTE This plugin only reads numerical measurements, strings and booleans
  ## will be ignored.

  ## Name for the service being polled.  Will be appended to the name of the
  ## measurement e.g. "httpjson_webserver_stats".
  ##
  ## Deprecated (1.3.0): Use name_override, name_suffix, name_prefix instead.
  name = "ultimaker_status"

  ## URL of each server in the service's cluster
  servers = [
    "http://192.168.5.15/api/v1/printer/network",
  ]
  ## Set response_timeout (default 5 seconds)
  response_timeout = "15s"

  ## HTTP method to use: GET or POST (case-sensitive)
  method = "GET"

  ## Tags to extract from top-level of JSON server response.
   tag_keys = [
     "ethernet:connected"
   ]

  ## Optional TLS Config
  # tls_ca = "/etc/telegraf/ca.pem"
  # tls_cert = "/etc/telegraf/cert.pem"
  # tls_key = "/etc/telegraf/key.pem"
  ## Use TLS but skip chain & host verification
  # insecure_skip_verify = false

  ## HTTP Request Parameters (all values must be strings).  For "GET" requests, data
  ## will be included in the query.  For "POST" requests, data will be included
  ## in the request body as "x-www-form-urlencoded".
  # [inputs.httpjson.parameters]
  #   event_type = "cpu_spike"
  #   threshold = "0.75"

  ## HTTP Request Headers (all values must be strings).
  # [inputs.httpjson.headers]
  #   X-Auth-Token = "my-xauth-token"
  #   apiVersion = "v1"

The HTTP request to http://192.168.5.15/api/v1/printer/network returns the following stringhttp://192.168.5.15/api/v1/printer/network的 HTTP 请求返回以下字符串

{"ethernet": {"connected": true, "enabled": true}, "wifi": {"connected": false, "enabled": false, "mode": "CABLE", "ssid": "UM-NO-HOTSPOT-NAME-SET"}, "wifi_networks": []} {“以太网”:{“连接”:真,“启用”:真},“wifi”:{“连接”:假,“启用”:假,“模式”:“电缆”,“ssid”:“UM -NO-HOTSPOT-NAME-SET"}, "wifi_networks": []}

Telegraf --test returns the following Telegraf --test 返回以下内容

httpjson_ultimaker_status,host=dmon-virtual-machine,server= http://192.168.5.15/api/v1/printer/network response_time=7.333277032 1551105770000000000 httpjson_ultimaker_status,host=dmon-virtual-machine,server= http://192.168.5.15/api/v1/printer/network response_time=7.333277032 1551105770000000000

I would expect the string "connected" not 1551105770000000000我希望字符串“连接”不是 1551105770000000000

How can I get the expected result我怎样才能得到预期的结果

Thanks for your help谢谢你的帮助

In the telegraf.conf file in the beginning of the block with the json plugin it says:在带有 json 插件的块开头的 telegraf.conf 文件中,它说:
"NOTE This plugin only reads numerical measurements, strings and booleans will be ignored." “注意此插件仅读取数值测量值,字符串和布尔值将被忽略。”

JSON String are ignored unless specified in the tag_key or json_string_fields options.除非在 tag_key 或 json_string_fields 选项中指定,否则忽略 JSON 字符串。

May be you can try, something like也许你可以尝试,比如

tag_keys = ["ethernet_connected"] json_string_fields = ["ethernet_connected"] tag_keys = ["ethernet_connected"] json_string_fields = ["ethernet_connected"]

More you can find at https://github.com/influxdata/telegraf/tree/master/plugins/parsers/json您可以在https://github.com/influxdata/telegraf/tree/master/plugins/parsers/json找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM