rrdcalc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "rrd.h"
  3. #ifndef NETDATA_RRDCALC_H
  4. #define NETDATA_RRDCALC_H 1
  5. // calculated variables (defined in health configuration)
  6. // These aggregate time-series data at fixed intervals
  7. // (defined in their update_every member below)
  8. // They increase the overhead of netdata.
  9. //
  10. // These calculations are stored under RRDHOST.
  11. // Then are also linked to RRDSET (of course only when a
  12. // matching chart is found).
  13. typedef enum {
  14. RRDCALC_FLAG_DB_ERROR = (1 << 0),
  15. RRDCALC_FLAG_DB_NAN = (1 << 1),
  16. // RRDCALC_FLAG_DB_STALE = (1 << 2),
  17. RRDCALC_FLAG_CALC_ERROR = (1 << 3),
  18. RRDCALC_FLAG_WARN_ERROR = (1 << 4),
  19. RRDCALC_FLAG_CRIT_ERROR = (1 << 5),
  20. RRDCALC_FLAG_RUNNABLE = (1 << 6),
  21. RRDCALC_FLAG_DISABLED = (1 << 7),
  22. RRDCALC_FLAG_SILENCED = (1 << 8),
  23. RRDCALC_FLAG_RUN_ONCE = (1 << 9),
  24. RRDCALC_FLAG_FROM_TEMPLATE = (1 << 10), // the rrdcalc has been created from a template
  25. } RRDCALC_FLAGS;
  26. void rrdcalc_flags_to_json_array(BUFFER *wb, const char *key, RRDCALC_FLAGS flags);
  27. typedef enum {
  28. // This list uses several other options from RRDR_OPTIONS for db lookups.
  29. // To add an item here, you need to reserve a bit in RRDR_OPTIONS.
  30. RRDCALC_OPTION_NO_CLEAR_NOTIFICATION = RRDR_OPTION_HEALTH_RSRVD1,
  31. } RRDCALC_OPTIONS;
  32. #define RRDCALC_ALL_OPTIONS_EXCLUDING_THE_RRDR_ONES (RRDCALC_OPTION_NO_CLEAR_NOTIFICATION)
  33. struct rrdcalc {
  34. STRING *key; // the unique key in the host's rrdcalc_root_index
  35. uint32_t id; // the unique id of this alarm
  36. uint32_t next_event_id; // the next event id that will be used for this alarm
  37. uuid_t config_hash_id; // a predictable hash_id based on specific alert configuration
  38. STRING *name; // the name of this alarm
  39. STRING *chart; // the chart id this should be linked to
  40. STRING *exec; // the command to execute when this alarm switches state
  41. STRING *recipient; // the recipient of the alarm (the first parameter to exec)
  42. STRING *classification; // the class that this alarm belongs
  43. STRING *component; // the component that this alarm refers to
  44. STRING *type; // type of the alarm
  45. STRING *plugin_match; // the plugin name that should be linked to
  46. SIMPLE_PATTERN *plugin_pattern;
  47. STRING *module_match; // the module name that should be linked to
  48. SIMPLE_PATTERN *module_pattern;
  49. STRING *source; // the source of this alarm
  50. STRING *units; // the units of the alarm
  51. STRING *summary; // a short alert summary
  52. STRING *original_summary; // the original summary field before any variable replacement
  53. STRING *original_info; // the original info field before any variable replacement
  54. STRING *info; // a description of the alarm
  55. int update_every; // update frequency for the alarm
  56. // the red and green threshold of this alarm (to be set to the chart)
  57. NETDATA_DOUBLE green;
  58. NETDATA_DOUBLE red;
  59. // ------------------------------------------------------------------------
  60. // database lookup settings
  61. STRING *dimensions; // the chart dimensions
  62. STRING *foreach_dimension; // the group of dimensions that the `foreach` will be applied.
  63. SIMPLE_PATTERN *foreach_dimension_pattern; // used if and only if there is a simple pattern for the chart.
  64. RRDR_TIME_GROUPING group; // grouping method: average, max, etc.
  65. int before; // ending point in time-series
  66. int after; // starting point in time-series
  67. RRDCALC_OPTIONS options; // configuration options
  68. // ------------------------------------------------------------------------
  69. // expressions related to the alarm
  70. EVAL_EXPRESSION *calculation; // expression to calculate the value of the alarm
  71. EVAL_EXPRESSION *warning; // expression to check the warning condition
  72. EVAL_EXPRESSION *critical; // expression to check the critical condition
  73. // ------------------------------------------------------------------------
  74. // notification delay settings
  75. int delay_up_duration; // duration to delay notifications when alarm raises
  76. int delay_down_duration; // duration to delay notifications when alarm lowers
  77. int delay_max_duration; // the absolute max delay to apply to this alarm
  78. float delay_multiplier; // multiplier for all delays when alarms switch status
  79. // while now < delay_up_to
  80. // ------------------------------------------------------------------------
  81. // notification repeat settings
  82. uint32_t warn_repeat_every; // interval between repeating warning notifications
  83. uint32_t crit_repeat_every; // interval between repeating critical notifications
  84. // ------------------------------------------------------------------------
  85. // Labels settings
  86. STRING *host_labels; // the label read from an alarm file
  87. SIMPLE_PATTERN *host_labels_pattern; // the simple pattern of labels
  88. STRING *chart_labels; // the chart label read from an alarm file
  89. SIMPLE_PATTERN *chart_labels_pattern; // the simple pattern of chart labels
  90. // ------------------------------------------------------------------------
  91. // runtime information
  92. RRDCALC_STATUS old_status; // the old status of the alarm
  93. RRDCALC_STATUS status; // the current status of the alarm
  94. NETDATA_DOUBLE value; // the current value of the alarm
  95. NETDATA_DOUBLE old_value; // the previous value of the alarm
  96. NETDATA_DOUBLE last_status_change_value; // the value at the last status change
  97. RRDCALC_FLAGS run_flags; // check RRDCALC_FLAG_*
  98. time_t last_updated; // the last update timestamp of the alarm
  99. time_t next_update; // the next update timestamp of the alarm
  100. time_t last_status_change; // the timestamp of the last time this alarm changed status
  101. time_t last_repeat; // the last time the alarm got repeated
  102. uint32_t times_repeat; // number of times the alarm got repeated
  103. time_t db_after; // the first timestamp evaluated by the db lookup
  104. time_t db_before; // the last timestamp evaluated by the db lookup
  105. time_t delay_up_to_timestamp; // the timestamp up to which we should delay notifications
  106. int delay_up_current; // the current up notification delay duration
  107. int delay_down_current; // the current down notification delay duration
  108. int delay_last; // the last delay we used
  109. // ------------------------------------------------------------------------
  110. // variables this alarm exposes to the rest of the alarms
  111. const RRDVAR_ACQUIRED *rrdvar_local;
  112. const RRDVAR_ACQUIRED *rrdvar_family;
  113. const RRDVAR_ACQUIRED *rrdvar_host_chart_id;
  114. const RRDVAR_ACQUIRED *rrdvar_host_chart_name;
  115. // ------------------------------------------------------------------------
  116. // the chart this alarm it is linked to
  117. size_t labels_version;
  118. struct rrdset *rrdset;
  119. struct rrdcalc *next;
  120. struct rrdcalc *prev;
  121. };
  122. #define rrdcalc_name(rc) string2str((rc)->name)
  123. #define rrdcalc_chart_name(rc) string2str((rc)->chart)
  124. #define rrdcalc_exec(rc) string2str((rc)->exec)
  125. #define rrdcalc_recipient(rc) string2str((rc)->recipient)
  126. #define rrdcalc_classification(rc) string2str((rc)->classification)
  127. #define rrdcalc_component(rc) string2str((rc)->component)
  128. #define rrdcalc_type(rc) string2str((rc)->type)
  129. #define rrdcalc_plugin_match(rc) string2str((rc)->plugin_match)
  130. #define rrdcalc_module_match(rc) string2str((rc)->module_match)
  131. #define rrdcalc_source(rc) string2str((rc)->source)
  132. #define rrdcalc_units(rc) string2str((rc)->units)
  133. #define rrdcalc_original_summary(rc) string2str((rc)->original_summary)
  134. #define rrdcalc_summary(rc) string2str((rc)->summary)
  135. #define rrdcalc_original_info(rc) string2str((rc)->original_info)
  136. #define rrdcalc_info(rc) string2str((rc)->info)
  137. #define rrdcalc_dimensions(rc) string2str((rc)->dimensions)
  138. #define rrdcalc_foreachdim(rc) string2str((rc)->foreach_dimension)
  139. #define rrdcalc_host_labels(rc) string2str((rc)->host_labels)
  140. #define rrdcalc_chart_labels(rc) string2str((rc)->chart_labels)
  141. #define foreach_rrdcalc_in_rrdhost_read(host, rc) \
  142. dfe_start_read((host)->rrdcalc_root_index, rc) \
  143. #define foreach_rrdcalc_in_rrdhost_reentrant(host, rc) \
  144. dfe_start_reentrant((host)->rrdcalc_root_index, rc)
  145. #define foreach_rrdcalc_in_rrdhost_done(rc) \
  146. dfe_done(rc)
  147. struct alert_config {
  148. STRING *alarm;
  149. STRING *template_key;
  150. STRING *os;
  151. STRING *host;
  152. STRING *on;
  153. STRING *plugin;
  154. STRING *module;
  155. STRING *charts;
  156. STRING *lookup;
  157. STRING *calc;
  158. STRING *warn;
  159. STRING *crit;
  160. STRING *every;
  161. STRING *green;
  162. STRING *red;
  163. STRING *exec;
  164. STRING *to;
  165. STRING *units;
  166. STRING *summary;
  167. STRING *info;
  168. STRING *classification;
  169. STRING *component;
  170. STRING *type;
  171. STRING *delay;
  172. STRING *options;
  173. STRING *repeat;
  174. STRING *host_labels;
  175. STRING *chart_labels;
  176. STRING *source;
  177. STRING *p_db_lookup_dimensions;
  178. STRING *p_db_lookup_method;
  179. uint32_t p_db_lookup_options;
  180. int32_t p_db_lookup_after;
  181. int32_t p_db_lookup_before;
  182. int32_t p_update_every;
  183. };
  184. #define RRDCALC_HAS_DB_LOOKUP(rc) ((rc)->after)
  185. void rrdcalc_update_info_using_rrdset_labels(RRDCALC *rc);
  186. void rrdcalc_link_matching_alerts_to_rrdset(RRDSET *st);
  187. const RRDCALC_ACQUIRED *rrdcalc_from_rrdset_get(RRDSET *st, const char *alert_name);
  188. void rrdcalc_from_rrdset_release(RRDSET *st, const RRDCALC_ACQUIRED *rca);
  189. RRDCALC *rrdcalc_acquired_to_rrdcalc(const RRDCALC_ACQUIRED *rca);
  190. const char *rrdcalc_status2string(RRDCALC_STATUS status);
  191. void rrdcalc_free_unused_rrdcalc_loaded_from_config(RRDCALC *rc);
  192. uint32_t rrdcalc_get_unique_id(RRDHOST *host, STRING *chart, STRING *name, uint32_t *next_event_id, uuid_t *config_hash_id);
  193. void rrdcalc_add_from_rrdcalctemplate(RRDHOST *host, RRDCALCTEMPLATE *rt, RRDSET *st, const char *overwrite_alert_name, const char *overwrite_dimensions);
  194. int rrdcalc_add_from_config(RRDHOST *host, RRDCALC *rc);
  195. void rrdcalc_delete_alerts_not_matching_host_labels_from_all_hosts();
  196. void rrdcalc_delete_alerts_not_matching_host_labels_from_this_host(RRDHOST *host);
  197. static inline int rrdcalc_isrepeating(RRDCALC *rc) {
  198. if (unlikely(rc->warn_repeat_every > 0 || rc->crit_repeat_every > 0)) {
  199. return 1;
  200. }
  201. return 0;
  202. }
  203. void rrdcalc_unlink_all_rrdset_alerts(RRDSET *st);
  204. void rrdcalc_delete_all(RRDHOST *host);
  205. void rrdcalc_rrdhost_index_init(RRDHOST *host);
  206. void rrdcalc_rrdhost_index_destroy(RRDHOST *host);
  207. #define RRDCALC_VAR_MAX 100
  208. #define RRDCALC_VAR_FAMILY "${family}"
  209. #define RRDCALC_VAR_LABEL "${label:"
  210. #define RRDCALC_VAR_LABEL_LEN (sizeof(RRDCALC_VAR_LABEL)-1)
  211. #endif //NETDATA_RRDCALC_H