如何正确使用telegraf的inputs.http插件来获取所需的json数据

问题描述 投票:0回答:1

我从 url 获取 json 数据,并希望将 telegrafinput.http 插件一起使用。 我来自 url 的 json 数据:

{
"instance-id": "DEV-MLFF-ANPR",
"api-version": "2.1",
"devices:cameras:status": {
    "framerates": [
        "23.8095",
        "23.8095",
        "25",
        "25",
        "25",
        "25",
        "4.65116"
    ],
    "names": [
        "front_cam_1",
        "front_cam_2",
        "rear_cam_1",
        "rear_cam_2",
        "side_cam",
        "front_cam_3",
        "rear_cam_3"
    ],
    "statuses": [
        "live",
        "live",
        "live",
        "live",
        "live",
        "live",
        "live"
    ],
    "timestamps": [
        "1730383570132",
        "1730383570132",
        "1730383570132",
        "1730383570132",
        "1730383570132",
        "1730383570132",
        "1730383570132"
    ],
    "module-id": "devices:cameras:status",
    "timestamp": "1730383573830",
    "duration": "60168"
},
"lpr:app:cpu-load": {
    "load": "286.12740780135948",
    "module-id": "lpr:app:cpu-load",
    "timestamp": "1730383573825",
    "duration": "60168"
}
}

我尝试了几个配置文件,并使用此文档,但没有成功。我没有通过 telegraf 测试:

telegraf --config /my_cam.conf --test
。更准确地说,测试通过了,但没有数据发送到输出文件。 我的 my_cam.conf 文件:

  [inputs]
  [[inputs.http]]
  urls = [ "http://{my_api_server_ip}/monitor/system/status" ]
  method = "GET"
  name_override = "cam_data"
  data_format = "json_v2"
      [[inputs.http.json_v2]]
      [[inputs.http.json_v2.object]]
        path = "instance-id"
        field_keys = ["instance-id"]
        name_override = "instance_id"
        #timestamp_key = ""
      [[inputs.http.json_v2.object]]
        path = "api-version"
        field_keys = ["api-version"]
        name_override = "api_version"
      [[inputs.http.json_v2.object]]
        path = "devices:cameras:status"
        field_keys = ["devices:cameras:status.framerates.0"]
        name_override = "front_cam_1_framerates"
  success_status_codes = [200]

但是这个配置对于测试来说是正确的,但它不是返回我需要的数据:instance-id、api-version 和 devices:cameras:status.framerates.0 或 1。我也使用这个网站来检查路径 - https ://gjson.dev/,但无法获取所有 3 个值,我知道如何仅获取根值:

[api-version,instance-id]
我的主要目标:仅从 api 获取几个键。我做错了什么?我没有找到任何简单 json 的示例并使用 json_v2,因为我找到了示例,但我意识到我可能需要简单的 json 我已经陷入了死胡同

json influxdb telegraf
1个回答
0
投票

我决定。谢谢大家的帮助 我使用格式数据json_v2,我的配置看起来是这样的:

[inputs]
[[inputs.http]]
# this section working and return data
  urls = [ "http://{my_api_server_ip}/monitor/system/status" ]
  method = "GET"
  name_override = "device_info"
  #json_string_fields = ["instance-id", "api-version"]
  data_format = "json_v2"
  [[inputs.http.json_v2]]
    [[inputs.http.json_v2.field]]
      path = "instance-id"
      type = "string"
      optional = true
   [[inputs.http.json_v2.field]]
      path = "api-version"
      type = "string"
  success_status_codes = [200]
  [inputs.http.tags]
    devid = "64"
[[inputs.http]]
# this section IS working NOW and return data
  urls = [ "http://{my_api_server_ip}/monitor/system/status" ]
  method = "GET"
  name_override = "device_data_status"
  tagexclude = ["url", "host"]
  data_format = "json_v2"
  [[inputs.http.json_v2]]
    [[inputs.http.json_v2.field]]
      path = "devices:cameras:status.framerates.0"
      type = "string"
      rename = "front_cam_1_framerates"
      optional = true
  success_status_codes = [200]
  [inputs.http.tags]
    devid = "64"

[[inputs.http]]
# this section working and return data
  urls = [ "http://{my_api_server_ip}/monitor/system/status" ]
  method = "GET"
  name_override = "device_data_mem"
  data_format = "json"
  json_string_fields = ["mem-active", "mem-available", "mem-free", "mem-total", "swap-free", "swap-total"]
  json_query = "system:memory:usage"
  success_status_codes = [200]
  [inputs.http.tags]
    devid = "64"
© www.soinside.com 2019 - 2024. All rights reserved.