State monitoring of external service via HTTP request
This tutorial will guide you through creating a script that periodically checks whether an external HTTP service is available. The script runs every 5 seconds and reports the service’s status as a boolean value (true for available, false for unavailable).
Define a boolean value and link it to a script via
owner.scripts.Schedule the script to execute every 5 seconds using
detached.scripts.Send an HTTP GET request in JavaScript and set the value to true or false based on the response.
git checkout origin/osp-scripts-configuration .
git checkout origin/example-scripts-external-system-monitoring .
Prerequisites
A website that responds to HTTP requests.
Steps
1. Create a script that generates an integer value and runs every 5 seconds.
root/examples/scripts/external-service/status/value.ospp
{
"name": "Service status",
"description": "The status of an external service",
"type": "BOOLEAN"
}
root/examples/scripts/external-service/status/owner.scripts
{
"moduleId": "modules.scripts.scripts-1",
"scheduledExecutions": ["0/5 * * ? * * *"],
"accessedValues": [],
"sourceFile": "root/scripts/external-service/status/script.js"
}
2. Create a script that sends an HTTP GET request to an external service and retrieves the response.
root/examples/scripts/external-service/status/script.js
main();
function main() {
let result = http.doGet("http://localhost:8080/person/1");
if (!result.isSuccess()) {
log.warn("The service is unavailable cause [{}].", result.getError());
}
return result.isSuccess();
}