web_api_v1.c 54 KB

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