summaryrefslogtreecommitdiffstats
path: root/src/daemon/config/dyncfg.c
blob: 2a5696b2ac15f44ce8cd922c09c543fdc1adb9d4 (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
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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "dyncfg-internals.h"
#include "dyncfg.h"

struct dyncfg_globals dyncfg_globals = { 0 };

RRDHOST *dyncfg_rrdhost_by_uuid(UUID *uuid) {
    char uuid_str[UUID_STR_LEN];
    uuid_unparse_lower(uuid->uuid, uuid_str);

    RRDHOST *host = rrdhost_find_by_guid(uuid_str);
    if(!host)
        nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: cannot find host with UUID '%s'", uuid_str);

    return host;
}

RRDHOST *dyncfg_rrdhost(DYNCFG *df) {
    return dyncfg_rrdhost_by_uuid(&df->host_uuid);
}

void dyncfg_cleanup(DYNCFG *v) {
    string_freez(v->dyncfg.source);
    v->dyncfg.source = NULL;

    buffer_free(v->dyncfg.payload);
    v->dyncfg.payload = NULL;

    string_freez(v->path);
    v->path = NULL;

    string_freez(v->current.source);
    v->current.source = NULL;

    string_freez(v->function);
    v->function = NULL;

    string_freez(v->template);
    v->template = NULL;
}

static void dyncfg_normalize(DYNCFG *df) {
    usec_t now_ut = now_realtime_usec();

    if(!df->current.created_ut)
        df->current.created_ut = now_ut;

    if(!df->current.modified_ut)
        df->current.modified_ut = now_ut;
}

static void dyncfg_delete_cb(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused) {
    DYNCFG *df = value;
    dyncfg_cleanup(df);
}

static void dyncfg_insert_cb(const DICTIONARY_ITEM *item, void *value, void *data __maybe_unused) {
    DYNCFG *df = value;
    dyncfg_normalize(df);

    const char *id = dictionary_acquired_item_name(item);
    char buf[strlen(id) + 20];
    snprintfz(buf, sizeof(buf), PLUGINSD_FUNCTION_CONFIG " %s", id);
    df->function = string_strdupz(buf);

    if(df->type == DYNCFG_TYPE_JOB && !df->template) {
        const char *last_colon = strrchr(id, ':');
        if(last_colon)
            df->template = string_strndupz(id, last_colon - id);
        else
            nd_log(NDLS_DAEMON, NDLP_WARNING,
                   "DYNCFG: id '%s' is a job, but does not contain a colon to find the template", id);
    }
}

static void dyncfg_react_cb(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused) {
    DYNCFG *df = value; (void)df;
    ;
}

static bool dyncfg_conflict_cb(const DICTIONARY_ITEM *item __maybe_unused, void *old_value, void *new_value, void *data) {
    bool *overwrite_cb_ptr = data;
    bool overwrite_cb = (overwrite_cb_ptr && *overwrite_cb_ptr);

    DYNCFG *v = old_value;
    DYNCFG *nv = new_value;

    size_t changes = 0;

    dyncfg_normalize(nv);

    if(!UUIDeq(v->host_uuid, nv->host_uuid)) {
        SWAP(v->host_uuid, nv->host_uuid);
        changes++;
    }

    if(v->path != nv->path) {
        SWAP(v->path, nv->path);
        changes++;
    }

    if(v->cmds != nv->cmds) {
        SWAP(v->cmds, nv->cmds);
        changes++;
    }

    if(v->type != nv->type) {
        SWAP(v->type, nv->type);
        changes++;
    }

    if(v->view_access != nv->view_access) {
        SWAP(v->view_access, nv->view_access);
        changes++;
    }

    if(v->edit_access != nv->edit_access) {
        SWAP(v->edit_access, nv->edit_access);
        changes++;
    }

    if(v->current.status != nv->current.status) {
        SWAP(v->current.status, nv->current.status);
        changes++;
    }

    if (v->current.source_type != nv->current.source_type) {
        SWAP(v->current.source_type, nv->current.source_type);
        changes++;
    }

    if (v->current.source != nv->current.source) {
        SWAP(v->current.source, nv->current.source);
        changes++;
    }

    if(nv->current.created_ut < v->current.created_ut) {
        SWAP(v->current.created_ut, nv->current.created_ut);
        changes++;
    }

    if(nv->current.modified_ut > v->current.modified_ut) {
        SWAP(v->current.modified_ut, nv->current.modified_ut);
        changes++;
    }

    if(!v->execute_cb || (overwrite_cb && nv->execute_cb && (v->execute_cb != nv->execute_cb || v->execute_cb_data != nv->execute_cb_data))) {
        v->sync = nv->sync,
        v->execute_cb = nv->execute_cb;
        v->execute_cb_data = nv->execute_cb_data;
        changes++;
    }

    dyncfg_cleanup(nv);

    return changes > 0;
}

// ----------------------------------------------------------------------------

void dyncfg_init_low_level(bool load_saved) {
    if(!dyncfg_globals.nodes) {
        dyncfg_globals.nodes = dictionary_create_advanced(DICT_OPTION_FIXED_SIZE | DICT_OPTION_DONT_OVERWRITE_VALUE, NULL, sizeof(DYNCFG));
        dictionary_register_insert_callback(dyncfg_globals.nodes, dyncfg_insert_cb, NULL);
        dictionary_register_react_callback(dyncfg_globals.nodes, dyncfg_react_cb, NULL);
        dictionary_register_conflict_callback(dyncfg_globals.nodes, dyncfg_conflict_cb, NULL);
        dictionary_register_delete_callback(dyncfg_globals.nodes, dyncfg_delete_cb, NULL);

        char path[PATH_MAX];
        snprintfz(path, sizeof(path), "%s/%s", netdata_configured_varlib_dir, "config");

        if(mkdir(path, 0755) == -1) {
            if(errno != EEXIST)
                nd_log(NDLS_DAEMON, NDLP_CRIT, "DYNCFG: failed to create dynamic configuration directory '%s'", path);
        }

        dyncfg_globals.dir = strdupz(path);

        if(load_saved)
            dyncfg_load_all();
    }
}

// ----------------------------------------------------------------------------

const DICTIONARY_ITEM *dyncfg_add_internal(RRDHOST *host, const char *id, const char *path,
                                           DYNCFG_STATUS status, DYNCFG_TYPE type, DYNCFG_SOURCE_TYPE source_type,
                                           const char *source, DYNCFG_CMDS cmds,
                                           usec_t created_ut, usec_t modified_ut,
                                           bool sync, HTTP_ACCESS view_access, HTTP_ACCESS edit_access,
                                           rrd_function_execute_cb_t execute_cb, void *execute_cb_data,
                                           bool overwrite_cb) {
    DYNCFG tmp = {
        .host_uuid = uuid2UUID(host->host_uuid),
        .path = string_strdupz(path),
        .cmds = cmds,
        .type = type,
        .view_access = view_access,
        .edit_access = edit_access,
        .current = {
            .status = status,
            .source_type = source_type,
            .source = string_strdupz(source),
            .created_ut = created_ut,
            .modified_ut = modified_ut,
        },
        .sync = sync,
        .dyncfg = { 0 },
        .execute_cb = execute_cb,
        .execute_cb_data = execute_cb_data,
    };

    return dictionary_set_and_acquire_item_advanced(dyncfg_globals.nodes, id, -1, &tmp, sizeof(tmp), &overwrite_cb);
}

static void dyncfg_send_updates(const char *id) {
    const DICTIONARY_ITEM *item = dictionary_get_and_acquire_item_advanced(dyncfg_globals.nodes, id, -1);
    if(!item) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: asked to update plugin for configuration '%s', but it is not found.", id);
        return;
    }

    DYNCFG *df = dictionary_acquired_item_value(item);

    if(df->type == DYNCFG_TYPE_SINGLE || df->type == DYNCFG_TYPE_JOB) {
        if (df->cmds & DYNCFG_CMD_UPDATE && df->dyncfg.source_type == DYNCFG_SOURCE_TYPE_DYNCFG && df->dyncfg.payload && buffer_strlen(df->dyncfg.payload))
            dyncfg_echo_update(item, df, id);
    }
    else if(df->type == DYNCFG_TYPE_TEMPLATE && (df->cmds & DYNCFG_CMD_ADD)) {
        STRING *template = string_strdupz(id);

        size_t len = strlen(id);
        DYNCFG *df_job;
        dfe_start_reentrant(dyncfg_globals.nodes, df_job) {
            const char *id_template = df_job_dfe.name;
            if(df_job->type == DYNCFG_TYPE_JOB &&                   // it is a job
                df_job->current.source_type == DYNCFG_SOURCE_TYPE_DYNCFG && // it is dynamically configured
                df_job->template == template &&                     // it has the same template name
                strncmp(id_template, id, len) == 0 &&               // the template name matches (redundant)
                id_template[len] == ':' &&                          // immediately after the template there is ':'
                id_template[len + 1]) {                             // and there is something else after the ':'
                dyncfg_echo_add(item, df_job_dfe.item, df, df_job, id, &id_template[len + 1]);
            }
        }
        dfe_done(df_job);

        string_freez(template);
    }

    dictionary_acquired_item_release(dyncfg_globals.nodes, item);
}

