@cryptotaxi247 / netdata-1 / commits / f0a63cab0

allow filtering alarms by os and hostname; allow multiple versions of each alarm template to be loaded; #2608

Costa Tsaousis (ktsaou) committed Sep 3, 2017 at 16:24 UTC f0a63cab032db75281a23a81a995b3e2063a22a2
1 file changed +53 -7
src/health_config.c
+53 -7
@@ -6,6 +6,8 @@
6 #define HEALTH_ALARM_KEY "alarm"
7 #define HEALTH_TEMPLATE_KEY "template"
8 #define HEALTH_ON_KEY "on"
9 +#define HEALTH_HOST_KEY "host"
10 +#define HEALTH_OS_KEY "os"
11 #define HEALTH_FAMILIES_KEY "families"
12 #define HEALTH_LOOKUP_KEY "lookup"
13 #define HEALTH_CALC_KEY "calc"
@@ -88,7 +90,10 @@ static inline int rrdcalctemplate_add_template_from_config(RRDHOST *host, RRDCAL
90
91 RRDCALCTEMPLATE *t, *last = NULL;
92 for (t = host->templates; t ; last = t, t = t->next) {
91 - if(unlikely(t->hash_name == rt->hash_name && !strcmp(t->name, rt->name))) {
93 + if(unlikely(t->hash_name == rt->hash_name
94 + && !strcmp(t->name, rt->name)
95 + && !strcmp(t->family_match?t->family_match:"*", rt->family_match?rt->family_match:"*")
96 + )) {
97 error("Health configuration template '%s' already exists for host '%s'.", rt->name, host->hostname);
98 return 0;
99 }
@@ -400,7 +405,9 @@ int health_readfile(RRDHOST *host, const char *path, const char *filename) {
405 static uint32_t
406 hash_alarm = 0,
407 hash_template = 0,
408 + hash_os = 0,
409 hash_on = 0,
410 + hash_host = 0,
411 hash_families = 0,
412 hash_calc = 0,
413 hash_green = 0,
@@ -422,6 +429,8 @@ int health_readfile(RRDHOST *host, const char *path, const char *filename) {
429 hash_alarm = simple_uhash(HEALTH_ALARM_KEY);
430 hash_template = simple_uhash(HEALTH_TEMPLATE_KEY);
431 hash_on = simple_uhash(HEALTH_ON_KEY);
432 + hash_os = simple_uhash(HEALTH_OS_KEY);
433 + hash_host = simple_uhash(HEALTH_HOST_KEY);
434 hash_families = simple_uhash(HEALTH_FAMILIES_KEY);
435 hash_calc = simple_uhash(HEALTH_CALC_KEY);
436 hash_lookup = simple_uhash(HEALTH_LOOKUP_KEY);
@@ -448,6 +457,7 @@ int health_readfile(RRDHOST *host, const char *path, const char *filename) {
457 RRDCALC *rc = NULL;
458 RRDCALCTEMPLATE *rt = NULL;
459
460 + int ignore_this = 0;
461 size_t line = 0, append = 0;
462 char *s;
463 while((s = fgets(&buffer[append], (int)(HEALTH_CONF_MAX_LINE - append), fp)) || append) {
@@ -494,11 +504,11 @@ int health_readfile(RRDHOST *host, const char *path, const char *filename) {
504 uint32_t hash = simple_uhash(key);
505
506 if(hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) {
497 - if(rc && !rrdcalc_add_alarm_from_config(host, rc))
507 + if (rc && (ignore_this || !rrdcalc_add_alarm_from_config(host, rc)))
508 rrdcalc_free(host, rc);
509
510 if(rt) {
501 - if (!rrdcalctemplate_add_template_from_config(host, rt))
511 + if (ignore_this || !rrdcalctemplate_add_template_from_config(host, rt))
512 rrdcalctemplate_free(host, rt);
513 rt = NULL;
514 }
@@ -516,15 +526,17 @@ int health_readfile(RRDHOST *host, const char *path, const char *filename) {
526
527 if(rrdvar_fix_name(rc->name))
528 error("Health configuration renamed alarm '%s' to '%s'", value, rc->name);
529 +
530 + ignore_this = 0;
531 }
532 else if(hash == hash_template && !strcasecmp(key, HEALTH_TEMPLATE_KEY)) {
533 if(rc) {
522 - if(!rrdcalc_add_alarm_from_config(host, rc))
534 + if(ignore_this || !rrdcalc_add_alarm_from_config(host, rc))
535 rrdcalc_free(host, rc);
536 rc = NULL;
537 }
538
527 - if(rt && !rrdcalctemplate_add_template_from_config(host, rt))
539 + if(rt && (ignore_this || !rrdcalctemplate_add_template_from_config(host, rt)))
540 rrdcalctemplate_free(host, rt);
541
542 rt = callocz(1, sizeof(RRDCALCTEMPLATE));
@@ -537,6 +549,40 @@ int health_readfile(RRDHOST *host, const char *path, const char *filename) {
549
550 if(rrdvar_fix_name(rt->name))
551 error("Health configuration renamed template '%s' to '%s'", value, rt->name);
552 +
553 + ignore_this = 0;
554 + }
555 + else if(hash == hash_os && !strcasecmp(key, HEALTH_OS_KEY)) {
556 + char *os_match = value;
557 + SIMPLE_PATTERN *os_pattern = simple_pattern_create(os_match, SIMPLE_PATTERN_EXACT);
558 +
559 + if(!simple_pattern_matches(os_pattern, host->os)) {
560 + if(rc)
561 + debug(D_HEALTH, "HEALTH on '%s' ignoring alarm '%s' defined at %zu@%s/%s: host O/S does not match '%s'", host->hostname, rc->name, line, path, filename, os_match);
562 +
563 + if(rt)
564 + debug(D_HEALTH, "HEALTH on '%s' ignoring template '%s' defined at %zu@%s/%s: host O/S does not match '%s'", host->hostname, rt->name, line, path, filename, os_match);
565 +
566 + ignore_this = 1;
567 + }
568 +
569 + simple_pattern_free(os_pattern);
570 + }
571 + else if(hash == hash_host && !strcasecmp(key, HEALTH_HOST_KEY)) {
572 + char *host_match = value;
573 + SIMPLE_PATTERN *host_pattern = simple_pattern_create(host_match, SIMPLE_PATTERN_EXACT);
574 +
575 + if(!simple_pattern_matches(host_pattern, host->hostname)) {
576 + if(rc)
577 + debug(D_HEALTH, "HEALTH on '%s' ignoring alarm '%s' defined at %zu@%s/%s: hostname does not match '%s'", host->hostname, rc->name, line, path, filename, host_match);
578 +
579 + if(rt)
580 + debug(D_HEALTH, "HEALTH on '%s' ignoring template '%s' defined at %zu@%s/%s: hostname does not match '%s'", host->hostname, rt->name, line, path, filename, host_match);
581 +
582 + ignore_this = 1;
583 + }
584 +
585 + simple_pattern_free(host_pattern);
586 }
587 else if(rc) {
588 if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
@@ -786,10 +832,10 @@ int health_readfile(RRDHOST *host, const char *path, const char *filename) {
832 }
833 }
834
789 - if(rc && !rrdcalc_add_alarm_from_config(host, rc))
835 + if(rc && (ignore_this || !rrdcalc_add_alarm_from_config(host, rc)))
836 rrdcalc_free(host, rc);
837
792 - if(rt && !rrdcalctemplate_add_template_from_config(host, rt))
838 + if(rt && (ignore_this || !rrdcalctemplate_add_template_from_config(host, rt)))
839 rrdcalctemplate_free(host, rt);
840
841 fclose(fp);