web_api_v1.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  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. else if(!strcmp(tok, "hidden"))
  332. options |= RRDCONTEXT_OPTION_SHOW_HIDDEN;
  333. }
  334. return options;
  335. }
  336. static int web_client_api_request_v1_context(RRDHOST *host, struct web_client *w, char *url) {
  337. char *context = NULL;
  338. RRDCONTEXT_TO_JSON_OPTIONS options = RRDCONTEXT_OPTION_NONE;
  339. time_t after = 0, before = 0;
  340. const char *chart_label_key = NULL, *chart_labels_filter = NULL;
  341. BUFFER *dimensions = NULL;
  342. buffer_flush(w->response.data);
  343. while(url) {
  344. char *value = mystrsep(&url, "&");
  345. if(!value || !*value) continue;
  346. char *name = mystrsep(&value, "=");
  347. if(!name || !*name) continue;
  348. if(!value || !*value) continue;
  349. // name and value are now the parameters
  350. // they are not null and not empty
  351. if(!strcmp(name, "context") || !strcmp(name, "ctx")) context = value;
  352. else if(!strcmp(name, "after")) after = str2l(value);
  353. else if(!strcmp(name, "before")) before = str2l(value);
  354. else if(!strcmp(name, "options")) options = rrdcontext_to_json_parse_options(value);
  355. else if(!strcmp(name, "chart_label_key")) chart_label_key = value;
  356. else if(!strcmp(name, "chart_labels_filter")) chart_labels_filter = value;
  357. else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
  358. if(!dimensions) dimensions = buffer_create(100);
  359. buffer_strcat(dimensions, "|");
  360. buffer_strcat(dimensions, value);
  361. }
  362. }
  363. if(!context || !*context) {
  364. buffer_sprintf(w->response.data, "No context is given at the request.");
  365. return HTTP_RESP_BAD_REQUEST;
  366. }
  367. SIMPLE_PATTERN *chart_label_key_pattern = NULL;
  368. SIMPLE_PATTERN *chart_labels_filter_pattern = NULL;
  369. SIMPLE_PATTERN *chart_dimensions_pattern = NULL;
  370. if(chart_label_key)
  371. chart_label_key_pattern = simple_pattern_create(chart_label_key, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  372. if(chart_labels_filter)
  373. chart_labels_filter_pattern = simple_pattern_create(chart_labels_filter, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  374. if(dimensions) {
  375. chart_dimensions_pattern = simple_pattern_create(buffer_tostring(dimensions), ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  376. buffer_free(dimensions);
  377. }
  378. w->response.data->contenttype = CT_APPLICATION_JSON;
  379. int ret = rrdcontext_to_json(host, w->response.data, after, before, options, context, chart_label_key_pattern, chart_labels_filter_pattern, chart_dimensions_pattern);
  380. simple_pattern_free(chart_label_key_pattern);
  381. simple_pattern_free(chart_labels_filter_pattern);
  382. simple_pattern_free(chart_dimensions_pattern);
  383. return ret;
  384. }
  385. static int web_client_api_request_v1_contexts(RRDHOST *host, struct web_client *w, char *url) {
  386. RRDCONTEXT_TO_JSON_OPTIONS options = RRDCONTEXT_OPTION_NONE;
  387. time_t after = 0, before = 0;
  388. const char *chart_label_key = NULL, *chart_labels_filter = NULL;
  389. BUFFER *dimensions = NULL;
  390. buffer_flush(w->response.data);
  391. while(url) {
  392. char *value = mystrsep(&url, "&");
  393. if(!value || !*value) continue;
  394. char *name = mystrsep(&value, "=");
  395. if(!name || !*name) continue;
  396. if(!value || !*value) continue;
  397. // name and value are now the parameters
  398. // they are not null and not empty
  399. if(!strcmp(name, "after")) after = str2l(value);
  400. else if(!strcmp(name, "before")) before = str2l(value);
  401. else if(!strcmp(name, "options")) options = rrdcontext_to_json_parse_options(value);
  402. else if(!strcmp(name, "chart_label_key")) chart_label_key = value;
  403. else if(!strcmp(name, "chart_labels_filter")) chart_labels_filter = value;
  404. else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
  405. if(!dimensions) dimensions = buffer_create(100);
  406. buffer_strcat(dimensions, "|");
  407. buffer_strcat(dimensions, value);
  408. }
  409. }
  410. SIMPLE_PATTERN *chart_label_key_pattern = NULL;
  411. SIMPLE_PATTERN *chart_labels_filter_pattern = NULL;
  412. SIMPLE_PATTERN *chart_dimensions_pattern = NULL;
  413. if(chart_label_key)
  414. chart_label_key_pattern = simple_pattern_create(chart_label_key, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  415. if(chart_labels_filter)
  416. chart_labels_filter_pattern = simple_pattern_create(chart_labels_filter, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  417. if(dimensions) {
  418. chart_dimensions_pattern = simple_pattern_create(buffer_tostring(dimensions), ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  419. buffer_free(dimensions);
  420. }
  421. w->response.data->contenttype = CT_APPLICATION_JSON;
  422. int ret = rrdcontexts_to_json(host, w->response.data, after, before, options, chart_label_key_pattern, chart_labels_filter_pattern, chart_dimensions_pattern);
  423. simple_pattern_free(chart_label_key_pattern);
  424. simple_pattern_free(chart_labels_filter_pattern);
  425. simple_pattern_free(chart_dimensions_pattern);
  426. return ret;
  427. }
  428. inline int web_client_api_request_v1_charts(RRDHOST *host, struct web_client *w, char *url) {
  429. (void)url;
  430. buffer_flush(w->response.data);
  431. w->response.data->contenttype = CT_APPLICATION_JSON;
  432. charts2json(host, w->response.data, 0, 0);
  433. return HTTP_RESP_OK;
  434. }
  435. inline int web_client_api_request_v1_archivedcharts(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  436. (void)url;
  437. buffer_flush(w->response.data);
  438. w->response.data->contenttype = CT_APPLICATION_JSON;
  439. #ifdef ENABLE_DBENGINE
  440. if (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  441. sql_rrdset2json(host, w->response.data);
  442. #endif
  443. return HTTP_RESP_OK;
  444. }
  445. inline int web_client_api_request_v1_chart(RRDHOST *host, struct web_client *w, char *url) {
  446. return web_client_api_request_single_chart(host, w, url, rrd_stats_api_v1_chart);
  447. }
  448. void fix_google_param(char *s) {
  449. if(unlikely(!s)) return;
  450. for( ; *s ;s++) {
  451. if(!isalnum(*s) && *s != '.' && *s != '_' && *s != '-')
  452. *s = '_';
  453. }
  454. }
  455. // returns the HTTP code
  456. inline int web_client_api_request_v1_data(RRDHOST *host, struct web_client *w, char *url) {
  457. debug(D_WEB_CLIENT, "%llu: API v1 data with URL '%s'", w->id, url);
  458. int ret = HTTP_RESP_BAD_REQUEST;
  459. BUFFER *dimensions = NULL;
  460. buffer_flush(w->response.data);
  461. char *google_version = "0.6",
  462. *google_reqId = "0",
  463. *google_sig = "0",
  464. *google_out = "json",
  465. *responseHandler = NULL,
  466. *outFileName = NULL;
  467. time_t last_timestamp_in_data = 0, google_timestamp = 0;
  468. char *chart = NULL;
  469. char *before_str = NULL;
  470. char *after_str = NULL;
  471. char *group_time_str = NULL;
  472. char *points_str = NULL;
  473. char *timeout_str = NULL;
  474. char *max_anomaly_rates_str = NULL;
  475. char *context = NULL;
  476. char *chart_label_key = NULL;
  477. char *chart_labels_filter = NULL;
  478. char *group_options = NULL;
  479. int tier = 0;
  480. int group = RRDR_GROUPING_AVERAGE;
  481. int show_dimensions = 0;
  482. uint32_t format = DATASOURCE_JSON;
  483. uint32_t options = 0x00000000;
  484. while(url) {
  485. char *value = mystrsep(&url, "&");
  486. if(!value || !*value) continue;
  487. char *name = mystrsep(&value, "=");
  488. if(!name || !*name) continue;
  489. if(!value || !*value) continue;
  490. debug(D_WEB_CLIENT, "%llu: API v1 data query param '%s' with value '%s'", w->id, name, value);
  491. // name and value are now the parameters
  492. // they are not null and not empty
  493. if(!strcmp(name, "context")) context = value;
  494. else if(!strcmp(name, "chart_label_key")) chart_label_key = value;
  495. else if(!strcmp(name, "chart_labels_filter")) chart_labels_filter = value;
  496. else if(!strcmp(name, "chart")) chart = value;
  497. else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
  498. if(!dimensions) dimensions = buffer_create(100);
  499. buffer_strcat(dimensions, "|");
  500. buffer_strcat(dimensions, value);
  501. }
  502. else if(!strcmp(name, "show_dimensions")) show_dimensions = 1;
  503. else if(!strcmp(name, "after")) after_str = value;
  504. else if(!strcmp(name, "before")) before_str = value;
  505. else if(!strcmp(name, "points")) points_str = value;
  506. else if(!strcmp(name, "timeout")) timeout_str = value;
  507. else if(!strcmp(name, "gtime")) group_time_str = value;
  508. else if(!strcmp(name, "group_options")) group_options = value;
  509. else if(!strcmp(name, "group")) {
  510. group = web_client_api_request_v1_data_group(value, RRDR_GROUPING_AVERAGE);
  511. }
  512. else if(!strcmp(name, "format")) {
  513. format = web_client_api_request_v1_data_format(value);
  514. }
  515. else if(!strcmp(name, "options")) {
  516. options |= web_client_api_request_v1_data_options(value);
  517. }
  518. else if(!strcmp(name, "callback")) {
  519. responseHandler = value;
  520. }
  521. else if(!strcmp(name, "filename")) {
  522. outFileName = value;
  523. }
  524. else if(!strcmp(name, "tqx")) {
  525. // parse Google Visualization API options
  526. // https://developers.google.com/chart/interactive/docs/dev/implementing_data_source
  527. char *tqx_name, *tqx_value;
  528. while(value) {
  529. tqx_value = mystrsep(&value, ";");
  530. if(!tqx_value || !*tqx_value) continue;
  531. tqx_name = mystrsep(&tqx_value, ":");
  532. if(!tqx_name || !*tqx_name) continue;
  533. if(!tqx_value || !*tqx_value) continue;
  534. if(!strcmp(tqx_name, "version"))
  535. google_version = tqx_value;
  536. else if(!strcmp(tqx_name, "reqId"))
  537. google_reqId = tqx_value;
  538. else if(!strcmp(tqx_name, "sig")) {
  539. google_sig = tqx_value;
  540. google_timestamp = strtoul(google_sig, NULL, 0);
  541. }
  542. else if(!strcmp(tqx_name, "out")) {
  543. google_out = tqx_value;
  544. format = web_client_api_request_v1_data_google_format(google_out);
  545. }
  546. else if(!strcmp(tqx_name, "responseHandler"))
  547. responseHandler = tqx_value;
  548. else if(!strcmp(tqx_name, "outFileName"))
  549. outFileName = tqx_value;
  550. }
  551. }
  552. else if(!strcmp(name, "max_anomaly_rates")) {
  553. max_anomaly_rates_str = value;
  554. }
  555. else if(!strcmp(name, "tier")) {
  556. tier = str2i(value);
  557. if(tier >= 0 && tier < storage_tiers)
  558. options |= RRDR_OPTION_SELECTED_TIER;
  559. }
  560. }
  561. // validate the google parameters given
  562. fix_google_param(google_out);
  563. fix_google_param(google_sig);
  564. fix_google_param(google_reqId);
  565. fix_google_param(google_version);
  566. fix_google_param(responseHandler);
  567. fix_google_param(outFileName);
  568. RRDSET *st = NULL;
  569. ONEWAYALLOC *owa = onewayalloc_create(0);
  570. if((!chart || !*chart) && (!context)) {
  571. buffer_sprintf(w->response.data, "No chart id is given at the request.");
  572. goto cleanup;
  573. }
  574. struct context_param *context_param_list = NULL;
  575. if (context && !chart) {
  576. RRDSET *st1;
  577. uint32_t context_hash = simple_hash(context);
  578. SIMPLE_PATTERN *chart_label_key_pattern = NULL;
  579. if(chart_label_key)
  580. chart_label_key_pattern = simple_pattern_create(chart_label_key, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  581. SIMPLE_PATTERN *chart_labels_filter_pattern = NULL;
  582. if(chart_labels_filter)
  583. chart_labels_filter_pattern = simple_pattern_create(chart_labels_filter, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  584. rrdhost_rdlock(host);
  585. rrdset_foreach_read(st1, host) {
  586. if (st1->hash_context == context_hash && !strcmp(st1->context, context) &&
  587. (!chart_label_key_pattern || rrdlabels_match_simple_pattern_parsed(st1->state->chart_labels, chart_label_key_pattern, ':')) &&
  588. (!chart_labels_filter_pattern || rrdlabels_match_simple_pattern_parsed(st1->state->chart_labels, chart_labels_filter_pattern, ':')))
  589. build_context_param_list(owa, &context_param_list, st1);
  590. }
  591. rrdhost_unlock(host);
  592. if (likely(context_param_list && context_param_list->rd)) // Just set the first one
  593. st = context_param_list->rd->rrdset;
  594. else {
  595. if (!chart_label_key && !chart_labels_filter)
  596. sql_build_context_param_list(owa, &context_param_list, host, context, NULL);
  597. }
  598. }
  599. else {
  600. st = rrdset_find(host, chart);
  601. if (!st)
  602. st = rrdset_find_byname(host, chart);
  603. if (likely(st))
  604. st->last_accessed_time = now_realtime_sec();
  605. else
  606. sql_build_context_param_list(owa, &context_param_list, host, NULL, chart);
  607. }
  608. if (!st) {
  609. if (likely(context_param_list && context_param_list->rd && context_param_list->rd->rrdset))
  610. st = context_param_list->rd->rrdset;
  611. else {
  612. free_context_param_list(owa, &context_param_list);
  613. context_param_list = NULL;
  614. }
  615. }
  616. if (!st && !context_param_list) {
  617. if (context && !chart) {
  618. if (!chart_label_key) {
  619. buffer_strcat(w->response.data, "Context is not found: ");
  620. buffer_strcat_htmlescape(w->response.data, context);
  621. } else {
  622. buffer_strcat(w->response.data, "Context: ");
  623. buffer_strcat_htmlescape(w->response.data, context);
  624. buffer_strcat(w->response.data, " or chart label key: ");
  625. buffer_strcat_htmlescape(w->response.data, chart_label_key);
  626. buffer_strcat(w->response.data, " not found");
  627. }
  628. }
  629. else {
  630. buffer_strcat(w->response.data, "Chart is not found: ");
  631. buffer_strcat_htmlescape(w->response.data, chart);
  632. }
  633. ret = HTTP_RESP_NOT_FOUND;
  634. goto cleanup;
  635. }
  636. long long before = (before_str && *before_str)?str2l(before_str):0;
  637. long long after = (after_str && *after_str) ?str2l(after_str):-600;
  638. int points = (points_str && *points_str)?str2i(points_str):0;
  639. int timeout = (timeout_str && *timeout_str)?str2i(timeout_str): 0;
  640. long group_time = (group_time_str && *group_time_str)?str2l(group_time_str):0;
  641. int max_anomaly_rates = (max_anomaly_rates_str && *max_anomaly_rates_str) ? str2i(max_anomaly_rates_str) : 0;
  642. if (timeout) {
  643. struct timeval now;
  644. now_realtime_timeval(&now);
  645. int inqueue = (int)dt_usec(&w->tv_in, &now) / 1000;
  646. timeout -= inqueue;
  647. if (timeout <= 0) {
  648. buffer_flush(w->response.data);
  649. buffer_strcat(w->response.data, "Query timeout exceeded");
  650. return HTTP_RESP_BACKEND_FETCH_FAILED;
  651. }
  652. }
  653. 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'"
  654. , w->id
  655. , chart
  656. , (dimensions)?buffer_tostring(dimensions):""
  657. , after
  658. , before
  659. , points
  660. , group
  661. , format
  662. , options
  663. );
  664. if(outFileName && *outFileName) {
  665. buffer_sprintf(w->response.header, "Content-Disposition: attachment; filename=\"%s\"\r\n", outFileName);
  666. debug(D_WEB_CLIENT, "%llu: generating outfilename header: '%s'", w->id, outFileName);
  667. }
  668. if(format == DATASOURCE_DATATABLE_JSONP) {
  669. if(responseHandler == NULL)
  670. responseHandler = "google.visualization.Query.setResponse";
  671. debug(D_WEB_CLIENT_ACCESS, "%llu: GOOGLE JSON/JSONP: version = '%s', reqId = '%s', sig = '%s', out = '%s', responseHandler = '%s', outFileName = '%s'",
  672. w->id, google_version, google_reqId, google_sig, google_out, responseHandler, outFileName
  673. );
  674. buffer_sprintf(
  675. w->response.data,
  676. "%s({version:'%s',reqId:'%s',status:'ok',sig:'%"PRId64"',table:",
  677. responseHandler,
  678. google_version,
  679. google_reqId,
  680. (int64_t)st->last_updated.tv_sec);
  681. }
  682. else if(format == DATASOURCE_JSONP) {
  683. if(responseHandler == NULL)
  684. responseHandler = "callback";
  685. buffer_strcat(w->response.data, responseHandler);
  686. buffer_strcat(w->response.data, "(");
  687. }
  688. QUERY_PARAMS query_params = {
  689. .context_param_list = context_param_list,
  690. .timeout = timeout,
  691. .max_anomaly_rates = max_anomaly_rates,
  692. .show_dimensions = show_dimensions,
  693. .chart_label_key = chart_label_key,
  694. .wb = w->response.data};
  695. ret = rrdset2anything_api_v1(owa, st, &query_params, dimensions, format,
  696. points, after, before, group, group_options, group_time, options, &last_timestamp_in_data, tier);
  697. free_context_param_list(owa, &context_param_list);
  698. if(format == DATASOURCE_DATATABLE_JSONP) {
  699. if(google_timestamp < last_timestamp_in_data)
  700. buffer_strcat(w->response.data, "});");
  701. else {
  702. // the client already has the latest data
  703. buffer_flush(w->response.data);
  704. buffer_sprintf(w->response.data,
  705. "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'not_modified',message:'Data not modified'}]});",
  706. responseHandler, google_version, google_reqId);
  707. }
  708. }
  709. else if(format == DATASOURCE_JSONP)
  710. buffer_strcat(w->response.data, ");");
  711. cleanup:
  712. onewayalloc_destroy(owa);
  713. buffer_free(dimensions);
  714. return ret;
  715. }
  716. // Pings a netdata server:
  717. // /api/v1/registry?action=hello
  718. //
  719. // Access to a netdata registry:
  720. // /api/v1/registry?action=access&machine=${machine_guid}&name=${hostname}&url=${url}
  721. //
  722. // Delete from a netdata registry:
  723. // /api/v1/registry?action=delete&machine=${machine_guid}&name=${hostname}&url=${url}&delete_url=${delete_url}
  724. //
  725. // Search for the URLs of a machine:
  726. // /api/v1/registry?action=search&machine=${machine_guid}&name=${hostname}&url=${url}&for=${machine_guid}
  727. //
  728. // Impersonate:
  729. // /api/v1/registry?action=switch&machine=${machine_guid}&name=${hostname}&url=${url}&to=${new_person_guid}
  730. inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *w, char *url) {
  731. static uint32_t hash_action = 0, hash_access = 0, hash_hello = 0, hash_delete = 0, hash_search = 0,
  732. hash_switch = 0, hash_machine = 0, hash_url = 0, hash_name = 0, hash_delete_url = 0, hash_for = 0,
  733. hash_to = 0 /*, hash_redirects = 0 */;
  734. if(unlikely(!hash_action)) {
  735. hash_action = simple_hash("action");
  736. hash_access = simple_hash("access");
  737. hash_hello = simple_hash("hello");
  738. hash_delete = simple_hash("delete");
  739. hash_search = simple_hash("search");
  740. hash_switch = simple_hash("switch");
  741. hash_machine = simple_hash("machine");
  742. hash_url = simple_hash("url");
  743. hash_name = simple_hash("name");
  744. hash_delete_url = simple_hash("delete_url");
  745. hash_for = simple_hash("for");
  746. hash_to = simple_hash("to");
  747. /*
  748. hash_redirects = simple_hash("redirects");
  749. */
  750. }
  751. char person_guid[GUID_LEN + 1] = "";
  752. debug(D_WEB_CLIENT, "%llu: API v1 registry with URL '%s'", w->id, url);
  753. // TODO
  754. // The browser may send multiple cookies with our id
  755. char *cookie = strstr(w->response.data->buffer, NETDATA_REGISTRY_COOKIE_NAME "=");
  756. if(cookie)
  757. strncpyz(person_guid, &cookie[sizeof(NETDATA_REGISTRY_COOKIE_NAME)], 36);
  758. char action = '\0';
  759. char *machine_guid = NULL,
  760. *machine_url = NULL,
  761. *url_name = NULL,
  762. *search_machine_guid = NULL,
  763. *delete_url = NULL,
  764. *to_person_guid = NULL;
  765. /*
  766. int redirects = 0;
  767. */
  768. // Don't cache registry responses
  769. buffer_no_cacheable(w->response.data);
  770. while(url) {
  771. char *value = mystrsep(&url, "&");
  772. if (!value || !*value) continue;
  773. char *name = mystrsep(&value, "=");
  774. if (!name || !*name) continue;
  775. if (!value || !*value) continue;
  776. debug(D_WEB_CLIENT, "%llu: API v1 registry query param '%s' with value '%s'", w->id, name, value);
  777. uint32_t hash = simple_hash(name);
  778. if(hash == hash_action && !strcmp(name, "action")) {
  779. uint32_t vhash = simple_hash(value);
  780. if(vhash == hash_access && !strcmp(value, "access")) action = 'A';
  781. else if(vhash == hash_hello && !strcmp(value, "hello")) action = 'H';
  782. else if(vhash == hash_delete && !strcmp(value, "delete")) action = 'D';
  783. else if(vhash == hash_search && !strcmp(value, "search")) action = 'S';
  784. else if(vhash == hash_switch && !strcmp(value, "switch")) action = 'W';
  785. #ifdef NETDATA_INTERNAL_CHECKS
  786. else error("unknown registry action '%s'", value);
  787. #endif /* NETDATA_INTERNAL_CHECKS */
  788. }
  789. /*
  790. else if(hash == hash_redirects && !strcmp(name, "redirects"))
  791. redirects = atoi(value);
  792. */
  793. else if(hash == hash_machine && !strcmp(name, "machine"))
  794. machine_guid = value;
  795. else if(hash == hash_url && !strcmp(name, "url"))
  796. machine_url = value;
  797. else if(action == 'A') {
  798. if(hash == hash_name && !strcmp(name, "name"))
  799. url_name = value;
  800. }
  801. else if(action == 'D') {
  802. if(hash == hash_delete_url && !strcmp(name, "delete_url"))
  803. delete_url = value;
  804. }
  805. else if(action == 'S') {
  806. if(hash == hash_for && !strcmp(name, "for"))
  807. search_machine_guid = value;
  808. }
  809. else if(action == 'W') {
  810. if(hash == hash_to && !strcmp(name, "to"))
  811. to_person_guid = value;
  812. }
  813. #ifdef NETDATA_INTERNAL_CHECKS
  814. else error("unused registry URL parameter '%s' with value '%s'", name, value);
  815. #endif /* NETDATA_INTERNAL_CHECKS */
  816. }
  817. if(unlikely(respect_web_browser_do_not_track_policy && web_client_has_donottrack(w))) {
  818. buffer_flush(w->response.data);
  819. 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.");
  820. return HTTP_RESP_BAD_REQUEST;
  821. }
  822. if(unlikely(action == 'H')) {
  823. // HELLO request, dashboard ACL
  824. analytics_log_dashboard();
  825. if(unlikely(!web_client_can_access_dashboard(w)))
  826. return web_client_permission_denied(w);
  827. }
  828. else {
  829. // everything else, registry ACL
  830. if(unlikely(!web_client_can_access_registry(w)))
  831. return web_client_permission_denied(w);
  832. }
  833. switch(action) {
  834. case 'A':
  835. if(unlikely(!machine_guid || !machine_url || !url_name)) {
  836. 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");
  837. buffer_flush(w->response.data);
  838. buffer_strcat(w->response.data, "Invalid registry Access request.");
  839. return HTTP_RESP_BAD_REQUEST;
  840. }
  841. web_client_enable_tracking_required(w);
  842. return registry_request_access_json(host, w, person_guid, machine_guid, machine_url, url_name, now_realtime_sec());
  843. case 'D':
  844. if(unlikely(!machine_guid || !machine_url || !delete_url)) {
  845. 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");
  846. buffer_flush(w->response.data);
  847. buffer_strcat(w->response.data, "Invalid registry Delete request.");
  848. return HTTP_RESP_BAD_REQUEST;
  849. }
  850. web_client_enable_tracking_required(w);
  851. return registry_request_delete_json(host, w, person_guid, machine_guid, machine_url, delete_url, now_realtime_sec());
  852. case 'S':
  853. if(unlikely(!machine_guid || !machine_url || !search_machine_guid)) {
  854. 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");
  855. buffer_flush(w->response.data);
  856. buffer_strcat(w->response.data, "Invalid registry Search request.");
  857. return HTTP_RESP_BAD_REQUEST;
  858. }
  859. web_client_enable_tracking_required(w);
  860. return registry_request_search_json(host, w, person_guid, machine_guid, machine_url, search_machine_guid, now_realtime_sec());
  861. case 'W':
  862. if(unlikely(!machine_guid || !machine_url || !to_person_guid)) {
  863. 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");
  864. buffer_flush(w->response.data);
  865. buffer_strcat(w->response.data, "Invalid registry Switch request.");
  866. return HTTP_RESP_BAD_REQUEST;
  867. }
  868. web_client_enable_tracking_required(w);
  869. return registry_request_switch_json(host, w, person_guid, machine_guid, machine_url, to_person_guid, now_realtime_sec());
  870. case 'H':
  871. return registry_request_hello_json(host, w);
  872. default:
  873. buffer_flush(w->response.data);
  874. buffer_strcat(w->response.data, "Invalid registry request - you need to set an action: hello, access, delete, search");
  875. return HTTP_RESP_BAD_REQUEST;
  876. }
  877. }
  878. static inline void web_client_api_request_v1_info_summary_alarm_statuses(RRDHOST *host, BUFFER *wb) {
  879. int alarm_normal = 0, alarm_warn = 0, alarm_crit = 0;
  880. RRDCALC *rc;
  881. rrdhost_rdlock(host);
  882. for(rc = host->alarms; rc ; rc = rc->next) {
  883. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  884. continue;
  885. switch(rc->status) {
  886. case RRDCALC_STATUS_WARNING:
  887. alarm_warn++;
  888. break;
  889. case RRDCALC_STATUS_CRITICAL:
  890. alarm_crit++;
  891. break;
  892. default:
  893. alarm_normal++;
  894. }
  895. }
  896. rrdhost_unlock(host);
  897. buffer_sprintf(wb, "\t\t\"normal\": %d,\n", alarm_normal);
  898. buffer_sprintf(wb, "\t\t\"warning\": %d,\n", alarm_warn);
  899. buffer_sprintf(wb, "\t\t\"critical\": %d\n", alarm_crit);
  900. }
  901. static inline void web_client_api_request_v1_info_mirrored_hosts(BUFFER *wb) {
  902. RRDHOST *host;
  903. int count = 0;
  904. buffer_strcat(wb, "\t\"mirrored_hosts\": [\n");
  905. rrd_rdlock();
  906. rrdhost_foreach_read(host) {
  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 (count > 0)
  917. buffer_strcat(wb, ",\n");
  918. netdata_mutex_lock(&host->receiver_lock);
  919. buffer_sprintf(
  920. wb, "\t\t{ \"guid\": \"%s\", \"hostname\": \"%s\", \"reachable\": %s, \"hops\": %d"
  921. , host->machine_guid
  922. , host->hostname
  923. , (host->receiver || host == localhost) ? "true" : "false"
  924. , host->system_info ? host->system_info->hops : (host == localhost) ? 0 : 1
  925. );
  926. netdata_mutex_unlock(&host->receiver_lock);
  927. rrdhost_aclk_state_lock(host);
  928. if (host->aclk_state.claimed_id)
  929. buffer_sprintf(wb, ", \"claim_id\": \"%s\"", host->aclk_state.claimed_id);
  930. else
  931. buffer_strcat(wb, ", \"claim_id\": null");
  932. rrdhost_aclk_state_unlock(host);
  933. if (host->node_id) {
  934. char node_id_str[GUID_LEN + 1];
  935. uuid_unparse_lower(*host->node_id, node_id_str);
  936. buffer_sprintf(wb, ", \"node_id\": \"%s\" }", node_id_str);
  937. } else
  938. buffer_strcat(wb, ", \"node_id\": null }");
  939. count++;
  940. }
  941. rrd_unlock();
  942. buffer_strcat(wb, "\n\t],\n");
  943. }
  944. inline void host_labels2json(RRDHOST *host, BUFFER *wb, size_t indentation) {
  945. char tabs[11];
  946. if (indentation > 10)
  947. indentation = 10;
  948. tabs[0] = '\0';
  949. while (indentation) {
  950. strcat(tabs, "\t");
  951. indentation--;
  952. }
  953. rrdlabels_to_buffer(host->host_labels, wb, tabs, ":", "\"", ",\n", NULL, NULL, NULL, NULL);
  954. buffer_strcat(wb, "\n");
  955. }
  956. extern int aclk_connected;
  957. inline int web_client_api_request_v1_info_fill_buffer(RRDHOST *host, BUFFER *wb)
  958. {
  959. buffer_strcat(wb, "{\n");
  960. buffer_sprintf(wb, "\t\"version\": \"%s\",\n", host->program_version);
  961. buffer_sprintf(wb, "\t\"uid\": \"%s\",\n", host->machine_guid);
  962. web_client_api_request_v1_info_mirrored_hosts(wb);
  963. buffer_strcat(wb, "\t\"alarms\": {\n");
  964. web_client_api_request_v1_info_summary_alarm_statuses(host, wb);
  965. buffer_strcat(wb, "\t},\n");
  966. buffer_sprintf(wb, "\t\"os_name\": \"%s\",\n", (host->system_info->host_os_name) ? host->system_info->host_os_name : "");
  967. buffer_sprintf(wb, "\t\"os_id\": \"%s\",\n", (host->system_info->host_os_id) ? host->system_info->host_os_id : "");
  968. buffer_sprintf(wb, "\t\"os_id_like\": \"%s\",\n", (host->system_info->host_os_id_like) ? host->system_info->host_os_id_like : "");
  969. buffer_sprintf(wb, "\t\"os_version\": \"%s\",\n", (host->system_info->host_os_version) ? host->system_info->host_os_version : "");
  970. buffer_sprintf(wb, "\t\"os_version_id\": \"%s\",\n", (host->system_info->host_os_version_id) ? host->system_info->host_os_version_id : "");
  971. buffer_sprintf(wb, "\t\"os_detection\": \"%s\",\n", (host->system_info->host_os_detection) ? host->system_info->host_os_detection : "");
  972. buffer_sprintf(wb, "\t\"cores_total\": \"%s\",\n", (host->system_info->host_cores) ? host->system_info->host_cores : "");
  973. buffer_sprintf(wb, "\t\"total_disk_space\": \"%s\",\n", (host->system_info->host_disk_space) ? host->system_info->host_disk_space : "");
  974. buffer_sprintf(wb, "\t\"cpu_freq\": \"%s\",\n", (host->system_info->host_cpu_freq) ? host->system_info->host_cpu_freq : "");
  975. buffer_sprintf(wb, "\t\"ram_total\": \"%s\",\n", (host->system_info->host_ram_total) ? host->system_info->host_ram_total : "");
  976. if (host->system_info->container_os_name)
  977. buffer_sprintf(wb, "\t\"container_os_name\": \"%s\",\n", host->system_info->container_os_name);
  978. if (host->system_info->container_os_id)
  979. buffer_sprintf(wb, "\t\"container_os_id\": \"%s\",\n", host->system_info->container_os_id);
  980. if (host->system_info->container_os_id_like)
  981. buffer_sprintf(wb, "\t\"container_os_id_like\": \"%s\",\n", host->system_info->container_os_id_like);
  982. if (host->system_info->container_os_version)
  983. buffer_sprintf(wb, "\t\"container_os_version\": \"%s\",\n", host->system_info->container_os_version);
  984. if (host->system_info->container_os_version_id)
  985. buffer_sprintf(wb, "\t\"container_os_version_id\": \"%s\",\n", host->system_info->container_os_version_id);
  986. if (host->system_info->container_os_detection)
  987. buffer_sprintf(wb, "\t\"container_os_detection\": \"%s\",\n", host->system_info->container_os_detection);
  988. if (host->system_info->is_k8s_node)
  989. buffer_sprintf(wb, "\t\"is_k8s_node\": \"%s\",\n", host->system_info->is_k8s_node);
  990. buffer_sprintf(wb, "\t\"kernel_name\": \"%s\",\n", (host->system_info->kernel_name) ? host->system_info->kernel_name : "");
  991. buffer_sprintf(wb, "\t\"kernel_version\": \"%s\",\n", (host->system_info->kernel_version) ? host->system_info->kernel_version : "");
  992. buffer_sprintf(wb, "\t\"architecture\": \"%s\",\n", (host->system_info->architecture) ? host->system_info->architecture : "");
  993. buffer_sprintf(wb, "\t\"virtualization\": \"%s\",\n", (host->system_info->virtualization) ? host->system_info->virtualization : "");
  994. buffer_sprintf(wb, "\t\"virt_detection\": \"%s\",\n", (host->system_info->virt_detection) ? host->system_info->virt_detection : "");
  995. buffer_sprintf(wb, "\t\"container\": \"%s\",\n", (host->system_info->container) ? host->system_info->container : "");
  996. buffer_sprintf(wb, "\t\"container_detection\": \"%s\",\n", (host->system_info->container_detection) ? host->system_info->container_detection : "");
  997. if (host->system_info->cloud_provider_type)
  998. buffer_sprintf(wb, "\t\"cloud_provider_type\": \"%s\",\n", host->system_info->cloud_provider_type);
  999. if (host->system_info->cloud_instance_type)
  1000. buffer_sprintf(wb, "\t\"cloud_instance_type\": \"%s\",\n", host->system_info->cloud_instance_type);
  1001. if (host->system_info->cloud_instance_region)
  1002. buffer_sprintf(wb, "\t\"cloud_instance_region\": \"%s\",\n", host->system_info->cloud_instance_region);
  1003. buffer_strcat(wb, "\t\"host_labels\": {\n");
  1004. host_labels2json(host, wb, 2);
  1005. buffer_strcat(wb, "\t},\n");
  1006. buffer_strcat(wb, "\t\"collectors\": [");
  1007. chartcollectors2json(host, wb);
  1008. buffer_strcat(wb, "\n\t],\n");
  1009. #ifdef DISABLE_CLOUD
  1010. buffer_strcat(wb, "\t\"cloud-enabled\": false,\n");
  1011. #else
  1012. buffer_sprintf(wb, "\t\"cloud-enabled\": %s,\n",
  1013. appconfig_get_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "enabled", 1) ? "true" : "false");
  1014. #endif
  1015. #ifdef ENABLE_ACLK
  1016. buffer_strcat(wb, "\t\"cloud-available\": true,\n");
  1017. buffer_strcat(wb, "\t\"aclk-ng-available\": true,\n");
  1018. buffer_strcat(wb, "\t\"aclk-ng-new-cloud-protocol\": true,\n");
  1019. buffer_strcat(wb, "\t\"aclk-legacy-available\": false,\n");
  1020. buffer_strcat(wb, "\t\"aclk-implementation\": \"Next Generation\",\n");
  1021. #else
  1022. buffer_strcat(wb, "\t\"cloud-available\": false,\n");
  1023. buffer_strcat(wb, "\t\"aclk-ng-available\": false,\n");
  1024. buffer_strcat(wb, "\t\"aclk-legacy-available\": false,\n");
  1025. #endif
  1026. char *agent_id = get_agent_claimid();
  1027. if (agent_id == NULL)
  1028. buffer_strcat(wb, "\t\"agent-claimed\": false,\n");
  1029. else {
  1030. buffer_strcat(wb, "\t\"agent-claimed\": true,\n");
  1031. freez(agent_id);
  1032. }
  1033. #ifdef ENABLE_ACLK
  1034. if (aclk_connected) {
  1035. buffer_strcat(wb, "\t\"aclk-available\": true,\n");
  1036. buffer_strcat(wb, "\t\"aclk-available-protocol\": \"New\",\n");
  1037. }
  1038. else
  1039. #endif
  1040. buffer_strcat(wb, "\t\"aclk-available\": false,\n\t\"aclk-available-protocol\": null,\n"); // Intentionally valid with/without #ifdef above
  1041. buffer_strcat(wb, "\t\"memory-mode\": ");
  1042. analytics_get_data(analytics_data.netdata_config_memory_mode, wb);
  1043. buffer_strcat(wb, ",\n");
  1044. buffer_strcat(wb, "\t\"multidb-disk-quota\": ");
  1045. analytics_get_data(analytics_data.netdata_config_multidb_disk_quota, wb);
  1046. buffer_strcat(wb, ",\n");
  1047. buffer_strcat(wb, "\t\"page-cache-size\": ");
  1048. analytics_get_data(analytics_data.netdata_config_page_cache_size, wb);
  1049. buffer_strcat(wb, ",\n");
  1050. buffer_strcat(wb, "\t\"stream-enabled\": ");
  1051. analytics_get_data(analytics_data.netdata_config_stream_enabled, wb);
  1052. buffer_strcat(wb, ",\n");
  1053. #ifdef ENABLE_COMPRESSION
  1054. if(host->sender){
  1055. buffer_strcat(wb, "\t\"stream-compression\": ");
  1056. buffer_strcat(wb, (host->sender->rrdpush_compression ? "true" : "false"));
  1057. buffer_strcat(wb, ",\n");
  1058. }else{
  1059. buffer_strcat(wb, "\t\"stream-compression\": null,\n");
  1060. }
  1061. #else
  1062. buffer_strcat(wb, "\t\"stream-compression\": null,\n");
  1063. #endif //ENABLE_COMPRESSION
  1064. buffer_strcat(wb, "\t\"hosts-available\": ");
  1065. analytics_get_data(analytics_data.netdata_config_hosts_available, wb);
  1066. buffer_strcat(wb, ",\n");
  1067. buffer_strcat(wb, "\t\"https-enabled\": ");
  1068. analytics_get_data(analytics_data.netdata_config_https_enabled, wb);
  1069. buffer_strcat(wb, ",\n");
  1070. buffer_strcat(wb, "\t\"buildinfo\": ");
  1071. analytics_get_data(analytics_data.netdata_buildinfo, wb);
  1072. buffer_strcat(wb, ",\n");
  1073. buffer_strcat(wb, "\t\"release-channel\": ");
  1074. analytics_get_data(analytics_data.netdata_config_release_channel, wb);
  1075. buffer_strcat(wb, ",\n");
  1076. buffer_strcat(wb, "\t\"web-enabled\": ");
  1077. analytics_get_data(analytics_data.netdata_config_web_enabled, wb);
  1078. buffer_strcat(wb, ",\n");
  1079. buffer_strcat(wb, "\t\"notification-methods\": ");
  1080. analytics_get_data(analytics_data.netdata_notification_methods, wb);
  1081. buffer_strcat(wb, ",\n");
  1082. buffer_strcat(wb, "\t\"exporting-enabled\": ");
  1083. analytics_get_data(analytics_data.netdata_config_exporting_enabled, wb);
  1084. buffer_strcat(wb, ",\n");
  1085. buffer_strcat(wb, "\t\"exporting-connectors\": ");
  1086. analytics_get_data(analytics_data.netdata_exporting_connectors, wb);
  1087. buffer_strcat(wb, ",\n");
  1088. buffer_strcat(wb, "\t\"allmetrics-prometheus-used\": ");
  1089. analytics_get_data(analytics_data.netdata_allmetrics_prometheus_used, wb);
  1090. buffer_strcat(wb, ",\n");
  1091. buffer_strcat(wb, "\t\"allmetrics-shell-used\": ");
  1092. analytics_get_data(analytics_data.netdata_allmetrics_shell_used, wb);
  1093. buffer_strcat(wb, ",\n");
  1094. buffer_strcat(wb, "\t\"allmetrics-json-used\": ");
  1095. analytics_get_data(analytics_data.netdata_allmetrics_json_used, wb);
  1096. buffer_strcat(wb, ",\n");
  1097. buffer_strcat(wb, "\t\"dashboard-used\": ");
  1098. analytics_get_data(analytics_data.netdata_dashboard_used, wb);
  1099. buffer_strcat(wb, ",\n");
  1100. buffer_strcat(wb, "\t\"charts-count\": ");
  1101. analytics_get_data(analytics_data.netdata_charts_count, wb);
  1102. buffer_strcat(wb, ",\n");
  1103. buffer_strcat(wb, "\t\"metrics-count\": ");
  1104. analytics_get_data(analytics_data.netdata_metrics_count, wb);
  1105. #if defined(ENABLE_ML)
  1106. buffer_strcat(wb, ",\n");
  1107. char *ml_info = ml_get_host_info(host);
  1108. buffer_strcat(wb, "\t\"ml-info\": ");
  1109. buffer_strcat(wb, ml_info);
  1110. free(ml_info);
  1111. #endif
  1112. buffer_strcat(wb, "\n}");
  1113. return 0;
  1114. }
  1115. #if defined(ENABLE_ML)
  1116. int web_client_api_request_v1_anomaly_events(RRDHOST *host, struct web_client *w, char *url) {
  1117. if (!netdata_ready)
  1118. return HTTP_RESP_BACKEND_FETCH_FAILED;
  1119. uint32_t after = 0, before = 0;
  1120. while (url) {
  1121. char *value = mystrsep(&url, "&");
  1122. if (!value || !*value)
  1123. continue;
  1124. char *name = mystrsep(&value, "=");
  1125. if (!name || !*name)
  1126. continue;
  1127. if (!value || !*value)
  1128. continue;
  1129. if (!strcmp(name, "after"))
  1130. after = (uint32_t) (strtoul(value, NULL, 0) / 1000);
  1131. else if (!strcmp(name, "before"))
  1132. before = (uint32_t) (strtoul(value, NULL, 0) / 1000);
  1133. }
  1134. char *s;
  1135. if (!before || !after)
  1136. s = strdupz("{\"error\": \"missing after/before parameters\" }\n");
  1137. else {
  1138. s = ml_get_anomaly_events(host, "AD1", 1, after, before);
  1139. if (!s)
  1140. s = strdupz("{\"error\": \"json string is empty\" }\n");
  1141. }
  1142. BUFFER *wb = w->response.data;
  1143. buffer_flush(wb);
  1144. wb->contenttype = CT_APPLICATION_JSON;
  1145. buffer_strcat(wb, s);
  1146. buffer_no_cacheable(wb);
  1147. freez(s);
  1148. return HTTP_RESP_OK;
  1149. }
  1150. int web_client_api_request_v1_anomaly_event_info(RRDHOST *host, struct web_client *w, char *url) {
  1151. if (!netdata_ready)
  1152. return HTTP_RESP_BACKEND_FETCH_FAILED;
  1153. uint32_t after = 0, before = 0;
  1154. while (url) {
  1155. char *value = mystrsep(&url, "&");
  1156. if (!value || !*value)
  1157. continue;
  1158. char *name = mystrsep(&value, "=");
  1159. if (!name || !*name)
  1160. continue;
  1161. if (!value || !*value)
  1162. continue;
  1163. if (!strcmp(name, "after"))
  1164. after = (uint32_t) strtoul(value, NULL, 0);
  1165. else if (!strcmp(name, "before"))
  1166. before = (uint32_t) strtoul(value, NULL, 0);
  1167. }
  1168. char *s;
  1169. if (!before || !after)
  1170. s = strdupz("{\"error\": \"missing after/before parameters\" }\n");
  1171. else {
  1172. s = ml_get_anomaly_event_info(host, "AD1", 1, after, before);
  1173. if (!s)
  1174. s = strdupz("{\"error\": \"json string is empty\" }\n");
  1175. }
  1176. BUFFER *wb = w->response.data;
  1177. buffer_flush(wb);
  1178. wb->contenttype = CT_APPLICATION_JSON;
  1179. buffer_strcat(wb, s);
  1180. buffer_no_cacheable(wb);
  1181. freez(s);
  1182. return HTTP_RESP_OK;
  1183. }
  1184. int web_client_api_request_v1_ml_info(RRDHOST *host, struct web_client *w, char *url) {
  1185. (void) url;
  1186. if (!netdata_ready)
  1187. return HTTP_RESP_BACKEND_FETCH_FAILED;
  1188. char *s = ml_get_host_runtime_info(host);
  1189. if (!s)
  1190. s = strdupz("{\"error\": \"json string is empty\" }\n");
  1191. BUFFER *wb = w->response.data;
  1192. buffer_flush(wb);
  1193. wb->contenttype = CT_APPLICATION_JSON;
  1194. buffer_strcat(wb, s);
  1195. buffer_no_cacheable(wb);
  1196. freez(s);
  1197. return HTTP_RESP_OK;
  1198. }
  1199. #endif // defined(ENABLE_ML)
  1200. inline int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, char *url) {
  1201. (void)url;
  1202. if (!netdata_ready) return HTTP_RESP_BACKEND_FETCH_FAILED;
  1203. BUFFER *wb = w->response.data;
  1204. buffer_flush(wb);
  1205. wb->contenttype = CT_APPLICATION_JSON;
  1206. web_client_api_request_v1_info_fill_buffer(host, wb);
  1207. buffer_no_cacheable(wb);
  1208. return HTTP_RESP_OK;
  1209. }
  1210. static int web_client_api_request_v1_aclk_state(RRDHOST *host, struct web_client *w, char *url) {
  1211. UNUSED(url);
  1212. UNUSED(host);
  1213. if (!netdata_ready) return HTTP_RESP_BACKEND_FETCH_FAILED;
  1214. BUFFER *wb = w->response.data;
  1215. buffer_flush(wb);
  1216. char *str = aclk_state_json();
  1217. buffer_strcat(wb, str);
  1218. freez(str);
  1219. wb->contenttype = CT_APPLICATION_JSON;
  1220. buffer_no_cacheable(wb);
  1221. return HTTP_RESP_OK;
  1222. }
  1223. static int web_client_api_request_v1_weights_internal(RRDHOST *host, struct web_client *w, char *url, WEIGHTS_METHOD method, WEIGHTS_FORMAT format) {
  1224. if (!netdata_ready)
  1225. return HTTP_RESP_BACKEND_FETCH_FAILED;
  1226. long long baseline_after = 0, baseline_before = 0, after = 0, before = 0, points = 0;
  1227. RRDR_OPTIONS options = RRDR_OPTION_NOT_ALIGNED | RRDR_OPTION_NONZERO | RRDR_OPTION_NULL2ZERO;
  1228. int options_count = 0;
  1229. RRDR_GROUPING group = RRDR_GROUPING_AVERAGE;
  1230. int timeout = 0;
  1231. int tier = 0;
  1232. const char *group_options = NULL, *contexts_str = NULL;
  1233. while (url) {
  1234. char *value = mystrsep(&url, "&");
  1235. if (!value || !*value)
  1236. continue;
  1237. char *name = mystrsep(&value, "=");
  1238. if (!name || !*name)
  1239. continue;
  1240. if (!value || !*value)
  1241. continue;
  1242. if (!strcmp(name, "baseline_after"))
  1243. baseline_after = (long long) strtoul(value, NULL, 0);
  1244. else if (!strcmp(name, "baseline_before"))
  1245. baseline_before = (long long) strtoul(value, NULL, 0);
  1246. else if (!strcmp(name, "after") || !strcmp(name, "highlight_after"))
  1247. after = (long long) strtoul(value, NULL, 0);
  1248. else if (!strcmp(name, "before") || !strcmp(name, "highlight_before"))
  1249. before = (long long) strtoul(value, NULL, 0);
  1250. else if (!strcmp(name, "points") || !strcmp(name, "max_points"))
  1251. points = (long long) strtoul(value, NULL, 0);
  1252. else if (!strcmp(name, "timeout"))
  1253. timeout = (int) strtoul(value, NULL, 0);
  1254. else if(!strcmp(name, "group"))
  1255. group = web_client_api_request_v1_data_group(value, RRDR_GROUPING_AVERAGE);
  1256. else if(!strcmp(name, "options")) {
  1257. if(!options_count) options = RRDR_OPTION_NOT_ALIGNED | RRDR_OPTION_NULL2ZERO;
  1258. options |= web_client_api_request_v1_data_options(value);
  1259. options_count++;
  1260. }
  1261. else if(!strcmp(name, "method"))
  1262. method = weights_string_to_method(value);
  1263. else if(!strcmp(name, "context") || !strcmp(name, "contexts"))
  1264. contexts_str = value;
  1265. else if(!strcmp(name, "tier")) {
  1266. tier = str2i(value);
  1267. if(tier >= 0 && tier < storage_tiers)
  1268. options |= RRDR_OPTION_SELECTED_TIER;
  1269. }
  1270. }
  1271. BUFFER *wb = w->response.data;
  1272. buffer_flush(wb);
  1273. wb->contenttype = CT_APPLICATION_JSON;
  1274. SIMPLE_PATTERN *contexts = (contexts_str) ? simple_pattern_create(contexts_str, ",|\t\r\n\f\v", SIMPLE_PATTERN_EXACT) : NULL;
  1275. int ret = web_api_v1_weights(host, wb, method, format, group, group_options, baseline_after, baseline_before, after, before, points, options, contexts, tier, timeout);
  1276. simple_pattern_free(contexts);
  1277. return ret;
  1278. }
  1279. int web_client_api_request_v1_metric_correlations(RRDHOST *host, struct web_client *w, char *url) {
  1280. return web_client_api_request_v1_weights_internal(host, w, url, default_metric_correlations_method, WEIGHTS_FORMAT_CHARTS);
  1281. }
  1282. int web_client_api_request_v1_weights(RRDHOST *host, struct web_client *w, char *url) {
  1283. return web_client_api_request_v1_weights_internal(host, w, url, WEIGHTS_METHOD_ANOMALY_RATE, WEIGHTS_FORMAT_CONTEXTS);
  1284. }
  1285. #ifndef ENABLE_DBENGINE
  1286. int web_client_api_request_v1_dbengine_stats(RRDHOST *host, struct web_client *w, char *url) {
  1287. return HTTP_RESP_NOT_FOUND;
  1288. }
  1289. #else
  1290. static void web_client_api_v1_dbengine_stats_for_tier(BUFFER *wb, int tier) {
  1291. RRDENG_SIZE_STATS stats = rrdeng_size_statistics(multidb_ctx[tier]);
  1292. buffer_sprintf(wb,
  1293. "\n\t\t\"default_granularity_secs\":%zu"
  1294. ",\n\t\t\"sizeof_metric\":%zu"
  1295. ",\n\t\t\"sizeof_metric_in_index\":%zu"
  1296. ",\n\t\t\"sizeof_page\":%zu"
  1297. ",\n\t\t\"sizeof_page_in_index\":%zu"
  1298. ",\n\t\t\"sizeof_extent\":%zu"
  1299. ",\n\t\t\"sizeof_page_in_extent\":%zu"
  1300. ",\n\t\t\"sizeof_datafile\":%zu"
  1301. ",\n\t\t\"sizeof_page_in_cache\":%zu"
  1302. ",\n\t\t\"sizeof_point_data\":%zu"
  1303. ",\n\t\t\"sizeof_page_data\":%zu"
  1304. ",\n\t\t\"pages_per_extent\":%zu"
  1305. ",\n\t\t\"datafiles\":%zu"
  1306. ",\n\t\t\"extents\":%zu"
  1307. ",\n\t\t\"extents_pages\":%zu"
  1308. ",\n\t\t\"points\":%zu"
  1309. ",\n\t\t\"metrics\":%zu"
  1310. ",\n\t\t\"metrics_pages\":%zu"
  1311. ",\n\t\t\"extents_compressed_bytes\":%zu"
  1312. ",\n\t\t\"pages_uncompressed_bytes\":%zu"
  1313. ",\n\t\t\"pages_duration_secs\":%ld"
  1314. ",\n\t\t\"single_point_pages\":%zu"
  1315. ",\n\t\t\"first_t\":%llu"
  1316. ",\n\t\t\"last_t\":%llu"
  1317. ",\n\t\t\"database_retention_secs\":%ld"
  1318. ",\n\t\t\"average_compression_savings\":%0.2f"
  1319. ",\n\t\t\"average_point_duration_secs\":%0.2f"
  1320. ",\n\t\t\"average_metric_retention_secs\":%0.2f"
  1321. ",\n\t\t\"ephemeral_metrics_per_day_percent\":%0.2f"
  1322. ",\n\t\t\"average_page_size_bytes\":%0.2f"
  1323. ",\n\t\t\"estimated_concurrently_collected_metrics\":%zu"
  1324. ",\n\t\t\"currently_collected_metrics\":%zu"
  1325. ",\n\t\t\"max_concurrently_collected_metrics\":%zu"
  1326. ",\n\t\t\"disk_space\":%zu"
  1327. ",\n\t\t\"max_disk_space\":%zu"
  1328. , stats.default_granularity_secs
  1329. , stats.sizeof_metric
  1330. , stats.sizeof_metric_in_index
  1331. , stats.sizeof_page
  1332. , stats.sizeof_page_in_index
  1333. , stats.sizeof_extent
  1334. , stats.sizeof_page_in_extent
  1335. , stats.sizeof_datafile
  1336. , stats.sizeof_page_in_cache
  1337. , stats.sizeof_point_data
  1338. , stats.sizeof_page_data
  1339. , stats.pages_per_extent
  1340. , stats.datafiles
  1341. , stats.extents
  1342. , stats.extents_pages
  1343. , stats.points
  1344. , stats.metrics
  1345. , stats.metrics_pages
  1346. , stats.extents_compressed_bytes
  1347. , stats.pages_uncompressed_bytes
  1348. , stats.pages_duration_secs
  1349. , stats.single_point_pages
  1350. , stats.first_t
  1351. , stats.last_t
  1352. , stats.database_retention_secs
  1353. , stats.average_compression_savings
  1354. , stats.average_point_duration_secs
  1355. , stats.average_metric_retention_secs
  1356. , stats.ephemeral_metrics_per_day_percent
  1357. , stats.average_page_size_bytes
  1358. , stats.estimated_concurrently_collected_metrics
  1359. , stats.currently_collected_metrics
  1360. , stats.max_concurrently_collected_metrics
  1361. , stats.disk_space
  1362. , stats.max_disk_space
  1363. );
  1364. }
  1365. int web_client_api_request_v1_dbengine_stats(RRDHOST *host __maybe_unused, struct web_client *w, char *url __maybe_unused) {
  1366. if (!netdata_ready)
  1367. return HTTP_RESP_BACKEND_FETCH_FAILED;
  1368. BUFFER *wb = w->response.data;
  1369. buffer_flush(wb);
  1370. wb->contenttype = CT_APPLICATION_JSON;
  1371. buffer_no_cacheable(wb);
  1372. buffer_strcat(wb, "{");
  1373. for(int tier = 0; tier < storage_tiers ;tier++) {
  1374. buffer_sprintf(wb, "%s\n\t\"tier%d\": {", tier?",":"", tier);
  1375. web_client_api_v1_dbengine_stats_for_tier(wb, tier);
  1376. buffer_strcat(wb, "\n\t}");
  1377. }
  1378. buffer_strcat(wb, "\n}");
  1379. return HTTP_RESP_OK;
  1380. }
  1381. #endif
  1382. static struct api_command {
  1383. const char *command;
  1384. uint32_t hash;
  1385. WEB_CLIENT_ACL acl;
  1386. int (*callback)(RRDHOST *host, struct web_client *w, char *url);
  1387. } api_commands[] = {
  1388. { "info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_info },
  1389. { "data", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_data },
  1390. { "chart", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_chart },
  1391. { "charts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_charts },
  1392. { "context", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_context },
  1393. { "contexts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_contexts },
  1394. { "archivedcharts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_archivedcharts },
  1395. // registry checks the ACL by itself, so we allow everything
  1396. { "registry", 0, WEB_CLIENT_ACL_NOCHECK, web_client_api_request_v1_registry },
  1397. // badges can be fetched with both dashboard and badge permissions
  1398. { "badge.svg", 0, WEB_CLIENT_ACL_DASHBOARD|WEB_CLIENT_ACL_BADGE, web_client_api_request_v1_badge },
  1399. { "alarms", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarms },
  1400. { "alarms_values", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarms_values },
  1401. { "alarm_log", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_log },
  1402. { "alarm_variables", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_variables },
  1403. { "alarm_count", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_count },
  1404. { "allmetrics", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_allmetrics },
  1405. #if defined(ENABLE_ML)
  1406. { "anomaly_events", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_anomaly_events },
  1407. { "anomaly_event_info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_anomaly_event_info },
  1408. { "ml_info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_ml_info },
  1409. #endif
  1410. { "manage/health", 0, WEB_CLIENT_ACL_MGMT, web_client_api_request_v1_mgmt_health },
  1411. { "aclk", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_aclk_state },
  1412. { "metric_correlations", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_metric_correlations },
  1413. { "weights", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_weights },
  1414. { "dbengine_stats", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_dbengine_stats },
  1415. // terminator
  1416. { NULL, 0, WEB_CLIENT_ACL_NONE, NULL },
  1417. };
  1418. inline int web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *url) {
  1419. static int initialized = 0;
  1420. int i;
  1421. if(unlikely(initialized == 0)) {
  1422. initialized = 1;
  1423. for(i = 0; api_commands[i].command ; i++)
  1424. api_commands[i].hash = simple_hash(api_commands[i].command);
  1425. }
  1426. // get the command
  1427. if(url) {
  1428. debug(D_WEB_CLIENT, "%llu: Searching for API v1 command '%s'.", w->id, url);
  1429. uint32_t hash = simple_hash(url);
  1430. for(i = 0; api_commands[i].command ;i++) {
  1431. if(unlikely(hash == api_commands[i].hash && !strcmp(url, api_commands[i].command))) {
  1432. if(unlikely(api_commands[i].acl != WEB_CLIENT_ACL_NOCHECK) && !(w->acl & api_commands[i].acl))
  1433. return web_client_permission_denied(w);
  1434. //return api_commands[i].callback(host, w, url);
  1435. return api_commands[i].callback(host, w, (w->decoded_query_string + 1));
  1436. }
  1437. }
  1438. buffer_flush(w->response.data);
  1439. buffer_strcat(w->response.data, "Unsupported v1 API command: ");
  1440. buffer_strcat_htmlescape(w->response.data, url);
  1441. return HTTP_RESP_NOT_FOUND;
  1442. }
  1443. else {
  1444. buffer_flush(w->response.data);
  1445. buffer_sprintf(w->response.data, "Which API v1 command?");
  1446. return HTTP_RESP_BAD_REQUEST;
  1447. }
  1448. }