bool dyncfg_is_user_disabled(const char *id) {
    const DICTIONARY_ITEM *item = dictionary_get_and_acquire_item(dyncfg_globals.nodes, id);
    if(!item)
        return false;

    DYNCFG *df = dictionary_acquired_item_value(item);
    bool ret = df->dyncfg.user_disabled;
    dictionary_acquired_item_release(dyncfg_globals.nodes, item);
    return ret;
}

bool dyncfg_job_has_registered_template(const char *id) {
    char buf[strlen(id) + 1];
    memcpy(buf, id, sizeof(buf));
    char *colon = strrchr(buf, ':');
    if(!colon)
        return false;

    *colon = '\0';
    const DICTIONARY_ITEM *item = dictionary_get_and_acquire_item(dyncfg_globals.nodes, buf);
    if(!item)
        return false;

    DYNCFG *df = dictionary_acquired_item_value(item);
    bool ret = df->type == DYNCFG_TYPE_TEMPLATE;

    dictionary_acquired_item_release(dyncfg_globals.nodes, item);
    return ret;
}

bool dyncfg_add_low_level(RRDHOST *host, const char *id, const char *path,
                          DYNCFG_STATUS status, DYNCFG_TYPE type, DYNCFG_SOURCE_TYPE source_type, const char *source,
                          DYNCFG_CMDS cmds, usec_t created_ut, usec_t modified_ut, bool sync,
                          HTTP_ACCESS view_access, HTTP_ACCESS edit_access,
                          rrd_function_execute_cb_t execute_cb, void *execute_cb_data) {

    if(view_access == HTTP_ACCESS_NONE)
        view_access = HTTP_ACCESS_SIGNED_ID | HTTP_ACCESS_SAME_SPACE | HTTP_ACCESS_VIEW_AGENT_CONFIG;

    if(edit_access == HTTP_ACCESS_NONE)
        edit_access = HTTP_ACCESS_SIGNED_ID | HTTP_ACCESS_SAME_SPACE | HTTP_ACCESS_EDIT_AGENT_CONFIG | HTTP_ACCESS_COMMERCIAL_SPACE;

    if(!dyncfg_is_valid_id(id)) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: id '%s' is invalid. Ignoring dynamic configuration for it.", id);
        return false;
    }

    if(type == DYNCFG_TYPE_JOB && !dyncfg_job_has_registered_template(id)) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: job id '%s' does not have a registered template. Ignoring dynamic configuration for it.", id);
        return false;
    }

    DYNCFG_CMDS old_cmds = cmds;

    // all configurations support schema
    cmds |= DYNCFG_CMD_SCHEMA;

    // if there is either enable or disable, both are supported
    if(cmds & (DYNCFG_CMD_ENABLE | DYNCFG_CMD_DISABLE))
        cmds |= DYNCFG_CMD_ENABLE | DYNCFG_CMD_DISABLE;

    // add
    if(type == DYNCFG_TYPE_TEMPLATE) {
        // templates must always support "add"
        cmds |= DYNCFG_CMD_ADD;
    }
    else {
        // only templates can have "add"
        cmds &= ~DYNCFG_CMD_ADD;
    }

    // remove
    if(source_type != DYNCFG_SOURCE_TYPE_DYNCFG || type != DYNCFG_TYPE_JOB) {
        // remove is only available for dyncfg jobs
        cmds &= ~DYNCFG_CMD_REMOVE;
    }

    // data
    if(type == DYNCFG_TYPE_TEMPLATE) {
        // templates do not have data
        cmds &= ~(DYNCFG_CMD_GET | DYNCFG_CMD_UPDATE);
    }

    if(cmds != old_cmds) {
        CLEAN_BUFFER *t = buffer_create(1024, NULL);
        buffer_sprintf(t, "DYNCFG: id '%s' was declared with cmds: ", id);
        dyncfg_cmds2buffer(old_cmds, t);
        buffer_strcat(t, ", but they have sanitized to: ");
        dyncfg_cmds2buffer(cmds, t);
        nd_log(NDLS_DAEMON, NDLP_NOTICE, "%s", buffer_tostring(t));
    }

    const DICTIONARY_ITEM *item = dyncfg_add_internal(host, id, path, status, type, source_type, source, cmds,
                                                      created_ut, modified_ut, sync, view_access, edit_access,
                                                      execute_cb, execute_cb_data, true);
    DYNCFG *df = dictionary_acquired_item_value(item);

