| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "rrdfunctions-internals.h" |
| 4 | #include "rrdfunctions-exporters.h" |
| 5 | |
| 6 | void stream_sender_send_rrdset_functions(RRDSET *st, BUFFER *wb) { |
| 7 | if(!st->functions_view) |
| 8 | return; |
| 9 | |
| 10 | struct rrd_host_function *t; |
| 11 | dfe_start_read(st->functions_view, t) { |
| 12 | if(t->options & RRD_FUNCTION_DYNCFG) continue; |
| 13 | |
| 14 | buffer_sprintf(wb |
| 15 | , PLUGINSD_KEYWORD_FUNCTION " \"%s\" %d \"%s\" \"%s\" "HTTP_ACCESS_FORMAT" %d %"PRIu32"\n" |
| 16 | , t_dfe.name |
| 17 | , t->timeout |
| 18 | , string2str(t->help) |
| 19 | , string2str(t->tags) |
| 20 | , (HTTP_ACCESS_FORMAT_CAST)t->access |
| 21 | , t->priority |
| 22 | , t->version |
| 23 | ); |
| 24 | } |
| 25 | dfe_done(t); |
| 26 | } |
| 27 | |
| 28 | void stream_sender_send_global_rrdhost_functions(RRDHOST *host, BUFFER *wb, bool dyncfg) { |
| 29 | rrdhost_flag_clear(host, RRDHOST_FLAG_GLOBAL_FUNCTIONS_UPDATED); |
| 30 | |
| 31 | size_t configs = 0; |
| 32 | |
| 33 | struct rrd_host_function *tmp; |
| 34 | dfe_start_read(host->functions, tmp) { |
| 35 | if(tmp->options & RRD_FUNCTION_LOCAL) continue; |
| 36 | if(tmp->options & RRD_FUNCTION_DYNCFG) { |
| 37 | // we should not send dyncfg to this parent |
| 38 | configs++; |
| 39 | continue; |
| 40 | } |
| 41 | |
| 42 | buffer_sprintf(wb |
| 43 | , PLUGINSD_KEYWORD_FUNCTION " GLOBAL \"%s\" %d \"%s\" \"%s\" "HTTP_ACCESS_FORMAT" %d %"PRIu32"\n" |
| 44 | , tmp_dfe.name |
| 45 | , tmp->timeout |
| 46 | , string2str(tmp->help) |
| 47 | , string2str(tmp->tags) |
| 48 | , (HTTP_ACCESS_FORMAT_CAST)tmp->access |
| 49 | , tmp->priority |
| 50 | , tmp->version |
| 51 | ); |
| 52 | } |
| 53 | dfe_done(tmp); |
| 54 | |
| 55 | if(dyncfg && configs) |
| 56 | dyncfg_add_streaming(wb); |
| 57 | } |
| 58 | |
| 59 | static void functions2json(DICTIONARY *functions, BUFFER *wb) { |
| 60 | struct rrd_host_function *t; |
| 61 | dfe_start_read(functions, t) { |
| 62 | if (!rrd_collector_running(t->collector)) continue; |
| 63 | if(t->options & (RRD_FUNCTION_DYNCFG| RRD_FUNCTION_RESTRICTED)) continue; |
| 64 | |
| 65 | buffer_json_member_add_object(wb, t_dfe.name); |
| 66 | { |
| 67 | buffer_json_member_add_string_or_empty(wb, "help", string2str(t->help)); |
| 68 | buffer_json_member_add_int64(wb, "timeout", (int64_t) t->timeout); |
| 69 | buffer_json_member_add_uint64(wb, "version", (uint64_t) t->version); |
| 70 | |
| 71 | char options[65]; |
| 72 | snprintfz( |
| 73 | options, 64 |
| 74 | , "%s%s" |
| 75 | , (t->options & RRD_FUNCTION_LOCAL) ? "LOCAL " : "" |
| 76 | , (t->options & RRD_FUNCTION_GLOBAL) ? "GLOBAL" : "" |
| 77 | ); |
| 78 | |
| 79 | buffer_json_member_add_string_or_empty(wb, "options", options); |
| 80 | buffer_json_member_add_string_or_empty(wb, "tags", string2str(t->tags)); |
| 81 | http_access2buffer_json_array(wb, "access", t->access); |
| 82 | buffer_json_member_add_uint64(wb, "priority", t->priority); |
| 83 | } |
| 84 | buffer_json_object_close(wb); |
| 85 | } |
| 86 | dfe_done(t); |
| 87 | } |
| 88 | |
| 89 | void chart_functions2json(RRDSET *st, BUFFER *wb) { |
| 90 | if(!st || !st->functions_view) return; |
| 91 | |
| 92 | functions2json(st->functions_view, wb); |
| 93 | } |
| 94 | |
| 95 | void host_functions2json(RRDHOST *host, BUFFER *wb) { |
| 96 | if(!host || !host->functions) return; |
| 97 | |
| 98 | buffer_json_member_add_object(wb, "functions"); |
| 99 | |
| 100 | struct rrd_host_function *t; |
| 101 | dfe_start_read(host->functions, t) { |
| 102 | if(!rrd_collector_running(t->collector)) continue; |
| 103 | if(t->options & (RRD_FUNCTION_DYNCFG| RRD_FUNCTION_RESTRICTED)) continue; |
| 104 | |
| 105 | buffer_json_member_add_object(wb, t_dfe.name); |
| 106 | { |
| 107 | buffer_json_member_add_string(wb, "help", string2str(t->help)); |
| 108 | buffer_json_member_add_int64(wb, "timeout", t->timeout); |
| 109 | buffer_json_member_add_uint64(wb, "version", (uint64_t) t->version); |
| 110 | buffer_json_member_add_array(wb, "options"); |
| 111 | { |
| 112 | if (t->options & RRD_FUNCTION_GLOBAL) |
| 113 | buffer_json_add_array_item_string(wb, "GLOBAL"); |
| 114 | if (t->options & RRD_FUNCTION_LOCAL) |
| 115 | buffer_json_add_array_item_string(wb, "LOCAL"); |
| 116 | } |
| 117 | buffer_json_array_close(wb); |
| 118 | buffer_json_member_add_string(wb, "tags", string2str(t->tags)); |
| 119 | http_access2buffer_json_array(wb, "access", t->access); |
| 120 | buffer_json_member_add_uint64(wb, "priority", t->priority); |
| 121 | } |
| 122 | buffer_json_object_close(wb); |
| 123 | } |
| 124 | dfe_done(t); |
| 125 | |
| 126 | buffer_json_object_close(wb); |
| 127 | } |
| 128 | |
| 129 | void chart_functions_to_dict(DICTIONARY *rrdset_functions_view, DICTIONARY *dst, void *value, size_t value_size) { |
| 130 | if(!rrdset_functions_view || !dst) return; |
| 131 | |
| 132 | struct rrd_host_function *t; |
| 133 | dfe_start_read(rrdset_functions_view, t) { |
| 134 | if(!rrd_collector_running(t->collector)) continue; |
| 135 | if(t->options & (RRD_FUNCTION_DYNCFG| RRD_FUNCTION_RESTRICTED)) continue; |
| 136 | |
| 137 | dictionary_set(dst, t_dfe.name, value, value_size); |
| 138 | } |
| 139 | dfe_done(t); |
| 140 | } |
| 141 | |
| 142 | void host_functions_to_dict(RRDHOST *host, DICTIONARY *dst, void *value, size_t value_size, |
| 143 | STRING **help, STRING **tags, HTTP_ACCESS *access, int *priority, uint32_t *version) { |
| 144 | if(!host || !host->functions || !dictionary_entries(host->functions) || !dst) return; |
| 145 | |
| 146 | struct rrd_host_function *t; |
| 147 | dfe_start_read(host->functions, t) { |
| 148 | if(!rrd_collector_running(t->collector)) continue; |
| 149 | if(t->options & (RRD_FUNCTION_DYNCFG| RRD_FUNCTION_RESTRICTED)) continue; |
| 150 | |
| 151 | if(help) |
| 152 | *help = t->help; |
| 153 | |
| 154 | if(tags) |
| 155 | *tags = t->tags; |
| 156 | |
| 157 | if(access) |
| 158 | *access = t->access; |
| 159 | |
| 160 | if(priority) |
| 161 | *priority = t->priority; |
| 162 | |
| 163 | if(version) |
| 164 | *version = t->version; |
| 165 | |
| 166 | char key[UINT64_MAX_LENGTH + sizeof(RRDFUNCTIONS_VERSION_SEPARATOR) + strlen(t_dfe.name)]; |
| 167 | snprintfz(key, sizeof(key), "%"PRIu32 RRDFUNCTIONS_VERSION_SEPARATOR "%s", |
| 168 | t->version, t_dfe.name); |
| 169 | |
| 170 | dictionary_set(dst, key, value, value_size); |
| 171 | } |
| 172 | dfe_done(t); |
| 173 | } |