Config.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "Config.h"
  3. #include "ml-private.h"
  4. using namespace ml;
  5. /*
  6. * Global configuration instance to be shared between training and
  7. * prediction threads.
  8. */
  9. Config ml::Cfg;
  10. template <typename T>
  11. static T clamp(const T& Value, const T& Min, const T& Max) {
  12. return std::max(Min, std::min(Value, Max));
  13. }
  14. /*
  15. * Initialize global configuration variable.
  16. */
  17. void Config::readMLConfig(void) {
  18. const char *ConfigSectionML = CONFIG_SECTION_ML;
  19. bool EnableAnomalyDetection = config_get_boolean(ConfigSectionML, "enabled", true);
  20. /*
  21. * Read values
  22. */
  23. unsigned MaxTrainSamples = config_get_number(ConfigSectionML, "maximum num samples to train", 4 * 3600);
  24. unsigned MinTrainSamples = config_get_number(ConfigSectionML, "minimum num samples to train", 1 * 900);
  25. unsigned TrainEvery = config_get_number(ConfigSectionML, "train every", 1 * 3600);
  26. unsigned NumModelsToUse = config_get_number(ConfigSectionML, "number of models per dimension", 1);
  27. unsigned DiffN = config_get_number(ConfigSectionML, "num samples to diff", 1);
  28. unsigned SmoothN = config_get_number(ConfigSectionML, "num samples to smooth", 3);
  29. unsigned LagN = config_get_number(ConfigSectionML, "num samples to lag", 5);
  30. double RandomSamplingRatio = config_get_float(ConfigSectionML, "random sampling ratio", 1.0 / LagN);
  31. unsigned MaxKMeansIters = config_get_number(ConfigSectionML, "maximum number of k-means iterations", 1000);
  32. double DimensionAnomalyScoreThreshold = config_get_float(ConfigSectionML, "dimension anomaly score threshold", 0.99);
  33. double HostAnomalyRateThreshold = config_get_float(ConfigSectionML, "host anomaly rate threshold", 1.0);
  34. std::string AnomalyDetectionGroupingMethod = config_get(ConfigSectionML, "anomaly detection grouping method", "average");
  35. time_t AnomalyDetectionQueryDuration = config_get_number(ConfigSectionML, "anomaly detection grouping duration", 5 * 60);
  36. /*
  37. * Clamp
  38. */
  39. MaxTrainSamples = clamp<unsigned>(MaxTrainSamples, 1 * 3600, 24 * 3600);
  40. MinTrainSamples = clamp<unsigned>(MinTrainSamples, 1 * 900, 6 * 3600);
  41. TrainEvery = clamp<unsigned>(TrainEvery, 1 * 3600, 6 * 3600);
  42. NumModelsToUse = clamp<unsigned>(NumModelsToUse, 1, 7 * 24);
  43. DiffN = clamp(DiffN, 0u, 1u);
  44. SmoothN = clamp(SmoothN, 0u, 5u);
  45. LagN = clamp(LagN, 1u, 5u);
  46. RandomSamplingRatio = clamp(RandomSamplingRatio, 0.2, 1.0);
  47. MaxKMeansIters = clamp(MaxKMeansIters, 500u, 1000u);
  48. DimensionAnomalyScoreThreshold = clamp(DimensionAnomalyScoreThreshold, 0.01, 5.00);
  49. HostAnomalyRateThreshold = clamp(HostAnomalyRateThreshold, 0.1, 10.0);
  50. AnomalyDetectionQueryDuration = clamp<time_t>(AnomalyDetectionQueryDuration, 60, 15 * 60);
  51. /*
  52. * Validate
  53. */
  54. if (MinTrainSamples >= MaxTrainSamples) {
  55. error("invalid min/max train samples found (%u >= %u)", MinTrainSamples, MaxTrainSamples);
  56. MinTrainSamples = 1 * 3600;
  57. MaxTrainSamples = 4 * 3600;
  58. }
  59. /*
  60. * Assign to config instance
  61. */
  62. Cfg.EnableAnomalyDetection = EnableAnomalyDetection;
  63. Cfg.MaxTrainSamples = MaxTrainSamples;
  64. Cfg.MinTrainSamples = MinTrainSamples;
  65. Cfg.TrainEvery = TrainEvery;
  66. Cfg.NumModelsToUse = NumModelsToUse;
  67. Cfg.DiffN = DiffN;
  68. Cfg.SmoothN = SmoothN;
  69. Cfg.LagN = LagN;
  70. Cfg.RandomSamplingRatio = RandomSamplingRatio;
  71. Cfg.MaxKMeansIters = MaxKMeansIters;
  72. Cfg.DimensionAnomalyScoreThreshold = DimensionAnomalyScoreThreshold;
  73. Cfg.HostAnomalyRateThreshold = HostAnomalyRateThreshold;
  74. Cfg.AnomalyDetectionGroupingMethod = web_client_api_request_v1_data_group(AnomalyDetectionGroupingMethod.c_str(), RRDR_GROUPING_AVERAGE);
  75. Cfg.AnomalyDetectionQueryDuration = AnomalyDetectionQueryDuration;
  76. Cfg.HostsToSkip = config_get(ConfigSectionML, "hosts to skip from training", "!*");
  77. Cfg.SP_HostsToSkip = simple_pattern_create(Cfg.HostsToSkip.c_str(), NULL, SIMPLE_PATTERN_EXACT);
  78. // Always exclude anomaly_detection charts from training.
  79. Cfg.ChartsToSkip = "anomaly_detection.* ";
  80. Cfg.ChartsToSkip += config_get(ConfigSectionML, "charts to skip from training", "netdata.*");
  81. Cfg.SP_ChartsToSkip = simple_pattern_create(Cfg.ChartsToSkip.c_str(), NULL, SIMPLE_PATTERN_EXACT);
  82. Cfg.StreamADCharts = config_get_boolean(ConfigSectionML, "stream anomaly detection charts", true);
  83. }