Monitor the state - JSON data - body mismatch

heartbeat.yml

heartbeat.monitors:

  • type: http
    urls: ["https://x.x.x.x:xxx/x/x/x"]
    ssl.verification_mode: none
    check.request:
    method: GET
    headers:
    'x-auth-token': 'xxxxx'
    check.response:
    status: 200
    body: '{"state": "copy"}'
    schedule: '@every 10s'

When I use the following curl cmd I get the json response
curl -k --request GET --url https://x.x.x.x:xxx/x/x/x --header 'x-auth-token: xxxx'

Json response

{
"server": {
"status": "ok",
"code": "",
"message": "Operation done successfully."
},
"counts": {
"data_counts": 21,
"total_counts": 21
},
"data": {
"xxx": [
{
"sourcevolume": {
"id": "xxx",
"link": {
"rel": "self",
"href": "https://x.x.x.x:xxx/x/x/x"
}
},
"targetvolume": {
"id": "xxx",
"link": {}
},
"targetsystem": {
"id": "xxx",
"link": {}
},
"type": "globalcopy",
"state": "copy"
},
]
}
}

I get body mismatch error, how do I use the json data 'state' in body?

Hi @shiv94,

The check you are trying to do looks for the string in plain text, so it is looking literally for {"state": "copy"}, what is not present as is in the response body.
This would probably work if you remove the curly brackets:

check.response:
  status: 200
    body: 
      - '"state": "copy"'

Notice also that the body field needs to be defined as a list that contains a list of expressions.

Starting on heartbeat 6.6.0 (see #8667) it will be also possible to use specific JSON checks. Though your case would need quite complex conditions (not sure if even possible) as this state field is inside a list and an object of unknown name.

But you will be able to check for example the server status with:

  check.response:
    status: 200
    json:
      - description: 'check server status' 
        condition:
          equals:
            status: ok

Heartbeat 7.0.0-alpha1 pre-release already contains this feature in case you want to give it a try :wink:

1 Like

Thanks for your response @jsoriano

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.