summaryrefslogtreecommitdiffstats
path: root/database/rrdcalctemplate.c
diff options
context:
space:
mode:
Diffstat (limited to 'database/rrdcalctemplate.c')
-rw-r--r--database/rrdcalctemplate.c202
1 files changed, 174 insertions, 28 deletions
diff --git a/database/rrdcalctemplate.c b/database/rrdcalctemplate.c
index 0db76d6c04..87e085c932 100644
--- a/database/rrdcalctemplate.c
+++ b/database/rrdcalctemplate.c
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: GPL-3.0-or-later
-#define NETDATA_HEALTH_INTERNALS
#include "rrd.h"
// ----------------------------------------------------------------------------
@@ -11,45 +10,84 @@
* @param rt is the template used to create the chart.
* @param st is the chart where the alarm will be attached.
*/
-void rrdcalctemplate_check_conditions_and_link(RRDCALCTEMPLATE *rt, RRDSET *st, RRDHOST *host) {
+
+static char *rrdcalc_alert_name_with_dimension(const char *name, size_t namelen, const char *dim, size_t dimlen) {
+ char *newname,*move;
+
+ newname = mallocz(namelen + dimlen + 2);
+ move = newname;
+ memcpy(move, name, namelen);
+ move += namelen;
+
+ *move++ = '_';
+ memcpy(move, dim, dimlen);
+ move += dimlen;
+ *move = '\0';
+
+ return newname;
+}
+
+bool rrdcalctemplate_check_rrdset_conditions(RRDCALCTEMPLATE *rt, RRDSET *st, RRDHOST *host) {
if(rt->context != st->context)
- return;
+ return false;
- if (rt->charts_pattern && !simple_pattern_matches(rt->charts_pattern, rrdset_name(st)))
- return;
+ if(rt->foreach_dimension_pattern && !rrdset_number_of_dimensions(st))
+ return false;
+
+ if (rt->charts_pattern && !simple_pattern_matches(rt->charts_pattern, rrdset_name(st)) && !simple_pattern_matches(rt->charts_pattern, rrdset_id(st)))
+ return false;
if (rt->family_pattern && !simple_pattern_matches(rt->family_pattern, rrdset_family(st)))
- return;
+ return false;
if (rt->module_pattern && !simple_pattern_matches(rt->module_pattern, rrdset_module_name(st)))
- return;
+ return false;
if (rt->plugin_pattern && !simple_pattern_matches(rt->plugin_pattern, rrdset_plugin_name(st)))
- return;
+ return false;
if(host->rrdlabels && rt->host_labels_pattern && !rrdlabels_match_simple_pattern_parsed(host->rrdlabels, rt->host_labels_pattern, '='))
+ return false;
+
+ return true;
+}
+
+void rrdcalctemplate_check_rrddim_conditions_and_link(RRDCALCTEMPLATE *rt, RRDSET *st, RRDDIM *rd, RRDHOST *host) {
+ if (simple_pattern_matches(rt->foreach_dimension_pattern, rrddim_id(rd)) || simple_pattern_matches(rt->foreach_dimension_pattern, rrddim_name(rd))) {
+ char *overwrite_alert_name = rrdcalc_alert_name_with_dimension(
+ rrdcalctemplate_name(rt), string_strlen(rt->name), rrddim_name(rd), string_strlen(rd->name));
+ rrdcalc_add_from_rrdcalctemplate(host, rt, st, overwrite_alert_name, rrddim_name(rd));
+ freez(overwrite_alert_name);
+ }
+}
+
+void rrdcalctemplate_check_conditions_and_link(RRDCALCTEMPLATE *rt, RRDSET *st, RRDHOST *host) {
+ if(!rrdcalctemplate_check_rrdset_conditions(rt, st, host))
+ return;
+
+ if(!rt->foreach_dimension_pattern) {
+ rrdcalc_add_from_rrdcalctemplate(host, rt, st, NULL, NULL);
return;
+ }
- RRDCALC *rc = rrdcalc_create_from_template(host, rt, rrdset_id(st));
- if (unlikely(!rc))
- info("Health tried to create alarm from template '%s' on chart '%s' of host '%s', but it failed", rrdcalctemplate_name(rt), rrdset_id(st), rrdhost_hostname(host));
-#ifdef NETDATA_INTERNAL_CHECKS
- else if (rc->rrdset != st && !rc->foreachdim) //When we have a template with foreachdim, the child will be added to the index late
- error("Health alarm '%s.%s' should be linked to chart '%s', but it is not", rrdcalc_chart_name(rc), rrdcalc_name(rc), rrdset_id(st));
-#endif
+ RRDDIM *rd;
+ rrddim_foreach_read(rd, st) {
+ rrdcalctemplate_check_rrddim_conditions_and_link(rt, st, rd, host);
+ }
+ rrddim_foreach_done(rd);
}
-void rrdcalctemplate_link_matching(RRDSET *st) {
+void rrdcalctemplate_link_matching_templates_to_rrdset(RRDSET *st) {
RRDHOST *host = st->rrdhost;
- RRDCALCTEMPLATE *rt;
- foreach_rrdcalctemplate_in_rrdhost(host, rt)
+ RRDCALCTEMPLATE *rt;
+ foreach_rrdcalctemplate_read(host, rt) {
rrdcalctemplate_check_conditions_and_link(rt, st, host);
+ }
+ foreach_rrdcalctemplate_done(rt);
}
-inline void rrdcalctemplate_free(RRDCALCTEMPLATE *rt) {
- if(unlikely(!rt)) return;
-
+static void rrdcalctemplate_free_internals(RRDCALCTEMPLATE *rt) {
expression_free(rt->calculation);
expression_free(rt->warning);
expression_free(rt->critical);
@@ -77,19 +115,127 @@ inline void rrdcalctemplate_free(RRDCALCTEMPLATE *rt) {
string_freez(rt->units);
string_freez(rt->info);
string_freez(rt->dimensions);
- string_freez(rt->foreachdim);
+ string_freez(rt->foreach_dimension);
string_freez(rt->host_labels);
- simple_pattern_free(rt->spdim);
+ simple_pattern_free(rt->foreach_dimension_pattern);
simple_pattern_free(rt->host_labels_pattern);
- freez(rt);
}
-inline void rrdcalctemplate_unlink_and_free(RRDHOST *host, RRDCALCTEMPLATE *rt) {
+void rrdcalctemplate_free_unused_rrdcalctemplate_loaded_from_config(RRDCALCTEMPLATE *rt) {
if(unlikely(!rt)) return;
- debug(D_HEALTH, "Health removing template '%s' of host '%s'", rrdcalctemplate_name(rt), rrdhost_hostname(host));
+ rrdcalctemplate_free_internals(rt);
+ freez(rt);
+}
+static void rrdcalctemplate_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdcalctemplate, void *added_bool) {
+ RRDCALCTEMPLATE *rt = rrdcalctemplate; (void)rt;
+
+ bool *added = added_bool;
+ *added = true;
+
+ debug(D_HEALTH, "Health configuration adding template '%s'"
+ ": context '%s'"
+ ", exec '%s'"
+ ", recipient '%s'"
+ ", green " NETDATA_DOUBLE_FORMAT_AUTO
+ ", red " NETDATA_DOUBLE_FORMAT_AUTO
+ ", lookup: group %d"
+ ", after %d"
+ ", before %d"
+ ", options %u"
+ ", dimensions '%s'"
+ ", for each dimension '%s'"
+ ", update every %d"
+ ", calculation '%s'"
+ ", warning '%s'"
+ ", critical '%s'"
+ ", source '%s'"
+ ", delay up %d"
+ ", delay down %d"
+ ", delay max %d"
+ ", delay_multiplier %f"
+ ", warn_repeat_every %u"
+ ", crit_repeat_every %u",
+ rrdcalctemplate_name(rt),
+ (rt->context)?string2str(rt->context):"NONE",
+ (rt->exec)?rrdcalctemplate_exec(rt):"DEFAULT",
+ (rt->recipient)?rrdcalctemplate_recipient(rt):"DEFAULT",
+ rt->green,
+ rt->red,
+ (int)rt->group,
+ rt->after,
+ rt->before,
+ rt->options,
+ (rt->dimensions)?rrdcalctemplate_dimensions(rt):"NONE",
+ (rt->foreach_dimension)?rrdcalctemplate_foreachdim(rt):"NONE",
+ rt->update_every,
+ (rt->calculation)?rt->calculation->parsed_as:"NONE",
+ (rt->warning)?rt->warning->parsed_as:"NONE",
+ (rt->critical)?rt->critical->parsed_as:"NONE",
+ rrdcalctemplate_source(rt),
+ rt->delay_up_duration,
+ rt->delay_down_duration,
+ rt->delay_max_duration,
+ rt->delay_multiplier,
+ rt->warn_repeat_every,
+ rt->crit_repeat_every
+ );
+}
+
+static void rrdcalctemplate_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdcalctemplate, void *rrdhost __maybe_unused) {
+ RRDCALCTEMPLATE *rt = rrdcalctemplate;
+ rrdcalctemplate_free_internals(rt);
+}
+
+void rrdcalctemplate_index_init(RRDHOST *host) {
+ if(!host->rrdcalctemplate_root_index) {
+ host->rrdcalctemplate_root_index = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE);
+
+ dictionary_register_insert_callback(host->rrdcalctemplate_root_index, rrdcalctemplate_insert_callback, NULL);
+ dictionary_register_delete_callback(host->rrdcalctemplate_root_index, rrdcalctemplate_delete_callback, host);
+ }
+}
+
+void rrdcalctemplate_index_destroy(RRDHOST *host) {
+ dictionary_destroy(host->rrdcalctemplate_root_index);
+ host->rrdcalctemplate_root_index = NULL;
+}
+
+inline void rrdcalctemplate_delete_all(RRDHOST *host) {
+ dictionary_flush(host->rrdcalctemplate_root_index);
+}
+
+#define RRDCALCTEMPLATE_MAX_KEY_SIZE 1024
+static size_t rrdcalctemplate_key(char *dst, size_t dst_len, const char *name, const char *family_match) {
+ return snprintfz(dst, dst_len, "%s/%s", name, (family_match && *family_match)?family_match:"*");
+}
+
+void rrdcalctemplate_add_from_config(RRDHOST *host, RRDCALCTEMPLATE *rt) {
+ if(unlikely(!rt->context)) {
+ error("Health configuration for template '%s' does not have a context", rrdcalctemplate_name(rt));
+ return;
+ }
+
+ if(unlikely(!rt->update_every)) {
+ error("Health configuration for template '%s' has no frequency (parameter 'every'). Ignoring it.", rrdcalctemplate_name(rt));
+ return;
+ }
+
+ if(unlikely(!RRDCALCTEMPLATE_HAS_DB_LOOKUP(rt) && !rt->calculation && !rt->warning && !rt->critical)) {
+ error("Health configuration for template '%s' is useless (no calculation, no warning and no critical evaluation)", rrdcalctemplate_name(rt));
+ return;
+ }
+
+ char key[RRDCALCTEMPLATE_MAX_KEY_SIZE + 1];
+ size_t key_len = rrdcalctemplate_key(key, RRDCALCTEMPLATE_MAX_KEY_SIZE, rrdcalctemplate_name(rt), rrdcalctemplate_family_match(rt));
- DOUBLE_LINKED_LIST_REMOVE_UNSAFE(host->alarms_templates, rt, prev, next);
+ bool added = false;
+ dictionary_set_advanced(host->rrdcalctemplate_root_index, key, (ssize_t)(key_len + 1), rt, sizeof(*rt), &added);
- rrdcalctemplate_free(rt);
+ if(added)
+ freez(rt);
+ else {
+ info("Health configuration template '%s' already exists for host '%s'.", rrdcalctemplate_name(rt), rrdhost_hostname(host));
+ rrdcalctemplate_free_unused_rrdcalctemplate_loaded_from_config(rt);
+ }
}