1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include <dlib/statistics.h>
#include "Config.h"
#include "Host.h"
#include "json/single_include/nlohmann/json.hpp"
using namespace ml;
static void updateDimensionsChart(RRDHOST *RH,
collected_number NumTrainedDimensions,
collected_number NumNormalDimensions,
collected_number NumAnomalousDimensions) {
static thread_local RRDSET *RS = nullptr;
static thread_local RRDDIM *NumTotalDimensionsRD = nullptr;
static thread_local RRDDIM *NumTrainedDimensionsRD = nullptr;
static thread_local RRDDIM *NumNormalDimensionsRD = nullptr;
static thread_local RRDDIM *NumAnomalousDimensionsRD = nullptr;
if (!RS) {
RS = rrdset_create(
RH, // host
"anomaly_detection", // type
"dimensions", // id
NULL, // name
"dimensions", // family
NULL, // ctx
"Anomaly detection dimensions", // title
"dimensions", // units
"netdata", // plugin
"ml", // module
39183, // priority
RH->rrd_update_every, // update_every
RRDSET_TYPE_LINE // chart_type
);
NumTotalDimensionsRD = rrddim_add(RS, "total", NULL,
1, 1, RRD_ALGORITHM_ABSOLUTE);
NumTrainedDimensionsRD = rrddim_add(RS, "trained", NULL,
1, 1, RRD_ALGORITHM_ABSOLUTE);
NumNormalDimensionsRD = rrddim_add(RS, "normal", NULL,
1, 1, RRD_ALGORITHM_ABSOLUTE);
NumAnomalousDimensionsRD = rrddim_add(RS, "anomalous", NULL,
1, 1, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(RS);
rrddim_set_by_pointer(RS, NumTotalDimensionsRD, NumNormalDimensions + NumAnomalousDimensions);
rrddim_set_by_pointer(RS, NumTrainedDimensionsRD, NumTrainedDimensions);
rrddim_set_by_pointer(RS, NumNormalDimensionsRD, NumNormalDimensions);
rrddim_set_by_pointer(RS, NumAnomalousDimensionsRD, NumAnomalousDimensions);
rrdset_done(RS);
}
static void updateRateChart(RRDHOST *RH, collected_number AnomalyRate) {
static thread_local RRDSET *RS = nullptr;
static thread_local RRDDIM *AnomalyRateRD = nullptr;
if (!RS) {
RS = rrdset_create(
RH, // host
"anomaly_detection", // type
"anomaly_rate", // id
NULL, // name
"anomaly_rate", // family
NULL, // ctx
"Percentage of anomalous dimensions", // title
"percentage", // units
"netdata", // plugin
"ml", // module
39184, // priority
RH->rrd_update_every, // update_every
RRDSET_TYPE_LINE // chart_type
);
AnomalyRateRD = rrddim_add(RS, "anomaly_rate", NULL,
1, 100, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(RS);
rrddim_set_by_pointer(RS, AnomalyRateRD, AnomalyRate);
rrdset_done(RS);
}
static void updateWindowLengthChart(RRDHOST *RH, collected_number WindowLength) {
static thread_local RRDSET *RS = nullptr;
static thread_local RRDDIM *WindowLengthRD = nullptr;
if (!RS) {
RS = rrdset_create(
RH, // host
"anomaly_detection", // type
"detector_window", // id
NULL, // name
"detector_window", // family
NULL, // ctx
"Anomaly detector window length", // title
"seconds", // units
"netdata", // plugin
"ml", // module
39185, // priority
RH->rrd_update_every, // update_every
RRDSET_TYPE_LINE // chart_type
);
WindowLengthRD = rrddim_add(RS, "duration", NULL,
1, 1, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(RS);
rrddim_set_by_pointer(RS, WindowLengthRD, WindowLength * RH->rrd_update_every);
rrdset_done(RS);
}
static void updateEventsChart(RRDHOST *RH,
std::pair<BitRateWindow::Edge, size_t> P,
bool ResetBitCounter,
bool NewAnomalyEvent) {
static thread_local RRDSET *RS = nullptr;
static thread_local RRDDIM *AboveThresholdRD = nullptr;
static thread_local RRDDIM *ResetBitCounterRD = nullptr;
static thread_local RRDDIM *NewAnomalyEventRD = nullptr;
if (!RS) {
RS = rrdset_create(
RH, // host
"anomaly_detection", // type
"detector_events", // id
NULL, // name
"detector_events", // family
NULL, // ctx
"Anomaly events triggered", // title
"boolean", // units
"netdata", // plugin
"ml", // module
39186, // priority
RH->rrd_update_every, // update_every
RRDSET_TYPE_LINE // chart_type
);
AboveThresholdRD = rrddim_add(RS, "above_threshold", NULL,
1, 1, RRD_ALGORITHM_ABSOLUTE);
ResetBitCounterRD = rrddim_add(RS, "reset_bit_counter", NULL,
1, 1, RRD_ALGORITHM_ABSOLUTE);
NewAnomalyEventRD = rrddim_add(RS, "new_anomaly_event", NULL,
1, 1, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(RS);
BitRateWindow::Edge E = P.first;
bool AboveThreshold = E.second == BitRateWindow::State::AboveThreshold;
rrddim_set_by_pointer(RS, AboveThresholdRD, AboveThreshold);
rrddim_set_by_pointer(RS, ResetBitCounterRD, ResetBitCounter);
rrddim_set_by_pointer(RS, NewAnomalyEventRD, NewAnomalyEvent);
rrdset_done(RS);
}
static void updateDetectionChart(RRDHOST *RH, collected_number PredictionDuration) {
static thread_local RRDSET *RS = nullptr;
static thread_local RRDDIM *PredictiobDurationRD = nullptr;
if (!RS) {
RS = rrdset_create(
RH, // host
"anomaly_detection", // type
"prediction_stats", // id
NULL, // name
"prediction_stats", // family
NULL, // ctx
"Time it took to run prediction", // title
"milliseconds", // units
"netdata", // plugin
"ml", // module
39187, // priority
RH->rrd_update_every, // update_every
RRDSET_TYPE_LINE // chart_type
);
PredictiobDurationRD = rrddim_add(RS, "duration", NULL,
1, 1, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(RS);
rrddim_set_by_pointer(RS, PredictiobDurationRD, PredictionDuration);
rrdset_done(RS);
}
static void updateTrainingChart(RRDHOST *RH,
collected_number TotalTrainingDuration,
collected_number MaxTrainingDuration)
{
static thread_local RRDSET *RS = nullptr;
static thread_local RRDDIM *TotalTrainingDurationRD = nullptr;
static thread_local RRDDIM *MaxTrainingDurationRD = nullptr;
if (!RS) {
RS = rrdset_create(
RH, // host
"anomaly_detection", // type
"training_stats", // id
NULL, // name
"training_stats", // family
NULL, // ctx
"Training step statistics", // title
"milliseconds", // units
"netdata", // plugin
"ml", // module
39188, // priority
RH->rrd_update_every, // update_every
RRDSET_TYPE_LINE // chart_type
);
TotalTrainingDurationRD = rrddim_add(RS, "total_training_duration", NULL,
1, 1, RRD_ALGORITHM_ABSOLUTE);
MaxTrainingDurationRD = rrddim_add(RS, "max_training_duration", NULL,
1, 1, RRD_ALGORITHM_ABSOLUTE);
} else
rrdset_next(RS);
rrddim_set_by_pointer(RS, TotalTrainingDurationRD, TotalTrainingDuration);
rrddim_set_by_pointer(RS, MaxTrainingDurationRD, MaxTrainingDuration);
rrdset_done(RS);
}
void RrdHost::addDimension(Dimension *D) {
std::lock_guard<std::mutex> Lock(Mutex);
DimensionsMap[D->getRD()] = D;
// Default construct mutex for dimension
LocksMap[D];
}
void RrdHost::removeDimension(Dimension *D) {
// Remove the dimension from the hosts map.
{
std::lock_guard<std::mutex> Lock(Mutex);
DimensionsMap.erase(D->getRD());
}
// Delete the dimension by locking the mutex that protects it.
{
std::lock_guard<std::mutex> Lock(LocksMap[D]);
delete D;
}
// Remove the lock entry for the deleted dimension.
{
std::lock_guard<std::mutex> Lock(Mutex);
LocksMap.erase(D);
}
}
void RrdHost::getConfigAsJson(nlohmann::json &Json) const {
Json["version"] = 1;
Json["enabled"] = Cfg.EnableAnomalyDetection;
Json["min-train-samples"] = Cfg.MinTrainSamples;
Json["max-train-samples"] = Cfg.MaxTrainSamples;
Json["train-every"] = Cfg.TrainEvery;
Json["diff-n"] = Cfg.DiffN;
Json["smooth-n"] = Cfg.SmoothN;
Json["lag-n"] = Cfg.LagN;
Json["max-kmeans-iters"] = Cfg.MaxKMeansIters;
Json["dimension-anomaly-score-threshold"] = Cfg.DimensionAnomalyScoreThreshold;
Json["host-anomaly-rate-threshold"] = Cfg.HostAnomalyRateThreshold;
Json["min-window-size"] = Cfg.ADMinWindowSize;
Json["max-window-size"] = Cfg.ADMaxWindowSize;
Json["idle-window-size"] = Cfg.ADIdleWindowSize;
Json["window-rate-threshold"] = Cfg.ADWindowRateThreshold;
Json["dimension-rate-threshold"] = Cfg.ADDimensionRateThreshold;
}
std::pair<Dimension *, Duration<double>>
TrainableHost::findDimensionToTrain(const TimePoint &NowTP) {
std::lock_guard<std::mutex> Lock(Mutex);
Duration<double> AllottedDuration = Duration<double>{Cfg.TrainEvery * updateEvery()} / (DimensionsMap.size() + 1);
for (auto &DP : DimensionsMap) {
Dimension *D = DP.second;
if (D->shouldTrain(NowTP)) {
LocksMap[D].lock();
return { D, AllottedDuration };
}
}
return { nullptr, AllottedDuration };
}
void TrainableHost::trainDimension(Dimension *D, const TimePoint &NowTP) {
if (D == nullptr)
return;
D->LastTrainedAt = NowTP + Seconds{D->updateEvery()};
TimePoint StartTP = SteadyClock::now();
D->trainModel();
Duration<double> Duration = SteadyClock::now() - StartTP;
D->updateTrainingDuration(Duration.count());
{
std::lock_guard<std::mutex> Lock(Mutex);
LocksMap[D].unlock();
}
}
void TrainableHost::train() {
Duration<double> MaxSleepFor = Seconds{updateEvery()};
while (!netdata_exit) {
TimePoint NowTP = SteadyClock::now();
auto P = findDimensionToTrain(NowTP);
trainDimension(P.first, NowTP);
Duration<double> AllottedDuration = P.second;
Duration<double> RealDuration = SteadyClock::now() - NowTP;
Duration<double> SleepFor;
if (RealDuration >= AllottedDuration)
continue;
SleepFor = std::min(AllottedDuration - RealDuration, MaxSleepFor);
std::this_thread::sleep_for(SleepFor);
}
}
void DetectableHost::detectOnce() {
auto P = BRW.insert(AnomalyRate >= Cfg.HostAnomalyRateThreshold);
BitRateWindow::Edge Edge = P.first;
size_t WindowLength = P.second;
bool ResetBitCounter = (Edge.first != BitRateWindow::State::AboveThreshold);
bool NewAnomalyEvent = (Edge.first == BitRateWindow::State::AboveThreshold) &&
(Edge.second == BitRateWindow::State::Idle);
std::vector<std::pair<double, std::string>> DimsOverThreshold;
size_t NumAnomalousDimensions = 0;
size_t NumNormalDimensions = 0;
size_t NumTrainedDimensions = 0;
double TotalTrainingDuration = 0.0;
double MaxTrainingDuration = 0.0;
{
std::lock_guard<std::mutex> Lock(Mutex);
DimsOverThreshold.reserve(DimensionsMap.size());
for (auto &DP : DimensionsMap) {
Dimension *D = DP.second;
auto P = D->detect(WindowLength, ResetBitCounter);
bool IsAnomalous = P.first;
double AnomalyRate = P.second;
NumTrainedDimensions += D->isTrained();
double DimTrainingDuration = D->updateTrainingDuration(0.0);
MaxTrainingDuration = std::max(MaxTrainingDuration, DimTrainingDuration);
TotalTrainingDuration += DimTrainingDuration;
if (IsAnomalous)
NumAnomalousDimensions += 1;
if (NewAnomalyEvent && (AnomalyRate >= Cfg.ADDimensionRateThreshold))
DimsOverThreshold.push_back({ AnomalyRate, D->getID() });
}
if (NumAnomalousDimensions)
AnomalyRate = static_cast<double>(NumAnomalousDimensions) / DimensionsMap.size();
else
AnomalyRate = 0.0;
NumNormalDimensions = DimensionsMap.size() - NumAnomalousDimensions;
}
this->NumAnomalousDimensions = NumAnomalousDimensions;
this->NumNormalDimensions = NumNormalDimensions;
this->NumTrainedDimensions = NumTrainedDimensions;
updateDimensionsChart(getRH(), NumTrainedDimensions, NumNormalDimensions, NumAnomalousDimensions);
updateRateChart(getRH(), AnomalyRate * 10000.0);
updateWindowLengthChart(getRH(), WindowLength);
updateEventsChart(getRH(), P, ResetBitCounter, NewAnomalyEvent);
updateTrainingChart(getRH(), TotalTrainingDuration * 1000.0, MaxTrainingDuration * 1000.0);
if (!NewAnomalyEvent || (DimsOverThreshold.size() == 0))
return;
std::sort(DimsOverThreshold.begin(), DimsOverThreshold.end());
std::reverse(DimsOverThreshold.begin(), DimsOverThreshold.end());
// Make sure the JSON response won't grow beyond a specific number
// of dimensions. Log an error message if this happens, because it
// most likely means that the user specified a very-low anomaly rate
// threshold.
size_t NumMaxDimsOverThreshold = 2000;
if (DimsOverThreshold.size() > NumMaxDimsOverThreshold) {
error("Found %zu dimensions over threshold. Reducing JSON result to %zu dimensions.",
DimsOverThreshold.size(), NumMaxDimsOverThreshold);
DimsOverThreshold.resize(NumMaxDimsOverThreshold);
}
nlohmann::json JsonResult = DimsOverThreshold;
time_t Before = now_realtime_sec();
time_t After = Before - (WindowLength * updateEvery());
DB.insertAnomaly("AD1", 1, getUUID(), After, Before, JsonResult.dump(4));
}
void DetectableHost::detect() {
std::this_thread::sleep_for(Seconds{10});
while (!netdata_exit) {
TimePoint StartTP = SteadyClock::now();
detectOnce();
TimePoint EndTP = SteadyClock::now();
Duration<double> Dur = EndTP - StartTP;
updateDetectionChart(getRH(), Dur.count() * 1000);
std::this_thread::sleep_for(Seconds{updateEvery()});
}
}
void DetectableHost::getDetectionInfoAsJson(nlohmann::json &Json) const {
Json["anomalous-dimensions"] = NumAnomalousDimensions;
Json["normal-dimensions"] = NumNormalDimensions;
Json["total-dimensions"] = NumAnomalousDimensions + NumNormalDimensions;
Json["trained-dimensions"] = NumTrainedDimensions;
}
void DetectableHost::startAnomalyDetectionThreads() {
TrainingThread = std::thread(&TrainableHost::train, this);
DetectionThread = std::thread(&DetectableHost::detect, this);
}
void DetectableHost::stopAnomalyDetectionThreads() {
TrainingThread.join();
DetectionThread.join();
}
|