Search code examples
jsonbashjqinfluxdbtelegraf

dynamic use of jq json for http request


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"

Solution

  • 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:

    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