//    if(df->source_type == DYNCFG_SOURCE_TYPE_DYNCFG && !df->saves)
//        nd_log(NDLS_DAEMON, NDLP_WARNING, "DYNCFG: configuration '%s' is created with source type dyncfg, but we don't have a saved configuration for it", id);

    rrd_collector_started();
    rrd_function_add(
        host,
        NULL,
        string2str(df->function),
        120,
        1000,
        "Dynamic configuration",
        "config",
        (view_access & edit_access),
        sync,
        dyncfg_function_intercept_cb,
        NULL);

    if(df->type != DYNCFG_TYPE_TEMPLATE && (df->cmds & (DYNCFG_CMD_ENABLE|DYNCFG_CMD_DISABLE))) {
        DYNCFG_CMDS status_to_send_to_plugin =
            (df->dyncfg.user_disabled || df->current.status == DYNCFG_STATUS_DISABLED) ? DYNCFG_CMD_DISABLE : DYNCFG_CMD_ENABLE;

        if (status_to_send_to_plugin == DYNCFG_CMD_ENABLE && dyncfg_is_user_disabled(string2str(df->template)))
            status_to_send_to_plugin = DYNCFG_CMD_DISABLE;

        dyncfg_echo(item, df, id, status_to_send_to_plugin);
    }

    if(!(df->current.source_type == DYNCFG_SOURCE_TYPE_DYNCFG && df->type == DYNCFG_TYPE_JOB))
        dyncfg_send_updates(id);

    dictionary_acquired_item_release(dyncfg_globals.nodes, item);

    return true;
}

