Create an alarm from a script

Description

In this tutorial, you will learn how to create a script that periodically creates a health-check alarm and logs the result of the insertion.

The script will be executed every 10 seconds and generate a new alarm each time.

Configuration structure

@startuml
skinparam backgroundColor transparent

package "root/.../alarm-generator" {
    [detached.scripts] as trigger
}

node ospscripts as "osp-scripts"
node ospalarms as "osp-alarms"

ospscripts -[#black]> trigger : <size:11><color:black> **executes**
trigger -[#firebrick]r-> ospalarms : <size:11><color:firebrick> **Create alarm**
@enduml

Steps

1. Create a detached script that will be executed every hour

root/alarm-generator/detached.scripts

{
    "moduleId": "modules.scripts.scripts-1",
    "scheduledExecutions": ["0/10 * * ? * * *"],
    "accessedValues": [],
    "sourceFile": "root/alarm-generator/script.js"
}

2. Write a script that creates an alarm and logs the result of the insertion

root/alarm-generator/script.js

const CLEAR_SEVERITY = 100;

main();

function main() {
    let timestamp = trigger.content;
    if (!!timestamp) {
        let result = alarms.create(CLEAR_SEVERITY, "Healtcheck alarm " + timestamp, "Internal", "localhost", "" + timestamp, ["Healthcheck"]);
        if (result.success) {
            log.info("Healthcheck alarm successfully inserted at [{}].", timestamp);
        } else {
            log.error("Healthcheck alarm failed to be inserted at [{}].", timestamp);
        }
    }
}