Hi,

Trying to use a custom rest sensor to monitor an endpoint, but I'm having trouble finding the correct syntax to do calculations.

For example:

This is the JSON returned:

[ { "request": { "path": "used", "mbean": "java.lang:type=Memory", "attribute": "HeapMemoryUsage", "type": "read" }, "value": 505026552, "timestamp": 1674569295, "status": 200 }, { "request": { "path": "max", "mbean": "java.lang:type=Memory", "attribute": "HeapMemoryUsage", "type": "read" }, "value": 4294967296, "timestamp": 1674569295, "status": 200 } ]

This is my template:

{ "prtg": { "result": [ { "channel": "Used Heapmemory", "unit": "Percent", "value": $[?@.request.attribute=="HeapMemoryUsage" && @.request.path=="used"].value * 100 } ] } }

This gives me an error:

invalid operation ([]interface {}) * (float64)

Ultimately I want to calculate a percent, like so:

"value": 100 * $[?@.request.attribute=="HeapMemoryUsage" && @.request.path=="used"].value / $[?@.request.attribute=="HeapMemoryUsage" && @.request.path=="max"].value

But for now I'm just trying to get it to multiply with 100.

This works: "value": $[?@.request.attribute=="HeapMemoryUsage" && @.request.path=="used"].value returns: 505026552

This also works: "value": $[0].value * 100 returns: 50502655200

But the combination gives me the error.

Based on another KB item I've also tried:

"value": $($[?@.request.attribute=="HeapMemoryUsage" && @.request.path=="used"].value) * 100

But alas, same error.

What's the correct syntax here?


Article Comments

Found the answer.

A filter, like this, always returns an array, not a value:

$[?@.request.attribute=="HeapMemoryUsage" && @.request.path=="used"].value

Because more than one element may returned by the filter. And since it’s not possible to multiply an array with a constant you need to do this:

100* $($[?@.request.attribute=="HeapMemoryUsage" && @.request.path=="used"].value)[0]

In other words, select the first element of the array returned by the filter (I designed the filter to only return one element so I know the first one is the one I want).


Jan, 2023 - Permalink