简体   繁体   中英

Problems parsing JSON with Telegraf

I want to collect data from a swagger API using Telegraf

this is the DS definition in my telegraf.conf

[[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 string

{"ethernet": {"connected": true, "enabled": true}, "wifi": {"connected": false, "enabled": false, "mode": "CABLE", "ssid": "UM-NO-HOTSPOT-NAME-SET"}, "wifi_networks": []}

Telegraf --test returns the following

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

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:
"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.

May be you can try, something like

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

More you can find at https://github.com/influxdata/telegraf/tree/master/plugins/parsers/json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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