动态使用jq json进行http请求

pkwftd7m  于 2022-12-24  发布在  其他
关注(0)|答案(2)|浏览(106)

我一直在尝试找到一种方法来确保我可以将这个json的键和值动态地传递到influxDB中。我有下面的批处理文件:
json文件在下面从该链接:

{
  "status": "UP",
  "WBAD": {
    "status": "UP",
    "count": "0",
    "minDateTime": "",
    "description": "Workbasket Admin"
  },
  "WBCA": {
    "status": "UP",
    "count": "0",
    "minDateTime": "",
    "description": "Workbasket CreateAppWait"
  },
  "WBDE": {
    "status": "UP",
    "count": "0",
    "minDateTime": "",
    "description": "Workbasket default@"
  },
  "WBEW": {
    "status": "UP",
    "count": "0",
    "minDateTime": "",
    "description": "Workbasket eFormWriteFailure"
  },
  "WBFB": {
    "status": "UP",
    "count": "0",
    "minDateTime": "",
    "description": "Workbasket BackgroundProcessing"
  },
  "WBIC": {
    "status": "UP",
    "count": "0",
    "minDateTime": "",
    "description": "Workbasket IncompleteConnections"
  },
  "WBLB": {
    "status": "UP",
    "count": "17",
    "minDateTime": "23/12/2022 14:50",
    "description": "Workbasket LRBackgroundProcess"
  },
  "AEWB": {
    "status": "UP",
    "count": "0",
    "minDateTime": "",
    "description": "Assignment errors for Assign-WorkBasket"
  },
  "AEWL": {
    "status": "UP",
    "count": "0",
    "minDateTime": "",
    "description": "Assignment errors for Assign-Worklist"
  },
  "FEWB": {
    "status": "UP",
    "count": "0",
    "minDateTime": "",
    "description": "Flow errors for Assign-WorkBasket"
  },
  "FEWL": {
    "status": "UP",
    "count": "0",
    "minDateTime": "",
    "description": "Flow errors for Assign-Worklist"
  },
  "BQBP": {
    "status": "UP",
    "count": "0",
    "minDateTime": "",
    "description": "Broken queue System-Queue-BackgroundProcess"
  },
  "BQDE": {
    "status": "UP",
    "count": "0",
    "minDateTime": "",
    "description": "Broken queue System-Queue-DefaultEntry"
  },
  "CQCA": {
    "status": "UP",
    "count": "0",
    "minDateTime": "",
    "description": "Custom query Create App Requests"
  },
  "JS02": {
    "status": "UP",
    "description": "Job Scheduler CaseDocumentDeletion (Any one associated node:BackgroundProcessing)"
  },
  "JS04": {
    "status": "UP",
    "description": "Job Scheduler PurgeOldSIDExchangeRecords (All associated nodes:BackgroundProcessing)"
  },
  "JS01": {
    "status": "UP",
    "description": "Job Scheduler UpdateReferenceData (Any one associated node:BackgroundProcessing)"

}

批处理文件:

#!/bin/bash  
## GET  STATUS
WBLB_STATUS=`curl -s http://example:8080/Cluster | jq -r '.WBLB.status'`

WBCA_STATUS=`curl -s http://example:8080/Cluster | jq -r '.WBCA.status'`
       
if [ "$WBLB_STATUS" = "UP" ]; then
    echo "app_custom,wblb_status="UP" wlb_status_code=1"

elif [ "$WBLB_STATUS" = "DOWN" ]; then
    echo "app_custom,wblb_status="DOWN" wblb_status_code=0"
    
fi    
exit

输出为

app_custom,wblb_status=UP wlb_status_code=1

我不希望每个键都带有if,但希望输出如下所示

app_custom,app_wblb_status=UP wlb_status_code=1
app_custom,app_wblc_status=UP wlb_status_code=1
app_custom,app_wbad_status=UP wlb_status_code=1
......

我想使用这个conf将它传递给influx DB:

[[inputs.exec]]
  commands = ["/etc/telegraf/telegraf.d/app_test.sh"]
  data_format = "influx"
  timeout = "30s"
  interval = "2m"
mwngjboj

mwngjboj1#

使用to_entries函数将您的条目转换为包含keyvalue成员的字典列表,这样您就有了一个更容易过滤的数据结构。
也就是说,运行jq to_entries data.json(其中data.json包含示例数据)将生成如下输出:

[
  {
    "key": "status",
    "value": "UP"
  },
  {
    "key": "WBAD",
    "value": {
      "status": "UP",
      "count": "0",
      "minDateTime": "",
      "description": "Workbasket Admin"
    }
  },
  ...

我们可以从中提取所需的键和值,然后使用@tsv过滤器生成更易于在shell中使用的输出。

jq -r 'to_entries[]|select(.key != "status")|[.key, .value.status]|@tsv' data.json

产出:

WBAD    UP
WBCA    UP
WBDE    UP
WBEW    UP
WBFB    UP
WBIC    UP
WBLB    UP
AEWB    UP
AEWL    UP
FEWB    UP
FEWL    UP
BQBP    UP
BQDE    UP
CQCA    UP
JS02    UP
JS04    UP
JS01    UP

我们可以使用read shell函数读取值:

#!/bin/bash

jq -r 'to_entries[]|select(.key != "status")|[.key, .value.status]|@tsv' data.json |
while read key status; do
    [[ $status = "UP" ]] && code=1 || code=0
    echo "app_custom,${key,,}_status=\"$status\" wlb_status_code=$code"
done

它产生:

app_custom,wbad_status="UP" wlb_status_code=1
app_custom,wbca_status="UP" wlb_status_code=1
app_custom,wbde_status="UP" wlb_status_code=1
app_custom,wbew_status="UP" wlb_status_code=1
app_custom,wbfb_status="UP" wlb_status_code=1
app_custom,wbic_status="UP" wlb_status_code=1
app_custom,wblb_status="UP" wlb_status_code=1
app_custom,aewb_status="UP" wlb_status_code=1
app_custom,aewl_status="UP" wlb_status_code=1
app_custom,fewb_status="UP" wlb_status_code=1
app_custom,fewl_status="UP" wlb_status_code=1
app_custom,bqbp_status="UP" wlb_status_code=1
app_custom,bqde_status="UP" wlb_status_code=1
app_custom,cqca_status="UP" wlb_status_code=1
app_custom,js02_status="UP" wlb_status_code=1
app_custom,js04_status="UP" wlb_status_code=1
app_custom,js01_status="UP" wlb_status_code=1
7y4bm7vi

7y4bm7vi2#

可以使用to_entries将项拆分为键-值对数组,然后使用字符串插值将输出字符串拼凑在一起:

curl -s http://example:8080/Cluster | jq -r '
  to_entries[] | select(.value | type == "object")
  | [.key, .value.status] as [$id, $status]
  | "app_custom,\($id | ascii_downcase)_status=\($status) wlb_status_code=\(["DOWN","UP"] | index($status))"
'
app_custom,wbad_status=UP wlb_status_code=1
app_custom,wbca_status=UP wlb_status_code=1
app_custom,wbde_status=UP wlb_status_code=1
app_custom,wbew_status=UP wlb_status_code=1
app_custom,wbfb_status=UP wlb_status_code=1
app_custom,wbic_status=UP wlb_status_code=1
app_custom,wblb_status=UP wlb_status_code=1
app_custom,aewb_status=UP wlb_status_code=1
app_custom,aewl_status=UP wlb_status_code=1
app_custom,fewb_status=UP wlb_status_code=1
app_custom,fewl_status=UP wlb_status_code=1
app_custom,bqbp_status=UP wlb_status_code=1
app_custom,bqde_status=UP wlb_status_code=1
app_custom,cqca_status=UP wlb_status_code=1
app_custom,js02_status=UP wlb_status_code=1
app_custom,js04_status=UP wlb_status_code=1
app_custom,js01_status=UP wlb_status_code=1

Demo

相关问题