07 Nov 2024 06:32 PM
We are monitoring JSON status page for an app, where multiple checks return Healthy or Unhealthy as it's status.
I'm looking to somehow being able to tell which check fails exactly if my "Fail" condition will be "Unhealthy", and then be able to report on history of checks.
I understand that I should be able to do that with post-execution script. Via probably getting html response with response.getResponseBody();, somehow comparing it with my regex, and then returning message that's interesting for me with api.fail(). I'm missing matching examples to build such post-execution script myself. Anyone bothers enough to help me out?
Previously in SiteScope, I could cover that with regex, where while creating a regex condition, I could set a group with () that would be returned with error message.
Example response body:
{
"First": {
"status": "Healthy",
},
"Second": {
"status": "Unhealthy",
},
"Third": {
"status": "Healthy",
},
}
Solved! Go to Solution.
07 Nov 2024 09:47 PM - edited 12 Nov 2024 08:54 PM
@mariusz_kuczeba - not the perfect one , but an example you can use
var responseBody = response.getResponseBody();
var data = JSON.parse(responseBody);
var unhealthyNodes = [];
// Iterate through the nodes for unhealthy
for (var node in data) {
if (data[node].status === "Unhealthy") {
unhealthyNodes.push(node);
}
}
// If found, fail the request and return the node names
if (unhealthyNodes.length > 0) {
api.fail("Unhealthy nodes found: " + unhealthyNodes.join(","));
} else {
api.info("All nodes are healthy.");
}
12 Nov 2024 03:18 PM
There seem to be some issue in the script (always returns "All nodes are healthy."), but it's a great starting point for my anyway. Thanks!