我一直在尝试找到一种方法来确保我可以将这个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"
2条答案
按热度按时间mwngjboj1#
使用
to_entries
函数将您的条目转换为包含key
和value
成员的字典列表,这样您就有了一个更容易过滤的数据结构。也就是说,运行
jq to_entries data.json
(其中data.json
包含示例数据)将生成如下输出:我们可以从中提取所需的键和值,然后使用
@tsv
过滤器生成更易于在shell中使用的输出。产出:
我们可以使用
read
shell函数读取值:它产生:
7y4bm7vi2#
可以使用
to_entries
将项拆分为键-值对数组,然后使用字符串插值将输出字符串拼凑在一起:Demo