I have been trying to find a way to make sure I can pass the key and values of this json in this link dynamically into influxDB. I have the batch file that is below :
The json file is below from that link :
{
"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)"
}
The batch file :
#!/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
The output is
app_custom,wblb_status=UP wlb_status_code=1
I do not want to each key with if but would like to have the output like this below
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
......
I would want to use this to pass to the influx DB using this conf :
[[inputs.exec]]
commands = ["/etc/telegraf/telegraf.d/app_test.sh"]
data_format = "influx"
timeout = "30s"
interval = "2m"
2
Answers
You can use
to_entries
to split up the items into an array of key-value pairs, then string interpolation to piece together the output strings:Demo
Use the
to_entries
function to transform your entries into a list of dictionaries withkey
andvalue
members, and then you have a data structure that is much easier to filter.That is, running
jq to_entries data.json
(wheredata.json
contains your sample data) produces output like:We can extract the keys and values we want from that, and then use the
@tsv
filter to generate output that’s easier to work with in the shell. The followingjq
command line:Produces as output:
We can read values using the
read
shell function:Which produces: