diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-10-17 09:30:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-10-17 09:30:20 +0000 |
commit | 386ccdd61e8256c8b21ee27ee2fc12438fc5ca98 (patch) | |
tree | c9fbcacdb01f029f46133a5ba7ecd610c2bcb041 /web/api/web_api_v2.c | |
parent | Adding upstream version 1.42.4. (diff) | |
download | netdata-386ccdd61e8256c8b21ee27ee2fc12438fc5ca98.tar.xz netdata-386ccdd61e8256c8b21ee27ee2fc12438fc5ca98.zip |
Adding upstream version 1.43.0.upstream/1.43.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'web/api/web_api_v2.c')
-rw-r--r-- | web/api/web_api_v2.c | 123 |
1 files changed, 119 insertions, 4 deletions
diff --git a/web/api/web_api_v2.c b/web/api/web_api_v2.c index e288a5c6b..9daf80b9d 100644 --- a/web/api/web_api_v2.c +++ b/web/api/web_api_v2.c @@ -663,13 +663,27 @@ static int web_client_api_request_v2_webrtc(RRDHOST *host __maybe_unused, struct } #define CONFIG_API_V2_URL "/api/v2/config" -static int web_client_api_request_v2_config(RRDHOST *host __maybe_unused, struct web_client *w, char *query) { +static int web_client_api_request_v2_config(RRDHOST *host __maybe_unused, struct web_client *w, char *query __maybe_unused) { char *url = strdupz(buffer_tostring(w->url_as_received)); char *url_full = url; + buffer_flush(w->response.data); + + if (strncmp(url, "/host/", strlen("/host/")) == 0) { + url += strlen("/host/"); + char *host_id_end = strchr(url, '/'); + if (host_id_end == NULL) { + buffer_sprintf(w->response.data, "Invalid URL"); + freez(url_full); + return HTTP_RESP_BAD_REQUEST; + } + url += host_id_end - url; + } + if (strncmp(url, CONFIG_API_V2_URL, strlen(CONFIG_API_V2_URL)) != 0) { buffer_sprintf(w->response.data, "Invalid URL"); + freez(url_full); return HTTP_RESP_BAD_REQUEST; } url += strlen(CONFIG_API_V2_URL); @@ -680,7 +694,6 @@ static int web_client_api_request_v2_config(RRDHOST *host __maybe_unused, struct char *job_id = strtok_r(NULL, "/", &save_ptr); char *extra = strtok_r(NULL, "/", &save_ptr); - buffer_flush(w->response.data); if (extra != NULL) { buffer_sprintf(w->response.data, "Invalid URL"); freez(url_full); @@ -708,7 +721,7 @@ static int web_client_api_request_v2_config(RRDHOST *host __maybe_unused, struct return HTTP_RESP_BAD_REQUEST; } - struct uni_http_response resp = dyn_conf_process_http_request(http_method, plugin, module, job_id, w->post_payload, w->post_payload_size); + struct uni_http_response resp = dyn_conf_process_http_request(host->configurable_plugins, http_method, plugin, module, job_id, w->post_payload, w->post_payload_size); if (resp.content[resp.content_length - 1] != '\0') { char *con = mallocz(resp.content_length + 1); memcpy(con, resp.content, resp.content_length); @@ -726,6 +739,105 @@ static int web_client_api_request_v2_config(RRDHOST *host __maybe_unused, struct return resp.status; } +static json_object *job_statuses_grouped() { + json_object *top_obj = json_object_new_object(); + json_object *host_vec = json_object_new_array(); + + + RRDHOST *host; + + dfe_start_reentrant(rrdhost_root_index, host) { + json_object *host_obj = json_object_new_object(); + json_object *host_sub_obj = json_object_new_string(host->machine_guid); + json_object_object_add(host_obj, "host_guid", host_sub_obj); + host_sub_obj = json_object_new_array(); + + DICTIONARY *plugins_dict = host->configurable_plugins; + + struct configurable_plugin *plugin; + dfe_start_read(plugins_dict, plugin) { + json_object *plugin_obj = json_object_new_object(); + json_object *plugin_sub_obj = json_object_new_string(plugin->name); + json_object_object_add(plugin_obj, "name", plugin_sub_obj); + plugin_sub_obj = json_object_new_array(); + + struct module *module; + dfe_start_read(plugin->modules, module) { + json_object *module_obj = json_object_new_object(); + json_object *module_sub_obj = json_object_new_string(module->name); + json_object_object_add(module_obj, "name", module_sub_obj); + module_sub_obj = json_object_new_array(); + + struct job *job; + dfe_start_read(module->jobs, job) { + json_object *job_obj = json_object_new_object(); + json_object *job_sub_obj = json_object_new_string(job->name); + json_object_object_add(job_obj, "name", job_sub_obj); + job_sub_obj = job2json(job); + json_object_object_add(job_obj, "job", job_sub_obj); + json_object_array_add(module_sub_obj, job_obj); + } dfe_done(job); + json_object_object_add(module_obj, "jobs", module_sub_obj); + json_object_array_add(plugin_sub_obj, module_obj); + } dfe_done(module); + json_object_object_add(plugin_obj, "modules", plugin_sub_obj); + json_object_array_add(host_sub_obj, plugin_obj); + } dfe_done(plugin); + json_object_object_add(host_obj, "plugins", host_sub_obj); + json_object_array_add(host_vec, host_obj); + } + dfe_done(host); + + json_object_object_add(top_obj, "hosts", host_vec); + return top_obj; +} + +static json_object *job_statuses_flat() { + RRDHOST *host; + + json_object *ret = json_object_new_array(); + + dfe_start_reentrant(rrdhost_root_index, host) { + DICTIONARY *plugins_dict = host->configurable_plugins; + + struct configurable_plugin *plugin; + dfe_start_read(plugins_dict, plugin) { + struct module *module; + dfe_start_read(plugin->modules, module) { + struct job *job; + dfe_start_read(module->jobs, job) { + json_object *job_rich = json_object_new_object(); + json_object *obj = json_object_new_string(host->machine_guid); + json_object_object_add(job_rich, "host_guid", obj); + obj = json_object_new_string(plugin->name); + json_object_object_add(job_rich, "plugin_name", obj); + obj = json_object_new_string(module->name); + json_object_object_add(job_rich, "module_name", obj); + obj = job2json(job); + json_object_object_add(job_rich, "job", obj); + json_object_array_add(ret, job_rich); + } dfe_done(job); + } dfe_done(module); + } dfe_done(plugin); + } + dfe_done(host); + + return ret; +} + +static int web_client_api_request_v2_job_statuses(RRDHOST *host __maybe_unused, struct web_client *w, char *query) { + json_object *json; + if (strstr(query, "grouped") != NULL) + json = job_statuses_grouped(); + else + json = job_statuses_flat(); + + buffer_flush(w->response.data); + buffer_strcat(w->response.data, json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY)); + w->response.data->content_type = CT_APPLICATION_JSON; + return HTTP_RESP_OK; +} + static struct web_api_command api_commands_v2[] = { {"info", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_info, 0}, @@ -749,7 +861,10 @@ static struct web_api_command api_commands_v2[] = { {"bearer_protection", 0, WEB_CLIENT_ACL_ACLK | ACL_DEV_OPEN_ACCESS, api_v2_bearer_protection, 0}, {"bearer_get_token", 0, WEB_CLIENT_ACL_ACLK | ACL_DEV_OPEN_ACCESS, api_v2_bearer_token, 0}, - {"config", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_config, 1}, + {"config", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_config, 1}, + {"job_statuses", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_job_statuses, 0}, + + { "ilove.svg", 0, WEB_CLIENT_ACL_NOCHECK, web_client_api_request_v2_ilove, 0 }, // terminator {NULL, 0, WEB_CLIENT_ACL_NONE, NULL, 0}, |