rrdcalc.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 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. char *exec; // the command to execute when this alarm switches state
  35. char *recipient; // the recipient of the alarm (the first parameter to exec)
  36. char *chart; // the chart id this should be linked to
  37. uint32_t hash_chart;
  38. char *source; // the source of this alarm
  39. char *units; // the units of the alarm
  40. char *info; // a short description of the alarm
  41. int update_every; // update frequency for the alarm
  42. // the red and green threshold of this alarm (to be set to the chart)
  43. calculated_number green;
  44. calculated_number red;
  45. // ------------------------------------------------------------------------
  46. // database lookup settings
  47. char *dimensions; // the chart dimensions
  48. char *foreachdim; // the group of dimensions that the `foreach` will be applied.
  49. SIMPLE_PATTERN *spdim; // used if and only if there is a simple pattern for the chart.
  50. int foreachcounter; // the number of alarms created with foreachdim, this also works as an id of the
  51. // children
  52. RRDR_GROUPING group; // grouping method: average, max, etc.
  53. int before; // ending point in time-series
  54. int after; // starting point in time-series
  55. uint32_t options; // calculation options
  56. // ------------------------------------------------------------------------
  57. // expressions related to the alarm
  58. EVAL_EXPRESSION *calculation; // expression to calculate the value of the alarm
  59. EVAL_EXPRESSION *warning; // expression to check the warning condition
  60. EVAL_EXPRESSION *critical; // expression to check the critical condition
  61. // ------------------------------------------------------------------------
  62. // notification delay settings
  63. int delay_up_duration; // duration to delay notifications when alarm raises
  64. int delay_down_duration; // duration to delay notifications when alarm lowers
  65. int delay_max_duration; // the absolute max delay to apply to this alarm
  66. float delay_multiplier; // multiplier for all delays when alarms switch status
  67. // while now < delay_up_to
  68. // ------------------------------------------------------------------------
  69. // notification repeat settings
  70. uint32_t warn_repeat_every; // interval between repeating warning notifications
  71. uint32_t crit_repeat_every; // interval between repeating critical notifications
  72. // ------------------------------------------------------------------------
  73. // Labels settings
  74. char *labels; // the label read from an alarm file
  75. SIMPLE_PATTERN *splabels; // the simple pattern of labels
  76. // ------------------------------------------------------------------------
  77. // runtime information
  78. RRDCALC_STATUS old_status; // the old status of the alarm
  79. RRDCALC_STATUS status; // the current status of the alarm
  80. calculated_number value; // the current value of the alarm
  81. calculated_number old_value; // the previous value of the alarm
  82. uint32_t rrdcalc_flags; // check RRDCALC_FLAG_*
  83. time_t last_updated; // the last update timestamp of the alarm
  84. time_t next_update; // the next update timestamp of the alarm
  85. time_t last_status_change; // the timestamp of the last time this alarm changed status
  86. time_t last_repeat; // the last time the alarm got repeated
  87. time_t db_after; // the first timestamp evaluated by the db lookup
  88. time_t db_before; // the last timestamp evaluated by the db lookup
  89. time_t delay_up_to_timestamp; // the timestamp up to which we should delay notifications
  90. int delay_up_current; // the current up notification delay duration
  91. int delay_down_current; // the current down notification delay duration
  92. int delay_last; // the last delay we used
  93. // ------------------------------------------------------------------------
  94. // variables this alarm exposes to the rest of the alarms
  95. RRDVAR *local;
  96. RRDVAR *family;
  97. RRDVAR *hostid;
  98. RRDVAR *hostname;
  99. // ------------------------------------------------------------------------
  100. // the chart this alarm it is linked to
  101. struct rrdset *rrdset;
  102. // linking of this alarm on its chart
  103. struct rrdcalc *rrdset_next;
  104. struct rrdcalc *rrdset_prev;
  105. struct rrdcalc *next;
  106. };
  107. extern int alarm_isrepeating(RRDHOST *host, uint32_t alarm_id);
  108. extern int alarm_entry_isrepeating(RRDHOST *host, ALARM_ENTRY *ae);
  109. extern RRDCALC *alarm_max_last_repeat(RRDHOST *host, char *alarm_name, uint32_t hash);
  110. #define RRDCALC_HAS_DB_LOOKUP(rc) ((rc)->after)
  111. extern void rrdsetcalc_link_matching(RRDSET *st);
  112. extern void rrdsetcalc_unlink(RRDCALC *rc);
  113. extern RRDCALC *rrdcalc_find(RRDSET *st, const char *name);
  114. extern const char *rrdcalc_status2string(RRDCALC_STATUS status);
  115. extern void rrdcalc_free(RRDCALC *rc);
  116. extern void rrdcalc_unlink_and_free(RRDHOST *host, RRDCALC *rc);
  117. extern int rrdcalc_exists(RRDHOST *host, const char *chart, const char *name, uint32_t hash_chart, uint32_t hash_name);
  118. extern uint32_t rrdcalc_get_unique_id(RRDHOST *host, const char *chart, const char *name, uint32_t *next_event_id);
  119. extern RRDCALC *rrdcalc_create_from_template(RRDHOST *host, RRDCALCTEMPLATE *rt, const char *chart);
  120. extern RRDCALC *rrdcalc_create_from_rrdcalc(RRDCALC *rc, RRDHOST *host, const char *name, const char *dimension);
  121. extern void rrdcalc_add_to_host(RRDHOST *host, RRDCALC *rc);
  122. extern void dimension_remove_pipe_comma(char *str);
  123. extern char *alarm_name_with_dim(char *name, size_t namelen, const char *dim, size_t dimlen);
  124. extern void rrdcalc_labels_unlink();
  125. extern void rrdcalc_labels_unlink_alarm_from_host(RRDHOST *host);
  126. static inline int rrdcalc_isrepeating(RRDCALC *rc) {
  127. if (unlikely(rc->warn_repeat_every > 0 || rc->crit_repeat_every > 0)) {
  128. return 1;
  129. }
  130. return 0;
  131. }
  132. #endif //NETDATA_RRDCALC_H