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