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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "ad_charts.h"
void ml_update_dimensions_chart(ml_host_t *host, const ml_machine_learning_stats_t &mls) {
/*
* Machine learning status
*/
if (Cfg.enable_statistics_charts) {
if (!host->machine_learning_status_rs) {
char id_buf[1024];
char name_buf[1024];
snprintfz(id_buf, 1024, "machine_learning_status_on_%s", localhost->machine_guid);
snprintfz(name_buf, 1024, "machine_learning_status_on_%s", rrdhost_hostname(localhost));
host->machine_learning_status_rs = rrdset_create(
host->rh,
"netdata", // type
id_buf,
name_buf, // name
NETDATA_ML_CHART_FAMILY, // family
"netdata.machine_learning_status", // ctx
"Machine learning status", // title
"dimensions", // units
NETDATA_ML_PLUGIN, // plugin
NETDATA_ML_MODULE_TRAINING, // module
NETDATA_ML_CHART_PRIO_MACHINE_LEARNING_STATUS, // priority
localhost->rrd_update_every, // update_every
RRDSET_TYPE_LINE // chart_type
);
rrdset_flag_set(host->machine_learning_status_rs , RRDSET_FLAG_ANOMALY_DETECTION);
host->machine_learning_status_enabled_rd =
rrddim_add(host->machine_learning_status_rs, "enabled", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
host->machine_learning_status_disabled_sp_rd =
rrddim_add(host->machine_learning_status_rs, "disabled-sp", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
}
rrddim_set_by_pointer(host->machine_learning_status_rs,
host->machine_learning_status_enabled_rd, mls.num_machine_learning_status_enabled);
rrddim_set_by_pointer(host->machine_learning_status_rs,
host->machine_learning_status_disabled_sp_rd, mls.num_machine_learning_status_disabled_sp);
rrdset_done(host->machine_learning_status_rs);
}
/*
* Metric type
*/
if (Cfg.enable_statistics_charts) {
if (!host->metric_type_rs) {
char id_buf[1024];
char name_buf[1024];
snprintfz(id_buf, 1024, "metric_types_on_%s", localhost->machine_guid);
snprintfz(name_buf, 1024, "metric_types_on_%s", rrdhost_hostname(localhost));
host->metric_type_rs = rrdset_create(
host->rh,
"netdata", // type
id_buf, // id
name_buf, // name
NETDATA_ML_CHART_FAMILY, // family
"netdata.metric_types", // ctx
"Dimensions by metric type", // title
"dimensions", // units
NETDATA_ML_PLUGIN, // plugin
NETDATA_ML_MODULE_TRAINING, // module
NETDATA_ML_CHART_PRIO_METRIC_TYPES, // priority
localhost->rrd_update_every, // update_every
RRDSET_TYPE_LINE // chart_type
);
rrdset_flag_set(host->metric_type_rs, RRDSET_FLAG_ANOMALY_DETECTION);
host->metric_type_constant_rd =
rrddim_add(host->metric_type_rs, "constant", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
host->metric_type_variable_rd =
rrddim_add(host->metric_type_rs, "variable", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
}
rrddim_set_by_pointer(host->metric_type_rs,
host->metric_type_constant_rd, mls.num_metric_type_constant);
rrddim_set_by_pointer(host->metric_type_rs,
host->metric_type_variable_rd, mls.num_metric_type_variable);
rrdset_done(host->metric_type_rs);
}
/*
* Training status
*/
if (Cfg.enable_statistics_charts) {
if (!host->training_status_rs) {
char id_buf[1024];
char name_buf[1024];
snprintfz(id_buf, 1024, "training_status_on_%s", localhost->machine_guid);
snprintfz(name_buf, 1024, "training_status_on_%s", rrdhost_hostname(localhost));
host->training_status_rs = rrdset_create(
host->rh,
"netdata", // type
id_buf, // id
name_buf, // name
NETDATA_ML_CHART_FAMILY, // family
"netdata.training_status", // ctx
"Training status of dimensions", // title
"dimensions", // units
NETDATA_ML_PLUGIN, // plugin
NETDATA_ML_MODULE_TRAINING, // module
NETDATA_ML_CHART_PRIO_TRAINING_STATUS, // priority
localhost->rrd_update_every, // update_every
RRDSET_TYPE_LINE // chart_type
);
rrdset_flag_set(host->training_status_rs, RRDSET_FLAG_ANOMALY_DETECTION);
host->training_status_untrained_rd =
rrddim_add(host->training_status_rs, "untrained", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
host->training_status_pending_without_model_rd =
rrddim_add(host->training_status_rs, "pending-without-model", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
host->training_status_trained_rd =
rrddim_add(host->training_status_rs, "trained", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
host->training_status_pending_with_model_rd =
rrddim_add(host->training_status_rs, "pending-with-model", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
}
rrddim_set_by_pointer(host->training_status_rs,
host->training_status_untrained_rd, mls.num_training_status_untrained);
rrddim_set_by_pointer(host->training_status_rs,
host->training_status_pending_without_model_rd, mls.num_training_status_pending_without_model);
rrddim_set_by_pointer(host->training_status_rs,
host->training_status_trained_rd, mls.num_training_status_trained);
rrddim_set_by_pointer(host->training_status_rs,
host->training_status_pending_with_model_rd, mls.num_training_status_pending_with_model);
rrdset_done(host->training_status_rs);
}
/*
* Prediction status
*/
{
if (!host->dimensions_rs) {
char id_buf[1024];
char name_buf[1024];
snprintfz(id_buf, 1024, "dimensions_on_%s", localhost->machine_guid);
snprintfz(name_buf, 1024, "dimensions_on_%s", rrdhost_hostname(localhost));
host->dimensions_rs = rrdset_create(
host->rh,
"anomaly_detection", // type
id_buf, // id
name_buf, // name
"dimensions", // family
"anomaly_detection.dimensions", // ctx
"Anomaly detection dimensions", // title
"dimensions", // units
NETDATA_ML_PLUGIN, // plugin
NETDATA_ML_MODULE_TRAINING, // module
ML_CHART_PRIO_DIMENSIONS, // priority
localhost->rrd_update_every, // update_every
RRDSET_TYPE_LINE // chart_type
);
rrdset_flag_set(host->dimensions_rs, RRDSET_FLAG_ANOMALY_DETECTION);
host->dimensions_anomalous_rd =
rrddim_add(host->dimensions_rs, "anomalous", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
host->dimensions_normal_rd =
rrddim_add(host->dimensions_rs, "normal", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
}
rrddim_set_by_pointer(host->dimensions_rs,
host->dimensions_anomalous_rd, mls.num_anomalous_dimensions);
rrddim_set_by_pointer(host->dimensions_rs,
host->dimensions_normal_rd, mls.num_normal_dimensions);
rrdset_done(host->dimensions_rs);
}
}
void ml_update_host_and_detection_rate_charts(ml_host_t *host, collected_number AnomalyRate) {
/*
* Anomaly rate
*/
{
if (!host->anomaly_rate_rs) {
char id_buf[1024];
char name_buf[1024];
snprintfz(id_buf, 1024, "anomaly_rate_on_%s", localhost->machine_guid);
snprintfz(name_buf, 1024, "anomaly_rate_on_%s", rrdhost_hostname(localhost));
host->anomaly_rate_rs = rrdset_create(
host->rh,
"anomaly_detection", // type
id_buf, // id
name_buf, // name
"anomaly_rate", // family
"anomaly_detection.anomaly_rate", // ctx
"Percentage of anomalous dimensions", // title
"percentage", // units
NETDATA_ML_PLUGIN, // plugin
NETDATA_ML_MODULE_DETECTION, // module
ML_CHART_PRIO_ANOMALY_RATE, // priority
localhost->rrd_update_every, // update_every
RRDSET_TYPE_LINE // chart_type
);
rrdset_flag_set(host->anomaly_rate_rs, RRDSET_FLAG_ANOMALY_DETECTION);
host->anomaly_rate_rd =
rrddim_add(host->anomaly_rate_rs, "anomaly_rate", NULL, 1, 100, RRD_ALGORITHM_ABSOLUTE);
}
rrddim_set_by_pointer(host->anomaly_rate_rs, host->anomaly_rate_rd, AnomalyRate);
rrdset_done(host->anomaly_rate_rs);
}
/*
* Detector Events
*/
{
if (!host->detector_events_rs) {
char id_buf[1024];
char name_buf[1024];
snprintfz(id_buf, 1024, "anomaly_detection_on_%s", localhost->machine_guid);
snprintfz(name_buf, 1024, "anomaly_detection_on_%s", rrdhost_hostname(localhost));
host->detector_events_rs = rrdset_create(
host->rh,
"anomaly_detection", // type
id_buf, // id
name_buf, // name
"anomaly_detection", // family
"anomaly_detection.detector_events", // ctx
"Anomaly detection events", // title
"percentage", // units
NETDATA_ML_PLUGIN, // plugin
NETDATA_ML_MODULE_DETECTION, // module
ML_CHART_PRIO_DETECTOR_EVENTS, // priority
localhost->rrd_update_every, // update_every
RRDSET_TYPE_LINE // chart_type
);
rrdset_flag_set(host->detector_events_rs, RRDSET_FLAG_ANOMALY_DETECTION);
host->detector_events_above_threshold_rd =
rrddim_add(host->detector_events_rs, "above_threshold", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
host->detector_events_new_anomaly_event_rd =
rrddim_add(host->detector_events_rs, "new_anomaly_event", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
}
/*
* Compute the values of the dimensions based on the host rate chart
*/
ONEWAYALLOC *OWA = onewayalloc_create(0);
time_t Now = now_realtime_sec();
time_t Before = Now - host->rh->rrd_update_every;
time_t After = Before - Cfg.anomaly_detection_query_duration;
RRDR_OPTIONS Options = static_cast<RRDR_OPTIONS>(0x00000000);
RRDR *R = rrd2rrdr_legacy(
OWA,
host->anomaly_rate_rs,
1 /* points wanted */,
After,
Before,
Cfg.anomaly_detection_grouping_method,
0 /* resampling time */,
Options, "anomaly_rate",
NULL /* group options */,
0, /* timeout */
0, /* tier */
QUERY_SOURCE_ML,
STORAGE_PRIORITY_SYNCHRONOUS
);
if (R) {
if (R->d == 1 && R->n == 1 && R->rows == 1) {
static thread_local bool prev_above_threshold = false;
bool above_threshold = R->v[0] >= Cfg.host_anomaly_rate_threshold;
bool new_anomaly_event = above_threshold && !prev_above_threshold;
prev_above_threshold = above_threshold;
rrddim_set_by_pointer(host->detector_events_rs,
host->detector_events_above_threshold_rd, above_threshold);
rrddim_set_by_pointer(host->detector_events_rs,
host->detector_events_new_anomaly_event_rd, new_anomaly_event);
rrdset_done(host->detector_events_rs);
}
rrdr_free(OWA, R);
}
onewayalloc_destroy(OWA);
}
}
void ml_update_training_statistics_chart(ml_training_thread_t *training_thread, const ml_training_stats_t &ts) {
/*
* queue stats
*/
{
if (!training_thread->queue_stats_rs) {
char id_buf[1024];
char name_buf[1024];
snprintfz(id_buf, 1024, "training_queue_%zu_stats", training_thread->id);
snprintfz(name_buf, 1024, "training_queue_%zu_stats", training_thread->id);
training_thread->queue_stats_rs = rrdset_create(
localhost,
"netdata", // type
id_buf, // id
name_buf, // name
NETDATA_ML_CHART_FAMILY, // family
"netdata.queue_stats", // ctx
"Training queue stats", // title
"items", // units
NETDATA_ML_PLUGIN, // plugin
NETDATA_ML_MODULE_TRAINING, // module
NETDATA_ML_CHART_PRIO_QUEUE_STATS, // priority
localhost->rrd_update_every, // update_every
RRDSET_TYPE_LINE// chart_type
);
rrdset_flag_set(training_thread->queue_stats_rs, RRDSET_FLAG_ANOMALY_DETECTION);
training_thread->queue_stats_queue_size_rd =
rrddim_add(training_thread->queue_stats_rs, "queue_size", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
training_thread->queue_stats_popped_items_rd =
rrddim_add(training_thread->queue_stats_rs, "popped_items", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
}
rrddim_set_by_pointer(training_thread->queue_stats_rs,
training_thread->queue_stats_queue_size_rd, ts.queue_size);
rrddim_set_by_pointer(training_thread->queue_stats_rs,
training_thread->queue_stats_popped_items_rd, ts.num_popped_items);
rrdset_done(training_thread->queue_stats_rs);
}
/*
* training stats
*/
{
if (!training_thread->training_time_stats_rs) {
char id_buf[1024];
char name_buf[1024];
snprintfz(id_buf, 1024, "training_queue_%zu_time_stats", training_thread->id);
snprintfz(name_buf, 1024, "training_queue_%zu_time_stats", training_thread->id);
training_thread->training_time_stats_rs = rrdset_create(
localhost,
"netdata", // type
id_buf, // id
name_buf, // name
NETDATA_ML_CHART_FAMILY, // family
"netdata.training_time_stats", // ctx
"Training time stats", // title
"milliseconds", // units
NETDATA_ML_PLUGIN, // plugin
NETDATA_ML_MODULE_TRAINING, // module
NETDATA_ML_CHART_PRIO_TRAINING_TIME_STATS, // priority
localhost->rrd_update_every, // update_every
RRDSET_TYPE_LINE// chart_type
);
rrdset_flag_set(training_thread->training_time_stats_rs, RRDSET_FLAG_ANOMALY_DETECTION);
training_thread->training_time_stats_allotted_rd =
rrddim_add(training_thread->training_time_stats_rs, "allotted", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
training_thread->training_time_stats_consumed_rd =
rrddim_add(training_thread->training_time_stats_rs, "consumed", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
training_thread->training_time_stats_remaining_rd =
rrddim_add(training_thread->training_time_stats_rs, "remaining", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
}
rrddim_set_by_pointer(training_thread->training_time_stats_rs,
training_thread->training_time_stats_allotted_rd, ts.allotted_ut);
rrddim_set_by_pointer(training_thread->training_time_stats_rs,
training_thread->training_time_stats_consumed_rd, ts.consumed_ut);
rrddim_set_by_pointer(training_thread->training_time_stats_rs,
training_thread->training_time_stats_remaining_rd, ts.remaining_ut);
rrdset_done(training_thread->training_time_stats_rs);
}
/*
* training result stats
*/
{
if (!training_thread->training_results_rs) {
char id_buf[1024];
char name_buf[1024];
snprintfz(id_buf, 1024, "training_queue_%zu_results", training_thread->id);
snprintfz(name_buf, 1024, "training_queue_%zu_results", training_thread->id);
training_thread->training_results_rs = rrdset_create(
localhost,
"netdata", // type
id_buf, // id
name_buf, // name
NETDATA_ML_CHART_FAMILY, // family
"netdata.training_results", // ctx
"Training results", // title
"events", // units
NETDATA_ML_PLUGIN, // plugin
NETDATA_ML_MODULE_TRAINING, // module
NETDATA_ML_CHART_PRIO_TRAINING_RESULTS, // priority
localhost->rrd_update_every, // update_every
RRDSET_TYPE_LINE// chart_type
);
rrdset_flag_set(training_thread->training_results_rs, RRDSET_FLAG_ANOMALY_DETECTION);
training_thread->training_results_ok_rd =
rrddim_add(training_thread->training_results_rs, "ok", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
training_thread->training_results_invalid_query_time_range_rd =
rrddim_add(training_thread->training_results_rs, "invalid-queries", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
training_thread->training_results_not_enough_collected_values_rd =
rrddim_add(training_thread->training_results_rs, "not-enough-values", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
training_thread->training_results_null_acquired_dimension_rd =
rrddim_add(training_thread->training_results_rs, "null-acquired-dimensions", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
training_thread->training_results_chart_under_replication_rd =
rrddim_add(training_thread->training_results_rs, "chart-under-replication", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
}
rrddim_set_by_pointer(training_thread->training_results_rs,
training_thread->training_results_ok_rd, ts.training_result_ok);
rrddim_set_by_pointer(training_thread->training_results_rs,
training_thread->training_results_invalid_query_time_range_rd, ts.training_result_invalid_query_time_range);
rrddim_set_by_pointer(training_thread->training_results_rs,
training_thread->training_results_not_enough_collected_values_rd, ts.training_result_not_enough_collected_values);
rrddim_set_by_pointer(training_thread->training_results_rs,
training_thread->training_results_null_acquired_dimension_rd, ts.training_result_null_acquired_dimension);
rrddim_set_by_pointer(training_thread->training_results_rs,
training_thread->training_results_chart_under_replication_rd, ts.training_result_chart_under_replication);
rrdset_done(training_thread->training_results_rs);
}
}
void ml_update_global_statistics_charts(uint64_t models_consulted) {
if (Cfg.enable_statistics_charts) {
static RRDSET *st = NULL;
static RRDDIM *rd = NULL;
if (unlikely(!st)) {
st = rrdset_create_localhost(
"netdata" // type
, "ml_models_consulted" // id
, NULL // name
, NETDATA_ML_CHART_FAMILY // family
, NULL // context
, "KMeans models used for prediction" // title
, "models" // units
, NETDATA_ML_PLUGIN // plugin
, NETDATA_ML_MODULE_DETECTION // module
, NETDATA_ML_CHART_PRIO_MACHINE_LEARNING_STATUS // priority
, localhost->rrd_update_every // update_every
, RRDSET_TYPE_AREA // chart_type
);
rd = rrddim_add(st, "num_models_consulted", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
}
rrddim_set_by_pointer(st, rd, (collected_number) models_consulted);
rrdset_done(st);
}
}
|