web_api.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "web_api.h"
  3. int web_client_api_request_vX(RRDHOST *host, struct web_client *w, char *url_path_endpoint, struct web_api_command *api_commands) {
  4. if(!web_client_flags_check_auth(w))
  5. w->access = HTTP_ACCESS_ANY;
  6. #ifdef NETDATA_GOD_MODE
  7. web_client_flag_set(w, WEB_CLIENT_FLAG_AUTH_GOD);
  8. w->access = HTTP_ACCESS_ADMIN;
  9. #endif
  10. buffer_no_cacheable(w->response.data);
  11. if(unlikely(!url_path_endpoint || !*url_path_endpoint)) {
  12. buffer_flush(w->response.data);
  13. buffer_sprintf(w->response.data, "Which API command?");
  14. return HTTP_RESP_BAD_REQUEST;
  15. }
  16. char *api_command = strchr(url_path_endpoint, '/');
  17. if (likely(api_command == NULL)) // only config command supports subpaths for now
  18. api_command = url_path_endpoint;
  19. else {
  20. size_t api_command_len = api_command - url_path_endpoint;
  21. api_command = callocz(1, api_command_len + 1);
  22. memcpy(api_command, url_path_endpoint, api_command_len);
  23. }
  24. uint32_t hash = simple_hash(api_command);
  25. for(int i = 0; api_commands[i].command ; i++) {
  26. if(unlikely(hash == api_commands[i].hash && !strcmp(api_command, api_commands[i].command))) {
  27. if(unlikely(!api_commands[i].allow_subpaths && api_command != url_path_endpoint)) {
  28. buffer_flush(w->response.data);
  29. buffer_sprintf(w->response.data, "API command '%s' does not support subpaths.", api_command);
  30. freez(api_command);
  31. return HTTP_RESP_BAD_REQUEST;
  32. }
  33. if (api_command != url_path_endpoint)
  34. freez(api_command);
  35. bool acl_allows = (w->acl & api_commands[i].acl) || (api_commands[i].acl & HTTP_ACL_NOCHECK);
  36. if(!acl_allows)
  37. return web_client_permission_denied(w);
  38. char *query_string = (char *)buffer_tostring(w->url_query_string_decoded);
  39. if(*query_string == '?')
  40. query_string = &query_string[1];
  41. return api_commands[i].callback(host, w, query_string);
  42. }
  43. }
  44. if (api_command != url_path_endpoint)
  45. freez(api_command);
  46. buffer_flush(w->response.data);
  47. buffer_strcat(w->response.data, "Unsupported API command: ");
  48. buffer_strcat_htmlescape(w->response.data, url_path_endpoint);
  49. return HTTP_RESP_NOT_FOUND;
  50. }
  51. RRDCONTEXT_TO_JSON_OPTIONS rrdcontext_to_json_parse_options(char *o) {
  52. RRDCONTEXT_TO_JSON_OPTIONS options = RRDCONTEXT_OPTION_NONE;
  53. char *tok;
  54. while(o && *o && (tok = strsep_skip_consecutive_separators(&o, ", |"))) {
  55. if(!*tok) continue;
  56. if(!strcmp(tok, "full") || !strcmp(tok, "all"))
  57. options |= RRDCONTEXT_OPTIONS_ALL;
  58. else if(!strcmp(tok, "charts") || !strcmp(tok, "instances"))
  59. options |= RRDCONTEXT_OPTION_SHOW_INSTANCES;
  60. else if(!strcmp(tok, "dimensions") || !strcmp(tok, "metrics"))
  61. options |= RRDCONTEXT_OPTION_SHOW_METRICS;
  62. else if(!strcmp(tok, "queue"))
  63. options |= RRDCONTEXT_OPTION_SHOW_QUEUED;
  64. else if(!strcmp(tok, "flags"))
  65. options |= RRDCONTEXT_OPTION_SHOW_FLAGS;
  66. else if(!strcmp(tok, "uuids"))
  67. options |= RRDCONTEXT_OPTION_SHOW_UUIDS;
  68. else if(!strcmp(tok, "deleted"))
  69. options |= RRDCONTEXT_OPTION_SHOW_DELETED;
  70. else if(!strcmp(tok, "labels"))
  71. options |= RRDCONTEXT_OPTION_SHOW_LABELS;
  72. else if(!strcmp(tok, "deepscan"))
  73. options |= RRDCONTEXT_OPTION_DEEPSCAN;
  74. else if(!strcmp(tok, "hidden"))
  75. options |= RRDCONTEXT_OPTION_SHOW_HIDDEN;
  76. }
  77. return options;
  78. }
  79. int web_client_api_request_weights(RRDHOST *host, struct web_client *w, char *url, WEIGHTS_METHOD method, WEIGHTS_FORMAT format, size_t api_version) {
  80. if (!netdata_ready)
  81. return HTTP_RESP_SERVICE_UNAVAILABLE;
  82. time_t baseline_after = 0, baseline_before = 0, after = 0, before = 0;
  83. size_t points = 0;
  84. RRDR_OPTIONS options = 0;
  85. RRDR_TIME_GROUPING time_group_method = RRDR_GROUPING_AVERAGE;
  86. time_t timeout_ms = 0;
  87. size_t tier = 0;
  88. const char *time_group_options = NULL, *scope_contexts = NULL, *scope_nodes = NULL, *contexts = NULL, *nodes = NULL,
  89. *instances = NULL, *dimensions = NULL, *labels = NULL, *alerts = NULL;
  90. struct group_by_pass group_by = {
  91. .group_by = RRDR_GROUP_BY_NONE,
  92. .group_by_label = NULL,
  93. .aggregation = RRDR_GROUP_BY_FUNCTION_AVERAGE,
  94. };
  95. while (url) {
  96. char *value = strsep_skip_consecutive_separators(&url, "&");
  97. if (!value || !*value)
  98. continue;
  99. char *name = strsep_skip_consecutive_separators(&value, "=");
  100. if (!name || !*name)
  101. continue;
  102. if (!value || !*value)
  103. continue;
  104. if (!strcmp(name, "baseline_after"))
  105. baseline_after = str2l(value);
  106. else if (!strcmp(name, "baseline_before"))
  107. baseline_before = str2l(value);
  108. else if (!strcmp(name, "after") || !strcmp(name, "highlight_after"))
  109. after = str2l(value);
  110. else if (!strcmp(name, "before") || !strcmp(name, "highlight_before"))
  111. before = str2l(value);
  112. else if (!strcmp(name, "points") || !strcmp(name, "max_points"))
  113. points = str2ul(value);
  114. else if (!strcmp(name, "timeout"))
  115. timeout_ms = str2l(value);
  116. else if((api_version == 1 && !strcmp(name, "group")) || (api_version >= 2 && !strcmp(name, "time_group")))
  117. time_group_method = time_grouping_parse(value, RRDR_GROUPING_AVERAGE);
  118. else if((api_version == 1 && !strcmp(name, "group_options")) || (api_version >= 2 && !strcmp(name, "time_group_options")))
  119. time_group_options = value;
  120. else if(!strcmp(name, "options"))
  121. options |= web_client_api_request_v1_data_options(value);
  122. else if(!strcmp(name, "method"))
  123. method = weights_string_to_method(value);
  124. else if(api_version == 1 && (!strcmp(name, "context") || !strcmp(name, "contexts")))
  125. scope_contexts = value;
  126. else if(api_version >= 2 && !strcmp(name, "scope_nodes")) scope_nodes = value;
  127. else if(api_version >= 2 && !strcmp(name, "scope_contexts")) scope_contexts = value;
  128. else if(api_version >= 2 && !strcmp(name, "nodes")) nodes = value;
  129. else if(api_version >= 2 && !strcmp(name, "contexts")) contexts = value;
  130. else if(api_version >= 2 && !strcmp(name, "instances")) instances = value;
  131. else if(api_version >= 2 && !strcmp(name, "dimensions")) dimensions = value;
  132. else if(api_version >= 2 && !strcmp(name, "labels")) labels = value;
  133. else if(api_version >= 2 && !strcmp(name, "alerts")) alerts = value;
  134. else if(api_version >= 2 && (!strcmp(name, "group_by") || !strcmp(name, "group_by[0]"))) {
  135. group_by.group_by = group_by_parse(value);
  136. }
  137. else if(api_version >= 2 && (!strcmp(name, "group_by_label") || !strcmp(name, "group_by_label[0]"))) {
  138. group_by.group_by_label = value;
  139. }
  140. else if(api_version >= 2 && (!strcmp(name, "aggregation") || !strcmp(name, "aggregation[0]"))) {
  141. group_by.aggregation = group_by_aggregate_function_parse(value);
  142. }
  143. else if(!strcmp(name, "tier")) {
  144. tier = str2ul(value);
  145. if(tier < storage_tiers)
  146. options |= RRDR_OPTION_SELECTED_TIER;
  147. else
  148. tier = 0;
  149. }
  150. }
  151. if(options == 0)
  152. // the user did not set any options
  153. options = RRDR_OPTION_NOT_ALIGNED | RRDR_OPTION_NULL2ZERO | RRDR_OPTION_NONZERO;
  154. else
  155. // the user set some options, add also these
  156. options |= RRDR_OPTION_NOT_ALIGNED | RRDR_OPTION_NULL2ZERO;
  157. if(options & RRDR_OPTION_PERCENTAGE)
  158. options |= RRDR_OPTION_ABSOLUTE;
  159. if(options & RRDR_OPTION_DEBUG)
  160. options &= ~RRDR_OPTION_MINIFY;
  161. BUFFER *wb = w->response.data;
  162. buffer_flush(wb);
  163. wb->content_type = CT_APPLICATION_JSON;
  164. QUERY_WEIGHTS_REQUEST qwr = {
  165. .version = api_version,
  166. .host = (api_version == 1) ? NULL : host,
  167. .scope_nodes = scope_nodes,
  168. .scope_contexts = scope_contexts,
  169. .nodes = nodes,
  170. .contexts = contexts,
  171. .instances = instances,
  172. .dimensions = dimensions,
  173. .labels = labels,
  174. .alerts = alerts,
  175. .group_by = {
  176. .group_by = group_by.group_by,
  177. .group_by_label = group_by.group_by_label,
  178. .aggregation = group_by.aggregation,
  179. },
  180. .method = method,
  181. .format = format,
  182. .time_group_method = time_group_method,
  183. .time_group_options = time_group_options,
  184. .baseline_after = baseline_after,
  185. .baseline_before = baseline_before,
  186. .after = after,
  187. .before = before,
  188. .points = points,
  189. .options = options,
  190. .tier = tier,
  191. .timeout_ms = timeout_ms,
  192. .interrupt_callback = web_client_interrupt_callback,
  193. .interrupt_callback_data = w,
  194. .transaction = &w->transaction,
  195. };
  196. return web_api_v12_weights(wb, &qwr);
  197. }
  198. bool web_client_interrupt_callback(void *data) {
  199. struct web_client *w = data;
  200. if(w->interrupt.callback)
  201. return w->interrupt.callback(w, w->interrupt.callback_data);
  202. return sock_has_output_error(w->ofd);
  203. }