Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::rule_builder Class Reference

Builds alert_rule from rule_definition. More...

#include <alert_config.h>

Collaboration diagram for kcenon::monitoring::rule_builder:
Collaboration graph

Static Public Member Functions

static common::Result< std::shared_ptr< alert_rule > > build (const rule_definition &def)
 Build alert_rule from definition.
 

Static Private Member Functions

static common::Result< alert_severityparse_severity (const std::string &str)
 
static common::Result< std::shared_ptr< alert_trigger > > build_trigger (const rule_definition::trigger_config &cfg)
 
static common::Result< comparison_operatorparse_operator (const std::string &str)
 

Detailed Description

Builds alert_rule from rule_definition.

Provides validation and construction of alert rules from configuration definitions.

Definition at line 220 of file alert_config.h.

Member Function Documentation

◆ build()

static common::Result< std::shared_ptr< alert_rule > > kcenon::monitoring::rule_builder::build ( const rule_definition & def)
inlinestatic

Build alert_rule from definition.

Parameters
defRule definition
Returns
Result containing built rule or error
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 227 of file alert_config.h.

227 {
228 // Validate required fields
229 if (def.name.empty()) {
230 return common::make_error<std::shared_ptr<alert_rule>>(
232 "Rule name is required");
233 }
234 if (def.metric_name.empty()) {
235 return common::make_error<std::shared_ptr<alert_rule>>(
237 "Metric name is required");
238 }
239
240 auto rule = std::make_shared<alert_rule>(def.name);
241
242 // Set group
243 if (!def.group.empty()) {
244 rule->set_group(def.group);
245 }
246
247 // Set metric name
248 rule->set_metric_name(def.metric_name);
249
250 // Set severity
251 auto severity = parse_severity(def.severity);
252 if (!severity.is_ok()) {
253 return common::make_error<std::shared_ptr<alert_rule>>(
255 severity.error().message);
256 }
257 rule->set_severity(severity.value());
258
259 // Set enabled
260 rule->set_enabled(def.enabled);
261
262 // Build trigger
263 auto trigger = build_trigger(def.trigger);
264 if (!trigger.is_ok()) {
265 return common::make_error<std::shared_ptr<alert_rule>>(
267 trigger.error().message);
268 }
269 rule->set_trigger(trigger.value());
270
271 // Set timing
272 rule->set_evaluation_interval(
273 std::chrono::seconds(def.evaluation_interval_seconds));
274 rule->set_for_duration(
275 std::chrono::seconds(def.for_duration_seconds));
276 rule->set_repeat_interval(
277 std::chrono::seconds(def.repeat_interval_seconds));
278
279 // Set labels
280 for (const auto& [key, value] : def.labels) {
281 rule->add_label(key, value);
282 }
283
284 // Set annotations
285 if (!def.summary.empty()) {
286 rule->set_summary(def.summary);
287 }
288 if (!def.description.empty()) {
289 rule->set_description(def.description);
290 }
291 if (!def.runbook_url.empty()) {
292 rule->set_runbook_url(def.runbook_url);
293 }
294
295 return common::ok(std::move(rule));
296 }
static common::Result< std::shared_ptr< alert_trigger > > build_trigger(const rule_definition::trigger_config &cfg)
static common::Result< alert_severity > parse_severity(const std::string &str)

References build_trigger(), kcenon::monitoring::rule_definition::description, kcenon::monitoring::rule_definition::enabled, kcenon::monitoring::rule_definition::evaluation_interval_seconds, kcenon::monitoring::rule_definition::for_duration_seconds, kcenon::monitoring::rule_definition::group, kcenon::monitoring::invalid_argument, kcenon::monitoring::rule_definition::labels, kcenon::monitoring::rule_definition::metric_name, kcenon::monitoring::rule_definition::name, parse_severity(), kcenon::monitoring::rule_definition::repeat_interval_seconds, kcenon::monitoring::rule_definition::runbook_url, kcenon::monitoring::rule_definition::severity, kcenon::monitoring::rule_definition::summary, and kcenon::monitoring::rule_definition::trigger.

Referenced by kcenon::monitoring::rule_registry::load_definitions().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ build_trigger()

static common::Result< std::shared_ptr< alert_trigger > > kcenon::monitoring::rule_builder::build_trigger ( const rule_definition::trigger_config & cfg)
inlinestaticprivate
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 317 of file alert_config.h.

