summaryrefslogtreecommitdiffstats
path: root/src/database/rrdfunctions-exporters.c
blob: afcdc8a981c10c1e8baf87c9188aa9bb8768ddac (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
// SPDX-License-Identifier: GPL-3.0-or-later

#define NETDATA_RRD_INTERNALS

#include "rrdfunctions-internals.h"
#include "rrdfunctions-exporters.h"

void rrd_chart_functions_expose_rrdpush(RRDSET *st, BUFFER *wb) {
    if(!st->functions_view)
        return;

    struct rrd_host_function *t;
    dfe_start_read(st->functions_view, t) {
        if(t->options & RRD_FUNCTION_DYNCFG) continue;

        buffer_sprintf(wb
                       , PLUGINSD_KEYWORD_FUNCTION " \"%s\" %d \"%s\" \"%s\" "HTTP_ACCESS_FORMAT" %d\n"
                       , t_dfe.name
                       , t->timeout
                       , string2str(t->help)
                       , string2str(t->tags)
                       , (HTTP_ACCESS_FORMAT_CAST)t->access
                       , t->priority
        );
    }
    dfe_done(t);
}

void rrd_global_functions_expose_rrdpush(RRDHOST *host, BUFFER *wb, bool dyncfg) {
    rrdhost_flag_clear(host, RRDHOST_FLAG_GLOBAL_FUNCTIONS_UPDATED);

    size_t configs = 0;

    struct rrd_host_function *tmp;
    dfe_start_read(host->functions, tmp) {
        if(tmp->options & RRD_FUNCTION_LOCAL) continue;
        if(tmp->options & RRD_FUNCTION_DYNCFG) {
            // we should not send dyncfg to this parent
            configs++;
            continue;
        }

        buffer_sprintf(wb
                       , PLUGINSD_KEYWORD_FUNCTION " GLOBAL \"%s\" %d \"%s\" \"%s\" "HTTP_ACCESS_FORMAT" %d\n"
                       , tmp_dfe.name
                       , tmp->timeout
                       , string2str(tmp->help)
                       , string2str(tmp->tags)
                       , (HTTP_ACCESS_FORMAT_CAST)tmp->access
                       , tmp->priority
        );
    }
    dfe_done(tmp);

    if(dyncfg && configs)
        dyncfg_add_streaming(wb);
}

static void functions2json(DICTIONARY *functions, BUFFER *wb) {
    struct rrd_host_function *t;
    dfe_start_read(functions, t) {
        if (!rrd_collector_running(t->collector)) continue;
        if(t->options & RRD_FUNCTION_DYNCFG) continue;

        buffer_json_member_add_object(wb, t_dfe.name);
        {
            buffer_json_member_add_string_or_empty(wb, "help", string2str(t->help));
            buffer_json_member_add_int64(wb, "timeout", (int64_t) t->timeout);

            char options[65];
            snprintfz(
                options, 64
                , "%s%s"
                , (t->options & RRD_FUNCTION_LOCAL) ? "LOCAL " : ""
                , (t->options & RRD_FUNCTION_GLOBAL) ? "GLOBAL" : ""
            );

            buffer_json_member_add_string_or_empty(wb, "options", options);
            buffer_json_member_add_string_or_empty(wb, "tags", string2str(t->tags));
            http_access2buffer_json_array(wb, "access", t->access);
            buffer_json_member_add_uint64(wb, "priority", t->priority);
        }
        buffer_json_object_close(wb);
    }
    dfe_done(t);
}

void chart_functions2json(RRDSET *st, BUFFER *wb) {
    if(!st || !st->functions_view) return;

    functions2json(st->functions_view, wb);
}

void host_functions2json(RRDHOST *host, BUFFER *wb) {
    if(!host || !host->functions) return;

    buffer_json_member_add_object(wb, "functions");

    struct rrd_host_function *t;
    dfe_start_read(host->functions, t) {
        if(!rrd_collector_running(t->collector)) continue;
        if(t->options & RRD_FUNCTION_DYNCFG) continue;

        buffer_json_member_add_object(wb, t_dfe.name);
        {
            buffer_json_member_add_string(wb, "help", string2str(t->help));
            buffer_json_member_add_int64(wb, "timeout", t->timeout);
            buffer_json_member_add_array(wb, "options");
            {
                if (t->options & RRD_FUNCTION_GLOBAL)
                    buffer_json_add_array_item_string(wb, "GLOBAL");
                if (t->options & RRD_FUNCTION_LOCAL)
                    buffer_json_add_array_item_string(wb, "LOCAL");
            }
            buffer_json_array_close(wb);
            buffer_json_member_add_string(wb, "tags", string2str(t->tags));
            http_access2buffer_json_array(wb, "access", t->access);
            buffer_json_member_add_uint64(wb, "priority", t->priority);
        }
        buffer_json_object_close(wb);
    }
    dfe_done(t);

    buffer_json_object_close(wb);
}

void chart_functions_to_dict(DICTIONARY *rrdset_functions_view, DICTIONARY *dst, void *value, size_t value_size) {
    if(!rrdset_functions_view || !dst) return;

    struct rrd_host_function *t;
    dfe_start_read(rrdset_functions_view, t) {
        if(!rrd_collector_running(t->collector)) continue;
        if(t->options & RRD_FUNCTION_DYNCFG) continue;

        dictionary_set(dst, t_dfe.name, value, value_size);
    }
    dfe_done(t);
}

void host_functions_to_dict(RRDHOST *host, DICTIONARY *dst, void *value, size_t value_size,
                            STRING **help, STRING **tags, HTTP_ACCESS *access, int *priority) {
    if(!host || !host->functions || !dictionary_entries(host->functions) || !dst) return;

    struct rrd_host_function *t;
    dfe_start_read(host->functions, t) {
        if(!rrd_collector_running(t->collector)) continue;
        if(t->options & RRD_FUNCTION_DYNCFG) continue;

        if(help)
            *help = t->help;

        if(tags)
            *tags = t->tags;

        if(access)
            *access = t->access;

        if(priority)
            *priority = t->priority;

        dictionary_set(dst, t_dfe.name, value, value_size);
    }
    dfe_done(t);
}