rrdcalctemplate.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "rrd.h"
  3. // ----------------------------------------------------------------------------
  4. // RRDCALCTEMPLATE management
  5. /**
  6. * RRDCALC TEMPLATE LINK MATCHING
  7. *
  8. * @param rt is the template used to create the chart.
  9. * @param st is the chart where the alarm will be attached.
  10. */
  11. static char *rrdcalc_alert_name_with_dimension(const char *name, size_t namelen, const char *dim, size_t dimlen) {
  12. char *newname,*move;
  13. newname = mallocz(namelen + dimlen + 2);
  14. move = newname;
  15. memcpy(move, name, namelen);
  16. move += namelen;
  17. *move++ = '_';
  18. memcpy(move, dim, dimlen);
  19. move += dimlen;
  20. *move = '\0';
  21. return newname;
  22. }
  23. bool rrdcalctemplate_check_rrdset_conditions(RRDCALCTEMPLATE *rt, RRDSET *st, RRDHOST *host) {
  24. if(rt->context != st->context)
  25. return false;
  26. if(rt->foreach_dimension_pattern && !rrdset_number_of_dimensions(st))
  27. return false;
  28. if (rt->charts_pattern && !simple_pattern_matches(rt->charts_pattern, rrdset_name(st)) && !simple_pattern_matches(rt->charts_pattern, rrdset_id(st)))
  29. return false;
  30. if (rt->family_pattern && !simple_pattern_matches(rt->family_pattern, rrdset_family(st)))
  31. return false;
  32. if (rt->module_pattern && !simple_pattern_matches(rt->module_pattern, rrdset_module_name(st)))
  33. return false;
  34. if (rt->plugin_pattern && !simple_pattern_matches(rt->plugin_pattern, rrdset_plugin_name(st)))
  35. return false;
  36. if(host->rrdlabels && rt->host_labels_pattern && !rrdlabels_match_simple_pattern_parsed(host->rrdlabels, rt->host_labels_pattern, '='))
  37. return false;
  38. return true;
  39. }
  40. void rrdcalctemplate_check_rrddim_conditions_and_link(RRDCALCTEMPLATE *rt, RRDSET *st, RRDDIM *rd, RRDHOST *host) {
  41. if (simple_pattern_matches(rt->foreach_dimension_pattern, rrddim_id(rd)) || simple_pattern_matches(rt->foreach_dimension_pattern, rrddim_name(rd))) {
  42. char *overwrite_alert_name = rrdcalc_alert_name_with_dimension(
  43. rrdcalctemplate_name(rt), string_strlen(rt->name), rrddim_name(rd), string_strlen(rd->name));
  44. rrdcalc_add_from_rrdcalctemplate(host, rt, st, overwrite_alert_name, rrddim_name(rd));
  45. freez(overwrite_alert_name);
  46. }
  47. }
  48. void rrdcalctemplate_check_conditions_and_link(RRDCALCTEMPLATE *rt, RRDSET *st, RRDHOST *host) {
  49. if(!rrdcalctemplate_check_rrdset_conditions(rt, st, host))
  50. return;
  51. if(!rt->foreach_dimension_pattern) {
  52. rrdcalc_add_from_rrdcalctemplate(host, rt, st, NULL, NULL);
  53. return;
  54. }
  55. RRDDIM *rd;
  56. rrddim_foreach_read(rd, st) {
  57. rrdcalctemplate_check_rrddim_conditions_and_link(rt, st, rd, host);
  58. }
  59. rrddim_foreach_done(rd);
  60. }
  61. void rrdcalctemplate_link_matching_templates_to_rrdset(RRDSET *st) {
  62. RRDHOST *host = st->rrdhost;
  63. RRDCALCTEMPLATE *rt;
  64. foreach_rrdcalctemplate_read(host, rt) {
  65. rrdcalctemplate_check_conditions_and_link(rt, st, host);
  66. }
  67. foreach_rrdcalctemplate_done(rt);
  68. }
  69. static void rrdcalctemplate_free_internals(RRDCALCTEMPLATE *rt) {
  70. expression_free(rt->calculation);
  71. expression_free(rt->warning);
  72. expression_free(rt->critical);
  73. string_freez(rt->family_match);
  74. simple_pattern_free(rt->family_pattern);
  75. string_freez(rt->plugin_match);
  76. simple_pattern_free(rt->plugin_pattern);
  77. string_freez(rt->module_match);
  78. simple_pattern_free(rt->module_pattern);
  79. string_freez(rt->charts_match);
  80. simple_pattern_free(rt->charts_pattern);
  81. string_freez(rt->name);
  82. string_freez(rt->exec);
  83. string_freez(rt->recipient);
  84. string_freez(rt->classification);
  85. string_freez(rt->component);
  86. string_freez(rt->type);
  87. string_freez(rt->context);
  88. string_freez(rt->source);
  89. string_freez(rt->units);
  90. string_freez(rt->info);
  91. string_freez(rt->dimensions);
  92. string_freez(rt->foreach_dimension);
  93. string_freez(rt->host_labels);
  94. simple_pattern_free(rt->foreach_dimension_pattern);
  95. simple_pattern_free(rt->host_labels_pattern);
  96. }
  97. void rrdcalctemplate_free_unused_rrdcalctemplate_loaded_from_config(RRDCALCTEMPLATE *rt) {
  98. if(unlikely(!rt)) return;
  99. rrdcalctemplate_free_internals(rt);
  100. freez(rt);
  101. }
  102. static void rrdcalctemplate_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdcalctemplate, void *added_bool) {
  103. RRDCALCTEMPLATE *rt = rrdcalctemplate; (void)rt;
  104. bool *added = added_bool;
  105. *added = true;
  106. debug(D_HEALTH, "Health configuration adding template '%s'"
  107. ": context '%s'"
  108. ", exec '%s'"
  109. ", recipient '%s'"
  110. ", green " NETDATA_DOUBLE_FORMAT_AUTO
  111. ", red " NETDATA_DOUBLE_FORMAT_AUTO
  112. ", lookup: group %d"
  113. ", after %d"
  114. ", before %d"
  115. ", options %u"
  116. ", dimensions '%s'"
  117. ", for each dimension '%s'"
  118. ", update every %d"
  119. ", calculation '%s'"
  120. ", warning '%s'"
  121. ", critical '%s'"
  122. ", source '%s'"
  123. ", delay up %d"
  124. ", delay down %d"
  125. ", delay max %d"
  126. ", delay_multiplier %f"
  127. ", warn_repeat_every %u"
  128. ", crit_repeat_every %u",
  129. rrdcalctemplate_name(rt),
  130. (rt->context)?string2str(rt->context):"NONE",
  131. (rt->exec)?rrdcalctemplate_exec(rt):"DEFAULT",
  132. (rt->recipient)?rrdcalctemplate_recipient(rt):"DEFAULT",
  133. rt->green,
  134. rt->red,
  135. (int)rt->group,
  136. rt->after,
  137. rt->before,
  138. rt->options,
  139. (rt->dimensions)?rrdcalctemplate_dimensions(rt):"NONE",
  140. (rt->foreach_dimension)?rrdcalctemplate_foreachdim(rt):"NONE",
  141. rt->update_every,
  142. (rt->calculation)?rt->calculation->parsed_as:"NONE",
  143. (rt->warning)?rt->warning->parsed_as:"NONE",
  144. (rt->critical)?rt->critical->parsed_as:"NONE",
  145. rrdcalctemplate_source(rt),
  146. rt->delay_up_duration,
  147. rt->delay_down_duration,
  148. rt->delay_max_duration,
  149. rt->delay_multiplier,
  150. rt->warn_repeat_every,
  151. rt->crit_repeat_every
  152. );
  153. }
  154. static void rrdcalctemplate_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdcalctemplate, void *rrdhost __maybe_unused) {
  155. RRDCALCTEMPLATE *rt = rrdcalctemplate;
  156. rrdcalctemplate_free_internals(rt);
  157. }
  158. void rrdcalctemplate_index_init(RRDHOST *host) {
  159. if(!host->rrdcalctemplate_root_index) {
  160. host->rrdcalctemplate_root_index = dictionary_create_advanced(DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  161. &dictionary_stats_category_rrdhealth, sizeof(RRDCALCTEMPLATE));
  162. dictionary_register_insert_callback(host->rrdcalctemplate_root_index, rrdcalctemplate_insert_callback, NULL);
  163. dictionary_register_delete_callback(host->rrdcalctemplate_root_index, rrdcalctemplate_delete_callback, host);
  164. }
  165. }
  166. void rrdcalctemplate_index_destroy(RRDHOST *host) {
  167. dictionary_destroy(host->rrdcalctemplate_root_index);
  168. host->rrdcalctemplate_root_index = NULL;
  169. }
  170. inline void rrdcalctemplate_delete_all(RRDHOST *host) {
  171. dictionary_flush(host->rrdcalctemplate_root_index);
  172. }
  173. #define RRDCALCTEMPLATE_MAX_KEY_SIZE 1024
  174. static size_t rrdcalctemplate_key(char *dst, size_t dst_len, const char *name, const char *family_match) {
  175. return snprintfz(dst, dst_len, "%s/%s", name, (family_match && *family_match)?family_match:"*");
  176. }
  177. void rrdcalctemplate_add_from_config(RRDHOST *host, RRDCALCTEMPLATE *rt) {
  178. if(unlikely(!rt->context)) {
  179. error("Health configuration for template '%s' does not have a context", rrdcalctemplate_name(rt));
  180. return;
  181. }
  182. if(unlikely(!rt->update_every)) {
  183. error("Health configuration for template '%s' has no frequency (parameter 'every'). Ignoring it.", rrdcalctemplate_name(rt));
  184. return;
  185. }
  186. if(unlikely(!RRDCALCTEMPLATE_HAS_DB_LOOKUP(rt) && !rt->calculation && !rt->warning && !rt->critical)) {
  187. error("Health configuration for template '%s' is useless (no calculation, no warning and no critical evaluation)", rrdcalctemplate_name(rt));
  188. return;
  189. }
  190. char key[RRDCALCTEMPLATE_MAX_KEY_SIZE + 1];
  191. size_t key_len = rrdcalctemplate_key(key, RRDCALCTEMPLATE_MAX_KEY_SIZE, rrdcalctemplate_name(rt), rrdcalctemplate_family_match(rt));
  192. bool added = false;
  193. dictionary_set_advanced(host->rrdcalctemplate_root_index, key, (ssize_t)(key_len + 1), rt, sizeof(*rt), &added);
  194. if(added)
  195. freez(rt);
  196. else {
  197. info("Health configuration template '%s' already exists for host '%s'.", rrdcalctemplate_name(rt), rrdhost_hostname(host));
  198. rrdcalctemplate_free_unused_rrdcalctemplate_loaded_from_config(rt);
  199. }
  200. }