318 {
319 if (cfg.type.empty() || cfg.type == "threshold") {
320 auto op = parse_operator(cfg.operator_str);
321 if (!op.is_ok()) {
322 return common::make_error<std::shared_ptr<alert_trigger>>(
324 op.error().message);
325 }
326 return common::ok(std::shared_ptr<alert_trigger>(
327 std::make_shared<threshold_trigger>(cfg.threshold, op.value())));
328 }
329
330 if (cfg.type == "rate") {
331 return common::ok(std::shared_ptr<alert_trigger>(
332 std::make_shared<rate_of_change_trigger>(
333 cfg.rate_threshold,
334 std::chrono::seconds(cfg.window_seconds))));
335 }
336
337 if (cfg.type == "anomaly") {
338 return common::ok(std::shared_ptr<alert_trigger>(
339 std::make_shared<anomaly_trigger>(
340 cfg.sensitivity,
341 static_cast<size_t>(cfg.window_seconds))));
342 }
343
344 if (cfg.type == "absent") {
345 return common::ok(std::shared_ptr<alert_trigger>(
346 std::make_shared<absent_trigger>(
347 std::chrono::seconds(cfg.absent_seconds))));
348 }
349
350 return common::make_error<std::shared_ptr<alert_trigger>>(
352 "Unknown trigger type: " + cfg.type);
353 }
static common::Result< comparison_operator > parse_operator(const std::string &str)

References kcenon::monitoring::rule_definition::trigger_config::absent_seconds, kcenon::monitoring::invalid_argument, kcenon::monitoring::rule_definition::trigger_config::operator_str, parse_operator(), kcenon::monitoring::rule_definition::trigger_config::rate_threshold, kcenon::monitoring::rule_definition::trigger_config::sensitivity, kcenon::monitoring::rule_definition::trigger_config::threshold, kcenon::monitoring::rule_definition::trigger_config::type, and kcenon::monitoring::rule_definition::trigger_config::window_seconds.

Referenced by build().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_operator()

static common::Result< comparison_operator > kcenon::monitoring::rule_builder::parse_operator ( const std::string & str)
inlinestaticprivate
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 355 of file alert_config.h.

355 {
356 if (str.empty() || str == ">") {
357 return common::ok(comparison_operator::greater_than);
358 }
359 if (str == ">=") {
360 return common::ok(comparison_operator::greater_or_equal);
361 }
362 if (str == "<") {
363 return common::ok(comparison_operator::less_than);
364 }
365 if (str == "<=") {
366 return common::ok(comparison_operator::less_or_equal);
367 }
368 if (str == "==" || str == "=") {
369 return common::ok(comparison_operator::equal);
370 }
371 if (str == "!=" || str == "<>") {
372 return common::ok(comparison_operator::not_equal);
373 }
374 return common::make_error<comparison_operator>(
376 "Unknown operator: " + str);
377 }
@ equal
value == threshold (with epsilon)
@ not_equal
value != threshold (with epsilon)

References kcenon::monitoring::equal, kcenon::monitoring::greater_or_equal, kcenon::monitoring::greater_than, kcenon::monitoring::invalid_argument, kcenon::monitoring::less_or_equal, kcenon::monitoring::less_than, and kcenon::monitoring::not_equal.

Referenced by build_trigger().

Here is the caller graph for this function:

◆ parse_severity()

static common::Result< alert_severity > kcenon::monitoring::rule_builder::parse_severity ( const std::string & str)
inlinestaticprivate
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 299 of file alert_config.h.

299 {
300 if (str.empty() || str == "warning") {
301 return common::ok(alert_severity::warning);
302 }
303 if (str == "info") {
304 return common::ok(alert_severity::info);
305 }
306 if (str == "critical") {
307 return common::ok(alert_severity::critical);
308 }
309 if (str == "emergency") {
310 return common::ok(alert_severity::emergency);
311 }
312 return common::make_error<alert_severity>(
314 "Unknown severity: " + str);
315 }
@ warning
Warning condition, may require attention.
@ critical
Critical condition, immediate attention required.
@ emergency
Emergency condition, system-wide impact.
@ info
Informational, no action required.

References kcenon::monitoring::critical, kcenon::monitoring::emergency, kcenon::monitoring::info, kcenon::monitoring::invalid_argument, and kcenon::monitoring::warning.

Referenced by build().

Here is the caller graph for this function:

The documentation for this class was generated from the following file: