rrdcalctemplate.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_check_conditions_and_link(RRDCALCTEMPLATE *rt, RRDSET *st, RRDHOST *host) {
  13. if(rt->hash_context != st->hash_context || strcmp(rt->context, st->context) != 0)
  14. return;
  15. if (rt->charts_pattern && !simple_pattern_matches(rt->charts_pattern, st->name))
  16. return;
  17. if (rt->family_pattern && !simple_pattern_matches(rt->family_pattern, st->family))
  18. return;
  19. if (rt->module_pattern && !simple_pattern_matches(rt->module_pattern, st->module_name))
  20. return;
  21. if (rt->plugin_pattern && !simple_pattern_matches(rt->plugin_pattern, st->plugin_name))
  22. return;
  23. if(host->host_labels && rt->host_labels_pattern && !rrdlabels_match_simple_pattern_parsed(host->host_labels, rt->host_labels_pattern, '='))
  24. return;
  25. RRDCALC *rc = rrdcalc_create_from_template(host, rt, st->id);
  26. if (unlikely(!rc))
  27. info("Health tried to create alarm from template '%s' on chart '%s' of host '%s', but it failed", rt->name, st->id, host->hostname);
  28. #ifdef NETDATA_INTERNAL_CHECKS
  29. else if (rc->rrdset != st && !rc->foreachdim) //When we have a template with foreadhdim, the child will be added to the index late
  30. error("Health alarm '%s.%s' should be linked to chart '%s', but it is not", rc->chart ? rc->chart : "NOCHART", rc->name, st->id);
  31. #endif
  32. }
  33. void rrdcalctemplate_link_matching(RRDSET *st) {
  34. RRDHOST *host = st->rrdhost;
  35. RRDCALCTEMPLATE *rt;
  36. for(rt = host->templates; rt ; rt = rt->next)
  37. rrdcalctemplate_check_conditions_and_link(rt, st, host);
  38. for(rt = host->alarms_template_with_foreach; rt ; rt = rt->next)
  39. rrdcalctemplate_check_conditions_and_link(rt, st, host);
  40. }
  41. inline void rrdcalctemplate_free(RRDCALCTEMPLATE *rt) {
  42. if(unlikely(!rt)) return;
  43. expression_free(rt->calculation);
  44. expression_free(rt->warning);
  45. expression_free(rt->critical);
  46. freez(rt->family_match);
  47. simple_pattern_free(rt->family_pattern);
  48. freez(rt->plugin_match);
  49. simple_pattern_free(rt->plugin_pattern);
  50. freez(rt->module_match);
  51. simple_pattern_free(rt->module_pattern);
  52. freez(rt->charts_match);
  53. simple_pattern_free(rt->charts_pattern);
  54. freez(rt->name);
  55. freez(rt->exec);
  56. freez(rt->recipient);
  57. freez(rt->classification);
  58. freez(rt->component);
  59. freez(rt->type);
  60. freez(rt->context);
  61. freez(rt->source);
  62. freez(rt->units);
  63. freez(rt->info);
  64. freez(rt->dimensions);
  65. freez(rt->foreachdim);
  66. freez(rt->host_labels);
  67. simple_pattern_free(rt->spdim);
  68. simple_pattern_free(rt->host_labels_pattern);
  69. freez(rt);
  70. }
  71. inline void rrdcalctemplate_unlink_and_free(RRDHOST *host, RRDCALCTEMPLATE *rt) {
  72. if(unlikely(!rt)) return;
  73. debug(D_HEALTH, "Health removing template '%s' of host '%s'", rt->name, host->hostname);
  74. if(host->templates == rt) {
  75. host->templates = rt->next;
  76. }
  77. else {
  78. RRDCALCTEMPLATE *t;
  79. for (t = host->templates; t && t->next != rt; t = t->next ) ;
  80. if(t) {
  81. t->next = rt->next;
  82. rt->next = NULL;
  83. }
  84. else
  85. error("Cannot find RRDCALCTEMPLATE '%s' linked in host '%s'", rt->name, host->hostname);
  86. }
  87. rrdcalctemplate_free(rt);
  88. }