rrdcalc.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 allocated and linked (->next)
  11. // under RRDHOST.
  12. // Then are also linked to RRDSET (of course only when the
  13. // chart is found, via ->rrdset_next and ->rrdset_prev).
  14. // This double-linked list is maintained sorted at all times
  15. // having as RRDSET.calculations the RRDCALC to be processed
  16. // next.
  17. #define RRDCALC_FLAG_DB_ERROR 0x00000001
  18. #define RRDCALC_FLAG_DB_NAN 0x00000002
  19. /* #define RRDCALC_FLAG_DB_STALE 0x00000004 */
  20. #define RRDCALC_FLAG_CALC_ERROR 0x00000008
  21. #define RRDCALC_FLAG_WARN_ERROR 0x00000010
  22. #define RRDCALC_FLAG_CRIT_ERROR 0x00000020
  23. #define RRDCALC_FLAG_RUNNABLE 0x00000040
  24. #define RRDCALC_FLAG_DISABLED 0x00000080
  25. #define RRDCALC_FLAG_SILENCED 0x00000100
  26. #define RRDCALC_FLAG_RUN_ONCE 0x00000200
  27. #define RRDCALC_FLAG_NO_CLEAR_NOTIFICATION 0x80000000
  28. struct rrdcalc {
  29. avl_t avl; // the index, with key the id - this has to be first!
  30. uint32_t id; // the unique id of this alarm
  31. uint32_t next_event_id; // the next event id that will be used for this alarm
  32. char *name; // the name of this alarm
  33. uint32_t hash; // the hash of the alarm name
  34. uuid_t config_hash_id; // a predictable hash_id based on specific alert configuration
  35. char *exec; // the command to execute when this alarm switches state
  36. char *recipient; // the recipient of the alarm (the first parameter to exec)
  37. char *classification; // the class that this alarm belongs
  38. char *component; // the component that this alarm refers to
  39. char *type; // type of the alarm
  40. char *chart; // the chart id this should be linked to
  41. uint32_t hash_chart;
  42. char *plugin_match; //the plugin name that should be linked to
  43. SIMPLE_PATTERN *plugin_pattern;
  44. char *module_match; //the module name that should be linked to
  45. SIMPLE_PATTERN *module_pattern;
  46. char *source; // the source of this alarm
  47. char *units; // the units of the alarm
  48. char *info; // a short description of the alarm
  49. int update_every; // update frequency for the alarm
  50. // the red and green threshold of this alarm (to be set to the chart)
  51. calculated_number green;
  52. calculated_number red;
  53. // ------------------------------------------------------------------------
  54. // database lookup settings
  55. char *dimensions; // the chart dimensions
  56. char *foreachdim; // the group of dimensions that the `foreach` will be applied.
  57. SIMPLE_PATTERN *spdim; // used if and only if there is a simple pattern for the chart.
  58. int foreachcounter; // the number of alarms created with foreachdim, this also works as an id of the
  59. // children
  60. RRDR_GROUPING group; // grouping method: average, max, etc.
  61. int before; // ending point in time-series
  62. int after; // starting point in time-series
  63. uint32_t options; // calculation options
  64. // ------------------------------------------------------------------------
  65. // expressions related to the alarm
  66. EVAL_EXPRESSION *calculation; // expression to calculate the value of the alarm
  67. EVAL_EXPRESSION *warning; // expression to check the warning condition
  68. EVAL_EXPRESSION *critical; // expression to check the critical condition
  69. // ------------------------------------------------------------------------
  70. // notification delay settings
  71. int delay_up_duration; // duration to delay notifications when alarm raises
  72. int delay_down_duration; // duration to delay notifications when alarm lowers
  73. int delay_max_duration; // the absolute max delay to apply to this alarm
  74. float delay_multiplier; // multiplier for all delays when alarms switch status
  75. // while now < delay_up_to
  76. // ------------------------------------------------------------------------
  77. // notification repeat settings
  78. uint32_t warn_repeat_every; // interval between repeating warning notifications
  79. uint32_t crit_repeat_every; // interval between repeating critical notifications
  80. // ------------------------------------------------------------------------
  81. // Labels settings
  82. char *labels; // the label read from an alarm file
  83. SIMPLE_PATTERN *splabels; // the simple pattern of labels
  84. // ------------------------------------------------------------------------
  85. // runtime information
  86. RRDCALC_STATUS old_status; // the old status of the alarm
  87. RRDCALC_STATUS status; // the current status of the alarm
  88. calculated_number value; // the current value of the alarm
  89. calculated_number old_value; // the previous value of the alarm
  90. uint32_t rrdcalc_flags; // check RRDCALC_FLAG_*
  91. time_t last_updated; // the last update timestamp of the alarm
  92. time_t next_update; // the next update timestamp of the alarm
  93. time_t last_status_change; // the timestamp of the last time this alarm changed status
  94. time_t last_repeat; // the last time the alarm got repeated
  95. uint32_t times_repeat; // number of times the alarm got repeated
  96. time_t db_after; // the first timestamp evaluated by the db lookup
  97. time_t db_before; // the last timestamp evaluated by the db lookup
  98. time_t delay_up_to_timestamp; // the timestamp up to which we should delay notifications
  99. int delay_up_current; // the current up notification delay duration
  100. int delay_down_current; // the current down notification delay duration
  101. int delay_last; // the last delay we used
  102. // ------------------------------------------------------------------------
  103. // variables this alarm exposes to the rest of the alarms
  104. RRDVAR *local;
  105. RRDVAR *family;
  106. RRDVAR *hostid;
  107. RRDVAR *hostname;
  108. // ------------------------------------------------------------------------
  109. // the chart this alarm it is linked to
  110. struct rrdset *rrdset;
  111. // linking of this alarm on its chart
  112. struct rrdcalc *rrdset_next;
  113. struct rrdcalc *rrdset_prev;
  114. struct rrdcalc *next;
  115. };
  116. struct alert_config {
  117. char *alarm;
  118. char *template_key;
  119. char *os;
  120. char *host;
  121. char *on;
  122. char *families;
  123. char *plugin;
  124. char *module;
  125. char *charts;
  126. char *lookup;
  127. char *calc;
  128. char *warn;
  129. char *crit;
  130. char *every;
  131. char *green;
  132. char *red;
  133. char *exec;
  134. char *to;
  135. char *units;
  136. char *info;
  137. char *classification;
  138. char *component;
  139. char *type;
  140. char *delay;
  141. char *options;
  142. char *repeat;
  143. char *host_labels;
  144. char *p_db_lookup_dimensions;
  145. char *p_db_lookup_method;
  146. uint32_t p_db_lookup_options;
  147. int32_t p_db_lookup_after;
  148. int32_t p_db_lookup_before;
  149. int32_t p_update_every;
  150. };
  151. extern int alarm_isrepeating(RRDHOST *host, uint32_t alarm_id);
  152. extern int alarm_entry_isrepeating(RRDHOST *host, ALARM_ENTRY *ae);
  153. extern RRDCALC *alarm_max_last_repeat(RRDHOST *host, char *alarm_name, uint32_t hash);
  154. #define RRDCALC_HAS_DB_LOOKUP(rc) ((rc)->after)
  155. extern void rrdsetcalc_link_matching(RRDSET *st);
  156. extern void rrdsetcalc_unlink(RRDCALC *rc);
  157. extern RRDCALC *rrdcalc_find(RRDSET *st, const char *name);
  158. extern const char *rrdcalc_status2string(RRDCALC_STATUS status);
  159. extern void rrdcalc_free(RRDCALC *rc);
  160. extern void rrdcalc_unlink_and_free(RRDHOST *host, RRDCALC *rc);
  161. extern int rrdcalc_exists(RRDHOST *host, const char *chart, const char *name, uint32_t hash_chart, uint32_t hash_name);
  162. extern uint32_t rrdcalc_get_unique_id(RRDHOST *host, const char *chart, const char *name, uint32_t *next_event_id);
  163. extern RRDCALC *rrdcalc_create_from_template(RRDHOST *host, RRDCALCTEMPLATE *rt, const char *chart);
  164. extern RRDCALC *rrdcalc_create_from_rrdcalc(RRDCALC *rc, RRDHOST *host, const char *name, const char *dimension);
  165. extern void rrdcalc_add_to_host(RRDHOST *host, RRDCALC *rc);
  166. extern void dimension_remove_pipe_comma(char *str);
  167. extern char *alarm_name_with_dim(char *name, size_t namelen, const char *dim, size_t dimlen);
  168. extern void rrdcalc_labels_unlink();
  169. extern void rrdcalc_labels_unlink_alarm_from_host(RRDHOST *host);
  170. static inline int rrdcalc_isrepeating(RRDCALC *rc) {
  171. if (unlikely(rc->warn_repeat_every > 0 || rc->crit_repeat_every > 0)) {
  172. return 1;
  173. }
  174. return 0;
  175. }
  176. #endif //NETDATA_RRDCALC_H