Custom Monitoring Logic for Health Dashboard

I was testing a monitoring approach recently that involved tracking the health of several objects on a tile and return an error state only when the number of objects in an “error” state passed a defined threshold.

For this, I’ve used a custom script that analyzes the data.state (state) field of each object. It calculates the number of objects in the “error” state and compares this count against a user-defined threshold (errorIfMoreThan). If the number of error objects exceeds this threshold, the monitor returns an “error” state; otherwise, it returns “success”. Additionally, the error count is returned as a scalar value for display.

Configuration is simple. The threshold is defined in the Config section, for example:

{
“errorIfMoreThan”: 2
}

Script:

async function getState(params, api) {
    const metrics = (await api.getColumnData(params.data, 'data.state'))
        .map(row => String(row.value).toLowerCase().trim());
    const errorCount = metrics.filter(state => state === 'error').length;
    const errorThreshold = params.config.errorIfMoreThan;
    let state = 'unknown';
    if (metrics.length === 0) {
        state = 'unknown';
    } else if (errorCount > errorThreshold) {
        state = 'error';
    } else {
        state = 'success';
    }
    return { state, scalar: errorCount };
}