jsoninfluxdbtelegraf

How correctly use inputs.http plugin for telegraf to get needed json data


I get json data from url and want to use telegraf with input.http plugin. My json data from urls:

{
"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"
}
}

I try several config files, and use this documentation, but have no success. I did't pass telegraf test: telegraf --config /my_cam.conf --test. To be more precise, the test passes, but no data is sent to the output file. my my_cam.conf file:

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

But this config is correctly for test, but it is not return data what i need: instance-id, api-version and devices:cameras:status.framerates.0 or 1. I also use this site for checking path - https://gjson.dev/, but can't to get all 3 values, i know how get only roots values: [api-version,instance-id] My main target: getting only several keys from api. What am i doing wrong? I didn't find any example for simple json and use json_v2, because i found example, but i i realize that i need simple json, perhaps I'm at a dead end


Solution

  • I decide. Thanks everybody for help I use format data json_v2 and my config looks that:

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