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

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

// ----------------------------------------------------------------------------
// echo is when we send requests to plugins without any caller
// it is used for:
// 1. the first enable/disable requests we send, and also
// 2. updates to stock or user configurations
// 3. saved dynamic jobs we need to add to templates

struct dyncfg_echo {
    const DICTIONARY_ITEM *item;
    DYNCFG *df;                     // for additions this is the job, not the template
    BUFFER *wb;
    DYNCFG_CMDS cmd;
    const char *cmd_str;
};

void dyncfg_echo_cb(BUFFER *wb __maybe_unused, int code __maybe_unused, void *result_cb_data) {
    struct dyncfg_echo *e = result_cb_data;
    DYNCFG *df = e->df;

    if(DYNCFG_RESP_SUCCESS(code)) {
        // successful response

        if(e->cmd == DYNCFG_CMD_ADD) {
            df->dyncfg.status = dyncfg_status_from_successful_response(code);
            dyncfg_update_status_on_successful_add_or_update(df, code);
        }
        else if(e->cmd == DYNCFG_CMD_UPDATE) {
            df->dyncfg.status = dyncfg_status_from_successful_response(code);
            dyncfg_update_status_on_successful_add_or_update(df, code);
        }
        else if(e->cmd == DYNCFG_CMD_DISABLE)
            df->dyncfg.status = df->current.status = DYNCFG_STATUS_DISABLED;
        else if(e->cmd == DYNCFG_CMD_ENABLE)
            df->dyncfg.status = df->current.status = dyncfg_status_from_successful_response(code);
    }
    else {
        // failed response

        nd_log(NDLS_DAEMON, NDLP_ERR,
               "DYNCFG: received response code %d on request to id '%s', cmd: %s",
               code, dictionary_acquired_item_name(e->item), e->cmd_str);

        if(e->cmd == DYNCFG_CMD_UPDATE || e->cmd == DYNCFG_CMD_ADD)
            e->df->dyncfg.plugin_rejected = true;
    }

    buffer_free(e->wb);
    dictionary_acquired_item_release(dyncfg_globals.nodes, e->item);

    e->wb = NULL;
    e->df = NULL;
    e->item = NULL;
    freez((void *)e->cmd_str);
    e->cmd_str = NULL;
    freez(e);
}

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

void dyncfg_echo(const DICTIONARY_ITEM *item, DYNCFG *df, const char *id __maybe_unused, DYNCFG_CMDS cmd) {
    RRDHOST *host = dyncfg_rrdhost(df);
    if(!host) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: cannot find host of configuration id '%s'", id);
        return;
    }

    if(!(df->cmds & cmd)) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: attempted to echo a cmd that is not supported");
        return;
    }

    const char *cmd_str = dyncfg_id2cmd_one(cmd);
    if(!cmd_str) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: command given does not resolve to a known command");
        return;
    }

    struct dyncfg_echo *e = callocz(1, sizeof(struct dyncfg_echo));
    e->item = dictionary_acquired_item_dup(dyncfg_globals.nodes, item);
    e->wb = buffer_create(0, NULL);
    e->df = df;
    e->cmd = cmd;
    e->cmd_str = strdupz(cmd_str);

    char buf[string_strlen(df->function) + strlen(e->cmd_str) + 20];
    snprintfz(buf, sizeof(buf), "%s %s", string2str(df->function), e->cmd_str);

    rrd_function_run(
        host, e->wb, 10,
        HTTP_ACCESS_ALL, buf, false, NULL,
        dyncfg_echo_cb, e,
        NULL, NULL,
        NULL, NULL,
        NULL, string2str(df->dyncfg.source));
}

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

void dyncfg_echo_update(const DICTIONARY_ITEM *item, DYNCFG *df, const char *id) {
    RRDHOST *host = dyncfg_rrdhost(df);
    if(!host) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: cannot find host of configuration id '%s'", id);
        return;
    }

    if(!df->dyncfg.payload) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: requested to send an update to '%s', but there is no payload", id);
        return;
    }

    struct dyncfg_echo *e = callocz(1, sizeof(struct dyncfg_echo));
    e->item = dictionary_acquired_item_dup(dyncfg_globals.nodes, item);
    e->wb = buffer_create(0, NULL);
    e->df = df;
    e->cmd = DYNCFG_CMD_UPDATE;
    e->cmd_str = strdupz("update");

    char buf[string_strlen(df->function) + strlen(e->cmd_str) + 20];
    snprintfz(buf, sizeof(buf), "%s %s", string2str(df->function), e->cmd_str);

    rrd_function_run(
        host, e->wb, 10,
        HTTP_ACCESS_ALL, buf, false, NULL,
        dyncfg_echo_cb, e,
        NULL, NULL,
        NULL, NULL,
        df->dyncfg.payload, string2str(df->dyncfg.source));
}

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

static void dyncfg_echo_payload_add(const DICTIONARY_ITEM *item_template __maybe_unused, const DICTIONARY_ITEM *item_job, DYNCFG *df_template, DYNCFG *df_job, const char *id_template, const char *cmd) {
    RRDHOST *host = dyncfg_rrdhost(df_template);
    if(!host) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: cannot find host of configuration id '%s'", id_template);
        return;
    }

    if(!df_job->dyncfg.payload) {
        nd_log(NDLS_DAEMON, NDLP_ERR,
               "DYNCFG: requested to send a '%s' to '%s', but there is no payload",
               cmd, id_template);
        return;
    }

    struct dyncfg_echo *e = callocz(1, sizeof(struct dyncfg_echo));
    e->item = dictionary_acquired_item_dup(dyncfg_globals.nodes, item_job);
    e->wb = buffer_create(0, NULL);
    e->df = df_job;
    e->cmd = DYNCFG_CMD_ADD;
    e->cmd_str = strdupz(cmd);

    char buf[string_strlen(df_template->function) + strlen(cmd) + 20];
    snprintfz(buf, sizeof(buf), "%s %s", string2str(df_template->function), cmd);

    rrd_function_run(
        host, e->wb, 10,
        HTTP_ACCESS_ALL, buf, false, NULL,
        dyncfg_echo_cb, e,
        NULL, NULL,
        NULL, NULL,
        df_job->dyncfg.payload, string2str(df_job->dyncfg.source));
}

void dyncfg_echo_add(const DICTIONARY_ITEM *item_template, const DICTIONARY_ITEM *item_job, DYNCFG *df_template, DYNCFG *df_job, const char *template_id, const char *job_name) {
    char buf[strlen(job_name) + 20];
    snprintfz(buf, sizeof(buf), "add %s", job_name);
    dyncfg_echo_payload_add(item_template, item_job, df_template, df_job, template_id, buf);
}