From a8220ab2d293bb7f4b014b79d16b2fb05090fa93 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Feb 2021 12:45:55 +0100 Subject: Adding upstream version 1.29.0. Signed-off-by: Daniel Baumann --- database/rrdcalctemplate.c | 82 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 8 deletions(-) (limited to 'database/rrdcalctemplate.c') diff --git a/database/rrdcalctemplate.c b/database/rrdcalctemplate.c index f7a085561..007b8c5d6 100644 --- a/database/rrdcalctemplate.c +++ b/database/rrdcalctemplate.c @@ -4,6 +4,59 @@ #include "rrd.h" // ---------------------------------------------------------------------------- + +static int rrdcalctemplate_is_there_label_restriction(RRDCALCTEMPLATE *rt, RRDHOST *host) { + if(!rt->labels) + return 0; + + errno = 0; + struct label *move = host->labels.head; + char cmp[CONFIG_FILE_LINE_MAX+1]; + + int ret; + if(move) { + rrdhost_check_rdlock(host); + netdata_rwlock_rdlock(&host->labels.labels_rwlock); + while(move) { + snprintfz(cmp, CONFIG_FILE_LINE_MAX, "%s=%s", move->key, move->value); + if (simple_pattern_matches(rt->splabels, move->key) || + simple_pattern_matches(rt->splabels, cmp)) { + break; + } + move = move->next; + } + netdata_rwlock_unlock(&host->labels.labels_rwlock); + + if(!move) { + error("Health template '%s' cannot be applied, because the host %s does not have the label(s) '%s'", + rt->name, + host->hostname, + rt->labels + ); + ret = 1; + } else { + ret = 0; + } + } else { + ret =0; + } + + return ret; +} + +static inline int rrdcalctemplate_test_additional_restriction(RRDCALCTEMPLATE *rt, RRDSET *st) { + if (rt->family_pattern && !simple_pattern_matches(rt->family_pattern, st->family)) + return 0; + + if (rt->module_pattern && !simple_pattern_matches(rt->module_pattern, st->module_name)) + return 0; + + if (rt->plugin_pattern && !simple_pattern_matches(rt->plugin_pattern, st->plugin_name)) + return 0; + + return 1; +} + // RRDCALCTEMPLATE management /** * RRDCALC TEMPLATE LINK MATCHING @@ -11,16 +64,21 @@ * @param rt is the template used to create the chart. * @param st is the chart where the alarm will be attached. */ -void rrdcalctemplate_link_matching_test(RRDCALCTEMPLATE *rt, RRDSET *st, RRDHOST *host ) { - if(rt->hash_context == st->hash_context && !strcmp(rt->context, st->context) - && (!rt->family_pattern || simple_pattern_matches(rt->family_pattern, st->family))) { - 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); +void rrdcalctemplate_link_matching_test(RRDCALCTEMPLATE *rt, RRDSET *st, RRDHOST *host) { + if(rt->hash_context == st->hash_context && !strcmp(rt->context, st->context) && + rrdcalctemplate_test_additional_restriction(rt, st) ) { + if (!rrdcalctemplate_is_there_label_restriction(rt, host)) { + 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); + 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 + } } } @@ -47,6 +105,12 @@ inline void rrdcalctemplate_free(RRDCALCTEMPLATE *rt) { freez(rt->family_match); simple_pattern_free(rt->family_pattern); + freez(rt->plugin_match); + simple_pattern_free(rt->plugin_pattern); + + freez(rt->module_match); + simple_pattern_free(rt->module_pattern); + freez(rt->name); freez(rt->exec); freez(rt->recipient); @@ -56,7 +120,9 @@ inline void rrdcalctemplate_free(RRDCALCTEMPLATE *rt) { freez(rt->info); freez(rt->dimensions); freez(rt->foreachdim); + freez(rt->labels); simple_pattern_free(rt->spdim); + simple_pattern_free(rt->splabels); freez(rt); } -- cgit v1.2.3