ml-private.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_ML_PRIVATE_H
  3. #define NETDATA_ML_PRIVATE_H
  4. #include "dlib/matrix.h"
  5. #include "ml/ml.h"
  6. #include <vector>
  7. #include <queue>
  8. typedef double calculated_number_t;
  9. typedef dlib::matrix<calculated_number_t, 6, 1> DSample;
  10. /*
  11. * Features
  12. */
  13. typedef struct {
  14. size_t diff_n;
  15. size_t smooth_n;
  16. size_t lag_n;
  17. calculated_number_t *dst;
  18. size_t dst_n;
  19. calculated_number_t *src;
  20. size_t src_n;
  21. std::vector<DSample> &preprocessed_features;
  22. } ml_features_t;
  23. /*
  24. * KMeans
  25. */
  26. typedef struct {
  27. size_t num_clusters;
  28. size_t max_iterations;
  29. std::vector<DSample> cluster_centers;
  30. calculated_number_t min_dist;
  31. calculated_number_t max_dist;
  32. } ml_kmeans_t;
  33. typedef struct machine_learning_stats_t {
  34. size_t num_machine_learning_status_enabled;
  35. size_t num_machine_learning_status_disabled_sp;
  36. size_t num_metric_type_constant;
  37. size_t num_metric_type_variable;
  38. size_t num_training_status_untrained;
  39. size_t num_training_status_pending_without_model;
  40. size_t num_training_status_trained;
  41. size_t num_training_status_pending_with_model;
  42. size_t num_anomalous_dimensions;
  43. size_t num_normal_dimensions;
  44. } ml_machine_learning_stats_t;
  45. typedef struct training_stats_t {
  46. size_t queue_size;
  47. size_t num_popped_items;
  48. usec_t allotted_ut;
  49. usec_t consumed_ut;
  50. usec_t remaining_ut;
  51. size_t training_result_ok;
  52. size_t training_result_invalid_query_time_range;
  53. size_t training_result_not_enough_collected_values;
  54. size_t training_result_null_acquired_dimension;
  55. size_t training_result_chart_under_replication;
  56. } ml_training_stats_t;
  57. enum ml_metric_type {
  58. // The dimension has constant values, no need to train
  59. METRIC_TYPE_CONSTANT,
  60. // The dimension's values fluctuate, we need to generate a model
  61. METRIC_TYPE_VARIABLE,
  62. };
  63. enum ml_machine_learning_status {
  64. // Enable training/prediction
  65. MACHINE_LEARNING_STATUS_ENABLED,
  66. // Disable because configuration pattern matches the chart's id
  67. MACHINE_LEARNING_STATUS_DISABLED_DUE_TO_EXCLUDED_CHART,
  68. };
  69. enum ml_training_status {
  70. // We don't have a model for this dimension
  71. TRAINING_STATUS_UNTRAINED,
  72. // Request for training sent, but we don't have any models yet
  73. TRAINING_STATUS_PENDING_WITHOUT_MODEL,
  74. // Request to update existing models sent
  75. TRAINING_STATUS_PENDING_WITH_MODEL,
  76. // Have a valid, up-to-date model
  77. TRAINING_STATUS_TRAINED,
  78. };
  79. enum ml_training_result {
  80. // We managed to create a KMeans model
  81. TRAINING_RESULT_OK,
  82. // Could not query DB with a correct time range
  83. TRAINING_RESULT_INVALID_QUERY_TIME_RANGE,
  84. // Did not gather enough data from DB to run KMeans
  85. TRAINING_RESULT_NOT_ENOUGH_COLLECTED_VALUES,
  86. // Acquired a null dimension
  87. TRAINING_RESULT_NULL_ACQUIRED_DIMENSION,
  88. // Chart is under replication
  89. TRAINING_RESULT_CHART_UNDER_REPLICATION,
  90. };
  91. typedef struct {
  92. // Chart/dimension we want to train
  93. STRING *chart_id;
  94. STRING *dimension_id;
  95. // Creation time of request
  96. time_t request_time;
  97. // First/last entry of this dimension in DB
  98. // at the point the request was made
  99. time_t first_entry_on_request;
  100. time_t last_entry_on_request;
  101. } ml_training_request_t;
  102. typedef struct {
  103. // Time when the request for this response was made
  104. time_t request_time;
  105. // First/last entry of the dimension in DB when generating the request
  106. time_t first_entry_on_request;
  107. time_t last_entry_on_request;
  108. // First/last entry of the dimension in DB when generating the response
  109. time_t first_entry_on_response;
  110. time_t last_entry_on_response;
  111. // After/Before timestamps of our DB query
  112. time_t query_after_t;
  113. time_t query_before_t;
  114. // Actual after/before returned by the DB query ops
  115. time_t db_after_t;
  116. time_t db_before_t;
  117. // Number of doubles returned by the DB query
  118. size_t collected_values;
  119. // Number of values we return to the caller
  120. size_t total_values;
  121. // Result of training response
  122. enum ml_training_result result;
  123. } ml_training_response_t;
  124. /*
  125. * Queue
  126. */
  127. typedef struct {
  128. std::queue<ml_training_request_t> internal;
  129. netdata_mutex_t mutex;
  130. pthread_cond_t cond_var;
  131. std::atomic<bool> exit;
  132. } ml_queue_t;
  133. typedef struct {
  134. RRDDIM *rd;
  135. enum ml_metric_type mt;
  136. enum ml_training_status ts;
  137. enum ml_machine_learning_status mls;
  138. ml_training_response_t tr;
  139. time_t last_training_time;
  140. std::vector<calculated_number_t> cns;
  141. std::vector<ml_kmeans_t> km_contexts;
  142. netdata_mutex_t mutex;
  143. ml_kmeans_t kmeans;
  144. std::vector<DSample> feature;
  145. } ml_dimension_t;
  146. typedef struct {
  147. RRDSET *rs;
  148. ml_machine_learning_stats_t mls;
  149. netdata_mutex_t mutex;
  150. } ml_chart_t;
  151. void ml_chart_update_dimension(ml_chart_t *chart, ml_dimension_t *dim, bool is_anomalous);
  152. typedef struct {
  153. RRDHOST *rh;
  154. ml_machine_learning_stats_t mls;
  155. ml_training_stats_t ts;
  156. calculated_number_t host_anomaly_rate;
  157. std::atomic<bool> threads_running;
  158. std::atomic<bool> threads_cancelled;
  159. std::atomic<bool> threads_joined;
  160. ml_queue_t *training_queue;
  161. netdata_mutex_t mutex;
  162. netdata_thread_t training_thread;
  163. /*
  164. * bookkeeping for anomaly detection charts
  165. */
  166. RRDSET *machine_learning_status_rs;
  167. RRDDIM *machine_learning_status_enabled_rd;
  168. RRDDIM *machine_learning_status_disabled_sp_rd;
  169. RRDSET *metric_type_rs;
  170. RRDDIM *metric_type_constant_rd;
  171. RRDDIM *metric_type_variable_rd;
  172. RRDSET *training_status_rs;
  173. RRDDIM *training_status_untrained_rd;
  174. RRDDIM *training_status_pending_without_model_rd;
  175. RRDDIM *training_status_trained_rd;
  176. RRDDIM *training_status_pending_with_model_rd;
  177. RRDSET *dimensions_rs;
  178. RRDDIM *dimensions_anomalous_rd;
  179. RRDDIM *dimensions_normal_rd;
  180. RRDSET *anomaly_rate_rs;
  181. RRDDIM *anomaly_rate_rd;
  182. RRDSET *detector_events_rs;
  183. RRDDIM *detector_events_above_threshold_rd;
  184. RRDDIM *detector_events_new_anomaly_event_rd;
  185. RRDSET *queue_stats_rs;
  186. RRDDIM *queue_stats_queue_size_rd;
  187. RRDDIM *queue_stats_popped_items_rd;
  188. RRDSET *training_time_stats_rs;
  189. RRDDIM *training_time_stats_allotted_rd;
  190. RRDDIM *training_time_stats_consumed_rd;
  191. RRDDIM *training_time_stats_remaining_rd;
  192. RRDSET *training_results_rs;
  193. RRDDIM *training_results_ok_rd;
  194. RRDDIM *training_results_invalid_query_time_range_rd;
  195. RRDDIM *training_results_not_enough_collected_values_rd;
  196. RRDDIM *training_results_null_acquired_dimension_rd;
  197. RRDDIM *training_results_chart_under_replication_rd;
  198. } ml_host_t;
  199. typedef struct {
  200. bool enable_anomaly_detection;
  201. unsigned max_train_samples;
  202. unsigned min_train_samples;
  203. unsigned train_every;
  204. unsigned num_models_to_use;
  205. unsigned db_engine_anomaly_rate_every;
  206. unsigned diff_n;
  207. unsigned smooth_n;
  208. unsigned lag_n;
  209. double random_sampling_ratio;
  210. unsigned max_kmeans_iters;
  211. double dimension_anomaly_score_threshold;
  212. double host_anomaly_rate_threshold;
  213. RRDR_TIME_GROUPING anomaly_detection_grouping_method;
  214. time_t anomaly_detection_query_duration;
  215. bool stream_anomaly_detection_charts;
  216. std::string hosts_to_skip;
  217. SIMPLE_PATTERN *sp_host_to_skip;
  218. std::string charts_to_skip;
  219. SIMPLE_PATTERN *sp_charts_to_skip;
  220. std::vector<uint32_t> random_nums;
  221. netdata_thread_t detection_thread;
  222. } ml_config_t;
  223. void ml_config_load(ml_config_t *cfg);
  224. extern ml_config_t Cfg;
  225. #endif /* NETDATA_ML_PRIVATE_H */