rrdcalctemplate.c 9.5 KB

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