Monitoring Multiple Metrics with Distinct Thresholds

Recently, I had a use case where I needed to monitor two metrics plotted on a tile and raise alert when either of them crossed their respective thresholds. You can do it today from the UI using Threshold Monitoring option but can’t specify separate threshold values to each metric. So, I used a Monitor Condition Script to evaluate them both against specific threshold conditions to determine overall health.

Data I Wanted to Monitor:

Here’s the grid view of performance data for a virtual machine (VM), pulled from an Azure Data Source.

The goal is to use a script that could evaluate both independently and return their respective states and scalar values.

Steps:

  1. Add or Edit a Dashboard: Edit the dashboard where you want to add the monitoring or create a new one.
  2. Configure the Data: Set up the tile to fetch the metrics you want to monitor (e.g., Available Memory Bytes and Percentage CPU).
  3. Enable Monitoring: Go to the Monitoring tab. Toggle the Monitoring option to On.
  4. Select Monitor Type: Set the Monitor Type to Script.
  5. Choose Evaluate by: Under Evaluate by, select a column if you want the script to run once per unique value in that column. I selected ‘Metric’ as I wanted it to run for each Metric.
  6. Add Your Script: Click Add under Script section. A new window opens with a sample script that you can customize to fit your use case.

Here’s the modified script:

async function getState(params, api) {
    // Get all counter names
    const counters = (await api.getColumnData(params.data, 'metric')).map(row => row.value);
    
    let state = 'unknown';
    let scalar = 0;

    for (const counter of counters) {
        // Get average metric values for each counter
        const metrics = (await api.getColumnData(params.data, 'data')).map(row => row.value);
        const counterConfig = params.config[counter] || {};

        const errorMore = counterConfig.errorIfMoreThan;
        const warnMore = counterConfig.warnIfMoreThan;
        const errorLess = counterConfig.errorIfLessThan;
        const warnLess = counterConfig.warnIfLessThan;

        const counterScalar = Math.max(...metrics);

        // Error condition
        if (
            (typeof errorMore === 'number' && counterScalar > errorMore) ||
            (typeof errorLess === 'number' && counterScalar < errorLess)
        ) {
            state = 'error';
            scalar = counterScalar;
            break;
        }

        // Warning condition
        if (
            (typeof warnMore === 'number' && counterScalar > warnMore) ||
            (typeof warnLess === 'number' && counterScalar < warnLess)
        ) {
            if (state !== 'error') {
                state = 'warning';
                scalar = counterScalar;
            }
        }

        // Success condition
        if (!Number.isNaN(counterScalar) && state === 'unknown') {
            state = 'success';
            scalar = counterScalar;
        }
    }

    return { state, scalar };
}

Just a quick note: ‘metric’ and ‘data’ names in the script must be replaced with original column names. Once that’s set, head back to the tile editor.
8. Script configuration: params.config section can be used to pass custom settings into your script like threshold values for each counter.
In my case, params.config looked like this:

{
  "Available Memory Percentage": {
    "warnIfLessThan": 70,
    "errorIfLessThan": 65
  },
  "Percentage CPU": {
       "warnIfMoreThan": 50,
    "errorIfMoreThan": 75      
  
  }
}

Available Memory Percentage: Warning below 70, error below 65.
Percentage CPU: Warning over 50, error over 75.
The Final Health States can be viewed in the Preview.

The most severe health State will automatically roll up to the dashboard and workspace, ensuring issues are visible immediately.

In my case, the script implements the OR logic, meaning that the alert will trigger when either of the thresholds are met, but you can also write the script in the way that it triggers alert when both the thresholds are met.

2 Likes