web_api_v1.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  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. , {"anomaly-bit" , 0 , RRDR_OPTION_ANOMALY_BIT}
  36. , {"selected-tier" , 0 , RRDR_OPTION_SELECTED_TIER}
  37. , {"raw" , 0 , RRDR_OPTION_RETURN_RAW}
  38. , {"jw-anomaly-rates" , 0 , RRDR_OPTION_RETURN_JWAR}
  39. , {"natural-points" , 0 , RRDR_OPTION_NATURAL_POINTS}
  40. , {"virtual-points" , 0 , RRDR_OPTION_VIRTUAL_POINTS}
  41. , {NULL , 0 , 0}
  42. };
  43. static struct {
  44. const char *name;
  45. uint32_t hash;
  46. uint32_t value;
  47. } api_v1_data_formats[] = {
  48. { DATASOURCE_FORMAT_DATATABLE_JSON , 0 , DATASOURCE_DATATABLE_JSON}
  49. , {DATASOURCE_FORMAT_DATATABLE_JSONP, 0 , DATASOURCE_DATATABLE_JSONP}
  50. , {DATASOURCE_FORMAT_JSON , 0 , DATASOURCE_JSON}
  51. , {DATASOURCE_FORMAT_JSONP , 0 , DATASOURCE_JSONP}
  52. , {DATASOURCE_FORMAT_SSV , 0 , DATASOURCE_SSV}
  53. , {DATASOURCE_FORMAT_CSV , 0 , DATASOURCE_CSV}
  54. , {DATASOURCE_FORMAT_TSV , 0 , DATASOURCE_TSV}
  55. , {"tsv-excel" , 0 , DATASOURCE_TSV}
  56. , {DATASOURCE_FORMAT_HTML , 0 , DATASOURCE_HTML}
  57. , {DATASOURCE_FORMAT_JS_ARRAY , 0 , DATASOURCE_JS_ARRAY}
  58. , {DATASOURCE_FORMAT_SSV_COMMA , 0 , DATASOURCE_SSV_COMMA}
  59. , {DATASOURCE_FORMAT_CSV_JSON_ARRAY , 0 , DATASOURCE_CSV_JSON_ARRAY}
  60. , {DATASOURCE_FORMAT_CSV_MARKDOWN , 0 , DATASOURCE_CSV_MARKDOWN}
  61. , { NULL, 0, 0}
  62. };
  63. static struct {
  64. const char *name;
  65. uint32_t hash;
  66. uint32_t value;
  67. } api_v1_data_google_formats[] = {
  68. // this is not error - when google requests json, it expects javascript
  69. // https://developers.google.com/chart/interactive/docs/dev/implementing_data_source#responseformat
  70. { "json" , 0 , DATASOURCE_DATATABLE_JSONP}
  71. , {"html" , 0 , DATASOURCE_HTML}
  72. , {"csv" , 0 , DATASOURCE_CSV}
  73. , {"tsv-excel", 0 , DATASOURCE_TSV}
  74. , { NULL, 0, 0}
  75. };
  76. void web_client_api_v1_init(void) {
  77. int i;
  78. for(i = 0; api_v1_data_options[i].name ; i++)
  79. api_v1_data_options[i].hash = simple_hash(api_v1_data_options[i].name);
  80. for(i = 0; api_v1_data_formats[i].name ; i++)
  81. api_v1_data_formats[i].hash = simple_hash(api_v1_data_formats[i].name);
  82. for(i = 0; api_v1_data_google_formats[i].name ; i++)
  83. api_v1_data_google_formats[i].hash = simple_hash(api_v1_data_google_formats[i].name);
  84. web_client_api_v1_init_grouping();
  85. uuid_t uuid;
  86. // generate
  87. uuid_generate(uuid);
  88. // unparse (to string)
  89. char uuid_str[37];
  90. uuid_unparse_lower(uuid, uuid_str);
  91. }
  92. char *get_mgmt_api_key(void) {
  93. char filename[FILENAME_MAX + 1];
  94. snprintfz(filename, FILENAME_MAX, "%s/netdata.api.key", netdata_configured_varlib_dir);
  95. char *api_key_filename=config_get(CONFIG_SECTION_REGISTRY, "netdata management api key file", filename);
  96. static char guid[GUID_LEN + 1] = "";
  97. if(likely(guid[0]))
  98. return guid;
  99. // read it from disk
  100. int fd = open(api_key_filename, O_RDONLY);
  101. if(fd != -1) {
  102. char buf[GUID_LEN + 1];
  103. if(read(fd, buf, GUID_LEN) != GUID_LEN)
  104. error("Failed to read management API key from '%s'", api_key_filename);
  105. else {
  106. buf[GUID_LEN] = '\0';
  107. if(regenerate_guid(buf, guid) == -1) {
  108. error("Failed to validate management API key '%s' from '%s'.",
  109. buf, api_key_filename);
  110. guid[0] = '\0';
  111. }
  112. }
  113. close(fd);
  114. }
  115. // generate a new one?
  116. if(!guid[0]) {
  117. uuid_t uuid;
  118. uuid_generate_time(uuid);
  119. uuid_unparse_lower(uuid, guid);
  120. guid[GUID_LEN] = '\0';
  121. // save it
  122. fd = open(api_key_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
  123. if(fd == -1) {
  124. error("Cannot create unique management API key file '%s'. Please adjust config parameter 'netdata management api key file' to a proper path and file.", api_key_filename);
  125. goto temp_key;
  126. }
  127. if(write(fd, guid, GUID_LEN) != GUID_LEN) {
  128. error("Cannot write the unique management API key file '%s'. Please adjust config parameter 'netdata management api key file' to a proper path and file with enough space left.", api_key_filename);
  129. close(fd);
  130. goto temp_key;
  131. }
  132. close(fd);
  133. }
  134. return guid;
  135. temp_key:
  136. info("You can still continue to use the alarm management API using the authorization token %s during this Netdata session only.", guid);
  137. return guid;
  138. }
  139. void web_client_api_v1_management_init(void) {
  140. api_secret = get_mgmt_api_key();
  141. }
  142. inline RRDR_OPTIONS web_client_api_request_v1_data_options(char *o) {
  143. RRDR_OPTIONS ret = 0x00000000;
  144. char *tok;
  145. while(o && *o && (tok = mystrsep(&o, ", |"))) {
  146. if(!*tok) continue;
  147. uint32_t hash = simple_hash(tok);
  148. int i;
  149. for(i = 0; api_v1_data_options[i].name ; i++) {
  150. if (unlikely(hash == api_v1_data_options[i].hash && !strcmp(tok, api_v1_data_options[i].name))) {
  151. ret |= api_v1_data_options[i].value;
  152. break;
  153. }
  154. }
  155. }
  156. return ret;
  157. }
  158. void web_client_api_request_v1_data_options_to_string(BUFFER *wb, RRDR_OPTIONS options) {
  159. RRDR_OPTIONS used = 0; // to prevent adding duplicates
  160. int added = 0;
  161. for(int i = 0; api_v1_data_options[i].name ; i++) {
  162. if (unlikely((api_v1_data_options[i].value & options) && !(api_v1_data_options[i].value & used))) {
  163. if(added) buffer_strcat(wb, ",");
  164. buffer_strcat(wb, api_v1_data_options[i].name);
  165. used |= api_v1_data_options[i].value;
  166. added++;
  167. }
  168. }
  169. }
  170. inline uint32_t web_client_api_request_v1_data_format(char *name) {
  171. uint32_t hash = simple_hash(name);
  172. int i;
  173. for(i = 0; api_v1_data_formats[i].name ; i++) {
  174. if (unlikely(hash == api_v1_data_formats[i].hash && !strcmp(name, api_v1_data_formats[i].name))) {
  175. return api_v1_data_formats[i].value;
  176. }
  177. }
  178. return DATASOURCE_JSON;
  179. }
  180. inline uint32_t web_client_api_request_v1_data_google_format(char *name) {
  181. uint32_t hash = simple_hash(name);
  182. int i;
  183. for(i = 0; api_v1_data_google_formats[i].name ; i++) {
  184. if (unlikely(hash == api_v1_data_google_formats[i].hash && !strcmp(name, api_v1_data_google_formats[i].name))) {
  185. return api_v1_data_google_formats[i].value;
  186. }
  187. }
  188. return DATASOURCE_JSON;
  189. }
  190. int web_client_api_request_v1_alarms_select (char *url) {
  191. int all = 0;
  192. while(url) {
  193. char *value = mystrsep(&url, "&");
  194. if (!value || !*value) continue;
  195. if(!strcmp(value, "all")) all = 1;
  196. else if(!strcmp(value, "active")) all = 0;
  197. }
  198. return all;
  199. }
  200. inline int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w, char *url) {
  201. int all = web_client_api_request_v1_alarms_select(url);
  202. buffer_flush(w->response.data);
  203. w->response.data->contenttype = CT_APPLICATION_JSON;
  204. health_alarms2json(host, w->response.data, all);
  205. buffer_no_cacheable(w->response.data);
  206. return HTTP_RESP_OK;
  207. }
  208. inline int web_client_api_request_v1_alarms_values(RRDHOST *host, struct web_client *w, char *url) {
  209. int all = web_client_api_request_v1_alarms_select(url);
  210. buffer_flush(w->response.data);
  211. w->response.data->contenttype = CT_APPLICATION_JSON;
  212. health_alarms_values2json(host, w->response.data, all);
  213. buffer_no_cacheable(w->response.data);
  214. return HTTP_RESP_OK;
  215. }
  216. inline int web_client_api_request_v1_alarm_count(RRDHOST *host, struct web_client *w, char *url) {
  217. RRDCALC_STATUS status = RRDCALC_STATUS_RAISED;
  218. BUFFER *contexts = NULL;
  219. buffer_flush(w->response.data);
  220. buffer_sprintf(w->response.data, "[");
  221. while(url) {
  222. char *value = mystrsep(&url, "&");
  223. if(!value || !*value) continue;
  224. char *name = mystrsep(&value, "=");
  225. if(!name || !*name) continue;
  226. if(!value || !*value) continue;
  227. debug(D_WEB_CLIENT, "%llu: API v1 alarm_count query param '%s' with value '%s'", w->id, name, value);
  228. char* p = value;
  229. if(!strcmp(name, "status")) {
  230. while ((*p = toupper(*p))) p++;
  231. if (!strcmp("CRITICAL", value)) status = RRDCALC_STATUS_CRITICAL;
  232. else if (!strcmp("WARNING", value)) status = RRDCALC_STATUS_WARNING;
  233. else if (!strcmp("UNINITIALIZED", value)) status = RRDCALC_STATUS_UNINITIALIZED;
  234. else if (!strcmp("UNDEFINED", value)) status = RRDCALC_STATUS_UNDEFINED;
  235. else if (!strcmp("REMOVED", value)) status = RRDCALC_STATUS_REMOVED;
  236. else if (!strcmp("CLEAR", value)) status = RRDCALC_STATUS_CLEAR;
  237. }
  238. else if(!strcmp(name, "context") || !strcmp(name, "ctx")) {
  239. if(!contexts) contexts = buffer_create(255);
  240. buffer_strcat(contexts, "|");
  241. buffer_strcat(contexts, value);
  242. }
  243. }
  244. health_aggregate_alarms(host, w->response.data, contexts, status);
  245. buffer_sprintf(w->response.data, "]\n");
  246. w->response.data->contenttype = CT_APPLICATION_JSON;
  247. buffer_no_cacheable(w->response.data);
  248. buffer_free(contexts);
  249. return 200;
  250. }
  251. inline int web_client_api_request_v1_alarm_log(RRDHOST *host, struct web_client *w, char *url) {
  252. uint32_t after = 0;
  253. char *chart = NULL;
  254. while(url) {
  255. char *value = mystrsep(&url, "&");
  256. if (!value || !*value) continue;
  257. char *name = mystrsep(&value, "=");
  258. if(!name || !*name) continue;
  259. if(!value || !*value) continue;
  260. if (!strcmp(name, "after")) after = (uint32_t)strtoul(value, NULL, 0);
  261. else if (!strcmp(name, "chart")) chart = value;
  262. }
  263. buffer_flush(w->response.data);
  264. w->response.data->contenttype = CT_APPLICATION_JSON;
  265. health_alarm_log2json(host, w->response.data, after, chart);
  266. return HTTP_RESP_OK;
  267. }
  268. inline int web_client_api_request_single_chart(RRDHOST *host, struct web_client *w, char *url, void callback(RRDSET *st, BUFFER *buf)) {
  269. int ret = HTTP_RESP_BAD_REQUEST;
  270. char *chart = NULL;
  271. buffer_flush(w->response.data);
  272. while(url) {
  273. char *value = mystrsep(&url, "&");
  274. if(!value || !*value) continue;
  275. char *name = mystrsep(&value, "=");
  276. if(!name || !*name) continue;
  277. if(!value || !*value) continue;
  278. // name and value are now the parameters
  279. // they are not null and not empty
  280. if(!strcmp(name, "chart")) chart = value;
  281. //else {
  282. /// buffer_sprintf(w->response.data, "Unknown parameter '%s' in request.", name);
  283. // goto cleanup;
  284. //}
  285. }
  286. if(!chart || !*chart) {
  287. buffer_sprintf(w->response.data, "No chart id is given at the request.");
  288. goto cleanup;
  289. }
  290. RRDSET *st = rrdset_find(host, chart);
  291. if(!st) st = rrdset_find_byname(host, chart);
  292. if(!st) {
  293. buffer_strcat(w->response.data, "Chart is not found: ");
  294. buffer_strcat_htmlescape(w->response.data, chart);
  295. ret = HTTP_RESP_NOT_FOUND;
  296. goto cleanup;
  297. }
  298. w->response.data->contenttype = CT_APPLICATION_JSON;
  299. st->last_accessed_time = now_realtime_sec();
  300. callback(st, w->response.data);
  301. return HTTP_RESP_OK;
  302. cleanup:
  303. return ret;
  304. }
  305. inline int web_client_api_request_v1_alarm_variables(RRDHOST *host, struct web_client *w, char *url) {
  306. return web_client_api_request_single_chart(host, w, url, health_api_v1_chart_variables2json);
  307. }
  308. static RRDCONTEXT_TO_JSON_OPTIONS rrdcontext_to_json_parse_options(char *o) {
  309. RRDCONTEXT_TO_JSON_OPTIONS options = RRDCONTEXT_OPTION_NONE;
  310. char *tok;
  311. while(o && *o && (tok = mystrsep(&o, ", |"))) {
  312. if(!*tok) continue;
  313. if(!strcmp(tok, "full") || !strcmp(tok, "all"))
  314. options |= RRDCONTEXT_OPTIONS_ALL;
  315. else if(!strcmp(tok, "charts") || !strcmp(tok, "instances"))
  316. options |= RRDCONTEXT_OPTION_SHOW_INSTANCES;
  317. else if(!strcmp(tok, "dimensions") || !strcmp(tok, "metrics"))
  318. options |= RRDCONTEXT_OPTION_SHOW_METRICS;
  319. else if(!strcmp(tok, "queue"))
  320. options |= RRDCONTEXT_OPTION_SHOW_QUEUED;
  321. else if(!strcmp(tok, "flags"))
  322. options |= RRDCONTEXT_OPTION_SHOW_FLAGS;
  323. else if(!strcmp(tok, "uuids"))
  324. options |= RRDCONTEXT_OPTION_SHOW_UUIDS;
  325. else if(!strcmp(tok, "deleted"))
  326. options |= RRDCONTEXT_OPTION_SHOW_DELETED;
  327. else if(!strcmp(tok, "labels"))
  328. options |= RRDCONTEXT_OPTION_SHOW_LABELS;
  329. else if(!strcmp(tok, "deepscan"))
  330. options |= RRDCONTEXT_OPTION_DEEPSCAN;
  331. }
  332. return options;
  333. }
  334. static int web_client_api_request_v1_context(RRDHOST *host, struct web_client *w, char *url) {
  335. char *context = NULL;
  336. RRDCONTEXT_TO_JSON_OPTIONS options = RRDCONTEXT_OPTION_NONE;
  337. time_t after = 0, before = 0;
  338. const char *chart_label_key = NULL, *chart_labels_filter = NULL;
  339. BUFFER *dimensions = NULL;
  340. buffer_flush(w->response.data);
  341. while(url) {
  342. char *value = mystrsep(&url, "&");
  343. if(!value || !*value) continue;
  344. char *name = mystrsep(&value, "=");
  345. if(!name || !*name) continue;
  346. if(!value || !*value) continue;
  347. // name and value are now the parameters
  348. // they are not null and not empty
  349. if(!strcmp(name, "context") || !strcmp(name, "ctx")) context = value;
  350. else if(!strcmp(name, "after")) after = str2l(value);
  351. else if(!strcmp(name, "before")) before = str2l(value);
  352. else if(!strcmp(name, "options")) options = rrdcontext_to_json_parse_options(value);
  353. else if(!strcmp(name, "chart_label_key")) chart_label_key = value;
  354. else if(!strcmp(name, "chart_labels_filter")) chart_labels_filter = value;
  355. else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
  356. if(!dimensions) dimensions = buffer_create(100);
  357. buffer_strcat(dimensions, "|");
  358. buffer_strcat(dimensions, value);
  359. }
  360. }
  361. if(!context || !*context) {
  362. buffer_sprintf(w->response.data, "No context is given at the request.");
  363. return HTTP_RESP_BAD_REQUEST;
  364. }
  365. SIMPLE_PATTERN *chart_label_key_pattern = NULL;
  366. SIMPLE_PATTERN *chart_labels_filter_pattern = NULL;
  367. SIMPLE_PATTERN *chart_dimensions_pattern = NULL;
  368. if(chart_label_key)
  369. chart_label_key_pattern = simple_pattern_create(chart_label_key, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  370. if(chart_labels_filter)
  371. chart_labels_filter_pattern = simple_pattern_create(chart_labels_filter, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  372. if(dimensions) {
  373. chart_dimensions_pattern = simple_pattern_create(buffer_tostring(dimensions), ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  374. buffer_free(dimensions);
  375. }
  376. w->response.data->contenttype = CT_APPLICATION_JSON;
  377. int ret = rrdcontext_to_json(host, w->response.data, after, before, options, context, chart_label_key_pattern, chart_labels_filter_pattern, chart_dimensions_pattern);
  378. simple_pattern_free(chart_label_key_pattern);
  379. simple_pattern_free(chart_labels_filter_pattern);
  380. simple_pattern_free(chart_dimensions_pattern);
  381. return ret;
  382. }
  383. static int web_client_api_request_v1_contexts(RRDHOST *host, struct web_client *w, char *url) {
  384. RRDCONTEXT_TO_JSON_OPTIONS options = RRDCONTEXT_OPTION_NONE;
  385. time_t after = 0, before = 0;
  386. const char *chart_label_key = NULL, *chart_labels_filter = NULL;
  387. BUFFER *dimensions = NULL;
  388. buffer_flush(w->response.data);
  389. while(url) {
  390. char *value = mystrsep(&url, "&");
  391. if(!value || !*value) continue;
  392. char *name = mystrsep(&value, "=");
  393. if(!name || !*name) continue;
  394. if(!value || !*value) continue;
  395. // name and value are now the parameters
  396. // they are not null and not empty
  397. if(!strcmp(name, "after")) after = str2l(value);
  398. else if(!strcmp(name, "before")) before = str2l(value);
  399. else if(!strcmp(name, "options")) options = rrdcontext_to_json_parse_options(value);
  400. else if(!strcmp(name, "chart_label_key")) chart_label_key = value;
  401. else if(!strcmp(name, "chart_labels_filter")) chart_labels_filter = value;
  402. else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
  403. if(!dimensions) dimensions = buffer_create(100);
  404. buffer_strcat(dimensions, "|");
  405. buffer_strcat(dimensions, value);
  406. }
  407. }
  408. SIMPLE_PATTERN *chart_label_key_pattern = NULL;
  409. SIMPLE_PATTERN *chart_labels_filter_pattern = NULL;
  410. SIMPLE_PATTERN *chart_dimensions_pattern = NULL;
  411. if(chart_label_key)
  412. chart_label_key_pattern = simple_pattern_create(chart_label_key, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  413. if(chart_labels_filter)
  414. chart_labels_filter_pattern = simple_pattern_create(chart_labels_filter, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  415. if(dimensions) {
  416. chart_dimensions_pattern = simple_pattern_create(buffer_tostring(dimensions), ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  417. buffer_free(dimensions);
  418. }
  419. w->response.data->contenttype = CT_APPLICATION_JSON;
  420. int ret = rrdcontexts_to_json(host, w->response.data, after, before, options, chart_label_key_pattern, chart_labels_filter_pattern, chart_dimensions_pattern);
  421. simple_pattern_free(chart_label_key_pattern);
  422. simple_pattern_free(chart_labels_filter_pattern);
  423. simple_pattern_free(chart_dimensions_pattern);
  424. return ret;
  425. }
  426. inline int web_client_api_request_v1_charts(RRDHOST *host, struct web_client *w, char *url) {
  427. (void)url;
  428. buffer_flush(w->response.data);
  429. w->response.data->contenttype = CT_APPLICATION_JSON;
  430. charts2json(host, w->response.data, 0, 0);
  431. return HTTP_RESP_OK;
  432. }
  433. inline int web_client_api_request_v1_archivedcharts(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  434. (void)url;
  435. buffer_flush(w->response.data);
  436. w->response.data->contenttype = CT_APPLICATION_JSON;
  437. #ifdef ENABLE_DBENGINE
  438. if (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  439. sql_rrdset2json(host, w->response.data);
  440. #endif
  441. return HTTP_RESP_OK;
  442. }
  443. inline int web_client_api_request_v1_chart(RRDHOST *host, struct web_client *w, char *url) {
  444. return web_client_api_request_single_chart(host, w, url, rrd_stats_api_v1_chart);
  445. }
  446. void fix_google_param(char *s) {
  447. if(unlikely(!s)) return;
  448. for( ; *s ;s++) {
  449. if(!isalnum(*s) && *s != '.' && *s != '_' && *s != '-')
  450. *s = '_';
  451. }
  452. }
  453. // returns the HTTP code
  454. inline int web_client_api_request_v1_data(RRDHOST *host, struct web_client *w, char *url) {
  455. debug(D_WEB_CLIENT, "%llu: API v1 data with URL '%s'", w->id, url);
  456. int ret = HTTP_RESP_BAD_REQUEST;
  457. BUFFER *dimensions = NULL;
  458. buffer_flush(w->response.data);
  459. char *google_version = "0.6",
  460. *google_reqId = "0",
  461. *google_sig = "0",
  462. *google_out = "json",
  463. *responseHandler = NULL,
  464. *outFileName = NULL;
  465. time_t last_timestamp_in_data = 0, google_timestamp = 0;
  466. char *chart = NULL;
  467. char *before_str = NULL;
  468. char *after_str = NULL;
  469. char *group_time_str = NULL;
  470. char *points_str = NULL;
  471. char *timeout_str = NULL;
  472. char *max_anomaly_rates_str = NULL;
  473. char *context = NULL;
  474. char *chart_label_key = NULL;
  475. char *chart_labels_filter = NULL;
  476. char *group_options = NULL;
  477. int tier = 0;
  478. int group = RRDR_GROUPING_AVERAGE;
  479. int show_dimensions = 0;
  480. uint32_t format = DATASOURCE_JSON;
  481. uint32_t options = 0x00000000;
  482. while(url) {
  483. char *value = mystrsep(&url, "&");
  484. if(!value || !*value) continue;
  485. char *name = mystrsep(&value, "=");
  486. if(!name || !*name) continue;
  487. if(!value || !*value) continue;
  488. debug(D_WEB_CLIENT, "%llu: API v1 data query param '%s' with value '%s'", w->id, name, value);
  489. // name and value are now the parameters
  490. // they are not null and not empty
  491. if(!strcmp(name, "context")) context = value;
  492. else if(!strcmp(name, "chart_label_key")) chart_label_key = value;
  493. else if(!strcmp(name, "chart_labels_filter")) chart_labels_filter = value;
  494. else if(!strcmp(name, "chart")) chart = value;
  495. else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
  496. if(!dimensions) dimensions = buffer_create(100);
  497. buffer_strcat(dimensions, "|");
  498. buffer_strcat(dimensions, value);
  499. }
  500. else if(!strcmp(name, "show_dimensions")) show_dimensions = 1;
  501. else if(!strcmp(name, "after")) after_str = value;
  502. else if(!strcmp(name, "before")) before_str = value;
  503. else if(!strcmp(name, "points")) points_str = value;
  504. else if(!strcmp(name, "timeout")) timeout_str = value;
  505. else if(!strcmp(name, "gtime")) group_time_str = value;
  506. else if(!strcmp(name, "group_options")) group_options = value;
  507. else if(!strcmp(name, "group")) {
  508. group = web_client_api_request_v1_data_group(value, RRDR_GROUPING_AVERAGE);
  509. }
  510. else if(!strcmp(name, "format")) {
  511. format = web_client_api_request_v1_data_format(value);
  512. }
  513. else if(!strcmp(name, "options")) {
  514. options |= web_client_api_request_v1_data_options(value);
  515. }
  516. else if(!strcmp(name, "callback")) {
  517. responseHandler = value;
  518. }
  519. else if(!strcmp(name, "filename")) {
  520. outFileName = value;
  521. }
  522. else if(!strcmp(name, "tqx")) {
  523. // parse Google Visualization API options
  524. // https://developers.google.com/chart/interactive/docs/dev/implementing_data_source
  525. char *tqx_name, *tqx_value;
  526. while(value) {
  527. tqx_value = mystrsep(&value, ";");
  528. if(!tqx_value || !*tqx_value) continue;
  529. tqx_name = mystrsep(&tqx_value, ":");
  530. if(!tqx_name || !*tqx_name) continue;
  531. if(!tqx_value || !*tqx_value) continue;
  532. if(!strcmp(tqx_name, "version"))
  533. google_version = tqx_value;
  534. else if(!strcmp(tqx_name, "reqId"))
  535. google_reqId = tqx_value;
  536. else if(!strcmp(tqx_name, "sig")) {
  537. google_sig = tqx_value;
  538. google_timestamp = strtoul(google_sig, NULL, 0);
  539. }
  540. else if(!strcmp(tqx_name, "out")) {
  541. google_out = tqx_value;
  542. format = web_client_api_request_v1_data_google_format(google_out);
  543. }
  544. else if(!strcmp(tqx_name, "responseHandler"))
  545. responseHandler = tqx_value;
  546. else if(!strcmp(tqx_name, "outFileName"))
  547. outFileName = tqx_value;
  548. }
  549. }
  550. else if(!strcmp(name, "max_anomaly_rates")) {
  551. max_anomaly_rates_str = value;
  552. }
  553. else if(!strcmp(name, "tier")) {
  554. tier = str2i(value);
  555. if(tier >= 0 && tier < storage_tiers)
  556. options |= RRDR_OPTION_SELECTED_TIER;
  557. }
  558. }
  559. // validate the google parameters given
  560. fix_google_param(google_out);
  561. fix_google_param(google_sig);
  562. fix_google_param(google_reqId);
  563. fix_google_param(google_version);
  564. fix_google_param(responseHandler);
  565. fix_google_param(outFileName);
  566. RRDSET *st = NULL;
  567. ONEWAYALLOC *owa = onewayalloc_create(0);
  568. if((!chart || !*chart) && (!context)) {
  569. buffer_sprintf(w->response.data, "No chart id is given at the request.");
  570. goto cleanup;
  571. }
  572. struct context_param *context_param_list = NULL;
  573. if (context && !chart) {
  574. RRDSET *st1;
  575. uint32_t context_hash = simple_hash(context);
  576. SIMPLE_PATTERN *chart_label_key_pattern = NULL;
  577. if(chart_label_key)
  578. chart_label_key_pattern = simple_pattern_create(chart_label_key, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  579. SIMPLE_PATTERN *chart_labels_filter_pattern = NULL;
  580. if(chart_labels_filter)
  581. chart_labels_filter_pattern = simple_pattern_create(chart_labels_filter, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  582. rrdhost_rdlock(host);
  583. rrdset_foreach_read(st1, host) {
  584. if (st1->hash_context == context_hash && !strcmp(st1->context, context) &&
  585. (!chart_label_key_pattern || rrdlabels_match_simple_pattern_parsed(st1->state->chart_labels, chart_label_key_pattern, ':')) &&
  586. (!chart_labels_filter_pattern || rrdlabels_match_simple_pattern_parsed(st1->state->chart_labels, chart_labels_filter_pattern, ':')))
  587. build_context_param_list(owa, &context_param_list, st1);
  588. }
  589. rrdhost_unlock(host);
  590. if (likely(context_param_list && context_param_list->rd)) // Just set the first one
  591. st = context_param_list->rd->rrdset;
  592. else {
  593. if (!chart_label_key && !chart_labels_filter)
  594. sql_build_context_param_list(owa, &context_param_list, host, context, NULL);
  595. }
  596. }
  597. else {
  598. st = rrdset_find(host, chart);
  599. if (!st)
  600. st = rrdset_find_byname(host, chart);
  601. if (likely(st))
  602. st->last_accessed_time = now_realtime_sec();
  603. else
  604. sql_build_context_param_list(owa, &context_param_list, host, NULL, chart);
  605. }
  606. if (!st) {
  607. if (likely(context_param_list && context_param_list->rd && context_param_list->rd->rrdset))
  608. st = context_param_list->rd->rrdset;
  609. else {
  610. free_context_param_list(owa, &context_param_list);
  611. context_param_list = NULL;
  612. }
  613. }
  614. if (!st && !context_param_list) {
  615. if (context && !chart) {
  616. if (!chart_label_key) {
  617. buffer_strcat(w->response.data, "Context is not found: ");
  618. buffer_strcat_htmlescape(w->response.data, context);
  619. } else {
  620. buffer_strcat(w->response.data, "Context: ");
  621. buffer_strcat_htmlescape(w->response.data, context);
  622. buffer_strcat(w->response.data, " or chart label key: ");
  623. buffer_strcat_htmlescape(w->response.data, chart_label_key);
  624. buffer_strcat(w->response.data, " not found");
  625. }
  626. }
  627. else {
  628. buffer_strcat(w->response.data, "Chart is not found: ");
  629. buffer_strcat_htmlescape(w->response.data, chart);
  630. }
  631. ret = HTTP_RESP_NOT_FOUND;
  632. goto cleanup;
  633. }
  634. long long before = (before_str && *before_str)?str2l(before_str):0;
  635. long long after = (after_str && *after_str) ?str2l(after_str):-600;
  636. int points = (points_str && *points_str)?str2i(points_str):0;
  637. int timeout = (timeout_str && *timeout_str)?str2i(timeout_str): 0;
  638. long group_time = (group_time_str && *group_time_str)?str2l(group_time_str):0;
  639. int max_anomaly_rates = (max_anomaly_rates_str && *max_anomaly_rates_str) ? str2i(max_anomaly_rates_str) : 0;
  640. if (timeout) {
  641. struct timeval now;
  642. now_realtime_timeval(&now);
  643. int inqueue = (int)dt_usec(&w->tv_in, &now) / 1000;
  644. timeout -= inqueue;
  645. if (timeout <= 0) {
  646. buffer_flush(w->response.data);
  647. buffer_strcat(w->response.data, "Query timeout exceeded");
  648. return HTTP_RESP_BACKEND_FETCH_FAILED;
  649. }
  650. }
  651. 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'"
  652. , w->id
  653. , chart
  654. , (dimensions)?buffer_tostring(dimensions):""
  655. , after
  656. , before
  657. , points
  658. , group
  659. , format
  660. , options
  661. );
  662. if(outFileName && *outFileName) {
  663. buffer_sprintf(w->response.header, "Content-Disposition: attachment; filename=\"%s\"\r\n", outFileName);
  664. debug(D_WEB_CLIENT, "%llu: generating outfilename header: '%s'", w->id, outFileName);
  665. }
  666. if(format == DATASOURCE_DATATABLE_JSONP) {
  667. if(responseHandler == NULL)
  668. responseHandler = "google.visualization.Query.setResponse";
  669. debug(D_WEB_CLIENT_ACCESS, "%llu: GOOGLE JSON/JSONP: version = '%s', reqId = '%s', sig = '%s', out = '%s', responseHandler = '%s', outFileName = '%s'",
  670. w->id, google_version, google_reqId, google_sig, google_out, responseHandler, outFileName
  671. );
  672. buffer_sprintf(
  673. w->response.data,
  674. "%s({version:'%s',reqId:'%s',status:'ok',sig:'%"PRId64"',table:",
  675. responseHandler,
  676. google_version,
  677. google_reqId,
  678. (int64_t)st->last_updated.tv_sec);
  679. }
  680. else if(format == DATASOURCE_JSONP) {
  681. if(responseHandler == NULL)
  682. responseHandler = "callback";
  683. buffer_strcat(w->response.data, responseHandler);
  684. buffer_strcat(w->response.data, "(");
  685. }
  686. QUERY_PARAMS query_params = {
  687. .context_param_list = context_param_list,
  688. .timeout = timeout,
  689. .max_anomaly_rates = max_anomaly_rates,
  690. .show_dimensions = show_dimensions,
  691. .chart_label_key = chart_label_key,
  692. .wb = w->response.data};
  693. ret = rrdset2anything_api_v1(owa, st, &query_params, dimensions, format,
  694. points, after, before, group, group_options, group_time, options, &last_timestamp_in_data, tier);
  695. free_context_param_list(owa, &context_param_list);
  696. if(format == DATASOURCE_DATATABLE_JSONP) {
  697. if(google_timestamp < last_timestamp_in_data)
  698. buffer_strcat(w->response.data, "});");
  699. else {
  700. // the client already has the latest data
  701. buffer_flush(w->response.data);
  702. buffer_sprintf(w->response.data,
  703. "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'not_modified',message:'Data not modified'}]});",
  704. responseHandler, google_version, google_reqId);
  705. }
  706. }
  707. else if(format == DATASOURCE_JSONP)
  708. buffer_strcat(w->response.data, ");");
  709. cleanup:
  710. onewayalloc_destroy(owa);
  711. buffer_free(dimensions);
  712. return ret;
  713. }
  714. // Pings a netdata server:
  715. // /api/v1/registry?action=hello
  716. //
  717. // Access to a netdata registry:
  718. // /api/v1/registry?action=access&machine=${machine_guid}&name=${hostname}&url=${url}
  719. //
  720. // Delete from a netdata registry:
  721. // /api/v1/registry?action=delete&machine=${machine_guid}&name=${hostname}&url=${url}&delete_url=${delete_url}
  722. //
  723. // Search for the URLs of a machine:
  724. // /api/v1/registry?action=search&machine=${machine_guid}&name=${hostname}&url=${url}&for=${machine_guid}
  725. //
  726. // Impersonate:
  727. // /api/v1/registry?action=switch&machine=${machine_guid}&name=${hostname}&url=${url}&to=${new_person_guid}
  728. inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *w, char *url) {
  729. static uint32_t hash_action = 0, hash_access = 0, hash_hello = 0, hash_delete = 0, hash_search = 0,
  730. hash_switch = 0, hash_machine = 0, hash_url = 0, hash_name = 0, hash_delete_url = 0, hash_for = 0,
  731. hash_to = 0 /*, hash_redirects = 0 */;
  732. if(unlikely(!hash_action)) {
  733. hash_action = simple_hash("action");
  734. hash_access = simple_hash("access");
  735. hash_hello = simple_hash("hello");
  736. hash_delete = simple_hash("delete");
  737. hash_search = simple_hash("search");
  738. hash_switch = simple_hash("switch");
  739. hash_machine = simple_hash("machine");
  740. hash_url = simple_hash("url");
  741. hash_name = simple_hash("name");
  742. hash_delete_url = simple_hash("delete_url");
  743. hash_for = simple_hash("for");
  744. hash_to = simple_hash("to");
  745. /*
  746. hash_redirects = simple_hash("redirects");
  747. */
  748. }
  749. char person_guid[GUID_LEN + 1] = "";
  750. debug(D_WEB_CLIENT, "%llu: API v1 registry with URL '%s'", w->id, url);
  751. // TODO
  752. // The browser may send multiple cookies with our id
  753. char *cookie = strstr(w->response.data->buffer, NETDATA_REGISTRY_COOKIE_NAME "=");
  754. if(cookie)
  755. strncpyz(person_guid, &cookie[sizeof(NETDATA_REGISTRY_COOKIE_NAME)], 36);
  756. char action = '\0';
  757. char *machine_guid = NULL,
  758. *machine_url = NULL,
  759. *url_name = NULL,
  760. *search_machine_guid = NULL,
  761. *delete_url = NULL,
  762. *to_person_guid = NULL;
  763. /*
  764. int redirects = 0;
  765. */
  766. // Don't cache registry responses
  767. buffer_no_cacheable(w->response.data);
  768. while(url) {
  769. char *value = mystrsep(&url, "&");
  770. if (!value || !*value) continue;
  771. char *name = mystrsep(&value, "=");
  772. if (!name || !*name) continue;
  773. if (!value || !*value) continue;
  774. debug(D_WEB_CLIENT, "%llu: API v1 registry query param '%s' with value '%s'", w->id, name, value);
  775. uint32_t hash = simple_hash(name);
  776. if(hash == hash_action && !strcmp(name, "action")) {
  777. uint32_t vhash = simple_hash(value);
  778. if(vhash == hash_access && !strcmp(value, "access")) action = 'A';
  779. else if(vhash == hash_hello && !strcmp(value, "hello")) action = 'H';
  780. else if(vhash == hash_delete && !strcmp(value, "delete")) action = 'D';
  781. else if(vhash == hash_search && !strcmp(value, "search")) action = 'S';
  782. else if(vhash == hash_switch && !strcmp(value, "switch")) action = 'W';
  783. #ifdef NETDATA_INTERNAL_CHECKS
  784. else error("unknown registry action '%s'", value);
  785. #endif /* NETDATA_INTERNAL_CHECKS */
  786. }
  787. /*
  788. else if(hash == hash_redirects && !strcmp(name, "redirects"))
  789. redirects = atoi(value);
  790. */
  791. else if(hash == hash_machine && !strcmp(name, "machine"))
  792. machine_guid = value;
  793. else if(hash == hash_url && !strcmp(name, "url"))
  794. machine_url = value;
  795. else if(action == 'A') {
  796. if(hash == hash_name && !strcmp(name, "name"))
  797. url_name = value;
  798. }
  799. else if(action == 'D') {
  800. if(hash == hash_delete_url && !strcmp(name, "delete_url"))
  801. delete_url = value;
  802. }
  803. else if(action == 'S') {
  804. if(hash == hash_for && !strcmp(name, "for"))
  805. search_machine_guid = value;
  806. }
  807. else if(action == 'W') {
  808. if(hash == hash_to && !strcmp(name, "to"))
  809. to_person_guid = value;
  810. }
  811. #ifdef NETDATA_INTERNAL_CHECKS
  812. else error("unused registry URL parameter '%s' with value '%s'", name, value);
  813. #endif /* NETDATA_INTERNAL_CHECKS */
  814. }
  815. if(unlikely(respect_web_browser_do_not_track_policy && web_client_has_donottrack(w))) {
  816. buffer_flush(w->response.data);
  817. 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.");
  818. return HTTP_RESP_BAD_REQUEST;
  819. }
  820. if(unlikely(action == 'H')) {
  821. // HELLO request, dashboard ACL
  822. analytics_log_dashboard();
  823. if(unlikely(!web_client_can_access_dashboard(w)))
  824. return web_client_permission_denied(w);
  825. }
  826. else {
  827. // everything else, registry ACL
  828. if(unlikely(!web_client_can_access_registry(w)))
  829. return web_client_permission_denied(w);
  830. }
  831. switch(action) {
  832. case 'A':
  833. if(unlikely(!machine_guid || !machine_url || !url_name)) {
  834. 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");
  835. buffer_flush(w->response.data);
  836. buffer_strcat(w->response.data, "Invalid registry Access request.");
  837. return HTTP_RESP_BAD_REQUEST;
  838. }
  839. web_client_enable_tracking_required(w);
  840. return registry_request_access_json(host, w, person_guid, machine_guid, machine_url, url_name, now_realtime_sec());
  841. case 'D':
  842. if(unlikely(!machine_guid || !machine_url || !delete_url)) {
  843. 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");
  844. buffer_flush(w->response.data);
  845. buffer_strcat(w->response.data, "Invalid registry Delete request.");
  846. return HTTP_RESP_BAD_REQUEST;
  847. }
  848. web_client_enable_tracking_required(w);
  849. return registry_request_delete_json(host, w, person_guid, machine_guid, machine_url, delete_url, now_realtime_sec());
  850. case 'S':
  851. if(unlikely(!machine_guid || !machine_url || !search_machine_guid)) {
  852. 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");
  853. buffer_flush(w->response.data);
  854. buffer_strcat(w->response.data, "Invalid registry Search request.");
  855. return HTTP_RESP_BAD_REQUEST;
  856. }
  857. web_client_enable_tracking_required(w);
  858. return registry_request_search_json(host, w, person_guid, machine_guid, machine_url, search_machine_guid, now_realtime_sec());
  859. case 'W':
  860. if(unlikely(!machine_guid || !machine_url || !to_person_guid)) {
  861. 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");
  862. buffer_flush(w->response.data);
  863. buffer_strcat(w->response.data, "Invalid registry Switch request.");
  864. return HTTP_RESP_BAD_REQUEST;
  865. }
  866. web_client_enable_tracking_required(w);
  867. return registry_request_switch_json(host, w, person_guid, machine_guid, machine_url, to_person_guid, now_realtime_sec());
  868. case 'H':
  869. return registry_request_hello_json(host, w);
  870. default:
  871. buffer_flush(w->response.data);
  872. buffer_strcat(w->response.data, "Invalid registry request - you need to set an action: hello, access, delete, search");
  873. return HTTP_RESP_BAD_REQUEST;
  874. }
  875. }
  876. static inline void web_client_api_request_v1_info_summary_alarm_statuses(RRDHOST *host, BUFFER *wb) {
  877. int alarm_normal = 0, alarm_warn = 0, alarm_crit = 0;
  878. RRDCALC *rc;
  879. rrdhost_rdlock(host);
  880. for(rc = host->alarms; rc ; rc = rc->next) {
  881. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  882. continue;
  883. switch(rc->status) {
  884. case RRDCALC_STATUS_WARNING:
  885. alarm_warn++;
  886. break;
  887. case RRDCALC_STATUS_CRITICAL:
  888. alarm_crit++;
  889. break;
  890. default:
  891. alarm_normal++;
  892. }
  893. }
  894. rrdhost_unlock(host);
  895. buffer_sprintf(wb, "\t\t\"normal\": %d,\n", alarm_normal);
  896. buffer_sprintf(wb, "\t\t\"warning\": %d,\n", alarm_warn);
  897. buffer_sprintf(wb, "\t\t\"critical\": %d\n", alarm_crit);
  898. }
  899. static inline void web_client_api_request_v1_info_mirrored_hosts(BUFFER *wb) {
  900. RRDHOST *host;
  901. int count = 0;
  902. buffer_strcat(wb, "\t\"mirrored_hosts\": [\n");
  903. rrd_rdlock();
  904. rrdhost_foreach_read(host) {
  905. if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))
  906. continue;
  907. if (count > 0)
  908. buffer_strcat(wb, ",\n");
  909. buffer_sprintf(wb, "\t\t\"%s\"", host->hostname);
  910. count++;
  911. }
  912. buffer_strcat(wb, "\n\t],\n\t\"mirrored_hosts_status\": [\n");
  913. count = 0;
  914. rrdhost_foreach_read(host)
  915. {
  916. if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))
  917. continue;
  918. if (count > 0)
  919. buffer_strcat(wb, ",\n");
  920. netdata_mutex_lock(&host->receiver_lock);
  921. buffer_sprintf(
  922. wb, "\t\t{ \"guid\": \"%s\", \"hostname\": \"%s\", \"reachable\": %s, \"hops\": %d"
  923. , host->machine_guid
  924. , host->hostname
  925. , (host->receiver || host == localhost) ? "true" : "false"
  926. , host->system_info ? host->system_info->hops : (host == localhost) ? 0 : 1
  927. );
  928. netdata_mutex_unlock(&host->receiver_lock);
  929. rrdhost_aclk_state_lock(host);
  930. if (host->aclk_state.claimed_id)
  931. buffer_sprintf(wb, ", \"claim_id\": \"%s\"", host->aclk_state.claimed_id);
  932. else
  933. buffer_strcat(wb, ", \"claim_id\": null");
  934. rrdhost_aclk_state_unlock(host);
  935. if (host->node_id) {
  936. char node_id_str[GUID_LEN + 1];
  937. uuid_unparse_lower(*host->node_id, node_id_str);
  938. buffer_sprintf(wb, ", \"node_id\": \"%s\" }", node_id_str);
  939. } else
  940. buffer_strcat(wb, ", \"node_id\": null }");
  941. count++;
  942. }
  943. rrd_unlock();
  944. buffer_strcat(wb, "\n\t],\n");
  945. }
  946. inline void host_labels2json(RRDHOST *host, BUFFER *wb, size_t indentation) {
  947. char tabs[11];
  948. if (indentation > 10)
  949. indentation = 10;
  950. tabs[0] = '\0';
  951. while (indentation) {
  952. strcat(tabs, "\t");
  953. indentation--;
  954. }
  955. rrdlabels_to_buffer(host->host_labels, wb, tabs, ":", "\"", ",\n", NULL, NULL, NULL, NULL);
  956. buffer_strcat(wb, "\n");
  957. }
  958. extern int aclk_connected;
  959. inline int web_client_api_request_v1_info_fill_buffer(RRDHOST *host, BUFFER *wb)
  960. {
  961. buffer_strcat(wb, "{\n");
  962. buffer_sprintf(wb, "\t\"version\": \"%s\",\n", host->program_version);
  963. buffer_sprintf(wb, "\t\"uid\": \"%s\",\n", host->machine_guid);
  964. web_client_api_request_v1_info_mirrored_hosts(wb);
  965. buffer_strcat(wb, "\t\"alarms\": {\n");
  966. web_client_api_request_v1_info_summary_alarm_statuses(host, wb);
  967. buffer_strcat(wb, "\t},\n");
  968. buffer_sprintf(wb, "\t\"os_name\": \"%s\",\n", (host->system_info->host_os_name) ? host->system_info->host_os_name : "");
  969. buffer_sprintf(wb, "\t\"os_id\": \"%s\",\n", (host->system_info->host_os_id) ? host->system_info->host_os_id : "");
  970. buffer_sprintf(wb, "\t\"os_id_like\": \"%s\",\n", (host->system_info->host_os_id_like) ? host->system_info->host_os_id_like : "");
  971. buffer_sprintf(wb, "\t\"os_version\": \"%s\",\n", (host->system_info->host_os_version) ? host->system_info->host_os_version : "");
  972. buffer_sprintf(wb, "\t\"os_version_id\": \"%s\",\n", (host->system_info->host_os_version_id) ? host->system_info->host_os_version_id : "");
  973. buffer_sprintf(wb, "\t\"os_detection\": \"%s\",\n", (host->system_info->host_os_detection) ? host->system_info->host_os_detection : "");
  974. buffer_sprintf(wb, "\t\"cores_total\": \"%s\",\n", (host->system_info->host_cores) ? host->system_info->host_cores : "");
  975. buffer_sprintf(wb, "\t\"total_disk_space\": \"%s\",\n", (host->system_info->host_disk_space) ? host->system_info->host_disk_space : "");
  976. buffer_sprintf(wb, "\t\"cpu_freq\": \"%s\",\n", (host->system_info->host_cpu_freq) ? host->system_info->host_cpu_freq : "");
  977. buffer_sprintf(wb, "\t\"ram_total\": \"%s\",\n", (host->system_info->host_ram_total) ? host->system_info->host_ram_total : "");
  978. if (host->system_info->container_os_name)
  979. buffer_sprintf(wb, "\t\"container_os_name\": \"%s\",\n", host->system_info->container_os_name);
  980. if (host->system_info->container_os_id)
  981. buffer_sprintf(wb, "\t\"container_os_id\": \"%s\",\n", host->system_info->container_os_id);
  982. if (host->system_info->container_os_id_like)
  983. buffer_sprintf(wb, "\t\"container_os_id_like\": \"%s\",\n", host->system_info->container_os_id_like);
  984. if (host->system_info->container_os_version)
  985. buffer_sprintf(wb, "\t\"container_os_version\": \"%s\",\n", host->system_info->container_os_version);
  986. if (host->system_info->container_os_version_id)
  987. buffer_sprintf(wb, "\t\"container_os_version_id\": \"%s\",\n", host->system_info->container_os_version_id);
  988. if (host->system_info->container_os_detection)
  989. buffer_sprintf(wb, "\t\"container_os_detection\": \"%s\",\n", host->system_info->container_os_detection);
  990. if (host->system_info->is_k8s_node)
  991. buffer_sprintf(wb, "\t\"is_k8s_node\": \"%s\",\n", host->system_info->is_k8s_node);
  992. buffer_sprintf(wb, "\t\"kernel_name\": \"%s\",\n", (host->system_info->kernel_name) ? host->system_info->kernel_name : "");
  993. buffer_sprintf(wb, "\t\"kernel_version\": \"%s\",\n", (host->system_info->kernel_version) ? host->system_info->kernel_version : "");
  994. buffer_sprintf(wb, "\t\"architecture\": \"%s\",\n", (host->system_info->architecture) ? host->system_info->architecture : "");
  995. buffer_sprintf(wb, "\t\"virtualization\": \"%s\",\n", (host->system_info->virtualization) ? host->system_info->virtualization : "");
  996. buffer_sprintf(wb, "\t\"virt_detection\": \"%s\",\n", (host->system_info->virt_detection) ? host->system_info->virt_detection : "");
  997. buffer_sprintf(wb, "\t\"container\": \"%s\",\n", (host->system_info->container) ? host->system_info->container : "");
  998. buffer_sprintf(wb, "\t\"container_detection\": \"%s\",\n", (host->system_info->container_detection) ? host->system_info->container_detection : "");
  999. if (host->system_info->cloud_provider_type)
  1000. buffer_sprintf(wb, "\t\"cloud_provider_type\": \"%s\",\n", host->system_info->cloud_provider_type);
  1001. if (host->system_info->cloud_instance_type)
  1002. buffer_sprintf(wb, "\t\"cloud_instance_type\": \"%s\",\n", host->system_info->cloud_instance_type);
  1003. if (host->system_info->cloud_instance_region)
  1004. buffer_sprintf(wb, "\t\"cloud_instance_region\": \"%s\",\n", host->system_info->cloud_instance_region);
  1005. buffer_strcat(wb, "\t\"host_labels\": {\n");
  1006. host_labels2json(host, wb, 2);
  1007. buffer_strcat(wb, "\t},\n");
  1008. buffer_strcat(wb, "\t\"collectors\": [");
  1009. chartcollectors2json(host, wb);
  1010. buffer_strcat(wb, "\n\t],\n");
  1011. #ifdef DISABLE_CLOUD
  1012. buffer_strcat(wb, "\t\"cloud-enabled\": false,\n");
  1013. #else
  1014. buffer_sprintf(wb, "\t\"cloud-enabled\": %s,\n",
  1015. appconfig_get_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "enabled", 1) ? "true" : "false");
  1016. #endif
  1017. #ifdef ENABLE_ACLK
  1018. buffer_strcat(wb, "\t\"cloud-available\": true,\n");
  1019. buffer_strcat(wb, "\t\"aclk-ng-available\": true,\n");
  1020. buffer_strcat(wb, "\t\"aclk-ng-new-cloud-protocol\": true,\n");
  1021. buffer_strcat(wb, "\t\"aclk-legacy-available\": false,\n");
  1022. buffer_strcat(wb, "\t\"aclk-implementation\": \"Next Generation\",\n");
  1023. #else
  1024. buffer_strcat(wb, "\t\"cloud-available\": false,\n");
  1025. buffer_strcat(wb, "\t\"aclk-ng-available\": false,\n");
  1026. buffer_strcat(wb, "\t\"aclk-legacy-available\": false,\n");
  1027. #endif
  1028. char *agent_id = get_agent_claimid();
  1029. if (agent_id == NULL)
  1030. buffer_strcat(wb, "\t\"agent-claimed\": false,\n");
  1031. else {
  1032. buffer_strcat(wb, "\t\"agent-claimed\": true,\n");
  1033. freez(agent_id);
  1034. }
  1035. #ifdef ENABLE_ACLK
  1036. if (aclk_connected) {
  1037. buffer_strcat(wb, "\t\"aclk-available\": true,\n");
  1038. buffer_strcat(wb, "\t\"aclk-available-protocol\": \"New\",\n");
  1039. }
  1040. else
  1041. #endif
  1042. buffer_strcat(wb, "\t\"aclk-available\": false,\n\t\"aclk-available-protocol\": null,\n"); // Intentionally valid with/without #ifdef above
  1043. buffer_strcat(wb, "\t\"memory-mode\": ");
  1044. analytics_get_data(analytics_data.netdata_config_memory_mode, wb);
  1045. buffer_strcat(wb, ",\n");
  1046. buffer_strcat(wb, "\t\"multidb-disk-quota\": ");
  1047. analytics_get_data(analytics_data.netdata_config_multidb_disk_quota, wb);
  1048. buffer_strcat(wb, ",\n");
  1049. buffer_strcat(wb, "\t\"page-cache-size\": ");
  1050. analytics_get_data(analytics_data.netdata_config_page_cache_size, wb);
  1051. buffer_strcat(wb, ",\n");
  1052. buffer_strcat(wb, "\t\"stream-enabled\": ");
  1053. analytics_get_data(analytics_data.netdata_config_stream_enabled, wb);
  1054. buffer_strcat(wb, ",\n");
  1055. #ifdef ENABLE_COMPRESSION
  1056. if(host->sender){
  1057. buffer_strcat(wb, "\t\"stream-compression\": ");
  1058. buffer_strcat(wb, (host->sender->rrdpush_compression ? "true" : "false"));
  1059. buffer_strcat(wb, ",\n");
  1060. }else{
  1061. buffer_strcat(wb, "\t\"stream-compression\": null,\n");
  1062. }
  1063. #else
  1064. buffer_strcat(wb, "\t\"stream-compression\": null,\n");
  1065. #endif //ENABLE_COMPRESSION
  1066. buffer_strcat(wb, "\t\"hosts-available\": ");
  1067. analytics_get_data(analytics_data.netdata_config_hosts_available, wb);
  1068. buffer_strcat(wb, ",\n");
  1069. buffer_strcat(wb, "\t\"https-enabled\": ");
  1070. analytics_get_data(analytics_data.netdata_config_https_enabled, wb);
  1071. buffer_strcat(wb, ",\n");
  1072. buffer_strcat(wb, "\t\"buildinfo\": ");
  1073. analytics_get_data(analytics_data.netdata_buildinfo, wb);
  1074. buffer_strcat(wb, ",\n");
  1075. buffer_strcat(wb, "\t\"release-channel\": ");
  1076. analytics_get_data(analytics_data.netdata_config_release_channel, wb);
  1077. buffer_strcat(wb, ",\n");
  1078. buffer_strcat(wb, "\t\"web-enabled\": ");
  1079. analytics_get_data(analytics_data.netdata_config_web_enabled, wb);
  1080. buffer_strcat(wb, ",\n");
  1081. buffer_strcat(wb, "\t\"notification-methods\": ");
  1082. analytics_get_data(analytics_data.netdata_notification_methods, wb);
  1083. buffer_strcat(wb, ",\n");
  1084. buffer_strcat(wb, "\t\"exporting-enabled\": ");
  1085. analytics_get_data(analytics_data.netdata_config_exporting_enabled, wb);
  1086. buffer_strcat(wb, ",\n");
  1087. buffer_strcat(wb, "\t\"exporting-connectors\": ");
  1088. analytics_get_data(analytics_data.netdata_exporting_connectors, wb);
  1089. buffer_strcat(wb, ",\n");
  1090. buffer_strcat(wb, "\t\"allmetrics-prometheus-used\": ");
  1091. analytics_get_data(analytics_data.netdata_allmetrics_prometheus_used, wb);
  1092. buffer_strcat(wb, ",\n");
  1093. buffer_strcat(wb, "\t\"allmetrics-shell-used\": ");
  1094. analytics_get_data(analytics_data.netdata_allmetrics_shell_used, wb);
  1095. buffer_strcat(wb, ",\n");
  1096. buffer_strcat(wb, "\t\"allmetrics-json-used\": ");
  1097. analytics_get_data(analytics_data.netdata_allmetrics_json_used, wb);
  1098. buffer_strcat(wb, ",\n");
  1099. buffer_strcat(wb, "\t\"dashboard-used\": ");
  1100. analytics_get_data(analytics_data.netdata_dashboard_used, wb);
  1101. buffer_strcat(wb, ",\n");
  1102. buffer_strcat(wb, "\t\"charts-count\": ");
  1103. analytics_get_data(analytics_data.netdata_charts_count, wb);
  1104. buffer_strcat(wb, ",\n");
  1105. buffer_strcat(wb, "\t\"metrics-count\": ");
  1106. analytics_get_data(analytics_data.netdata_metrics_count, wb);
  1107. #if defined(ENABLE_ML)
  1108. buffer_strcat(wb, ",\n");
  1109. char *ml_info = ml_get_host_info(host);
  1110. buffer_strcat(wb, "\t\"ml-info\": ");
  1111. buffer_strcat(wb, ml_info);
  1112. free(ml_info);
  1113. #endif
  1114. buffer_strcat(wb, "\n}");
  1115. return 0;
  1116. }
  1117. #if defined(ENABLE_ML)
  1118. int web_client_api_request_v1_anomaly_events(RRDHOST *host, struct web_client *w, char *url) {
  1119. if (!netdata_ready)
  1120. return HTTP_RESP_BACKEND_FETCH_FAILED;
  1121. uint32_t after = 0, before = 0;
  1122. while (url) {
  1123. char *value = mystrsep(&url, "&");
  1124. if (!value || !*value)
  1125. continue;
  1126. char *name = mystrsep(&value, "=");
  1127. if (!name || !*name)
  1128. continue;
  1129. if (!value || !*value)
  1130. continue;
  1131. if (!strcmp(name, "after"))
  1132. after = (uint32_t) (strtoul(value, NULL, 0) / 1000);
  1133. else if (!strcmp(name, "before"))
  1134. before = (uint32_t) (strtoul(value, NULL, 0) / 1000);
  1135. }
  1136. char *s;
  1137. if (!before || !after)
  1138. s = strdupz("{\"error\": \"missing after/before parameters\" }\n");
  1139. else {
  1140. s = ml_get_anomaly_events(host, "AD1", 1, after, before);
  1141. if (!s)
  1142. s = strdupz("{\"error\": \"json string is empty\" }\n");
  1143. }
  1144. BUFFER *wb = w->response.data;
  1145. buffer_flush(wb);
  1146. wb->contenttype = CT_APPLICATION_JSON;
  1147. buffer_strcat(wb, s);
  1148. buffer_no_cacheable(wb);
  1149. freez(s);
  1150. return HTTP_RESP_OK;
  1151. }
  1152. int web_client_api_request_v1_anomaly_event_info(RRDHOST *host, struct web_client *w, char *url) {
  1153. if (!netdata_ready)
  1154. return HTTP_RESP_BACKEND_FETCH_FAILED;
  1155. uint32_t after = 0, before = 0;
  1156. while (url) {
  1157. char *value = mystrsep(&url, "&");
  1158. if (!value || !*value)
  1159. continue;
  1160. char *name = mystrsep(&value, "=");
  1161. if (!name || !*name)
  1162. continue;
  1163. if (!value || !*value)
  1164. continue;
  1165. if (!strcmp(name, "after"))
  1166. after = (uint32_t) strtoul(value, NULL, 0);
  1167. else if (!strcmp(name, "before"))
  1168. before = (uint32_t) strtoul(value, NULL, 0);
  1169. }
  1170. char *s;
  1171. if (!before || !after)
  1172. s = strdupz("{\"error\": \"missing after/before parameters\" }\n");
  1173. else {
  1174. s = ml_get_anomaly_event_info(host, "AD1", 1, after, before);
  1175. if (!s)
  1176. s = strdupz("{\"error\": \"json string is empty\" }\n");
  1177. }
  1178. BUFFER *wb = w->response.data;
  1179. buffer_flush(wb);
  1180. wb->contenttype = CT_APPLICATION_JSON;
  1181. buffer_strcat(wb, s);
  1182. buffer_no_cacheable(wb);
  1183. freez(s);
  1184. return HTTP_RESP_OK;
  1185. }
  1186. int web_client_api_request_v1_ml_info(RRDHOST *host, struct web_client *w, char *url) {
  1187. (void) url;
  1188. if (!netdata_ready)
  1189. return HTTP_RESP_BACKEND_FETCH_FAILED;
  1190. char *s = ml_get_host_runtime_info(host);
  1191. if (!s)
  1192. s = strdupz("{\"error\": \"json string is empty\" }\n");
  1193. BUFFER *wb = w->response.data;
  1194. buffer_flush(wb);
  1195. wb->contenttype = CT_APPLICATION_JSON;
  1196. buffer_strcat(wb, s);
  1197. buffer_no_cacheable(wb);
  1198. freez(s);
  1199. return HTTP_RESP_OK;
  1200. }
  1201. #endif // defined(ENABLE_ML)
  1202. inline int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, char *url) {
  1203. (void)url;
  1204. if (!netdata_ready) return HTTP_RESP_BACKEND_FETCH_FAILED;
  1205. BUFFER *wb = w->response.data;
  1206. buffer_flush(wb);
  1207. wb->contenttype = CT_APPLICATION_JSON;
  1208. web_client_api_request_v1_info_fill_buffer(host, wb);
  1209. buffer_no_cacheable(wb);
  1210. return HTTP_RESP_OK;
  1211. }
  1212. static int web_client_api_request_v1_aclk_state(RRDHOST *host, struct web_client *w, char *url) {
  1213. UNUSED(url);
  1214. UNUSED(host);
  1215. if (!netdata_ready) return HTTP_RESP_BACKEND_FETCH_FAILED;
  1216. BUFFER *wb = w->response.data;
  1217. buffer_flush(wb);
  1218. char *str = aclk_state_json();
  1219. buffer_strcat(wb, str);
  1220. freez(str);
  1221. wb->contenttype = CT_APPLICATION_JSON;
  1222. buffer_no_cacheable(wb);
  1223. return HTTP_RESP_OK;
  1224. }
  1225. int web_client_api_request_v1_metric_correlations(RRDHOST *host, struct web_client *w, char *url) {
  1226. if (!netdata_ready)
  1227. return HTTP_RESP_BACKEND_FETCH_FAILED;
  1228. long long baseline_after = 0, baseline_before = 0, after = 0, before = 0, points = 0;
  1229. RRDR_OPTIONS options = RRDR_OPTION_NOT_ALIGNED | RRDR_OPTION_NONZERO | RRDR_OPTION_NULL2ZERO;
  1230. METRIC_CORRELATIONS_METHOD method = default_metric_correlations_method;
  1231. RRDR_GROUPING group = RRDR_GROUPING_AVERAGE;
  1232. int timeout = 0;
  1233. const char *group_options = NULL;
  1234. while (url) {
  1235. char *value = mystrsep(&url, "&");
  1236. if (!value || !*value)
  1237. continue;
  1238. char *name = mystrsep(&value, "=");
  1239. if (!name || !*name)
  1240. continue;
  1241. if (!value || !*value)
  1242. continue;
  1243. if (!strcmp(name, "baseline_after"))
  1244. baseline_after = (long long) strtoul(value, NULL, 0);
  1245. else if (!strcmp(name, "baseline_before"))
  1246. baseline_before = (long long) strtoul(value, NULL, 0);
  1247. else if (!strcmp(name, "after") || !strcmp(name, "highlight_after"))
  1248. after = (long long) strtoul(value, NULL, 0);
  1249. else if (!strcmp(name, "before") || !strcmp(name, "highlight_before"))
  1250. before = (long long) strtoul(value, NULL, 0);
  1251. else if (!strcmp(name, "points") || !strcmp(name, "max_points"))
  1252. points = (long long) strtoul(value, NULL, 0);
  1253. else if (!strcmp(name, "timeout"))
  1254. timeout = (int) strtoul(value, NULL, 0);
  1255. else if(!strcmp(name, "group"))
  1256. group = web_client_api_request_v1_data_group(value, RRDR_GROUPING_AVERAGE);
  1257. else if(!strcmp(name, "options"))
  1258. options |= web_client_api_request_v1_data_options(value);
  1259. else if(!strcmp(name, "method"))
  1260. method = mc_string_to_method(value);
  1261. }
  1262. BUFFER *wb = w->response.data;
  1263. buffer_flush(wb);
  1264. wb->contenttype = CT_APPLICATION_JSON;
  1265. buffer_no_cacheable(wb);
  1266. return metric_correlations(host, wb, method, group, group_options, baseline_after, baseline_before, after, before, points, options, timeout);
  1267. }
  1268. static struct api_command {
  1269. const char *command;
  1270. uint32_t hash;
  1271. WEB_CLIENT_ACL acl;
  1272. int (*callback)(RRDHOST *host, struct web_client *w, char *url);
  1273. } api_commands[] = {
  1274. { "info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_info },
  1275. { "data", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_data },
  1276. { "chart", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_chart },
  1277. { "charts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_charts },
  1278. { "context", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_context },
  1279. { "contexts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_contexts },
  1280. { "archivedcharts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_archivedcharts },
  1281. // registry checks the ACL by itself, so we allow everything
  1282. { "registry", 0, WEB_CLIENT_ACL_NOCHECK, web_client_api_request_v1_registry },
  1283. // badges can be fetched with both dashboard and badge permissions
  1284. { "badge.svg", 0, WEB_CLIENT_ACL_DASHBOARD|WEB_CLIENT_ACL_BADGE, web_client_api_request_v1_badge },
  1285. { "alarms", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarms },
  1286. { "alarms_values", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarms_values },
  1287. { "alarm_log", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_log },
  1288. { "alarm_variables", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_variables },
  1289. { "alarm_count", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_count },
  1290. { "allmetrics", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_allmetrics },
  1291. #if defined(ENABLE_ML)
  1292. { "anomaly_events", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_anomaly_events },
  1293. { "anomaly_event_info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_anomaly_event_info },
  1294. { "ml_info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_ml_info },
  1295. #endif
  1296. { "manage/health", 0, WEB_CLIENT_ACL_MGMT, web_client_api_request_v1_mgmt_health },
  1297. { "aclk", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_aclk_state },
  1298. { "metric_correlations", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_metric_correlations },
  1299. // terminator
  1300. { NULL, 0, WEB_CLIENT_ACL_NONE, NULL },
  1301. };
  1302. inline int web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *url) {
  1303. static int initialized = 0;
  1304. int i;
  1305. if(unlikely(initialized == 0)) {
  1306. initialized = 1;
  1307. for(i = 0; api_commands[i].command ; i++)
  1308. api_commands[i].hash = simple_hash(api_commands[i].command);
  1309. }
  1310. // get the command
  1311. if(url) {
  1312. debug(D_WEB_CLIENT, "%llu: Searching for API v1 command '%s'.", w->id, url);
  1313. uint32_t hash = simple_hash(url);
  1314. for(i = 0; api_commands[i].command ;i++) {
  1315. if(unlikely(hash == api_commands[i].hash && !strcmp(url, api_commands[i].command))) {
  1316. if(unlikely(api_commands[i].acl != WEB_CLIENT_ACL_NOCHECK) && !(w->acl & api_commands[i].acl))
  1317. return web_client_permission_denied(w);
  1318. //return api_commands[i].callback(host, w, url);
  1319. return api_commands[i].callback(host, w, (w->decoded_query_string + 1));
  1320. }
  1321. }
  1322. buffer_flush(w->response.data);
  1323. buffer_strcat(w->response.data, "Unsupported v1 API command: ");
  1324. buffer_strcat_htmlescape(w->response.data, url);
  1325. return HTTP_RESP_NOT_FOUND;
  1326. }
  1327. else {
  1328. buffer_flush(w->response.data);
  1329. buffer_sprintf(w->response.data, "Which API v1 command?");
  1330. return HTTP_RESP_BAD_REQUEST;
  1331. }
  1332. }