web_api_v1.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "web_api_v1.h"
  3. char *api_secret;
  4. static struct {
  5. const char *name;
  6. uint32_t hash;
  7. RRDR_OPTIONS value;
  8. } api_v1_data_options[] = {
  9. { "nonzero" , 0 , RRDR_OPTION_NONZERO}
  10. , {"flip" , 0 , RRDR_OPTION_REVERSED}
  11. , {"reversed" , 0 , RRDR_OPTION_REVERSED}
  12. , {"reverse" , 0 , RRDR_OPTION_REVERSED}
  13. , {"jsonwrap" , 0 , RRDR_OPTION_JSON_WRAP}
  14. , {"min2max" , 0 , RRDR_OPTION_MIN2MAX}
  15. , {"ms" , 0 , RRDR_OPTION_MILLISECONDS}
  16. , {"milliseconds" , 0 , RRDR_OPTION_MILLISECONDS}
  17. , {"abs" , 0 , RRDR_OPTION_ABSOLUTE}
  18. , {"absolute" , 0 , RRDR_OPTION_ABSOLUTE}
  19. , {"absolute_sum" , 0 , RRDR_OPTION_ABSOLUTE}
  20. , {"absolute-sum" , 0 , RRDR_OPTION_ABSOLUTE}
  21. , {"display_absolute", 0 , RRDR_OPTION_DISPLAY_ABS}
  22. , {"display-absolute", 0 , RRDR_OPTION_DISPLAY_ABS}
  23. , {"seconds" , 0 , RRDR_OPTION_SECONDS}
  24. , {"null2zero" , 0 , RRDR_OPTION_NULL2ZERO}
  25. , {"objectrows" , 0 , RRDR_OPTION_OBJECTSROWS}
  26. , {"google_json" , 0 , RRDR_OPTION_GOOGLE_JSON}
  27. , {"google-json" , 0 , RRDR_OPTION_GOOGLE_JSON}
  28. , {"percentage" , 0 , RRDR_OPTION_PERCENTAGE}
  29. , {"unaligned" , 0 , RRDR_OPTION_NOT_ALIGNED}
  30. , {"match_ids" , 0 , RRDR_OPTION_MATCH_IDS}
  31. , {"match-ids" , 0 , RRDR_OPTION_MATCH_IDS}
  32. , {"match_names" , 0 , RRDR_OPTION_MATCH_NAMES}
  33. , {"match-names" , 0 , RRDR_OPTION_MATCH_NAMES}
  34. , {"showcustomvars" , 0 , RRDR_OPTION_CUSTOM_VARS}
  35. , {"allow_past" , 0 , RRDR_OPTION_ALLOW_PAST}
  36. , { NULL, 0, 0}
  37. };
  38. static struct {
  39. const char *name;
  40. uint32_t hash;
  41. uint32_t value;
  42. } api_v1_data_formats[] = {
  43. { DATASOURCE_FORMAT_DATATABLE_JSON , 0 , DATASOURCE_DATATABLE_JSON}
  44. , {DATASOURCE_FORMAT_DATATABLE_JSONP, 0 , DATASOURCE_DATATABLE_JSONP}
  45. , {DATASOURCE_FORMAT_JSON , 0 , DATASOURCE_JSON}
  46. , {DATASOURCE_FORMAT_JSONP , 0 , DATASOURCE_JSONP}
  47. , {DATASOURCE_FORMAT_SSV , 0 , DATASOURCE_SSV}
  48. , {DATASOURCE_FORMAT_CSV , 0 , DATASOURCE_CSV}
  49. , {DATASOURCE_FORMAT_TSV , 0 , DATASOURCE_TSV}
  50. , {"tsv-excel" , 0 , DATASOURCE_TSV}
  51. , {DATASOURCE_FORMAT_HTML , 0 , DATASOURCE_HTML}
  52. , {DATASOURCE_FORMAT_JS_ARRAY , 0 , DATASOURCE_JS_ARRAY}
  53. , {DATASOURCE_FORMAT_SSV_COMMA , 0 , DATASOURCE_SSV_COMMA}
  54. , {DATASOURCE_FORMAT_CSV_JSON_ARRAY , 0 , DATASOURCE_CSV_JSON_ARRAY}
  55. , {DATASOURCE_FORMAT_CSV_MARKDOWN , 0 , DATASOURCE_CSV_MARKDOWN}
  56. , { NULL, 0, 0}
  57. };
  58. static struct {
  59. const char *name;
  60. uint32_t hash;
  61. uint32_t value;
  62. } api_v1_data_google_formats[] = {
  63. // this is not error - when google requests json, it expects javascript
  64. // https://developers.google.com/chart/interactive/docs/dev/implementing_data_source#responseformat
  65. { "json" , 0 , DATASOURCE_DATATABLE_JSONP}
  66. , {"html" , 0 , DATASOURCE_HTML}
  67. , {"csv" , 0 , DATASOURCE_CSV}
  68. , {"tsv-excel", 0 , DATASOURCE_TSV}
  69. , { NULL, 0, 0}
  70. };
  71. void web_client_api_v1_init(void) {
  72. int i;
  73. for(i = 0; api_v1_data_options[i].name ; i++)
  74. api_v1_data_options[i].hash = simple_hash(api_v1_data_options[i].name);
  75. for(i = 0; api_v1_data_formats[i].name ; i++)
  76. api_v1_data_formats[i].hash = simple_hash(api_v1_data_formats[i].name);
  77. for(i = 0; api_v1_data_google_formats[i].name ; i++)
  78. api_v1_data_google_formats[i].hash = simple_hash(api_v1_data_google_formats[i].name);
  79. web_client_api_v1_init_grouping();
  80. uuid_t uuid;
  81. // generate
  82. uuid_generate(uuid);
  83. // unparse (to string)
  84. char uuid_str[37];
  85. uuid_unparse_lower(uuid, uuid_str);
  86. }
  87. char *get_mgmt_api_key(void) {
  88. char filename[FILENAME_MAX + 1];
  89. snprintfz(filename, FILENAME_MAX, "%s/netdata.api.key", netdata_configured_varlib_dir);
  90. char *api_key_filename=config_get(CONFIG_SECTION_REGISTRY, "netdata management api key file", filename);
  91. static char guid[GUID_LEN + 1] = "";
  92. if(likely(guid[0]))
  93. return guid;
  94. // read it from disk
  95. int fd = open(api_key_filename, O_RDONLY);
  96. if(fd != -1) {
  97. char buf[GUID_LEN + 1];
  98. if(read(fd, buf, GUID_LEN) != GUID_LEN)
  99. error("Failed to read management API key from '%s'", api_key_filename);
  100. else {
  101. buf[GUID_LEN] = '\0';
  102. if(regenerate_guid(buf, guid) == -1) {
  103. error("Failed to validate management API key '%s' from '%s'.",
  104. buf, api_key_filename);
  105. guid[0] = '\0';
  106. }
  107. }
  108. close(fd);
  109. }
  110. // generate a new one?
  111. if(!guid[0]) {
  112. uuid_t uuid;
  113. uuid_generate_time(uuid);
  114. uuid_unparse_lower(uuid, guid);
  115. guid[GUID_LEN] = '\0';
  116. // save it
  117. fd = open(api_key_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
  118. if(fd == -1)
  119. fatal("Cannot create unique management API key file '%s'. Please fix this.", api_key_filename);
  120. if(write(fd, guid, GUID_LEN) != GUID_LEN)
  121. fatal("Cannot write the unique management API key file '%s'. Please fix this.", api_key_filename);
  122. close(fd);
  123. }
  124. return guid;
  125. }
  126. void web_client_api_v1_management_init(void) {
  127. api_secret = get_mgmt_api_key();
  128. }
  129. inline uint32_t web_client_api_request_v1_data_options(char *o) {
  130. uint32_t ret = 0x00000000;
  131. char *tok;
  132. while(o && *o && (tok = mystrsep(&o, ", |"))) {
  133. if(!*tok) continue;
  134. uint32_t hash = simple_hash(tok);
  135. int i;
  136. for(i = 0; api_v1_data_options[i].name ; i++) {
  137. if (unlikely(hash == api_v1_data_options[i].hash && !strcmp(tok, api_v1_data_options[i].name))) {
  138. ret |= api_v1_data_options[i].value;
  139. break;
  140. }
  141. }
  142. }
  143. return ret;
  144. }
  145. inline uint32_t web_client_api_request_v1_data_format(char *name) {
  146. uint32_t hash = simple_hash(name);
  147. int i;
  148. for(i = 0; api_v1_data_formats[i].name ; i++) {
  149. if (unlikely(hash == api_v1_data_formats[i].hash && !strcmp(name, api_v1_data_formats[i].name))) {
  150. return api_v1_data_formats[i].value;
  151. }
  152. }
  153. return DATASOURCE_JSON;
  154. }
  155. inline uint32_t web_client_api_request_v1_data_google_format(char *name) {
  156. uint32_t hash = simple_hash(name);
  157. int i;
  158. for(i = 0; api_v1_data_google_formats[i].name ; i++) {
  159. if (unlikely(hash == api_v1_data_google_formats[i].hash && !strcmp(name, api_v1_data_google_formats[i].name))) {
  160. return api_v1_data_google_formats[i].value;
  161. }
  162. }
  163. return DATASOURCE_JSON;
  164. }
  165. int web_client_api_request_v1_alarms_select (char *url) {
  166. int all = 0;
  167. while(url) {
  168. char *value = mystrsep(&url, "&");
  169. if (!value || !*value) continue;
  170. if(!strcmp(value, "all")) all = 1;
  171. else if(!strcmp(value, "active")) all = 0;
  172. }
  173. return all;
  174. }
  175. inline int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w, char *url) {
  176. int all = web_client_api_request_v1_alarms_select(url);
  177. buffer_flush(w->response.data);
  178. w->response.data->contenttype = CT_APPLICATION_JSON;
  179. health_alarms2json(host, w->response.data, all);
  180. buffer_no_cacheable(w->response.data);
  181. return HTTP_RESP_OK;
  182. }
  183. inline int web_client_api_request_v1_alarms_values(RRDHOST *host, struct web_client *w, char *url) {
  184. int all = web_client_api_request_v1_alarms_select(url);
  185. buffer_flush(w->response.data);
  186. w->response.data->contenttype = CT_APPLICATION_JSON;
  187. health_alarms_values2json(host, w->response.data, all);
  188. buffer_no_cacheable(w->response.data);
  189. return HTTP_RESP_OK;
  190. }
  191. inline int web_client_api_request_v1_alarm_count(RRDHOST *host, struct web_client *w, char *url) {
  192. RRDCALC_STATUS status = RRDCALC_STATUS_RAISED;
  193. BUFFER *contexts = NULL;
  194. buffer_flush(w->response.data);
  195. buffer_sprintf(w->response.data, "[");
  196. while(url) {
  197. char *value = mystrsep(&url, "&");
  198. if(!value || !*value) continue;
  199. char *name = mystrsep(&value, "=");
  200. if(!name || !*name) continue;
  201. if(!value || !*value) continue;
  202. debug(D_WEB_CLIENT, "%llu: API v1 alarm_count query param '%s' with value '%s'", w->id, name, value);
  203. char* p = value;
  204. if(!strcmp(name, "status")) {
  205. while ((*p = toupper(*p))) p++;
  206. if (!strcmp("CRITICAL", value)) status = RRDCALC_STATUS_CRITICAL;
  207. else if (!strcmp("WARNING", value)) status = RRDCALC_STATUS_WARNING;
  208. else if (!strcmp("UNINITIALIZED", value)) status = RRDCALC_STATUS_UNINITIALIZED;
  209. else if (!strcmp("UNDEFINED", value)) status = RRDCALC_STATUS_UNDEFINED;
  210. else if (!strcmp("REMOVED", value)) status = RRDCALC_STATUS_REMOVED;
  211. else if (!strcmp("CLEAR", value)) status = RRDCALC_STATUS_CLEAR;
  212. }
  213. else if(!strcmp(name, "context") || !strcmp(name, "ctx")) {
  214. if(!contexts) contexts = buffer_create(255);
  215. buffer_strcat(contexts, "|");
  216. buffer_strcat(contexts, value);
  217. }
  218. }
  219. health_aggregate_alarms(host, w->response.data, contexts, status);
  220. buffer_sprintf(w->response.data, "]\n");
  221. w->response.data->contenttype = CT_APPLICATION_JSON;
  222. buffer_no_cacheable(w->response.data);
  223. buffer_free(contexts);
  224. return 200;
  225. }
  226. inline int web_client_api_request_v1_alarm_log(RRDHOST *host, struct web_client *w, char *url) {
  227. uint32_t after = 0;
  228. while(url) {
  229. char *value = mystrsep(&url, "&");
  230. if (!value || !*value) continue;
  231. char *name = mystrsep(&value, "=");
  232. if(!name || !*name) continue;
  233. if(!value || !*value) continue;
  234. if(!strcmp(name, "after")) after = (uint32_t)strtoul(value, NULL, 0);
  235. }
  236. buffer_flush(w->response.data);
  237. w->response.data->contenttype = CT_APPLICATION_JSON;
  238. health_alarm_log2json(host, w->response.data, after);
  239. return HTTP_RESP_OK;
  240. }
  241. inline int web_client_api_request_single_chart(RRDHOST *host, struct web_client *w, char *url, void callback(RRDSET *st, BUFFER *buf)) {
  242. int ret = HTTP_RESP_BAD_REQUEST;
  243. char *chart = NULL;
  244. buffer_flush(w->response.data);
  245. while(url) {
  246. char *value = mystrsep(&url, "&");
  247. if(!value || !*value) continue;
  248. char *name = mystrsep(&value, "=");
  249. if(!name || !*name) continue;
  250. if(!value || !*value) continue;
  251. // name and value are now the parameters
  252. // they are not null and not empty
  253. if(!strcmp(name, "chart")) chart = value;
  254. //else {
  255. /// buffer_sprintf(w->response.data, "Unknown parameter '%s' in request.", name);
  256. // goto cleanup;
  257. //}
  258. }
  259. if(!chart || !*chart) {
  260. buffer_sprintf(w->response.data, "No chart id is given at the request.");
  261. goto cleanup;
  262. }
  263. RRDSET *st = rrdset_find(host, chart);
  264. if(!st) st = rrdset_find_byname(host, chart);
  265. if(!st) {
  266. buffer_strcat(w->response.data, "Chart is not found: ");
  267. buffer_strcat_htmlescape(w->response.data, chart);
  268. ret = HTTP_RESP_NOT_FOUND;
  269. goto cleanup;
  270. }
  271. w->response.data->contenttype = CT_APPLICATION_JSON;
  272. st->last_accessed_time = now_realtime_sec();
  273. callback(st, w->response.data);
  274. return HTTP_RESP_OK;
  275. cleanup:
  276. return ret;
  277. }
  278. inline int web_client_api_request_v1_alarm_variables(RRDHOST *host, struct web_client *w, char *url) {
  279. return web_client_api_request_single_chart(host, w, url, health_api_v1_chart_variables2json);
  280. }
  281. inline int web_client_api_request_v1_charts(RRDHOST *host, struct web_client *w, char *url) {
  282. (void)url;
  283. buffer_flush(w->response.data);
  284. w->response.data->contenttype = CT_APPLICATION_JSON;
  285. charts2json(host, w->response.data, 0, 0);
  286. return HTTP_RESP_OK;
  287. }
  288. inline int web_client_api_request_v1_archivedcharts(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  289. (void)url;
  290. buffer_flush(w->response.data);
  291. w->response.data->contenttype = CT_APPLICATION_JSON;
  292. #ifdef ENABLE_DBENGINE
  293. if (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  294. sql_rrdset2json(host, w->response.data);
  295. #endif
  296. return HTTP_RESP_OK;
  297. }
  298. inline int web_client_api_request_v1_chart(RRDHOST *host, struct web_client *w, char *url) {
  299. return web_client_api_request_single_chart(host, w, url, rrd_stats_api_v1_chart);
  300. }
  301. void fix_google_param(char *s) {
  302. if(unlikely(!s)) return;
  303. for( ; *s ;s++) {
  304. if(!isalnum(*s) && *s != '.' && *s != '_' && *s != '-')
  305. *s = '_';
  306. }
  307. }
  308. // returns the HTTP code
  309. inline int web_client_api_request_v1_data(RRDHOST *host, struct web_client *w, char *url) {
  310. debug(D_WEB_CLIENT, "%llu: API v1 data with URL '%s'", w->id, url);
  311. int ret = HTTP_RESP_BAD_REQUEST;
  312. BUFFER *dimensions = NULL;
  313. buffer_flush(w->response.data);
  314. char *google_version = "0.6",
  315. *google_reqId = "0",
  316. *google_sig = "0",
  317. *google_out = "json",
  318. *responseHandler = NULL,
  319. *outFileName = NULL;
  320. time_t last_timestamp_in_data = 0, google_timestamp = 0;
  321. char *chart = NULL
  322. , *before_str = NULL
  323. , *after_str = NULL
  324. , *group_time_str = NULL
  325. , *points_str = NULL
  326. , *context = NULL
  327. , *chart_label_key = NULL;
  328. int group = RRDR_GROUPING_AVERAGE;
  329. uint32_t format = DATASOURCE_JSON;
  330. uint32_t options = 0x00000000;
  331. while(url) {
  332. char *value = mystrsep(&url, "&");
  333. if(!value || !*value) continue;
  334. char *name = mystrsep(&value, "=");
  335. if(!name || !*name) continue;
  336. if(!value || !*value) continue;
  337. debug(D_WEB_CLIENT, "%llu: API v1 data query param '%s' with value '%s'", w->id, name, value);
  338. // name and value are now the parameters
  339. // they are not null and not empty
  340. if(!strcmp(name, "context")) context = value;
  341. else if(!strcmp(name, "chart_label_key")) chart_label_key = value;
  342. else if(!strcmp(name, "chart")) chart = value;
  343. else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
  344. if(!dimensions) dimensions = buffer_create(100);
  345. buffer_strcat(dimensions, "|");
  346. buffer_strcat(dimensions, value);
  347. }
  348. else if(!strcmp(name, "after")) after_str = value;
  349. else if(!strcmp(name, "before")) before_str = value;
  350. else if(!strcmp(name, "points")) points_str = value;
  351. else if(!strcmp(name, "gtime")) group_time_str = value;
  352. else if(!strcmp(name, "group")) {
  353. group = web_client_api_request_v1_data_group(value, RRDR_GROUPING_AVERAGE);
  354. }
  355. else if(!strcmp(name, "format")) {
  356. format = web_client_api_request_v1_data_format(value);
  357. }
  358. else if(!strcmp(name, "options")) {
  359. options |= web_client_api_request_v1_data_options(value);
  360. }
  361. else if(!strcmp(name, "callback")) {
  362. responseHandler = value;
  363. }
  364. else if(!strcmp(name, "filename")) {
  365. outFileName = value;
  366. }
  367. else if(!strcmp(name, "tqx")) {
  368. // parse Google Visualization API options
  369. // https://developers.google.com/chart/interactive/docs/dev/implementing_data_source
  370. char *tqx_name, *tqx_value;
  371. while(value) {
  372. tqx_value = mystrsep(&value, ";");
  373. if(!tqx_value || !*tqx_value) continue;
  374. tqx_name = mystrsep(&tqx_value, ":");
  375. if(!tqx_name || !*tqx_name) continue;
  376. if(!tqx_value || !*tqx_value) continue;
  377. if(!strcmp(tqx_name, "version"))
  378. google_version = tqx_value;
  379. else if(!strcmp(tqx_name, "reqId"))
  380. google_reqId = tqx_value;
  381. else if(!strcmp(tqx_name, "sig")) {
  382. google_sig = tqx_value;
  383. google_timestamp = strtoul(google_sig, NULL, 0);
  384. }
  385. else if(!strcmp(tqx_name, "out")) {
  386. google_out = tqx_value;
  387. format = web_client_api_request_v1_data_google_format(google_out);
  388. }
  389. else if(!strcmp(tqx_name, "responseHandler"))
  390. responseHandler = tqx_value;
  391. else if(!strcmp(tqx_name, "outFileName"))
  392. outFileName = tqx_value;
  393. }
  394. }
  395. }
  396. // validate the google parameters given
  397. fix_google_param(google_out);
  398. fix_google_param(google_sig);
  399. fix_google_param(google_reqId);
  400. fix_google_param(google_version);
  401. fix_google_param(responseHandler);
  402. fix_google_param(outFileName);
  403. RRDSET *st = NULL;
  404. if((!chart || !*chart) && (!context)) {
  405. buffer_sprintf(w->response.data, "No chart id is given at the request.");
  406. goto cleanup;
  407. }
  408. struct context_param *context_param_list = NULL;
  409. if (context && !chart) {
  410. RRDSET *st1;
  411. uint32_t context_hash = simple_hash(context);
  412. rrdhost_rdlock(host);
  413. rrdset_foreach_read(st1, host) {
  414. if (st1->hash_context == context_hash && !strcmp(st1->context, context) &&
  415. (!chart_label_key || rrdset_contains_label_keylist(st1, chart_label_key)))
  416. build_context_param_list(&context_param_list, st1);
  417. }
  418. rrdhost_unlock(host);
  419. if (likely(context_param_list && context_param_list->rd)) // Just set the first one
  420. st = context_param_list->rd->rrdset;
  421. }
  422. else {
  423. st = rrdset_find(host, chart);
  424. if (!st)
  425. st = rrdset_find_byname(host, chart);
  426. if (likely(st))
  427. st->last_accessed_time = now_realtime_sec();
  428. }
  429. if (!st && !context_param_list) {
  430. if (context && !chart) {
  431. if (!chart_label_key) {
  432. buffer_strcat(w->response.data, "Context is not found: ");
  433. buffer_strcat_htmlescape(w->response.data, context);
  434. } else {
  435. buffer_strcat(w->response.data, "Context: ");
  436. buffer_strcat_htmlescape(w->response.data, context);
  437. buffer_strcat(w->response.data, " or chart label key: ");
  438. buffer_strcat_htmlescape(w->response.data, chart_label_key);
  439. buffer_strcat(w->response.data, " not found");
  440. }
  441. }
  442. else {
  443. buffer_strcat(w->response.data, "Chart is not found: ");
  444. buffer_strcat_htmlescape(w->response.data, chart);
  445. }
  446. ret = HTTP_RESP_NOT_FOUND;
  447. goto cleanup;
  448. }
  449. long long before = (before_str && *before_str)?str2l(before_str):0;
  450. long long after = (after_str && *after_str) ?str2l(after_str):-600;
  451. int points = (points_str && *points_str)?str2i(points_str):0;
  452. long group_time = (group_time_str && *group_time_str)?str2l(group_time_str):0;
  453. debug(D_WEB_CLIENT, "%llu: API command 'data' for chart '%s', dimensions '%s', after '%lld', before '%lld', points '%d', group '%d', format '%u', options '0x%08x'"
  454. , w->id
  455. , chart
  456. , (dimensions)?buffer_tostring(dimensions):""
  457. , after
  458. , before
  459. , points
  460. , group
  461. , format
  462. , options
  463. );
  464. if(outFileName && *outFileName) {
  465. buffer_sprintf(w->response.header, "Content-Disposition: attachment; filename=\"%s\"\r\n", outFileName);
  466. debug(D_WEB_CLIENT, "%llu: generating outfilename header: '%s'", w->id, outFileName);
  467. }
  468. if(format == DATASOURCE_DATATABLE_JSONP) {
  469. if(responseHandler == NULL)
  470. responseHandler = "google.visualization.Query.setResponse";
  471. debug(D_WEB_CLIENT_ACCESS, "%llu: GOOGLE JSON/JSONP: version = '%s', reqId = '%s', sig = '%s', out = '%s', responseHandler = '%s', outFileName = '%s'",
  472. w->id, google_version, google_reqId, google_sig, google_out, responseHandler, outFileName
  473. );
  474. buffer_sprintf(w->response.data,
  475. "%s({version:'%s',reqId:'%s',status:'ok',sig:'%ld',table:",
  476. responseHandler, google_version, google_reqId, st->last_updated.tv_sec);
  477. }
  478. else if(format == DATASOURCE_JSONP) {
  479. if(responseHandler == NULL)
  480. responseHandler = "callback";
  481. buffer_strcat(w->response.data, responseHandler);
  482. buffer_strcat(w->response.data, "(");
  483. }
  484. ret = rrdset2anything_api_v1(st, w->response.data, dimensions, format, points, after, before, group, group_time
  485. , options, &last_timestamp_in_data, context_param_list, chart_label_key);
  486. free_context_param_list(&context_param_list);
  487. if(format == DATASOURCE_DATATABLE_JSONP) {
  488. if(google_timestamp < last_timestamp_in_data)
  489. buffer_strcat(w->response.data, "});");
  490. else {
  491. // the client already has the latest data
  492. buffer_flush(w->response.data);
  493. buffer_sprintf(w->response.data,
  494. "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'not_modified',message:'Data not modified'}]});",
  495. responseHandler, google_version, google_reqId);
  496. }
  497. }
  498. else if(format == DATASOURCE_JSONP)
  499. buffer_strcat(w->response.data, ");");
  500. cleanup:
  501. buffer_free(dimensions);
  502. return ret;
  503. }
  504. // Pings a netdata server:
  505. // /api/v1/registry?action=hello
  506. //
  507. // Access to a netdata registry:
  508. // /api/v1/registry?action=access&machine=${machine_guid}&name=${hostname}&url=${url}
  509. //
  510. // Delete from a netdata registry:
  511. // /api/v1/registry?action=delete&machine=${machine_guid}&name=${hostname}&url=${url}&delete_url=${delete_url}
  512. //
  513. // Search for the URLs of a machine:
  514. // /api/v1/registry?action=search&machine=${machine_guid}&name=${hostname}&url=${url}&for=${machine_guid}
  515. //
  516. // Impersonate:
  517. // /api/v1/registry?action=switch&machine=${machine_guid}&name=${hostname}&url=${url}&to=${new_person_guid}
  518. inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *w, char *url) {
  519. static uint32_t hash_action = 0, hash_access = 0, hash_hello = 0, hash_delete = 0, hash_search = 0,
  520. hash_switch = 0, hash_machine = 0, hash_url = 0, hash_name = 0, hash_delete_url = 0, hash_for = 0,
  521. hash_to = 0 /*, hash_redirects = 0 */;
  522. if(unlikely(!hash_action)) {
  523. hash_action = simple_hash("action");
  524. hash_access = simple_hash("access");
  525. hash_hello = simple_hash("hello");
  526. hash_delete = simple_hash("delete");
  527. hash_search = simple_hash("search");
  528. hash_switch = simple_hash("switch");
  529. hash_machine = simple_hash("machine");
  530. hash_url = simple_hash("url");
  531. hash_name = simple_hash("name");
  532. hash_delete_url = simple_hash("delete_url");
  533. hash_for = simple_hash("for");
  534. hash_to = simple_hash("to");
  535. /*
  536. hash_redirects = simple_hash("redirects");
  537. */
  538. }
  539. char person_guid[GUID_LEN + 1] = "";
  540. debug(D_WEB_CLIENT, "%llu: API v1 registry with URL '%s'", w->id, url);
  541. // TODO
  542. // The browser may send multiple cookies with our id
  543. char *cookie = strstr(w->response.data->buffer, NETDATA_REGISTRY_COOKIE_NAME "=");
  544. if(cookie)
  545. strncpyz(person_guid, &cookie[sizeof(NETDATA_REGISTRY_COOKIE_NAME)], 36);
  546. char action = '\0';
  547. char *machine_guid = NULL,
  548. *machine_url = NULL,
  549. *url_name = NULL,
  550. *search_machine_guid = NULL,
  551. *delete_url = NULL,
  552. *to_person_guid = NULL;
  553. /*
  554. int redirects = 0;
  555. */
  556. // Don't cache registry responses
  557. buffer_no_cacheable(w->response.data);
  558. while(url) {
  559. char *value = mystrsep(&url, "&");
  560. if (!value || !*value) continue;
  561. char *name = mystrsep(&value, "=");
  562. if (!name || !*name) continue;
  563. if (!value || !*value) continue;
  564. debug(D_WEB_CLIENT, "%llu: API v1 registry query param '%s' with value '%s'", w->id, name, value);
  565. uint32_t hash = simple_hash(name);
  566. if(hash == hash_action && !strcmp(name, "action")) {
  567. uint32_t vhash = simple_hash(value);
  568. if(vhash == hash_access && !strcmp(value, "access")) action = 'A';
  569. else if(vhash == hash_hello && !strcmp(value, "hello")) action = 'H';
  570. else if(vhash == hash_delete && !strcmp(value, "delete")) action = 'D';
  571. else if(vhash == hash_search && !strcmp(value, "search")) action = 'S';
  572. else if(vhash == hash_switch && !strcmp(value, "switch")) action = 'W';
  573. #ifdef NETDATA_INTERNAL_CHECKS
  574. else error("unknown registry action '%s'", value);
  575. #endif /* NETDATA_INTERNAL_CHECKS */
  576. }
  577. /*
  578. else if(hash == hash_redirects && !strcmp(name, "redirects"))
  579. redirects = atoi(value);
  580. */
  581. else if(hash == hash_machine && !strcmp(name, "machine"))
  582. machine_guid = value;
  583. else if(hash == hash_url && !strcmp(name, "url"))
  584. machine_url = value;
  585. else if(action == 'A') {
  586. if(hash == hash_name && !strcmp(name, "name"))
  587. url_name = value;
  588. }
  589. else if(action == 'D') {
  590. if(hash == hash_delete_url && !strcmp(name, "delete_url"))
  591. delete_url = value;
  592. }
  593. else if(action == 'S') {
  594. if(hash == hash_for && !strcmp(name, "for"))
  595. search_machine_guid = value;
  596. }
  597. else if(action == 'W') {
  598. if(hash == hash_to && !strcmp(name, "to"))
  599. to_person_guid = value;
  600. }
  601. #ifdef NETDATA_INTERNAL_CHECKS
  602. else error("unused registry URL parameter '%s' with value '%s'", name, value);
  603. #endif /* NETDATA_INTERNAL_CHECKS */
  604. }
  605. if(unlikely(respect_web_browser_do_not_track_policy && web_client_has_donottrack(w))) {
  606. buffer_flush(w->response.data);
  607. buffer_sprintf(w->response.data, "Your web browser is sending 'DNT: 1' (Do Not Track). The registry requires persistent cookies on your browser to work.");
  608. return HTTP_RESP_BAD_REQUEST;
  609. }
  610. if(unlikely(action == 'H')) {
  611. // HELLO request, dashboard ACL
  612. if(unlikely(!web_client_can_access_dashboard(w)))
  613. return web_client_permission_denied(w);
  614. }
  615. else {
  616. // everything else, registry ACL
  617. if(unlikely(!web_client_can_access_registry(w)))
  618. return web_client_permission_denied(w);
  619. }
  620. switch(action) {
  621. case 'A':
  622. if(unlikely(!machine_guid || !machine_url || !url_name)) {
  623. error("Invalid registry request - access requires these parameters: machine ('%s'), url ('%s'), name ('%s')", machine_guid ? machine_guid : "UNSET", machine_url ? machine_url : "UNSET", url_name ? url_name : "UNSET");
  624. buffer_flush(w->response.data);
  625. buffer_strcat(w->response.data, "Invalid registry Access request.");
  626. return HTTP_RESP_BAD_REQUEST;
  627. }
  628. web_client_enable_tracking_required(w);
  629. return registry_request_access_json(host, w, person_guid, machine_guid, machine_url, url_name, now_realtime_sec());
  630. case 'D':
  631. if(unlikely(!machine_guid || !machine_url || !delete_url)) {
  632. error("Invalid registry request - delete requires these parameters: machine ('%s'), url ('%s'), delete_url ('%s')", machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", delete_url?delete_url:"UNSET");
  633. buffer_flush(w->response.data);
  634. buffer_strcat(w->response.data, "Invalid registry Delete request.");
  635. return HTTP_RESP_BAD_REQUEST;
  636. }
  637. web_client_enable_tracking_required(w);
  638. return registry_request_delete_json(host, w, person_guid, machine_guid, machine_url, delete_url, now_realtime_sec());
  639. case 'S':
  640. if(unlikely(!machine_guid || !machine_url || !search_machine_guid)) {
  641. error("Invalid registry request - search requires these parameters: machine ('%s'), url ('%s'), for ('%s')", machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", search_machine_guid?search_machine_guid:"UNSET");
  642. buffer_flush(w->response.data);
  643. buffer_strcat(w->response.data, "Invalid registry Search request.");
  644. return HTTP_RESP_BAD_REQUEST;
  645. }
  646. web_client_enable_tracking_required(w);
  647. return registry_request_search_json(host, w, person_guid, machine_guid, machine_url, search_machine_guid, now_realtime_sec());
  648. case 'W':
  649. if(unlikely(!machine_guid || !machine_url || !to_person_guid)) {
  650. error("Invalid registry request - switching identity requires these parameters: machine ('%s'), url ('%s'), to ('%s')", machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", to_person_guid?to_person_guid:"UNSET");
  651. buffer_flush(w->response.data);
  652. buffer_strcat(w->response.data, "Invalid registry Switch request.");
  653. return HTTP_RESP_BAD_REQUEST;
  654. }
  655. web_client_enable_tracking_required(w);
  656. return registry_request_switch_json(host, w, person_guid, machine_guid, machine_url, to_person_guid, now_realtime_sec());
  657. case 'H':
  658. return registry_request_hello_json(host, w);
  659. default:
  660. buffer_flush(w->response.data);
  661. buffer_strcat(w->response.data, "Invalid registry request - you need to set an action: hello, access, delete, search");
  662. return HTTP_RESP_BAD_REQUEST;
  663. }
  664. }
  665. static inline void web_client_api_request_v1_info_summary_alarm_statuses(RRDHOST *host, BUFFER *wb) {
  666. int alarm_normal = 0, alarm_warn = 0, alarm_crit = 0;
  667. RRDCALC *rc;
  668. rrdhost_rdlock(host);
  669. for(rc = host->alarms; rc ; rc = rc->next) {
  670. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  671. continue;
  672. switch(rc->status) {
  673. case RRDCALC_STATUS_WARNING:
  674. alarm_warn++;
  675. break;
  676. case RRDCALC_STATUS_CRITICAL:
  677. alarm_crit++;
  678. break;
  679. default:
  680. alarm_normal++;
  681. }
  682. }
  683. rrdhost_unlock(host);
  684. buffer_sprintf(wb, "\t\t\"normal\": %d,\n", alarm_normal);
  685. buffer_sprintf(wb, "\t\t\"warning\": %d,\n", alarm_warn);
  686. buffer_sprintf(wb, "\t\t\"critical\": %d\n", alarm_crit);
  687. }
  688. static inline void web_client_api_request_v1_info_mirrored_hosts(BUFFER *wb) {
  689. RRDHOST *host;
  690. int count = 0;
  691. buffer_strcat(wb, "\t\"mirrored_hosts\": [\n");
  692. rrd_rdlock();
  693. rrdhost_foreach_read(host) {
  694. if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))
  695. continue;
  696. if (count > 0)
  697. buffer_strcat(wb, ",\n");
  698. buffer_sprintf(wb, "\t\t\"%s\"", host->hostname);
  699. count++;
  700. }
  701. buffer_strcat(wb, "\n\t],\n\t\"mirrored_hosts_status\": [\n");
  702. count = 0;
  703. rrdhost_foreach_read(host)
  704. {
  705. if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))
  706. continue;
  707. if (count > 0)
  708. buffer_strcat(wb, ",\n");
  709. netdata_mutex_lock(&host->receiver_lock);
  710. buffer_sprintf(
  711. wb, "\t\t{ \"guid\": \"%s\", \"reachable\": %s, \"claim_id\": ", host->machine_guid,
  712. (host->receiver || host == localhost) ? "true" : "false");
  713. netdata_mutex_unlock(&host->receiver_lock);
  714. rrdhost_aclk_state_lock(host);
  715. if (host->aclk_state.claimed_id)
  716. buffer_sprintf(wb, "\"%s\" }", host->aclk_state.claimed_id);
  717. else
  718. buffer_strcat(wb, "null }");
  719. rrdhost_aclk_state_unlock(host);
  720. count++;
  721. }
  722. rrd_unlock();
  723. buffer_strcat(wb, "\n\t],\n");
  724. }
  725. inline void host_labels2json(RRDHOST *host, BUFFER *wb, size_t indentation) {
  726. char tabs[11];
  727. if (indentation > 10)
  728. indentation = 10;
  729. tabs[0] = '\0';
  730. while (indentation) {
  731. strcat(tabs, "\t");
  732. indentation--;
  733. }
  734. int count = 0;
  735. rrdhost_rdlock(host);
  736. netdata_rwlock_rdlock(&host->labels.labels_rwlock);
  737. for (struct label *label = host->labels.head; label; label = label->next) {
  738. if(count > 0) buffer_strcat(wb, ",\n");
  739. buffer_strcat(wb, tabs);
  740. char value[CONFIG_MAX_VALUE * 2 + 1];
  741. sanitize_json_string(value, label->value, CONFIG_MAX_VALUE * 2);
  742. buffer_sprintf(wb, "\"%s\": \"%s\"", label->key, value);
  743. count++;
  744. }
  745. buffer_strcat(wb, "\n");
  746. netdata_rwlock_unlock(&host->labels.labels_rwlock);
  747. rrdhost_unlock(host);
  748. }
  749. extern int aclk_connected;
  750. inline int web_client_api_request_v1_info_fill_buffer(RRDHOST *host, BUFFER *wb)
  751. {
  752. buffer_strcat(wb, "{\n");
  753. buffer_sprintf(wb, "\t\"version\": \"%s\",\n", host->program_version);
  754. buffer_sprintf(wb, "\t\"uid\": \"%s\",\n", host->machine_guid);
  755. web_client_api_request_v1_info_mirrored_hosts(wb);
  756. buffer_strcat(wb, "\t\"alarms\": {\n");
  757. web_client_api_request_v1_info_summary_alarm_statuses(host, wb);
  758. buffer_strcat(wb, "\t},\n");
  759. buffer_sprintf(wb, "\t\"os_name\": \"%s\",\n", (host->system_info->host_os_name) ? host->system_info->host_os_name : "");
  760. buffer_sprintf(wb, "\t\"os_id\": \"%s\",\n", (host->system_info->host_os_id) ? host->system_info->host_os_id : "");
  761. buffer_sprintf(wb, "\t\"os_id_like\": \"%s\",\n", (host->system_info->host_os_id_like) ? host->system_info->host_os_id_like : "");
  762. buffer_sprintf(wb, "\t\"os_version\": \"%s\",\n", (host->system_info->host_os_version) ? host->system_info->host_os_version : "");
  763. buffer_sprintf(wb, "\t\"os_version_id\": \"%s\",\n", (host->system_info->host_os_version_id) ? host->system_info->host_os_version_id : "");
  764. buffer_sprintf(wb, "\t\"os_detection\": \"%s\",\n", (host->system_info->host_os_detection) ? host->system_info->host_os_detection : "");
  765. buffer_sprintf(wb, "\t\"cores_total\": \"%s\",\n", (host->system_info->host_cores) ? host->system_info->host_cores : "");
  766. buffer_sprintf(wb, "\t\"total_disk_space\": \"%s\",\n", (host->system_info->host_disk_space) ? host->system_info->host_disk_space : "");
  767. buffer_sprintf(wb, "\t\"cpu_freq\": \"%s\",\n", (host->system_info->host_cpu_freq) ? host->system_info->host_cpu_freq : "");
  768. buffer_sprintf(wb, "\t\"ram_total\": \"%s\",\n", (host->system_info->host_ram_total) ? host->system_info->host_ram_total : "");
  769. if (host->system_info->container_os_name)
  770. buffer_sprintf(wb, "\t\"container_os_name\": \"%s\",\n", host->system_info->container_os_name);
  771. if (host->system_info->container_os_id)
  772. buffer_sprintf(wb, "\t\"container_os_id\": \"%s\",\n", host->system_info->container_os_id);
  773. if (host->system_info->container_os_id_like)
  774. buffer_sprintf(wb, "\t\"container_os_id_like\": \"%s\",\n", host->system_info->container_os_id_like);
  775. if (host->system_info->container_os_version)
  776. buffer_sprintf(wb, "\t\"container_os_version\": \"%s\",\n", host->system_info->container_os_version);
  777. if (host->system_info->container_os_version_id)
  778. buffer_sprintf(wb, "\t\"container_os_version_id\": \"%s\",\n", host->system_info->container_os_version_id);
  779. if (host->system_info->container_os_detection)
  780. buffer_sprintf(wb, "\t\"container_os_detection\": \"%s\",\n", host->system_info->container_os_detection);
  781. buffer_sprintf(wb, "\t\"kernel_name\": \"%s\",\n", (host->system_info->kernel_name) ? host->system_info->kernel_name : "");
  782. buffer_sprintf(wb, "\t\"kernel_version\": \"%s\",\n", (host->system_info->kernel_version) ? host->system_info->kernel_version : "");
  783. buffer_sprintf(wb, "\t\"architecture\": \"%s\",\n", (host->system_info->architecture) ? host->system_info->architecture : "");
  784. buffer_sprintf(wb, "\t\"virtualization\": \"%s\",\n", (host->system_info->virtualization) ? host->system_info->virtualization : "");
  785. buffer_sprintf(wb, "\t\"virt_detection\": \"%s\",\n", (host->system_info->virt_detection) ? host->system_info->virt_detection : "");
  786. buffer_sprintf(wb, "\t\"container\": \"%s\",\n", (host->system_info->container) ? host->system_info->container : "");
  787. buffer_sprintf(wb, "\t\"container_detection\": \"%s\",\n", (host->system_info->container_detection) ? host->system_info->container_detection : "");
  788. buffer_strcat(wb, "\t\"host_labels\": {\n");
  789. host_labels2json(host, wb, 2);
  790. buffer_strcat(wb, "\t},\n");
  791. buffer_strcat(wb, "\t\"collectors\": [");
  792. chartcollectors2json(host, wb);
  793. buffer_strcat(wb, "\n\t],\n");
  794. #ifdef DISABLE_CLOUD
  795. buffer_strcat(wb, "\t\"cloud-enabled\": false,\n");
  796. #else
  797. buffer_sprintf(wb, "\t\"cloud-enabled\": %s,\n",
  798. appconfig_get_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "enabled", 1) ? "true" : "false");
  799. #endif
  800. #ifdef ENABLE_ACLK
  801. buffer_strcat(wb, "\t\"cloud-available\": true,\n");
  802. #else
  803. buffer_strcat(wb, "\t\"cloud-available\": false,\n");
  804. #endif
  805. char *agent_id = is_agent_claimed();
  806. if (agent_id == NULL)
  807. buffer_strcat(wb, "\t\"agent-claimed\": false,\n");
  808. else {
  809. buffer_strcat(wb, "\t\"agent-claimed\": true,\n");
  810. freez(agent_id);
  811. }
  812. #ifdef ENABLE_ACLK
  813. if (aclk_connected)
  814. buffer_strcat(wb, "\t\"aclk-available\": true\n");
  815. else
  816. #endif
  817. buffer_strcat(wb, "\t\"aclk-available\": false\n"); // Intentionally valid with/without #ifdef above
  818. buffer_strcat(wb, "}");
  819. return 0;
  820. }
  821. inline int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, char *url) {
  822. (void)url;
  823. if (!netdata_ready) return HTTP_RESP_BACKEND_FETCH_FAILED;
  824. BUFFER *wb = w->response.data;
  825. buffer_flush(wb);
  826. wb->contenttype = CT_APPLICATION_JSON;
  827. web_client_api_request_v1_info_fill_buffer(host, wb);
  828. buffer_no_cacheable(wb);
  829. return HTTP_RESP_OK;
  830. }
  831. static struct api_command {
  832. const char *command;
  833. uint32_t hash;
  834. WEB_CLIENT_ACL acl;
  835. int (*callback)(RRDHOST *host, struct web_client *w, char *url);
  836. } api_commands[] = {
  837. { "info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_info },
  838. { "data", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_data },
  839. { "chart", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_chart },
  840. { "charts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_charts },
  841. { "archivedcharts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_archivedcharts },
  842. // registry checks the ACL by itself, so we allow everything
  843. { "registry", 0, WEB_CLIENT_ACL_NOCHECK, web_client_api_request_v1_registry },
  844. // badges can be fetched with both dashboard and badge permissions
  845. { "badge.svg", 0, WEB_CLIENT_ACL_DASHBOARD|WEB_CLIENT_ACL_BADGE, web_client_api_request_v1_badge },
  846. { "alarms", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarms },
  847. { "alarms_values", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarms_values },
  848. { "alarm_log", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_log },
  849. { "alarm_variables", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_variables },
  850. { "alarm_count", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_count },
  851. { "allmetrics", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_allmetrics },
  852. { "manage/health", 0, WEB_CLIENT_ACL_MGMT, web_client_api_request_v1_mgmt_health },
  853. // terminator
  854. { NULL, 0, WEB_CLIENT_ACL_NONE, NULL },
  855. };
  856. inline int web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *url) {
  857. static int initialized = 0;
  858. int i;
  859. if(unlikely(initialized == 0)) {
  860. initialized = 1;
  861. for(i = 0; api_commands[i].command ; i++)
  862. api_commands[i].hash = simple_hash(api_commands[i].command);
  863. }
  864. // get the command
  865. if(url) {
  866. debug(D_WEB_CLIENT, "%llu: Searching for API v1 command '%s'.", w->id, url);
  867. uint32_t hash = simple_hash(url);
  868. for(i = 0; api_commands[i].command ;i++) {
  869. if(unlikely(hash == api_commands[i].hash && !strcmp(url, api_commands[i].command))) {
  870. if(unlikely(api_commands[i].acl != WEB_CLIENT_ACL_NOCHECK) && !(w->acl & api_commands[i].acl))
  871. return web_client_permission_denied(w);
  872. //return api_commands[i].callback(host, w, url);
  873. return api_commands[i].callback(host, w, (w->decoded_query_string + 1));
  874. }
  875. }
  876. buffer_flush(w->response.data);
  877. buffer_strcat(w->response.data, "Unsupported v1 API command: ");
  878. buffer_strcat_htmlescape(w->response.data, url);
  879. return HTTP_RESP_NOT_FOUND;
  880. }
  881. else {
  882. buffer_flush(w->response.data);
  883. buffer_sprintf(w->response.data, "Which API v1 command?");
  884. return HTTP_RESP_BAD_REQUEST;
  885. }
  886. }