rrdcalctemplate.c 9.0 KB

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