summaryrefslogtreecommitdiffstats
path: root/database/engine/metadata_log/metalogpluginsd.c
blob: 25af24143ba64dd88a85dffca169e8ec5fabb9d2 (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
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
// SPDX-License-Identifier: GPL-3.0-or-later
#define NETDATA_RRD_INTERNALS

#include "metadatalog.h"
#include "metalogpluginsd.h"

extern struct config stream_config;

PARSER_RC metalog_pluginsd_host_action(
    void *user, char *machine_guid, char *hostname, char *registry_hostname, int update_every, char *os, char *timezone,
    char *tags)
{
    int history = 5;
    RRD_MEMORY_MODE mode = RRD_MEMORY_MODE_DBENGINE;
    int health_enabled = default_health_enabled;
    int rrdpush_enabled = default_rrdpush_enabled;
    char *rrdpush_destination = default_rrdpush_destination;
    char *rrdpush_api_key = default_rrdpush_api_key;
    char *rrdpush_send_charts_matching = default_rrdpush_send_charts_matching;

    struct metalog_pluginsd_state *state = ((PARSER_USER_OBJECT *)user)->private;

    RRDHOST *host = rrdhost_find_by_guid(machine_guid, 0);
    if (host) {
        if (unlikely(host->rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)) {
            error("Archived host '%s' has memory mode '%s', but the archived one is '%s'. Ignoring archived state.",
                  host->hostname, rrd_memory_mode_name(host->rrd_memory_mode),
                  rrd_memory_mode_name(RRD_MEMORY_MODE_DBENGINE));
            ((PARSER_USER_OBJECT *) user)->host = NULL; /* Ignore objects if memory mode is not dbengine */
            return PARSER_RC_OK;
        }
        goto write_replay;
    }

    if (strcmp(machine_guid, registry_get_this_machine_guid()) == 0) {
        struct metalog_record record;
        struct metadata_logfile *metalogfile = state->metalogfile;

        uuid_parse(machine_guid, record.uuid);
        mlf_record_insert(metalogfile, &record);
        if (localhost->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
            ((PARSER_USER_OBJECT *) user)->host = localhost;
        else
            ((PARSER_USER_OBJECT *) user)->host = NULL;
        return PARSER_RC_OK;
    }

    // Fetch configuration options from streaming config
    update_every = (int)appconfig_get_number(&stream_config, machine_guid, "update every", update_every);
    if(update_every < 0) update_every = 1;

    //health_enabled = appconfig_get_boolean_ondemand(&stream_config, rpt->key, "health enabled by default", health_enabled);
    health_enabled = appconfig_get_boolean_ondemand(&stream_config, machine_guid, "health enabled", health_enabled);

    //rrdpush_enabled = appconfig_get_boolean(&stream_config, rpt->key, "default proxy enabled", rrdpush_enabled);
    rrdpush_enabled = appconfig_get_boolean(&stream_config, machine_guid, "proxy enabled", rrdpush_enabled);

    //rrdpush_destination = appconfig_get(&stream_config, rpt->key, "default proxy destination", rrdpush_destination);
    rrdpush_destination = appconfig_get(&stream_config, machine_guid, "proxy destination", rrdpush_destination);

    //rrdpush_api_key = appconfig_get(&stream_config, rpt->key, "default proxy api key", rrdpush_api_key);
    rrdpush_api_key = appconfig_get(&stream_config, machine_guid, "proxy api key", rrdpush_api_key);

    //rrdpush_send_charts_matching = appconfig_get(&stream_config, rpt->key, "default proxy send charts matching", rrdpush_send_charts_matching);
    rrdpush_send_charts_matching = appconfig_get(&stream_config, machine_guid, "proxy send charts matching", rrdpush_send_charts_matching);


    host = rrdhost_create(
        hostname
        , registry_hostname
        , machine_guid
        , os
        , timezone
        , tags
        , NULL
        , NULL
        , update_every
        , history   // entries
        , mode
        , health_enabled    // health enabled
        , rrdpush_enabled   // Push enabled
        , rrdpush_destination  //destination
        , rrdpush_api_key  // api key
        , rrdpush_send_charts_matching  // charts matching
        , callocz(1, sizeof(struct rrdhost_system_info))
        , 0     // localhost
        , 1     // archived
    );

write_replay:
    if (host) { /* It's a valid object */
        struct metalog_record record;
        struct metadata_logfile *metalogfile = state->metalogfile;

        uuid_copy(record.uuid, host->host_uuid);
        mlf_record_insert(metalogfile, &record);
    }
    ((PARSER_USER_OBJECT *) user)->host = host;
    return PARSER_RC_OK;
}

PARSER_RC metalog_pluginsd_chart_action(void *user, char *type, char *id, char *name, char *family, char *context,
                                        char *title, char *units, char *plugin, char *module, int priority,
                                        int update_every, RRDSET_TYPE chart_type, char *options)
{
    struct metalog_pluginsd_state *state = ((PARSER_USER_OBJECT *)user)->private;
    RRDSET *st = NULL;
    RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
    uuid_t *chart_uuid;

    if (unlikely(!host)) {
        debug(D_METADATALOG, "Ignoring chart belonging to missing or ignored host.");
        return PARSER_RC_OK;
    }
    chart_uuid = uuid_is_null(state->uuid) ? NULL : &state->uuid;
    st = rrdset_create_custom(
        host, type, id, name, family, context, title, units,
        plugin, module, priority, update_every,
        chart_type, RRD_MEMORY_MODE_DBENGINE, (host)->rrd_history_entries, 1, chart_uuid);

    rrdset_isnot_obsolete(st); /* archived charts cannot be obsolete */
    if (options && *options) {
        if (strstr(options, "detail"))
            rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
        else
            rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);

        if (strstr(options, "hidden"))
            rrdset_flag_set(st, RRDSET_FLAG_HIDDEN);
        else
            rrdset_flag_clear(st, RRDSET_FLAG_HIDDEN);

        if (strstr(options, "store_first"))
            rrdset_flag_set(st, RRDSET_FLAG_STORE_FIRST);
        else
            rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
    } else {
        rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
        rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
    }
    ((PARSER_USER_OBJECT *)user)->st = st;

    if (chart_uuid) { /* It's a valid object */
        struct metalog_record record;
        struct metadata_logfile *metalogfile = state->metalogfile;

        uuid_copy(record.uuid, state->uuid);
        mlf_record_insert(metalogfile, &record);
        uuid_clear(state->uuid); /* Consume UUID */
    }
    return PARSER_RC_OK;
}

PARSER_RC metalog_pluginsd_dimension_action(void *user, RRDSET *st, char *id, char *name, char *algorithm,
                                            long multiplier, long divisor, char *options, RRD_ALGORITHM algorithm_type)
{
    struct metalog_pluginsd_state *state = ((PARSER_USER_OBJECT *)user)->private;
    UNUSED(user);
    UNUSED(algorithm);
    uuid_t *dim_uuid;

    if (unlikely(!st)) {
        debug(D_METADATALOG, "Ignoring dimension belonging to missing or ignored chart.");
        return PARSER_RC_OK;
    }
    dim_uuid = uuid_is_null(state->uuid) ? NULL : &state->uuid;

    RRDDIM *rd = rrddim_add_custom(st, id, name, multiplier, divisor, algorithm_type, RRD_MEMORY_MODE_DBENGINE, 1,
                                   dim_uuid);
    rrddim_flag_clear(rd, RRDDIM_FLAG_HIDDEN);
    rrddim_flag_clear(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
    rrddim_isnot_obsolete(st, rd); /* archived dimensions cannot be obsolete */
    if (options && *options) {
        if (strstr(options, "hidden") != NULL)
            rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
        if (strstr(options, "noreset") != NULL)
            rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
        if (strstr(options, "nooverflow") != NULL)
            rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
    }
    if (dim_uuid) { /* It's a valid object */
        struct metalog_record record;
        struct metadata_logfile *metalogfile = state->metalogfile;

        uuid_copy(record.uuid, state->uuid);
        mlf_record_insert(metalogfile, &record);
        uuid_clear(