rrdcalc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 *original_info; // the original info field before any variable replacement
  52. STRING *info; // a short description of the alarm
  53. int update_every; // update frequency for the alarm
  54. // the red and green threshold of this alarm (to be set to the chart)
  55. NETDATA_DOUBLE green;
  56. NETDATA_DOUBLE red;
  57. // ------------------------------------------------------------------------
  58. // database lookup settings
  59. STRING *dimensions; // the chart dimensions
  60. STRING *foreach_dimension; // the group of dimensions that the `foreach` will be applied.
  61. SIMPLE_PATTERN *foreach_dimension_pattern; // used if and only if there is a simple pattern for the chart.
  62. RRDR_TIME_GROUPING group; // grouping method: average, max, etc.
  63. int before; // ending point in time-series
  64. int after; // starting point in time-series
  65. RRDCALC_OPTIONS options; // configuration options
  66. // ------------------------------------------------------------------------
  67. // expressions related to the alarm
  68. EVAL_EXPRESSION *calculation; // expression to calculate the value of the alarm
  69. EVAL_EXPRESSION *warning; // expression to check the warning condition
  70. EVAL_EXPRESSION *critical; // expression to check the critical condition
  71. // ------------------------------------------------------------------------
  72. // notification delay settings
  73. int delay_up_duration; // duration to delay notifications when alarm raises
  74. int delay_down_duration; // duration to delay notifications when alarm lowers
  75. int delay_max_duration; // the absolute max delay to apply to this alarm
  76. float delay_multiplier; // multiplier for all delays when alarms switch status
  77. // while now < delay_up_to
  78. // ------------------------------------------------------------------------
  79. // notification repeat settings
  80. uint32_t warn_repeat_every; // interval between repeating warning notifications
  81. uint32_t crit_repeat_every; // interval between repeating critical notifications
  82. // ------------------------------------------------------------------------
  83. // Labels settings
  84. STRING *host_labels; // the label read from an alarm file
  85. SIMPLE_PATTERN *host_labels_pattern; // the simple pattern of labels
  86. STRING *chart_labels; // the chart label read from an alarm file
  87. SIMPLE_PATTERN *chart_labels_pattern; // the simple pattern of chart labels
  88. // ------------------------------------------------------------------------
  89. // runtime information
  90. RRDCALC_STATUS old_status; // the old status of the alarm
  91. RRDCALC_STATUS status; // the current status of the alarm
  92. NETDATA_DOUBLE value; // the current value of the alarm
  93. NETDATA_DOUBLE old_value; // the previous value of the alarm
  94. NETDATA_DOUBLE last_status_change_value; // the value at the last status change
  95. RRDCALC_FLAGS run_flags; // check RRDCALC_FLAG_*
  96. time_t last_updated; // the last update timestamp of the alarm
  97. time_t next_update; // the next update timestamp of the alarm
  98. time_t last_status_change; // the timestamp of the last time this alarm changed status
  99. time_t last_repeat; // the last time the alarm got repeated
  100. uint32_t times_repeat; // number of times the alarm got repeated
  101. time_t db_after; // the first timestamp evaluated by the db lookup
  102. time_t db_before; // the last timestamp evaluated by the db lookup
  103. time_t delay_up_to_timestamp; // the timestamp up to which we should delay notifications
  104. int delay_up_current; // the current up notification delay duration
  105. int delay_down_current; // the current down notification delay duration
  106. int delay_last; // the last delay we used
  107. ALARM_ENTRY *ae; // last alarm entry
  108. // ------------------------------------------------------------------------
  109. // variables this alarm exposes to the rest of the alarms
  110. const RRDVAR_ACQUIRED *rrdvar_local;
  111. const RRDVAR_ACQUIRED *rrdvar_family;
  112. const RRDVAR_ACQUIRED *rrdvar_host_chart_id;
  113. const RRDVAR_ACQUIRED *rrdvar_host_chart_name;
  114. // ------------------------------------------------------------------------
  115. // the chart this alarm it is linked to
  116. size_t labels_version;
  117. struct rrdset *rrdset;
  118. struct rrdcalc *next;
  119. struct rrdcalc *prev;
  120. };
  121. #define rrdcalc_name(rc) string2str((rc)->name)
  122. #define rrdcalc_chart_name(rc) string2str((rc)->chart)
  123. #define rrdcalc_exec(rc) string2str((rc)->exec)
  124. #define rrdcalc_recipient(rc) string2str((rc)->recipient)
  125. #define rrdcalc_classification(rc) string2str((rc)->classification)
  126. #define rrdcalc_component(rc) string2str((rc)->component)
  127. #define rrdcalc_type(rc) string2str((rc)->type)
  128. #define rrdcalc_plugin_match(rc) string2str((rc)->plugin_match)
  129. #define rrdcalc_module_match(rc) string2str((rc)->module_match)
  130. #define rrdcalc_source(rc) string2str((rc)->source)
  131. #define rrdcalc_units(rc) string2str((rc)->units)
  132. #define rrdcalc_original_info(rc) string2str((rc)->original_info)
  133. #define rrdcalc_info(rc) string2str((rc)->info)
  134. #define rrdcalc_dimensions(rc) string2str((rc)->dimensions)
  135. #define rrdcalc_foreachdim(rc) string2str((rc)->foreach_dimension)
  136. #define rrdcalc_host_labels(rc) string2str((rc)->host_labels)
  137. #define rrdcalc_chart_labels(rc) string2str((rc)->chart_labels)
  138. #define foreach_rrdcalc_in_rrdhost_read(host, rc) \
  139. dfe_start_read((host)->rrdcalc_root_index, rc) \
  140. #define foreach_rrdcalc_in_rrdhost_reentrant(host, rc) \
  141. dfe_start_reentrant((host)->rrdcalc_root_index, rc)
  142. #define foreach_rrdcalc_in_rrdhost_done(rc) \
  143. dfe_done(rc)
  144. struct alert_config {
  145. STRING *alarm;
  146. STRING *template_key;
  147. STRING *os;
  148. STRING *host;
  149. STRING *on;
  150. STRING *families;
  151. STRING *plugin;
  152. STRING *module;
  153. STRING *charts;
  154. STRING *lookup;
  155. STRING *calc;
  156. STRING *warn;
  157. STRING *crit;
  158. STRING *every;
  159. STRING *green;
  160. STRING *red;
  161. STRING *exec;
  162. STRING *to;
  163. STRING *units;
  164. STRING *info;
  165. STRING *classification;
  166. STRING *component;
  167. STRING *type;
  168. STRING *delay;
  169. STRING *options;
  170. STRING *repeat;
  171. STRING *host_labels;
  172. STRING *chart_labels;
  173. STRING *source;
  174. STRING *p_db_lookup_dimensions;
  175. STRING *p_db_lookup_method;
  176. uint32_t p_db_lookup_options;
  177. int32_t p_db_lookup_after;
  178. int32_t p_db_lookup_before;
  179. int32_t p_update_every;
  180. };
  181. #define RRDCALC_HAS_DB_LOOKUP(rc) ((rc)->after)
  182. void rrdcalc_update_info_using_rrdset_labels(RRDCALC *rc);
  183. void rrdcalc_link_matching_alerts_to_rrdset(RRDSET *st);
  184. const RRDCALC_ACQUIRED *rrdcalc_from_rrdset_get(RRDSET *st, const char *alert_name);
  185. void rrdcalc_from_rrdset_release(RRDSET *st, const RRDCALC_ACQUIRED *rca);
  186. RRDCALC *rrdcalc_acquired_to_rrdcalc(const RRDCALC_ACQUIRED *rca);
  187. const char *rrdcalc_status2string(RRDCALC_STATUS status);
  188. void rrdcalc_free_unused_rrdcalc_loaded_from_config(RRDCALC *rc);
  189. uint32_t rrdcalc_get_unique_id(RRDHOST *host, STRING *chart, STRING *name, uint32_t *next_event_id, uuid_t *config_hash_id);
  190. void rrdcalc_add_from_rrdcalctemplate(RRDHOST *host, RRDCALCTEMPLATE *rt, RRDSET *st, const char *overwrite_alert_name, const char *overwrite_dimensions);
  191. int rrdcalc_add_from_config(RRDHOST *host, RRDCALC *rc);
  192. void rrdcalc_delete_alerts_not_matching_host_labels_from_all_hosts();
  193. void rrdcalc_delete_alerts_not_matching_host_labels_from_this_host(RRDHOST *host);
  194. static inline int rrdcalc_isrepeating(RRDCALC *rc) {
  195. if (unlikely(rc->warn_repeat_every > 0 || rc->crit_repeat_every > 0)) {
  196. return 1;
  197. }
  198. return 0;
  199. }
  200. void rrdcalc_unlink_all_rrdset_alerts(RRDSET *st);
  201. void rrdcalc_delete_all(RRDHOST *host);
  202. void rrdcalc_rrdhost_index_init(RRDHOST *host);
  203. void rrdcalc_rrdhost_index_destroy(RRDHOST *host);
  204. #define RRDCALC_VAR_MAX 100
  205. #define RRDCALC_VAR_FAMILY "${family}"
  206. #define RRDCALC_VAR_LABEL "${label:"
  207. #define RRDCALC_VAR_LABEL_LEN (sizeof(RRDCALC_VAR_LABEL)-1)
  208. #endif //NETDATA_RRDCALC_H