Counter Group Monitor
BACKEND SCRIPT
Monitor counter group activity.
Get called when metrics related to a counter group are computed and stored.
Structure
Counter Group Monitor skeleton script
Table cg_monitor
The table consists the following
| field | type | description |
|---|---|---|
| counter_guid | String, function returning string, or array of strings/functions | Counter group to attach to. A single value attaches one isolated Lua instance (unchanged). An array creates one isolated instance per GUID. |
| counter_name_match | String or array of strings | Case-insensitive substring match on counter group title. One isolated Lua instance per matched group. |
| counter_name_regex | String (RE2) | RE2 pattern matched against counter group title. One isolated instance per matched group. Can be combined with the fields above (union, deduplicated). |
| onbeginflush | engine , timestamp | Before starting to flush all metrics to db |
| onflushfilter | engine, timestamp, key, arrayofmetrics | Before each key is flushed. Return true to save, false to skip. Optional second return value can override meters. |
| onflush | engine, timestamp, key, arrayofmetrics | Called for each key as they are being flushed |
| onendflush | engine | After all keys have been flushed for this interval |
| onbegintopperflush | engine, timestamp, meter | Before flushing toppers for this meter. Return true to enable ontopperflush callbacks for this meter. |
| ontopperflushfilter | engine, key, metric | Before each topper item is flushed. Return true to save, false to skip. |
| ontopperflush | engine , key, metric | Called for each topper item |
| onendtopperflush | engine , meter | After topper flush |
| onupdate | engine, timestamp, key, arrayofmetrics | As each update happens (1sec resolution) |
| onnewkey | engine, timestamp, key | A new key was discovered within the stream window. |
| onmetronome | engine , timestamp, tick_count, tick_interval | called every second ( Tick Interval) |
Multi-group attachment
One script file can install the same logic onto multiple counter groups. Trisul creates a separate isolated Lua context for each matched group — each runs its own onload() and owns its own T.* state.
Before onload() on each instance, Trisul sets:
| field | description |
|---|---|
T.monitor_group_name | Title of the counter group this instance is bound to |
T.monitor_group_guid | Registry-format GUID string for that group |
See also Object Global table T.
If no counter groups match the attachment spec, the script is not loaded.
Example: attach to two explicit counter groups
cg_monitor = {
counter_guid = {
"{120A3124-E2BB-47BD-6C64-71BBB861C428}", -- Flow-ASN
"{2314BB8E-2BCC-4B86-8AA2-677E5554C0FE}", -- FlowGens
},
onload = function()
T.log("cg_monitor bound to " .. T.monitor_group_name)
end,
onflush = function(engine, ts, key, metrics)
-- called only for keys in this instance's counter group
end,
}
Example: all groups whose name contains "hosts"
cg_monitor = {
counter_name_match = "hosts",
onbeginflush = function(engine, ts)
T.keys_this_interval = {}
end,
onflush = function(engine, ts, key, metrics)
if key ~= "SYS:GROUP_TOTALS" then
T.keys_this_interval[key] = true
end
end,
}
Example: RE2 match on group title
cg_monitor = {
counter_name_regex = "(?i).*(host|subnet).*",
onflush = function(engine, ts, key, metrics)
-- your logic
end,
}
Functions Reference
Function onbeginflush
Purpose
Before a counter group is flushed to the Trisul database on the Hub node. Trisul is a streaming analytics system. By default every 60 seconds the analytics are snapshotted and sent to the database node (hub). The onbeginflush function is therefore called every 60 seconds.
When called
When an flush operation is about to start. The sequence goes
-- operation is invoked every 60 seconds by default
--
onbeginflush(..)
onflush(..)
onflush(..)
onflush(..)
..
..
onendflush(..)
Parameters
| parameter | description | usage notes |
|---|---|---|
| engine | An engine object | use this object to add metrics, counter items, or counter items into the Trisul framework |
| timestamp | Timestamp | Timestamps seconds tv_sec |
Return value
Ignored
Example
Function onflushfilter
Purpose
Control whether a counter item is flushed to the Hub database, and optionally rewrite meter values before flush.
When called
Just before each counter key is flushed to the database for the current interval. If the filter passes, Trisul sends the item to the Hub and then calls onflush.
Parameters
| parameter | description | usage notes |
|---|---|---|
| engine | An engine object | use this object to add metrics, counter items, or alerts into the Trisul framework |
| timestamp | Timestamp | Timestamp in seconds (tv_sec) for this flush interval |
| key | string | the key identifying the counter item |
| arrayofmetrics | array of numbers | array of metrics. array item 1 refers to meter 0 and so forth |
Return value
Return a boolean as the first value:
| return value | description |
|---|---|
| true | flush this counter item to the Hub |
| false | do not flush this counter item |
You may optionally return a second value: an array table of meter overrides. Index 1 corresponds to meter 0, index 2 to meter 1, and so on.
| meter value in table | effect |
|---|---|
>= 0 | replace the meter value with this number before flush |
-1 | leave that meter unchanged |
Example:
-- pass the item, but override meters 4, 5, and 6
return true, { -1, -1, -1, -1, recvutil, xmitutil, recv_xmit_ratio }
If you do not define onflushfilter, the item is flushed with the original meter values.
Voting considerations
If multiple scripts are attached to the same counter group, each script's onflushfilter vote is combined with logical OR:
- All scripts must return false for the item to be skipped.
- If any script returns true, or does not implement
onflushfilter, the item is flushed.
Example
Purpose
Custom processing before each counter item is flushed. Perhaps write to your own tools or logfiles.
When called
Just before each counter item is flushed to the database. The maximum delay between getting a onnewcounter item and a corresponding onflush(..) for that counter item is 60 seconds.
Parameters
| parameter | description | usage notes |
|---|---|---|
| engine | An engine object | use this object to add metrics, counter items, or counter items into the Trisul framework |
| timestamp | Timestamp | Timestamps seconds tv_sec |
| key | string | the key identifying the counter item |
| arrayofmetrics | array of numbers | array of metrics. array item 0 refers to meter 0 and so forth |
Return value
Ignored
Example
Function onflush
Purpose
Custom processing before each counter item is flushed. Perhaps write to your own tools or logfiles.
When called
Just before each counter item is flushed to the database. The maximum delay between getting a onnewcounter item and a corresponding onflush(..) for that counter item is 60 seconds.
Parameters
| parameter | description | usage notes |
|---|---|---|
| engine | An engine object | use this object to add metrics, counter items, or counter items into the Trisul framework |
| timestamp | Timestamp | Timestamps seconds tv_sec |
| key | string | the key identifying the counter item |
| arrayofmetrics | array of numbers | array of metrics. array item 0 refers to meter 0 and so forth |
Return value
Ignored