web_api_v2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "web_api_v2.h"
  3. #include "../rtc/webrtc.h"
  4. static int web_client_api_request_v2_contexts_internal(RRDHOST *host __maybe_unused, struct web_client *w, char *url, CONTEXTS_V2_OPTIONS options) {
  5. struct api_v2_contexts_request req = { 0 };
  6. while(url) {
  7. char *value = strsep_skip_consecutive_separators(&url, "&");
  8. if(!value || !*value) continue;
  9. char *name = strsep_skip_consecutive_separators(&value, "=");
  10. if(!name || !*name) continue;
  11. if(!value || !*value) continue;
  12. // name and value are now the parameters
  13. // they are not null and not empty
  14. if(!strcmp(name, "scope_nodes")) req.scope_nodes = value;
  15. else if((options & (CONTEXTS_V2_NODES | CONTEXTS_V2_CONTEXTS)) && !strcmp(name, "nodes")) req.nodes = value;
  16. else if((options & CONTEXTS_V2_CONTEXTS) && !strcmp(name, "scope_contexts")) req.scope_contexts = value;
  17. else if((options & CONTEXTS_V2_CONTEXTS) && !strcmp(name, "contexts")) req.contexts = value;
  18. else if((options & CONTEXTS_V2_SEARCH) && !strcmp(name, "q")) req.q = value;
  19. else if(!strcmp(name, "timeout")) req.timeout_ms = str2l(value);
  20. }
  21. options |= CONTEXTS_V2_DEBUG;
  22. buffer_flush(w->response.data);
  23. buffer_no_cacheable(w->response.data);
  24. return rrdcontext_to_json_v2(w->response.data, &req, options);
  25. }
  26. static int web_client_api_request_v2_q(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  27. return web_client_api_request_v2_contexts_internal(host, w, url, CONTEXTS_V2_SEARCH | CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_NODES);
  28. }
  29. static int web_client_api_request_v2_contexts(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  30. return web_client_api_request_v2_contexts_internal(host, w, url, CONTEXTS_V2_CONTEXTS);
  31. }
  32. static int web_client_api_request_v2_nodes(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  33. return web_client_api_request_v2_contexts_internal(host, w, url, CONTEXTS_V2_NODES | CONTEXTS_V2_NODES_DETAILED);
  34. }
  35. static int web_client_api_request_v2_weights(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  36. return web_client_api_request_weights(host, w, url, WEIGHTS_METHOD_VALUE,
  37. WEIGHTS_FORMAT_MULTINODE, 2);
  38. }
  39. #define GROUP_BY_KEY_MAX_LENGTH 30
  40. static struct {
  41. char group_by[GROUP_BY_KEY_MAX_LENGTH + 1];
  42. char aggregation[GROUP_BY_KEY_MAX_LENGTH + 1];
  43. char group_by_label[GROUP_BY_KEY_MAX_LENGTH + 1];
  44. } group_by_keys[MAX_QUERY_GROUP_BY_PASSES];
  45. __attribute__((constructor)) void initialize_group_by_keys(void) {
  46. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  47. snprintfz(group_by_keys[g].group_by, GROUP_BY_KEY_MAX_LENGTH, "group_by[%zu]", g);
  48. snprintfz(group_by_keys[g].aggregation, GROUP_BY_KEY_MAX_LENGTH, "aggregation[%zu]", g);
  49. snprintfz(group_by_keys[g].group_by_label, GROUP_BY_KEY_MAX_LENGTH, "group_by_label[%zu]", g);
  50. }
  51. }
  52. static int web_client_api_request_v2_data(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  53. usec_t received_ut = now_monotonic_usec();
  54. int ret = HTTP_RESP_BAD_REQUEST;
  55. buffer_flush(w->response.data);
  56. char *google_version = "0.6",
  57. *google_reqId = "0",
  58. *google_sig = "0",
  59. *google_out = "json",
  60. *responseHandler = NULL,
  61. *outFileName = NULL;
  62. time_t last_timestamp_in_data = 0, google_timestamp = 0;
  63. char *scope_nodes = NULL;
  64. char *scope_contexts = NULL;
  65. char *nodes = NULL;
  66. char *contexts = NULL;
  67. char *instances = NULL;
  68. char *dimensions = NULL;
  69. char *before_str = NULL;
  70. char *after_str = NULL;
  71. char *resampling_time_str = NULL;
  72. char *points_str = NULL;
  73. char *timeout_str = NULL;
  74. char *labels = NULL;
  75. char *alerts = NULL;
  76. char *time_group_options = NULL;
  77. char *tier_str = NULL;
  78. size_t tier = 0;
  79. RRDR_TIME_GROUPING time_group = RRDR_GROUPING_AVERAGE;
  80. DATASOURCE_FORMAT format = DATASOURCE_JSON2;
  81. RRDR_OPTIONS options = RRDR_OPTION_VIRTUAL_POINTS | RRDR_OPTION_JSON_WRAP | RRDR_OPTION_RETURN_JWAR;
  82. struct group_by_pass group_by[MAX_QUERY_GROUP_BY_PASSES] = {
  83. {
  84. .group_by = RRDR_GROUP_BY_DIMENSION,
  85. .group_by_label = NULL,
  86. .aggregation = RRDR_GROUP_BY_FUNCTION_AVERAGE,
  87. },
  88. };
  89. size_t group_by_idx = 0, group_by_label_idx = 0, aggregation_idx = 0;
  90. while(url) {
  91. char *value = strsep_skip_consecutive_separators(&url, "&");
  92. if(!value || !*value) continue;
  93. char *name = strsep_skip_consecutive_separators(&value, "=");
  94. if(!name || !*name) continue;
  95. if(!value || !*value) continue;
  96. // name and value are now the parameters
  97. // they are not null and not empty
  98. if(!strcmp(name, "scope_nodes")) scope_nodes = value;
  99. else if(!strcmp(name, "scope_contexts")) scope_contexts = value;
  100. else if(!strcmp(name, "nodes")) nodes = value;
  101. else if(!strcmp(name, "contexts")) contexts = value;
  102. else if(!strcmp(name, "instances")) instances = value;
  103. else if(!strcmp(name, "dimensions")) dimensions = value;
  104. else if(!strcmp(name, "labels")) labels = value;
  105. else if(!strcmp(name, "alerts")) alerts = value;
  106. else if(!strcmp(name, "after")) after_str = value;
  107. else if(!strcmp(name, "before")) before_str = value;
  108. else if(!strcmp(name, "points")) points_str = value;
  109. else if(!strcmp(name, "timeout")) timeout_str = value;
  110. else if(!strcmp(name, "group_by")) {
  111. group_by[group_by_idx++].group_by = group_by_parse(value);
  112. if(group_by_idx >= MAX_QUERY_GROUP_BY_PASSES)
  113. group_by_idx = MAX_QUERY_GROUP_BY_PASSES - 1;
  114. }
  115. else if(!strcmp(name, "group_by_label")) {
  116. group_by[group_by_label_idx++].group_by_label = value;
  117. if(group_by_label_idx >= MAX_QUERY_GROUP_BY_PASSES)
  118. group_by_label_idx = MAX_QUERY_GROUP_BY_PASSES - 1;
  119. }
  120. else if(!strcmp(name, "aggregation")) {
  121. group_by[aggregation_idx++].aggregation = group_by_aggregate_function_parse(value);
  122. if(aggregation_idx >= MAX_QUERY_GROUP_BY_PASSES)
  123. aggregation_idx = MAX_QUERY_GROUP_BY_PASSES - 1;
  124. }
  125. else if(!strcmp(name, "format")) format = web_client_api_request_v1_data_format(value);
  126. else if(!strcmp(name, "options")) options |= web_client_api_request_v1_data_options(value);
  127. else if(!strcmp(name, "time_group")) time_group = time_grouping_parse(value, RRDR_GROUPING_AVERAGE);
  128. else if(!strcmp(name, "time_group_options")) time_group_options = value;
  129. else if(!strcmp(name, "time_resampling")) resampling_time_str = value;
  130. else if(!strcmp(name, "tier")) tier_str = value;
  131. else if(!strcmp(name, "callback")) responseHandler = value;
  132. else if(!strcmp(name, "filename")) outFileName = value;
  133. else if(!strcmp(name, "tqx")) {
  134. // parse Google Visualization API options
  135. // https://developers.google.com/chart/interactive/docs/dev/implementing_data_source
  136. char *tqx_name, *tqx_value;
  137. while(value) {
  138. tqx_value = strsep_skip_consecutive_separators(&value, ";");
  139. if(!tqx_value || !*tqx_value) continue;
  140. tqx_name = strsep_skip_consecutive_separators(&tqx_value, ":");
  141. if(!tqx_name || !*tqx_name) continue;
  142. if(!tqx_value || !*tqx_value) continue;
  143. if(!strcmp(tqx_name, "version"))
  144. google_version = tqx_value;
  145. else if(!strcmp(tqx_name, "reqId"))
  146. google_reqId = tqx_value;
  147. else if(!strcmp(tqx_name, "sig")) {
  148. google_sig = tqx_value;
  149. google_timestamp = strtoul(google_sig, NULL, 0);
  150. }
  151. else if(!strcmp(tqx_name, "out")) {
  152. google_out = tqx_value;
  153. format = web_client_api_request_v1_data_google_format(google_out);
  154. }
  155. else if(!strcmp(tqx_name, "responseHandler"))
  156. responseHandler = tqx_value;
  157. else if(!strcmp(tqx_name, "outFileName"))
  158. outFileName = tqx_value;
  159. }
  160. }
  161. else {
  162. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  163. if(!strcmp(name, group_by_keys[g].group_by))
  164. group_by[g].group_by = group_by_parse(value);
  165. else if(!strcmp(name, group_by_keys[g].group_by_label))
  166. group_by[g].group_by_label = value;
  167. else if(!strcmp(name, group_by_keys[g].aggregation))
  168. group_by[g].aggregation = group_by_aggregate_function_parse(value);
  169. }
  170. }
  171. }
  172. // validate the google parameters given
  173. fix_google_param(google_out);
  174. fix_google_param(google_sig);
  175. fix_google_param(google_reqId);
  176. fix_google_param(google_version);
  177. fix_google_param(responseHandler);
  178. fix_google_param(outFileName);
  179. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  180. if (group_by[g].group_by_label && *group_by[g].group_by_label)
  181. group_by[g].group_by |= RRDR_GROUP_BY_LABEL;
  182. }
  183. if(group_by[0].group_by == RRDR_GROUP_BY_NONE)
  184. group_by[0].group_by = RRDR_GROUP_BY_DIMENSION;
  185. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  186. if ((group_by[g].group_by & ~(RRDR_GROUP_BY_DIMENSION)) || (options & RRDR_OPTION_PERCENTAGE)) {
  187. options |= RRDR_OPTION_ABSOLUTE;
  188. break;
  189. }
  190. }
  191. if(options & RRDR_OPTION_DEBUG)
  192. options &= ~RRDR_OPTION_MINIFY;
  193. if(tier_str && *tier_str) {
  194. tier = str2ul(tier_str);
  195. if(tier < storage_tiers)
  196. options |= RRDR_OPTION_SELECTED_TIER;
  197. else
  198. tier = 0;
  199. }
  200. time_t before = (before_str && *before_str)?str2l(before_str):0;
  201. time_t after = (after_str && *after_str) ?str2l(after_str):-600;
  202. size_t points = (points_str && *points_str)?str2u(points_str):0;
  203. int timeout = (timeout_str && *timeout_str)?str2i(timeout_str): 0;
  204. time_t resampling_time = (resampling_time_str && *resampling_time_str) ? str2l(resampling_time_str) : 0;
  205. QUERY_TARGET_REQUEST qtr = {
  206. .version = 2,
  207. .scope_nodes = scope_nodes,
  208. .scope_contexts = scope_contexts,
  209. .after = after,
  210. .before = before,
  211. .host = NULL,
  212. .st = NULL,
  213. .nodes = nodes,
  214. .contexts = contexts,
  215. .instances = instances,
  216. .dimensions = dimensions,
  217. .alerts = alerts,
  218. .timeout_ms = timeout,
  219. .points = points,
  220. .format = format,
  221. .options = options,
  222. .time_group_method = time_group,
  223. .time_group_options = time_group_options,
  224. .resampling_time = resampling_time,
  225. .tier = tier,
  226. .chart_label_key = NULL,
  227. .labels = labels,
  228. .query_source = QUERY_SOURCE_API_DATA,
  229. .priority = STORAGE_PRIORITY_NORMAL,
  230. .received_ut = received_ut,
  231. .interrupt_callback = web_client_interrupt_callback,
  232. .interrupt_callback_data = w,
  233. };
  234. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++)
  235. qtr.group_by[g] = group_by[g];
  236. QUERY_TARGET *qt = query_target_create(&qtr);
  237. ONEWAYALLOC *owa = NULL;
  238. if(!qt) {
  239. buffer_sprintf(w->response.data, "Failed to prepare the query.");
  240. ret = HTTP_RESP_INTERNAL_SERVER_ERROR;
  241. goto cleanup;
  242. }
  243. web_client_timeout_checkpoint_set(w, timeout);
  244. if(web_client_timeout_checkpoint_and_check(w, NULL)) {
  245. ret = w->response.code;
  246. goto cleanup;
  247. }
  248. if(outFileName && *outFileName) {
  249. buffer_sprintf(w->response.header, "Content-Disposition: attachment; filename=\"%s\"\r\n", outFileName);
  250. debug(D_WEB_CLIENT, "%llu: generating outfilename header: '%s'", w->id, outFileName);
  251. }
  252. if(format == DATASOURCE_DATATABLE_JSONP) {
  253. if(responseHandler == NULL)
  254. responseHandler = "google.visualization.Query.setResponse";
  255. debug(D_WEB_CLIENT_ACCESS, "%llu: GOOGLE JSON/JSONP: version = '%s', reqId = '%s', sig = '%s', out = '%s', responseHandler = '%s', outFileName = '%s'",
  256. w->id, google_version, google_reqId, google_sig, google_out, responseHandler, outFileName
  257. );
  258. buffer_sprintf(
  259. w->response.data,
  260. "%s({version:'%s',reqId:'%s',status:'ok',sig:'%"PRId64"',table:",
  261. responseHandler,
  262. google_version,
  263. google_reqId,
  264. (int64_t)now_realtime_sec());
  265. }
  266. else if(format == DATASOURCE_JSONP) {
  267. if(responseHandler == NULL)
  268. responseHandler = "callback";
  269. buffer_strcat(w->response.data, responseHandler);
  270. buffer_strcat(w->response.data, "(");
  271. }
  272. owa = onewayalloc_create(0);
  273. ret = data_query_execute(owa, w->response.data, qt, &last_timestamp_in_data);
  274. if(format == DATASOURCE_DATATABLE_JSONP) {
  275. if(google_timestamp < last_timestamp_in_data)
  276. buffer_strcat(w->response.data, "});");
  277. else {
  278. // the client already has the latest data
  279. buffer_flush(w->response.data);
  280. buffer_sprintf(w->response.data,
  281. "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'not_modified',message:'Data not modified'}]});",
  282. responseHandler, google_version, google_reqId);
  283. }
  284. }
  285. else if(format == DATASOURCE_JSONP)
  286. buffer_strcat(w->response.data, ");");
  287. cleanup:
  288. query_target_release(qt);
  289. onewayalloc_destroy(owa);
  290. return ret;
  291. }
  292. static int web_client_api_request_v2_webrtc(RRDHOST *host __maybe_unused, struct web_client *w, char *url __maybe_unused) {
  293. return webrtc_new_connection(w->post_payload, w->response.data);
  294. }
  295. static struct web_api_command api_commands_v2[] = {
  296. {"data", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_data},
  297. {"nodes", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_nodes},
  298. {"contexts", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_contexts},
  299. {"weights", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_weights},
  300. {"q", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_q},
  301. {"rtc_offer", 0, WEB_CLIENT_ACL_DASHBOARD | WEB_CLIENT_ACL_ACLK, web_client_api_request_v2_webrtc},
  302. // terminator
  303. {NULL, 0, WEB_CLIENT_ACL_NONE, NULL},
  304. };
  305. inline int web_client_api_request_v2(RRDHOST *host, struct web_client *w, char *url_path_endpoint) {
  306. static int initialized = 0;
  307. if(unlikely(initialized == 0)) {
  308. initialized = 1;
  309. for(int i = 0; api_commands_v2[i].command ; i++)
  310. api_commands_v2[i].hash = simple_hash(api_commands_v2[i].command);
  311. }
  312. return web_client_api_request_vX(host, w, url_path_endpoint, api_commands_v2);
  313. }