void dyncfg_del_low_level(RRDHOST *host, const char *id) {
    if(!dyncfg_is_valid_id(id)) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: id '%s' is invalid. Ignoring dynamic configuration for it.", id);
        return;
    }

    const DICTIONARY_ITEM *item = dictionary_get_and_acquire_item(dyncfg_globals.nodes, id);
    if(item) {
        DYNCFG *df = dictionary_acquired_item_value(item);
        rrd_function_del(host, NULL, string2str(df->function));

        bool garbage_collect = false;
        if(df->dyncfg.saves == 0) {
            dictionary_del(dyncfg_globals.nodes, id);
            garbage_collect = true;
        }

        dictionary_acquired_item_release(dyncfg_globals.nodes, item);

        if(garbage_collect)
            dictionary_garbage_collect(dyncfg_globals.nodes);
    }
}

void dyncfg_status_low_level(RRDHOST *host __maybe_unused, const char *id, DYNCFG_STATUS status) {
    if(!dyncfg_is_valid_id(id)) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: id '%s' is invalid. Ignoring dynamic configuration for it.", id);
        return;
    }

    if(status == DYNCFG_STATUS_NONE) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: status provided to id '%s' is invalid. Ignoring it.", id);
        return;
    }

    const DICTIONARY_ITEM *item = dictionary_get_and_acquire_item(dyncfg_globals.nodes, id);
    if(item) {
        DYNCFG *df = dictionary_acquired_item_value(item);
        df->current.status = status;
        dictionary_acquired_item_release(dyncfg_globals.nodes, item);
    }
}

// ----------------------------------------------------------------------------

void dyncfg_add_streaming(BUFFER *wb) {
    // when sending config functions to parents, we send only 1 function called 'config';
    // the parent will send the command to the child, and the child will validate it;
    // this way the parent does not need to receive removals of config functions;

    buffer_sprintf(wb
                   , PLUGINSD_KEYWORD_FUNCTION " GLOBAL " PLUGINSD_FUNCTION_CONFIG " %d \"%s\" \"%s\" "HTTP_ACCESS_FORMAT" %d\n"
                   , 120
                   , "Dynamic configuration"
                   , "config"
                   , (HTTP_ACCESS_FORMAT_CAST)(HTTP_ACCESS_SIGNED_ID | HTTP_ACCESS_SAME_SPACE | HTTP_ACCESS_SENSITIVE_DATA)
                   , 1000
    );
}

bool dyncfg_available_for_rrdhost(RRDHOST *host) {
    if(host == localhost || rrdhost_option_check(host, RRDHOST_OPTION_VIRTUAL_HOST))
        return true;

    return rrd_function_available(host, PLUGINSD_FUNCTION_CONFIG);
}

// ----------------------------------------------------------------------------