web_api.c 8.2 KB

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