prevent log errors while freeing incompatible alarms and alarm templates; #2382
Costa Tsaousis (ktsaou) committed
Sep 28, 2017 at 00:31 UTC
1f641fa172d252ba7d89750f90329f4e1ae03c27
6 files changed
+66
-45
src/health.c
+2
-2
@@ -34,10 +34,10 @@ void health_reload_host(RRDHOST *host) {
34
rrdhost_wrlock(host);
35
36
while(host->templates)
37
- rrdcalctemplate_free(host, host->templates);
37
+ rrdcalctemplate_unlink_and_free(host, host->templates);
38
39
while(host->alarms)
40
- rrdcalc_free(host, host->alarms);
40
+ rrdcalc_unlink_and_free(host, host->alarms);
41
42
rrdhost_unlock(host);
43
src/health.h
+5
-2
@@ -403,8 +403,11 @@ extern char *health_config_dir(void);
403
extern void health_reload_host(RRDHOST *host);
404
extern void health_alarm_log_free(RRDHOST *host);
405
406
-extern void rrdcalc_free(RRDHOST *host, RRDCALC *rc);
407
-extern void rrdcalctemplate_free(RRDHOST *host, RRDCALCTEMPLATE *rt);
406
+extern void rrdcalc_free(RRDCALC *rc);
407
+extern void rrdcalc_unlink_and_free(RRDHOST *host, RRDCALC *rc);
408
+
409
+extern void rrdcalctemplate_free(RRDCALCTEMPLATE *rt);
410
+extern void rrdcalctemplate_unlink_and_free(RRDHOST *host, RRDCALCTEMPLATE *rt);
411
412
#ifdef NETDATA_HEALTH_INTERNALS
413
#define RRDVAR_MAX_LENGTH 1024
src/health_config.c
+8
-6
@@ -505,11 +505,12 @@ int health_readfile(RRDHOST *host, const char *path, const char *filename) {
505
506
if(hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) {
507
if (rc && (ignore_this || !rrdcalc_add_alarm_from_config(host, rc)))
508
- rrdcalc_free(host, rc);
508
+ rrdcalc_free(rc);
509
510
if(rt) {
511
if (ignore_this || !rrdcalctemplate_add_template_from_config(host, rt))
512
- rrdcalctemplate_free(host, rt);
512
+ rrdcalctemplate_free(rt);
513
+
514
rt = NULL;
515
}
516
@@ -532,12 +533,13 @@ int health_readfile(RRDHOST *host, const char *path, const char *filename) {
533
else if(hash == hash_template && !strcasecmp(key, HEALTH_TEMPLATE_KEY)) {
534
if(rc) {
535
if(ignore_this || !rrdcalc_add_alarm_from_config(host, rc))
535
- rrdcalc_free(host, rc);
536
+ rrdcalc_free(rc);
537
+
538
rc = NULL;
539
}
540
541
if(rt && (ignore_this || !rrdcalctemplate_add_template_from_config(host, rt)))
540
- rrdcalctemplate_free(host, rt);
542
+ rrdcalctemplate_free(rt);
543
544
rt = callocz(1, sizeof(RRDCALCTEMPLATE));
545
rt->name = strdupz(value);
@@ -833,10 +835,10 @@ int health_readfile(RRDHOST *host, const char *path, const char *filename) {
835
}
836
837
if(rc && (ignore_this || !rrdcalc_add_alarm_from_config(host, rc)))
836
- rrdcalc_free(host, rc);
838
+ rrdcalc_free(rc);
839
840
if(rt && (ignore_this || !rrdcalctemplate_add_template_from_config(host, rt)))
839
- rrdcalctemplate_free(host, rt);
841
+ rrdcalctemplate_free(rt);
842
843
fclose(fp);
844
return 1;
src/rrdcalc.c
+22
-16
@@ -375,8 +375,27 @@ inline RRDCALC *rrdcalc_create(RRDHOST *host, RRDCALCTEMPLATE *rt, const char *c
375
return rc;
376
}
377
378
-void rrdcalc_free(RRDHOST *host, RRDCALC *rc) {
379
- if(!rc) return;
378
+void rrdcalc_free(RRDCALC *rc) {
379
+ if(unlikely(!rc)) return;
380
+
381
+ expression_free(rc->calculation);
382
+ expression_free(rc->warning);
383
+ expression_free(rc->critical);
384
+
385
+ freez(rc->name);
386
+ freez(rc->chart);
387
+ freez(rc->family);
388
+ freez(rc->dimensions);
389
+ freez(rc->exec);
390
+ freez(rc->recipient);
391
+ freez(rc->source);
392
+ freez(rc->units);
393
+ freez(rc->info);
394
+ freez(rc);
395
+}
396
+
397
+void rrdcalc_unlink_and_free(RRDHOST *host, RRDCALC *rc) {
398
+ if(unlikely(!rc)) return;
399
400
debug(D_HEALTH, "Health removing alarm '%s.%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
401
@@ -398,18 +417,5 @@ void rrdcalc_free(RRDHOST *host, RRDCALC *rc) {
417
error("Cannot unlink alarm '%s.%s' from host '%s': not found", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
418
}
419
401
- expression_free(rc->calculation);
402
- expression_free(rc->warning);
403
- expression_free(rc->critical);
404
-
405
- freez(rc->name);
406
- freez(rc->chart);
407
- freez(rc->family);
408
- freez(rc->dimensions);
409
- freez(rc->exec);
410
- freez(rc->recipient);
411
- freez(rc->source);
412
- freez(rc->units);
413
- freez(rc->info);
414
- freez(rc);
420
+ rrdcalc_free(rc);
421
}
src/rrdcalctemplate.c
+23
-17
@@ -22,25 +22,9 @@ void rrdcalctemplate_link_matching(RRDSET *st) {
22
}
23
}
24
25
-inline void rrdcalctemplate_free(RRDHOST *host, RRDCALCTEMPLATE *rt) {
25
+inline void rrdcalctemplate_free(RRDCALCTEMPLATE *rt) {
26
if(unlikely(!rt)) return;
27
28
- debug(D_HEALTH, "Health removing template '%s' of host '%s'", rt->name, host->hostname);
29
-
30
- if(host->templates == rt) {
31
- host->templates = rt->next;
32
- }
33
- else {
34
- RRDCALCTEMPLATE *t;
35
- for (t = host->templates; t && t->next != rt; t = t->next ) ;
36
- if(t) {
37
- t->next = rt->next;
38
- rt->next = NULL;
39
- }
40
- else
41
- error("Cannot find RRDCALCTEMPLATE '%s' linked in host '%s'", rt->name, host->hostname);
42
- }
43
-
28
expression_free(rt->calculation);
29
expression_free(rt->warning);
30
expression_free(rt->critical);
@@ -59,4 +43,26 @@ inline void rrdcalctemplate_free(RRDHOST *host, RRDCALCTEMPLATE *rt) {
43
freez(rt);
44
}
45
46
+inline void rrdcalctemplate_unlink_and_free(RRDHOST *host, RRDCALCTEMPLATE *rt) {
47
+ if(unlikely(!rt)) return;
48
+
49
+ debug(D_HEALTH, "Health removing template '%s' of host '%s'", rt->name, host->hostname);
50
+
51
+ if(host->templates == rt) {
52
+ host->templates = rt->next;
53
+ }
54
+ else {
55
+ RRDCALCTEMPLATE *t;
56
+ for (t = host->templates; t && t->next != rt; t = t->next ) ;
57
+ if(t) {
58
+ t->next = rt->next;
59
+ rt->next = NULL;
60
+ }
61
+ else
62
+ error("Cannot find RRDCALCTEMPLATE '%s' linked in host '%s'", rt->name, host->hostname);
63
+ }
64
+
65
+ rrdcalctemplate_free(rt);
66
+}
67
+
68
src/rrdhost.c
+6
-2
@@ -473,8 +473,12 @@ void rrdhost_free(RRDHOST *host) {
473
474
while(host->rrdset_root) rrdset_free(host->rrdset_root);
475
476
- while(host->alarms) rrdcalc_free(host, host->alarms);
477
- while(host->templates) rrdcalctemplate_free(host, host->templates);
476
+ while(host->alarms)
477
+ rrdcalc_unlink_and_free(host, host->alarms);
478
+
479
+ while(host->templates)
480
+ rrdcalctemplate_unlink_and_free(host, host->templates);
481
+
482
health_alarm_log_free(host);
483
484