rrdcalctemplate.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_HEALTH_INTERNALS
  3. #include "rrd.h"
  4. // ----------------------------------------------------------------------------
  5. // RRDCALCTEMPLATE management
  6. /**
  7. * RRDCALC TEMPLATE LINK MATCHING
  8. *
  9. * @param rt is the template used to create the chart.
  10. * @param st is the chart where the alarm will be attached.
  11. */
  12. void rrdcalctemplate_link_matching_test(RRDCALCTEMPLATE *rt, RRDSET *st, RRDHOST *host ) {
  13. if(rt->hash_context == st->hash_context && !strcmp(rt->context, st->context)
  14. && (!rt->family_pattern || simple_pattern_matches(rt->family_pattern, st->family))) {
  15. RRDCALC *rc = rrdcalc_create_from_template(host, rt, st->id);
  16. if(unlikely(!rc))
  17. info("Health tried to create alarm from template '%s' on chart '%s' of host '%s', but it failed", rt->name, st->id, host->hostname);
  18. #ifdef NETDATA_INTERNAL_CHECKS
  19. else if(rc->rrdset != st && !rc->foreachdim) //When we have a template with foreadhdim, the child will be added to the index late
  20. error("Health alarm '%s.%s' should be linked to chart '%s', but it is not", rc->chart?rc->chart:"NOCHART", rc->name, st->id);
  21. #endif
  22. }
  23. }
  24. void rrdcalctemplate_link_matching(RRDSET *st) {
  25. RRDHOST *host = st->rrdhost;
  26. RRDCALCTEMPLATE *rt;
  27. for(rt = host->templates; rt ; rt = rt->next) {
  28. rrdcalctemplate_link_matching_test(rt, st, host);
  29. }
  30. for(rt = host->alarms_template_with_foreach; rt ; rt = rt->next) {
  31. rrdcalctemplate_link_matching_test(rt, st, host);
  32. }
  33. }
  34. inline void rrdcalctemplate_free(RRDCALCTEMPLATE *rt) {
  35. if(unlikely(!rt)) return;
  36. expression_free(rt->calculation);
  37. expression_free(rt->warning);
  38. expression_free(rt->critical);
  39. freez(rt->family_match);
  40. simple_pattern_free(rt->family_pattern);
  41. freez(rt->name);
  42. freez(rt->exec);
  43. freez(rt->recipient);
  44. freez(rt->context);
  45. freez(rt->source);
  46. freez(rt->units);
  47. freez(rt->info);
  48. freez(rt->dimensions);
  49. freez(rt->foreachdim);
  50. simple_pattern_free(rt->spdim);
  51. freez(rt);
  52. }
  53. inline void rrdcalctemplate_unlink_and_free(RRDHOST *host, RRDCALCTEMPLATE *rt) {
  54. if(unlikely(!rt)) return;
  55. debug(D_HEALTH, "Health removing template '%s' of host '%s'", rt->name, host->hostname);
  56. if(host->templates == rt) {
  57. host->templates = rt->next;
  58. }
  59. else {
  60. RRDCALCTEMPLATE *t;
  61. for (t = host->templates; t && t->next != rt; t = t->next ) ;
  62. if(t) {
  63. t->next = rt->next;
  64. rt->next = NULL;
  65. }
  66. else
  67. error("Cannot find RRDCALCTEMPLATE '%s' linked in host '%s'", rt->name, host->hostname);
  68. }
  69. rrdcalctemplate_free(rt);
  70. }