The return
Statement
The previous expression we looked at had a small limitation: once it triggered a WARNING
alert, that alert would stay open indefinitely, even if the memory usage returned to normal. You'd have to close it manually or remove the condition.
Ideally, we want alerts to automatically resolve when the issue is fixed.
Technically, InfraSonar implicitly adds an empty return
at the end of your expression. It's like this:
// Test if memory usage exceeds 80 percent.
case item.memory_usage_ratio > 0.8:
return WARNING, "Memory usage of @item.name exceeds 80%"
return // Implicit return, means no change in alert state if condition is false
To automatically close the alert when memory usage drops back below 80%, we can explicitly tell InfraSonar to return an OK
status. This signals InfraSonar to close any open alerts for that condition.
Here's how we'd modify the expression:
// Test if memory usage exceeds 80 percent.
case item.memory_usage_ratio > 0.8:
return WARNING, "Memory usage of @item.name exceeds 80%"
return OK // Explicitly return OK if memory usage is 80% or below
With this change, if memory usage falls to 80% or below, the expression will evaluate to OK
, and an active alert generated by this specific expression for that item will automatically close.
Multiple return
Statements and Alert Threshholds
InfraSonar expressions support multiple return
statements, giving you fine-grained control over alert severities and allowing for "dead zones" where no state change occurs. This is crucial for preventing "flapping" alerts – where an alert rapidly opens and closes due to minor fluctuations around a single threshold.
Consider a scenario where memory usage hovers around an 80% warning threshold (e.g., 79.99% then 80.01%). Without a buffer, this would constantly trigger and resolve alerts, creating unnecessary noise.
To prevent this, we can introduce a lower "clear" threshold. Here's an example that defines different alert levels and a resolution window:
// Test if memory usage exceeds 80 percent.
case item.memory_usage_ratio > 0.9:
return ERROR, "Memory usage of @item.name exceeds 90%"
case item.memory_usage_ratio > 0.8:
return WARNING, "Memory usage of @item.name exceeds 80%"
case item.memory_usage_ratio < 0.78:
return OK // Explicitly return OK if memory usage is 78% or below
Only when the memory usage drops below 78% does the return OK
statement get hit, which will then explicitly close any open alerts generated by this expression for that item.