summaryrefslogtreecommitdiffstats
path: root/database/rrdcalctemplate.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2022-11-30 18:47:05 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2022-11-30 18:47:05 +0000
commit97e01009d69b8fbebfebf68f51e3d126d0ed43fc (patch)
tree02e8b836c3a9d89806f3e67d4a5fe9f52dbb0061 /database/rrdcalctemplate.c
parentReleasing debian version 1.36.1-1. (diff)
downloadnetdata-97e01009d69b8fbebfebf68f51e3d126d0ed43fc.tar.xz
netdata-97e01009d69b8fbebfebf68f51e3d126d0ed43fc.zip
Merging upstream version 1.37.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'database/rrdcalctemplate.c')
-rw-r--r--database/rrdcalctemplate.c253
1 files changed, 192 insertions, 61 deletions
diff --git a/database/rrdcalctemplate.c b/database/rrdcalctemplate.c
index 3f9804b9..87e085c9 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,100 +10,232 @@
* @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) {
- if(rt->hash_context != st->hash_context || strcmp(rt->context, st->context) != 0)
- return;
- if (rt->charts_pattern && !simple_pattern_matches(rt->charts_pattern, st->name))
- return;
+static char *rrdcalc_alert_name_with_dimension(const char *name, size_t namelen, const char *dim, size_t dimlen) {
+ char *newname,*move;
- if (rt->family_pattern && !simple_pattern_matches(rt->family_pattern, st->family))
- return;
+ newname = mallocz(namelen + dimlen + 2);
+ move = newname;
+ memcpy(move, name, namelen);
+ move += namelen;
- if (rt->module_pattern && !simple_pattern_matches(rt->module_pattern, st->module_name))
- return;
+ *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 false;
+
+ 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 false;
+
+ if (rt->module_pattern && !simple_pattern_matches(rt->module_pattern, rrdset_module_name(st)))
+ return false;
+
+ if (rt->plugin_pattern && !simple_pattern_matches(rt->plugin_pattern, rrdset_plugin_name(st)))
+ return false;
+
+ if(host->rrdlabels && rt->host_labels_pattern && !rrdlabels_match_simple_pattern_parsed(host->rrdlabels, rt->host_labels_pattern, '='))
+ return false;
- if (rt->plugin_pattern && !simple_pattern_matches(rt->plugin_pattern, st->plugin_name))
+ 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(host->host_labels && rt->host_labels_pattern && !rrdlabels_match_simple_pattern_parsed(host->host_labels, rt->host_labels_pattern, '='))
+ if(!rt->foreach_dimension_pattern) {
+ rrdcalc_add_from_rrdcalctemplate(host, rt, st, NULL, NULL);
return;
+ }
- RRDCALC *rc = rrdcalc_create_from_template(host, rt, st->id);
- if (unlikely(!rc))
- info("Health tried to create alarm from template '%s' on chart '%s' of host '%s', but it failed", rt->name, st->id, host->hostname);
-#ifdef NETDATA_INTERNAL_CHECKS
- else if (rc->rrdset != st && !rc->foreachdim) //When we have a template with foreadhdim, the child will be added to the index late
- error("Health alarm '%s.%s' should be linked to chart '%s', but it is not", rc->chart ? rc->chart : "NOCHART", rc->name, st->id);
-#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;
-
- for(rt = host->templates; rt ; rt = rt->next)
- rrdcalctemplate_check_conditions_and_link(rt, st, host);
- for(rt = host->alarms_template_with_foreach; rt ; rt = rt->next)
+ 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);
- freez(rt->family_match);
+ string_freez(rt->family_match);
simple_pattern_free(rt->family_pattern);
- freez(rt->plugin_match);
+ string_freez(rt->plugin_match);
simple_pattern_free(rt->plugin_pattern);
- freez(rt->module_match);
+ string_freez(rt->module_match);
simple_pattern_free(rt->module_pattern);
- freez(rt->charts_match);
+ string_freez(rt->charts_match);
simple_pattern_free(rt->charts_pattern);
- freez(rt->name);
- freez(rt->exec);
- freez(rt->recipient);
- freez(rt->classification);
- freez(rt->component);
- freez(rt->type);
- freez(rt->context);
- freez(rt->source);
- freez(rt->units);
- freez(rt->info);
- freez(rt->dimensions);
- freez(rt->foreachdim);
- freez(rt->host_labels);
- simple_pattern_free(rt->spdim);
+ string_freez(rt->name);
+ string_freez(rt->exec);
+ string_freez(rt->recipient);
+ string_freez(rt->classification);
+ string_freez(rt->component);
+ string_freez(rt->type);
+ string_freez(rt->context);
+ string_freez(rt->source);
+ string_freez(rt->units);
+ string_freez(rt->info);
+ string_freez(rt->dimensions);
+ string_freez(rt->foreach_dimension);
+ string_freez(rt->host_labels);
+ 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'", rt->name, host->hostname);
+ 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);
- if(host->templates == rt) {
- host->templates = rt->next;
+ dictionary_register_insert_callback(host->rrdcalctemplate_root_index, rrdcalctemplate_insert_callback, NULL);
+ dictionary_register_delete_callback(host->rrdcalctemplate_root_index, rrdcalctemplate_delete_callback, host);
}
- else {
- RRDCALCTEMPLATE *t;
- for (t = host->templates; t && t->next != rt; t = t->next ) ;
- if(t) {
- t->next = rt->next;
- rt->next = NULL;
- }
- else
- error("Cannot find RRDCALCTEMPLATE '%s' linked in host '%s'", rt->name, host->hostname);
+}
+
+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;
}
- rrdcalctemplate_free(rt);
+ 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));
+
+ bool added = false;
+ dictionary_set_advanced(host->rrdcalctemplate_root_index, key, (ssize_t)(key_len + 1), rt, sizeof(*rt), &added);
+
+ 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);
+ }
}