summaryrefslogtreecommitdiffstats
path: root/ml/Host.h
blob: 2715008f09b5eb948c1f59a8aefb5a514ce380ec (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef ML_HOST_H
#define ML_HOST_H

#include "BitRateWindow.h"
#include "Config.h"
#include "Database.h"
#include "Dimension.h"

#include "ml-private.h"

namespace ml {

class RrdHost {
public:
    RrdHost(RRDHOST *RH) : RH(RH) {
        AnomalyRateRS = rrdset_create(
            RH,
            "anomaly_detection",
            "anomaly_rates",
            NULL, // name
            "anomaly_rates",
            NULL, // ctx
            "Average anomaly rate",
            "anomaly rate",
            "netdata",
            "ml",
            39189,
            Cfg.DBEngineAnomalyRateEvery,
            RRDSET_TYPE_LINE
        );

        AnomalyRateRS->flags = static_cast<RRDSET_FLAGS>(
            static_cast<int>(AnomalyRateRS->flags) | RRDSET_FLAG_HIDDEN
        );
    }

    RRDHOST *getRH() { return RH; }

    unsigned updateEvery() { return RH->rrd_update_every; }

    std::string getUUID() {
        char S[UUID_STR_LEN];
        uuid_unparse_lower(RH->host_uuid, S);
        return S;
    }

    void addDimension(Dimension *D);
    void removeDimension(Dimension *D);

    void getConfigAsJson(nlohmann::json &Json) const;

    virtual ~RrdHost() {};

protected:
    RRDHOST *RH;
    RRDSET *AnomalyRateRS;

    // Protect dimension and lock maps
    std::mutex Mutex;

    std::unordered_map<RRDDIM *, Dimension *> DimensionsMap;
    std::unordered_map<Dimension *, std::mutex> LocksMap;
};

class TrainableHost : public RrdHost {
public:
    TrainableHost(RRDHOST *RH) : RrdHost(RH) {}

    void train();

    void updateResourceUsage() {
        std::lock_guard<std::mutex> Lock(ResourceUsageMutex);
        getrusage(RUSAGE_THREAD, &ResourceUsage);
    }

    void getResourceUsage(struct rusage *RU) {
        std::lock_guard<std::mutex> Lock(ResourceUsageMutex);
        memcpy(RU, &ResourceUsage, sizeof(struct rusage));
    }

private:
    std::pair<Dimension *, Duration<double>> findDimensionToTrain(const TimePoint &NowTP);
    void trainDimension(Dimension *D, const TimePoint &NowTP);

    struct rusage ResourceUsage{};
    std::mutex ResourceUsageMutex;
};

class DetectableHost : public TrainableHost {
public:
    DetectableHost(RRDHOST *RH) : TrainableHost(RH) {}

    void startAnomalyDetectionThreads();
    void stopAnomalyDetectionThreads();

    template<typename ...ArgTypes>
    bool getAnomalyInfo(ArgTypes&&... Args) {
        return DB.getAnomalyInfo(Args...);
    }

    template<typename ...ArgTypes>
    bool getAnomaliesInRange(ArgTypes&&... Args) {
        return DB.getAnomaliesInRange(Args...);
    }

    void getDetectionInfoAsJson(nlohmann::json &Json) const;

private:
    void detect();
    void detectOnce();

private:
    std::thread TrainingThread;
    std::thread DetectionThread;

    BitRateWindow BRW{
        static_cast<size_t>(Cfg.ADMinWindowSize),
        static_cast<size_t>(Cfg.ADMaxWindowSize),
        static_cast<size_t>(Cfg.ADIdleWindowSize),
        static_cast<size_t>(Cfg.ADMinWindowSize * Cfg.ADWindowRateThreshold)
    };

    CalculatedNumber WindowAnomalyRate{0.0};

    size_t NumAnomalousDimensions{0};
    size_t NumNormalDimensions{0};
    size_t NumTrainedDimensions{0};
    size_t NumActiveDimensions{0};

    unsigned AnomalyRateTimer{0};

    Database DB{Cfg.AnomalyDBPath};
};

using Host = DetectableHost;

} // namespace ml

#endif /* ML_HOST_H */