web_api_v1.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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. uint32_t format = DATASOURCE_JSON;
  345. uint32_t options = 0x00000000;
  346. while(url) {
  347. char *value = mystrsep(&url, "&");
  348. if(!value || !*value) continue;
  349. char *name = mystrsep(&value, "=");
  350. if(!name || !*name) continue;
  351. if(!value || !*value) continue;
  352. debug(D_WEB_CLIENT, "%llu: API v1 data query param '%s' with value '%s'", w->id, name, value);
  353. // name and value are now the parameters
  354. // they are not null and not empty
  355. if(!strcmp(name, "context")) context = value;
  356. else if(!strcmp(name, "chart_label_key")) chart_label_key = value;
  357. else if(!strcmp(name, "chart_labels_filter")) chart_labels_filter = value;
  358. else if(!strcmp(name, "chart")) chart = value;
  359. else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
  360. if(!dimensions) dimensions = buffer_create(100);
  361. buffer_strcat(dimensions, "|");
  362. buffer_strcat(dimensions, value);
  363. }
  364. else if(!strcmp(name, "after")) after_str = value;
  365. else if(!strcmp(name, "before")) before_str = value;
  366. else if(!strcmp(name, "points")) points_str = value;
  367. else if(!strcmp(name, "timeout")) timeout_str = value;
  368. else if(!strcmp(name, "gtime")) group_time_str = value;
  369. else if(!strcmp(name, "group")) {
  370. group = web_client_api_request_v1_data_group(value, RRDR_GROUPING_AVERAGE);
  371. }
  372. else if(!strcmp(name, "format")) {
  373. format = web_client_api_request_v1_data_format(value);
  374. }
  375. else if(!strcmp(name, "options")) {
  376. options |= web_client_api_request_v1_data_options(value);
  377. }
  378. else if(!strcmp(name, "callback")) {
  379. responseHandler = value;
  380. }
  381. else if(!strcmp(name, "filename")) {
  382. outFileName = value;
  383. }
  384. else if(!strcmp(name, "tqx")) {
  385. // parse Google Visualization API options
  386. // https://developers.google.com/chart/interactive/docs/dev/implementing_data_source
  387. char *tqx_name, *tqx_value;
  388. while(value) {
  389. tqx_value = mystrsep(&value, ";");
  390. if(!tqx_value || !*tqx_value) continue;
  391. tqx_name = mystrsep(&tqx_value, ":");
  392. if(!tqx_name || !*tqx_name) continue;
  393. if(!tqx_value || !*tqx_value) continue;
  394. if(!strcmp(tqx_name, "version"))
  395. google_version = tqx_value;
  396. else if(!strcmp(tqx_name, "reqId"))
  397. google_reqId = tqx_value;
  398. else if(!strcmp(tqx_name, "sig")) {
  399. google_sig = tqx_value;
  400. google_timestamp = strtoul(google_sig, NULL, 0);
  401. }
  402. else if(!strcmp(tqx_name, "out")) {
  403. google_out = tqx_value;
  404. format = web_client_api_request_v1_data_google_format(google_out);
  405. }
  406. else if(!strcmp(tqx_name, "responseHandler"))
  407. responseHandler = tqx_value;
  408. else if(!strcmp(tqx_name, "outFileName"))
  409. outFileName = tqx_value;
  410. }
  411. }
  412. else if(!strcmp(name, "max_anomaly_rates")) {
  413. max_anomaly_rates_str = value;
  414. }
  415. }
  416. // validate the google parameters given
  417. fix_google_param(google_out);
  418. fix_google_param(google_sig);
  419. fix_google_param(google_reqId);
  420. fix_google_param(google_version);
  421. fix_google_param(responseHandler);
  422. fix_google_param(outFileName);
  423. RRDSET *st = NULL;
  424. if((!chart || !*chart) && (!context)) {
  425. buffer_sprintf(w->response.data, "No chart id is given at the request.");
  426. goto cleanup;
  427. }
  428. struct context_param *context_param_list = NULL;
  429. if (context && !chart) {
  430. RRDSET *st1;
  431. uint32_t context_hash = simple_hash(context);
  432. rrdhost_rdlock(host);
  433. char *words[MAX_CHART_LABELS_FILTER];
  434. uint32_t hash_key_list[MAX_CHART_LABELS_FILTER];
  435. int word_count = 0;
  436. rrdset_foreach_read(st1, host) {
  437. if (st1->hash_context == context_hash && !strcmp(st1->context, context) &&
  438. (!chart_label_key || rrdset_contains_label_keylist(st1, chart_label_key)) &&
  439. (!chart_labels_filter ||
  440. rrdset_matches_label_keys(st1, chart_labels_filter, words, hash_key_list, &word_count, MAX_CHART_LABELS_FILTER)))
  441. build_context_param_list(&context_param_list, st1);
  442. }
  443. rrdhost_unlock(host);
  444. if (likely(context_param_list && context_param_list->rd)) // Just set the first one
  445. st = context_param_list->rd->rrdset;
  446. else {
  447. if (!chart_label_key && !chart_labels_filter)
  448. sql_build_context_param_list(&context_param_list, host, context, NULL);
  449. }
  450. }
  451. else {
  452. st = rrdset_find(host, chart);
  453. if (!st)
  454. st = rrdset_find_byname(host, chart);
  455. if (likely(st))
  456. st->last_accessed_time = now_realtime_sec();
  457. else
  458. sql_build_context_param_list(&context_param_list, host, NULL, chart);
  459. }
  460. if (!st) {
  461. if (likely(context_param_list && context_param_list->rd && context_param_list->rd->rrdset))
  462. st = context_param_list->rd->rrdset;
  463. else {
  464. free_context_param_list(&context_param_list);
  465. context_param_list = NULL;
  466. }
  467. }
  468. if (!st && !context_param_list) {
  469. if (context && !chart) {
  470. if (!chart_label_key) {
  471. buffer_strcat(w->response.data, "Context is not found: ");
  472. buffer_strcat_htmlescape(w->response.data, context);
  473. } else {
  474. buffer_strcat(w->response.data, "Context: ");
  475. buffer_strcat_htmlescape(w->response.data, context);
  476. buffer_strcat(w->response.data, " or chart label key: ");
  477. buffer_strcat_htmlescape(w->response.data, chart_label_key);
  478. buffer_strcat(w->response.data, " not found");
  479. }
  480. }
  481. else {
  482. buffer_strcat(w->response.data, "Chart is not found: ");
  483. buffer_strcat_htmlescape(w->response.data, chart);
  484. }
  485. ret = HTTP_RESP_NOT_FOUND;
  486. goto cleanup;
  487. }
  488. long long before = (before_str && *before_str)?str2l(before_str):0;
  489. long long after = (after_str && *after_str) ?str2l(after_str):-600;
  490. int points = (points_str && *points_str)?str2i(points_str):0;
  491. int timeout = (timeout_str && *timeout_str)?str2i(timeout_str): 0;
  492. long group_time = (group_time_str && *group_time_str)?str2l(group_time_str):0;
  493. int max_anomaly_rates = (max_anomaly_rates_str && *max_anomaly_rates_str) ? str2i(max_anomaly_rates_str) : 0;
  494. 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'"
  495. , w->id
  496. , chart
  497. , (dimensions)?buffer_tostring(dimensions):""
  498. , after
  499. , before
  500. , points
  501. , group
  502. , format
  503. , options
  504. );
  505. if(outFileName && *outFileName) {
  506. buffer_sprintf(w->response.header, "Content-Disposition: attachment; filename=\"%s\"\r\n", outFileName);
  507. debug(D_WEB_CLIENT, "%llu: generating outfilename header: '%s'", w->id, outFileName);
  508. }
  509. if(format == DATASOURCE_DATATABLE_JSONP) {
  510. if(responseHandler == NULL)
  511. responseHandler = "google.visualization.Query.setResponse";
  512. debug(D_WEB_CLIENT_ACCESS, "%llu: GOOGLE JSON/JSONP: version = '%s', reqId = '%s', sig = '%s', out = '%s', responseHandler = '%s', outFileName = '%s'",
  513. w->id, google_version, google_reqId, google_sig, google_out, responseHandler, outFileName
  514. );
  515. buffer_sprintf(
  516. w->response.data,
  517. "%s({version:'%s',reqId:'%s',status:'ok',sig:'%"PRId64"',table:",
  518. responseHandler,
  519. google_version,
  520. google_reqId,
  521. (int64_t)st->last_updated.tv_sec);
  522. }
  523. else if(format == DATASOURCE_JSONP) {
  524. if(responseHandler == NULL)
  525. responseHandler = "callback";
  526. buffer_strcat(w->response.data, responseHandler);
  527. buffer_strcat(w->response.data, "(");
  528. }
  529. ret = rrdset2anything_api_v1(st, w->response.data, dimensions, format,
  530. points, after, before, group, group_time,
  531. options, &last_timestamp_in_data, context_param_list,
  532. chart_label_key, max_anomaly_rates, timeout);
  533. free_context_param_list(&context_param_list);
  534. if(format == DATASOURCE_DATATABLE_JSONP) {
  535. if(google_timestamp < last_timestamp_in_data)
  536. buffer_strcat(w->response.data, "});");
  537. else {
  538. // the client already has the latest data
  539. buffer_flush(w->response.data);
  540. buffer_sprintf(w->response.data,
  541. "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'not_modified',message:'Data not modified'}]});",
  542. responseHandler, google_version, google_reqId);
  543. }
  544. }
  545. else if(format == DATASOURCE_JSONP)
  546. buffer_strcat(w->response.data, ");");
  547. cleanup:
  548. buffer_free(dimensions);
  549. return ret;
  550. }
  551. // Pings a netdata server:
  552. // /api/v1/registry?action=hello
  553. //
  554. // Access to a netdata registry:
  555. // /api/v1/registry?action=access&machine=${machine_guid}&name=${hostname}&url=${url}
  556. //
  557. // Delete from a netdata registry:
  558. // /api/v1/registry?action=delete&machine=${machine_guid}&name=${hostname}&url=${url}&delete_url=${delete_url}
  559. //
  560. // Search for the URLs of a machine:
  561. // /api/v1/registry?action=search&machine=${machine_guid}&name=${hostname}&url=${url}&for=${machine_guid}
  562. //
  563. // Impersonate:
  564. // /api/v1/registry?action=switch&machine=${machine_guid}&name=${hostname}&url=${url}&to=${new_person_guid}
  565. inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *w, char *url) {
  566. static uint32_t hash_action = 0, hash_access = 0, hash_hello = 0, hash_delete = 0, hash_search = 0,
  567. hash_switch = 0, hash_machine = 0, hash_url = 0, hash_name = 0, hash_delete_url = 0, hash_for = 0,
  568. hash_to = 0 /*, hash_redirects = 0 */;
  569. if(unlikely(!hash_action)) {
  570. hash_action = simple_hash("action");
  571. hash_access = simple_hash("access");
  572. hash_hello = simple_hash("hello");
  573. hash_delete = simple_hash("delete");
  574. hash_search = simple_hash("search");
  575. hash_switch = simple_hash("switch");
  576. hash_machine = simple_hash("machine");
  577. hash_url = simple_hash("url");
  578. hash_name = simple_hash("name");
  579. hash_delete_url = simple_hash("delete_url");
  580. hash_for = simple_hash("for");
  581. hash_to = simple_hash("to");
  582. /*
  583. hash_redirects = simple_hash("redirects");
  584. */
  585. }
  586. char person_guid[GUID_LEN + 1] = "";
  587. debug(D_WEB_CLIENT, "%llu: API v1 registry with URL '%s'", w->id, url);
  588. // TODO
  589. // The browser may send multiple cookies with our id
  590. char *cookie = strstr(w->response.data->buffer, NETDATA_REGISTRY_COOKIE_NAME "=");
  591. if(cookie)
  592. strncpyz(person_guid, &cookie[sizeof(NETDATA_REGISTRY_COOKIE_NAME)], 36);
  593. char action = '\0';
  594. char *machine_guid = NULL,
  595. *machine_url = NULL,
  596. *url_name = NULL,
  597. *search_machine_guid = NULL,
  598. *delete_url = NULL,
  599. *to_person_guid = NULL;
  600. /*
  601. int redirects = 0;
  602. */
  603. // Don't cache registry responses
  604. buffer_no_cacheable(w->response.data);
  605. while(url) {
  606. char *value = mystrsep(&url, "&");
  607. if (!value || !*value) continue;
  608. char *name = mystrsep(&value, "=");
  609. if (!name || !*name) continue;
  610. if (!value || !*value) continue;
  611. debug(D_WEB_CLIENT, "%llu: API v1 registry query param '%s' with value '%s'", w->id, name, value);
  612. uint32_t hash = simple_hash(name);
  613. if(hash == hash_action && !strcmp(name, "action")) {
  614. uint32_t vhash = simple_hash(value);
  615. if(vhash == hash_access && !strcmp(value, "access")) action = 'A';
  616. else if(vhash == hash_hello && !strcmp(value, "hello")) action = 'H';
  617. else if(vhash == hash_delete && !strcmp(value, "delete")) action = 'D';
  618. else if(vhash == hash_search && !strcmp(value, "search")) action = 'S';
  619. else if(vhash == hash_switch && !strcmp(value, "switch")) action = 'W';
  620. #ifdef NETDATA_INTERNAL_CHECKS
  621. else error("unknown registry action '%s'", value);
  622. #endif /* NETDATA_INTERNAL_CHECKS */
  623. }
  624. /*
  625. else if(hash == hash_redirects && !strcmp(name, "redirects"))
  626. redirects = atoi(value);
  627. */
  628. else if(hash == hash_machine && !strcmp(name, "machine"))
  629. machine_guid = value;
  630. else if(hash == hash_url && !strcmp(name, "url"))
  631. machine_url = value;
  632. else if(action == 'A') {
  633. if(hash == hash_name && !strcmp(name, "name"))
  634. url_name = value;
  635. }
  636. else if(action == 'D') {
  637. if(hash == hash_delete_url && !strcmp(name, "delete_url"))
  638. delete_url = value;
  639. }
  640. else if(action == 'S') {
  641. if(hash == hash_for && !strcmp(name, "for"))
  642. search_machine_guid = value;
  643. }
  644. else if(action == 'W') {
  645. if(hash == hash_to && !strcmp(name, "to"))
  646. to_person_guid = value;
  647. }
  648. #ifdef NETDATA_INTERNAL_CHECKS
  649. else error("unused registry URL parameter '%s' with value '%s'", name, value);
  650. #endif /* NETDATA_INTERNAL_CHECKS */
  651. }
  652. if(unlikely(respect_web_browser_do_not_track_policy && web_client_has_donottrack(w))) {
  653. buffer_flush(w->response.data);
  654. 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.");
  655. return HTTP_RESP_BAD_REQUEST;
  656. }
  657. if(unlikely(action == 'H')) {
  658. // HELLO request, dashboard ACL
  659. analytics_log_dashboard();
  660. if(unlikely(!web_client_can_access_dashboard(w)))
  661. return web_client_permission_denied(w);
  662. }
  663. else {
  664. // everything else, registry ACL
  665. if(unlikely(!web_client_can_access_registry(w)))
  666. return web_client_permission_denied(w);
  667. }
  668. switch(action) {
  669. case 'A':
  670. if(unlikely(!machine_guid || !machine_url || !url_name)) {
  671. 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");
  672. buffer_flush(w->response.data);
  673. buffer_strcat(w->response.data, "Invalid registry Access request.");
  674. return HTTP_RESP_BAD_REQUEST;
  675. }
  676. web_client_enable_tracking_required(w);
  677. return registry_request_access_json(host, w, person_guid, machine_guid, machine_url, url_name, now_realtime_sec());
  678. case 'D':
  679. if(unlikely(!machine_guid || !machine_url || !delete_url)) {
  680. 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");
  681. buffer_flush(w->response.data);
  682. buffer_strcat(w->response.data, "Invalid registry Delete request.");
  683. return HTTP_RESP_BAD_REQUEST;
  684. }
  685. web_client_enable_tracking_required(w);
  686. return registry_request_delete_json(host, w, person_guid, machine_guid, machine_url, delete_url, now_realtime_sec());
  687. case 'S':
  688. if(unlikely(!machine_guid || !machine_url || !search_machine_guid)) {
  689. 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");
  690. buffer_flush(w->response.data);
  691. buffer_strcat(w->response.data, "Invalid registry Search request.");
  692. return HTTP_RESP_BAD_REQUEST;
  693. }
  694. web_client_enable_tracking_required(w);
  695. return registry_request_search_json(host, w, person_guid, machine_guid, machine_url, search_machine_guid, now_realtime_sec());
  696. case 'W':
  697. if(unlikely(!machine_guid || !machine_url || !to_person_guid)) {
  698. 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");
  699. buffer_flush(w->response.data);
  700. buffer_strcat(w->response.data, "Invalid registry Switch request.");
  701. return HTTP_RESP_BAD_REQUEST;
  702. }
  703. web_client_enable_tracking_required(w);
  704. return registry_request_switch_json(host, w, person_guid, machine_guid, machine_url, to_person_guid, now_realtime_sec());
  705. case 'H':
  706. return registry_request_hello_json(host, w);
  707. default:
  708. buffer_flush(w->response.data);
  709. buffer_strcat(w->response.data, "Invalid registry request - you need to set an action: hello, access, delete, search");
  710. return HTTP_RESP_BAD_REQUEST;
  711. }
  712. }
  713. static inline void web_client_api_request_v1_info_summary_alarm_statuses(RRDHOST *host, BUFFER *wb) {
  714. int alarm_normal = 0, alarm_warn = 0, alarm_crit = 0;
  715. RRDCALC *rc;
  716. rrdhost_rdlock(host);
  717. for(rc = host->alarms; rc ; rc = rc->next) {
  718. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  719. continue;
  720. switch(rc->status) {
  721. case RRDCALC_STATUS_WARNING:
  722. alarm_warn++;
  723. break;
  724. case RRDCALC_STATUS_CRITICAL:
  725. alarm_crit++;
  726. break;
  727. default:
  728. alarm_normal++;
  729. }
  730. }
  731. rrdhost_unlock(host);
  732. buffer_sprintf(wb, "\t\t\"normal\": %d,\n", alarm_normal);
  733. buffer_sprintf(wb, "\t\t\"warning\": %d,\n", alarm_warn);
  734. buffer_sprintf(wb, "\t\t\"critical\": %d\n", alarm_crit);
  735. }
  736. static inline void web_client_api_request_v1_info_mirrored_hosts(BUFFER *wb) {
  737. RRDHOST *host;
  738. int count = 0;
  739. buffer_strcat(wb, "\t\"mirrored_hosts\": [\n");
  740. rrd_rdlock();
  741. rrdhost_foreach_read(host) {
  742. if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))
  743. continue;
  744. if (count > 0)
  745. buffer_strcat(wb, ",\n");
  746. buffer_sprintf(wb, "\t\t\"%s\"", host->hostname);
  747. count++;
  748. }
  749. buffer_strcat(wb, "\n\t],\n\t\"mirrored_hosts_status\": [\n");
  750. count = 0;
  751. rrdhost_foreach_read(host)
  752. {
  753. if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))
  754. continue;
  755. if (count > 0)
  756. buffer_strcat(wb, ",\n");
  757. netdata_mutex_lock(&host->receiver_lock);
  758. buffer_sprintf(
  759. wb, "\t\t{ \"guid\": \"%s\", \"reachable\": %s, \"hops\": %d, \"claim_id\": ", host->machine_guid,
  760. (host->receiver || host == localhost) ? "true" : "false", host->system_info ? host->system_info->hops : (host == localhost) ? 0 : 1);
  761. netdata_mutex_unlock(&host->receiver_lock);
  762. rrdhost_aclk_state_lock(host);
  763. if (host->aclk_state.claimed_id)
  764. buffer_sprintf(wb, "\"%s\", ", host->aclk_state.claimed_id);
  765. else
  766. buffer_strcat(wb, "null, ");
  767. rrdhost_aclk_state_unlock(host);
  768. if (host->node_id) {
  769. char node_id_str[GUID_LEN + 1];
  770. uuid_unparse_lower(*host->node_id, node_id_str);
  771. buffer_sprintf(wb, "\"node_id\": \"%s\" }", node_id_str);
  772. } else
  773. buffer_strcat(wb, "\"node_id\": null }");
  774. count++;
  775. }
  776. rrd_unlock();
  777. buffer_strcat(wb, "\n\t],\n");
  778. }
  779. inline void host_labels2json(RRDHOST *host, BUFFER *wb, size_t indentation) {
  780. char tabs[11];
  781. if (indentation > 10)
  782. indentation = 10;
  783. tabs[0] = '\0';
  784. while (indentation) {
  785. strcat(tabs, "\t");
  786. indentation--;
  787. }
  788. int count = 0;
  789. rrdhost_rdlock(host);
  790. netdata_rwlock_rdlock(&host->labels.labels_rwlock);
  791. for (struct label *label = host->labels.head; label; label = label->next) {
  792. if(count > 0) buffer_strcat(wb, ",\n");
  793. buffer_strcat(wb, tabs);
  794. char value[CONFIG_MAX_VALUE * 2 + 1];
  795. sanitize_json_string(value, label->value, CONFIG_MAX_VALUE * 2);
  796. buffer_sprintf(wb, "\"%s\": \"%s\"", label->key, value);
  797. count++;
  798. }
  799. buffer_strcat(wb, "\n");
  800. netdata_rwlock_unlock(&host->labels.labels_rwlock);
  801. rrdhost_unlock(host);
  802. }
  803. extern int aclk_connected;
  804. inline int web_client_api_request_v1_info_fill_buffer(RRDHOST *host, BUFFER *wb)
  805. {
  806. buffer_strcat(wb, "{\n");
  807. buffer_sprintf(wb, "\t\"version\": \"%s\",\n", host->program_version);
  808. buffer_sprintf(wb, "\t\"uid\": \"%s\",\n", host->machine_guid);
  809. web_client_api_request_v1_info_mirrored_hosts(wb);
  810. buffer_strcat(wb, "\t\"alarms\": {\n");
  811. web_client_api_request_v1_info_summary_alarm_statuses(host, wb);
  812. buffer_strcat(wb, "\t},\n");
  813. buffer_sprintf(wb, "\t\"os_name\": \"%s\",\n", (host->system_info->host_os_name) ? host->system_info->host_os_name : "");
  814. buffer_sprintf(wb, "\t\"os_id\": \"%s\",\n", (host->system_info->host_os_id) ? host->system_info->host_os_id : "");
  815. buffer_sprintf(wb, "\t\"os_id_like\": \"%s\",\n", (host->system_info->host_os_id_like) ? host->system_info->host_os_id_like : "");
  816. buffer_sprintf(wb, "\t\"os_version\": \"%s\",\n", (host->system_info->host_os_version) ? host->system_info->host_os_version : "");
  817. buffer_sprintf(wb, "\t\"os_version_id\": \"%s\",\n", (host->system_info->host_os_version_id) ? host->system_info->host_os_version_id : "");
  818. buffer_sprintf(wb, "\t\"os_detection\": \"%s\",\n", (host->system_info->host_os_detection) ? host->system_info->host_os_detection : "");
  819. buffer_sprintf(wb, "\t\"cores_total\": \"%s\",\n", (host->system_info->host_cores) ? host->system_info->host_cores : "");
  820. buffer_sprintf(wb, "\t\"total_disk_space\": \"%s\",\n", (host->system_info->host_disk_space) ? host->system_info->host_disk_space : "");
  821. buffer_sprintf(wb, "\t\"cpu_freq\": \"%s\",\n", (host->system_info->host_cpu_freq) ? host->system_info->host_cpu_freq : "");
  822. buffer_sprintf(wb, "\t\"ram_total\": \"%s\",\n", (host->system_info->host_ram_total) ? host->system_info->host_ram_total : "");
  823. if (host->system_info->container_os_name)
  824. buffer_sprintf(wb, "\t\"container_os_name\": \"%s\",\n", host->system_info->container_os_name);
  825. if (host->system_info->container_os_id)
  826. buffer_sprintf(wb, "\t\"container_os_id\": \"%s\",\n", host->system_info->container_os_id);
  827. if (host->system_info->container_os_id_like)
  828. buffer_sprintf(wb, "\t\"container_os_id_like\": \"%s\",\n", host->system_info->container_os_id_like);
  829. if (host->system_info->container_os_version)
  830. buffer_sprintf(wb, "\t\"container_os_version\": \"%s\",\n", host->system_info->container_os_version);
  831. if (host->system_info->container_os_version_id)
  832. buffer_sprintf(wb, "\t\"container_os_version_id\": \"%s\",\n", host->system_info->container_os_version_id);
  833. if (host->system_info->container_os_detection)
  834. buffer_sprintf(wb, "\t\"container_os_detection\": \"%s\",\n", host->system_info->container_os_detection);
  835. if (host->system_info->is_k8s_node)
  836. buffer_sprintf(wb, "\t\"is_k8s_node\": \"%s\",\n", host->system_info->is_k8s_node);
  837. buffer_sprintf(wb, "\t\"kernel_name\": \"%s\",\n", (host->system_info->kernel_name) ? host->system_info->kernel_name : "");
  838. buffer_sprintf(wb, "\t\"kernel_version\": \"%s\",\n", (host->system_info->kernel_version) ? host->system_info->kernel_version : "");
  839. buffer_sprintf(wb, "\t\"architecture\": \"%s\",\n", (host->system_info->architecture) ? host->system_info->architecture : "");
  840. buffer_sprintf(wb, "\t\"virtualization\": \"%s\",\n", (host->system_info->virtualization) ? host->system_info->virtualization : "");
  841. buffer_sprintf(wb, "\t\"virt_detection\": \"%s\",\n", (host->system_info->virt_detection) ? host->system_info->virt_detection : "");
  842. buffer_sprintf(wb, "\t\"container\": \"%s\",\n", (host->system_info->container) ? host->system_info->container : "");
  843. buffer_sprintf(wb, "\t\"container_detection\": \"%s\",\n", (host->system_info->container_detection) ? host->system_info->container_detection : "");
  844. if (host->system_info->cloud_provider_type)
  845. buffer_sprintf(wb, "\t\"cloud_provider_type\": \"%s\",\n", host->system_info->cloud_provider_type);
  846. if (host->system_info->cloud_instance_type)
  847. buffer_sprintf(wb, "\t\"cloud_instance_type\": \"%s\",\n", host->system_info->cloud_instance_type);
  848. if (host->system_info->cloud_instance_region)
  849. buffer_sprintf(wb, "\t\"cloud_instance_region\": \"%s\",\n", host->system_info->cloud_instance_region);
  850. buffer_strcat(wb, "\t\"host_labels\": {\n");
  851. host_labels2json(host, wb, 2);
  852. buffer_strcat(wb, "\t},\n");
  853. buffer_strcat(wb, "\t\"collectors\": [");
  854. chartcollectors2json(host, wb);
  855. buffer_strcat(wb, "\n\t],\n");
  856. #ifdef DISABLE_CLOUD
  857. buffer_strcat(wb, "\t\"cloud-enabled\": false,\n");
  858. #else
  859. buffer_sprintf(wb, "\t\"cloud-enabled\": %s,\n",
  860. appconfig_get_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "enabled", 1) ? "true" : "false");
  861. #endif
  862. #ifdef ENABLE_ACLK
  863. buffer_strcat(wb, "\t\"cloud-available\": true,\n");
  864. buffer_strcat(wb, "\t\"aclk-ng-available\": true,\n");
  865. #ifdef ENABLE_NEW_CLOUD_PROTOCOL
  866. buffer_strcat(wb, "\t\"aclk-ng-new-cloud-protocol\": true,\n");
  867. #else
  868. buffer_strcat(wb, "\t\"aclk-ng-new-cloud-protocol\": false,\n");
  869. #endif
  870. buffer_strcat(wb, "\t\"aclk-legacy-available\": false,\n");
  871. buffer_strcat(wb, "\t\"aclk-implementation\": \"Next Generation\",\n");
  872. #else
  873. buffer_strcat(wb, "\t\"cloud-available\": false,\n");
  874. buffer_strcat(wb, "\t\"aclk-ng-available\": false,\n");
  875. buffer_strcat(wb, "\t\"aclk-legacy-available\": false,\n");
  876. #endif
  877. char *agent_id = is_agent_claimed();
  878. if (agent_id == NULL)
  879. buffer_strcat(wb, "\t\"agent-claimed\": false,\n");
  880. else {
  881. buffer_strcat(wb, "\t\"agent-claimed\": true,\n");
  882. freez(agent_id);
  883. }
  884. #ifdef ENABLE_ACLK
  885. if (aclk_connected) {
  886. buffer_strcat(wb, "\t\"aclk-available\": true,\n");
  887. #ifdef ENABLE_NEW_CLOUD_PROTOCOL
  888. if (aclk_use_new_cloud_arch)
  889. buffer_strcat(wb, "\t\"aclk-available-protocol\": \"New\",\n");
  890. else
  891. #endif
  892. buffer_strcat(wb, "\t\"aclk-available-protocol\": \"Legacy\",\n");
  893. }
  894. else
  895. #endif
  896. buffer_strcat(wb, "\t\"aclk-available\": false,\n\t\"aclk-available-protocol\": null,\n"); // Intentionally valid with/without #ifdef above
  897. buffer_strcat(wb, "\t\"memory-mode\": ");
  898. analytics_get_data(analytics_data.netdata_config_memory_mode, wb);
  899. buffer_strcat(wb, ",\n");
  900. buffer_strcat(wb, "\t\"multidb-disk-quota\": ");
  901. analytics_get_data(analytics_data.netdata_config_multidb_disk_quota, wb);
  902. buffer_strcat(wb, ",\n");
  903. buffer_strcat(wb, "\t\"page-cache-size\": ");
  904. analytics_get_data(analytics_data.netdata_config_page_cache_size, wb);
  905. buffer_strcat(wb, ",\n");
  906. buffer_strcat(wb, "\t\"stream-enabled\": ");
  907. analytics_get_data(analytics_data.netdata_config_stream_enabled, wb);
  908. buffer_strcat(wb, ",\n");
  909. #ifdef ENABLE_COMPRESSION
  910. if(host->sender){
  911. buffer_strcat(wb, "\t\"stream-compression\": ");
  912. buffer_strcat(wb, (host->sender->rrdpush_compression ? "true" : "false"));
  913. buffer_strcat(wb, ",\n");
  914. }else{
  915. buffer_strcat(wb, "\t\"stream-compression\": null,\n");
  916. }
  917. #else
  918. buffer_strcat(wb, "\t\"stream-compression\": null,\n");
  919. #endif //ENABLE_COMPRESSION
  920. buffer_strcat(wb, "\t\"hosts-available\": ");
  921. analytics_get_data(analytics_data.netdata_config_hosts_available, wb);
  922. buffer_strcat(wb, ",\n");
  923. buffer_strcat(wb, "\t\"https-enabled\": ");
  924. analytics_get_data(analytics_data.netdata_config_https_enabled, wb);
  925. buffer_strcat(wb, ",\n");
  926. buffer_strcat(wb, "\t\"buildinfo\": ");
  927. analytics_get_data(analytics_data.netdata_buildinfo, wb);
  928. buffer_strcat(wb, ",\n");
  929. buffer_strcat(wb, "\t\"release-channel\": ");
  930. analytics_get_data(analytics_data.netdata_config_release_channel, wb);
  931. buffer_strcat(wb, ",\n");
  932. buffer_strcat(wb, "\t\"web-enabled\": ");
  933. analytics_get_data(analytics_data.netdata_config_web_enabled, wb);
  934. buffer_strcat(wb, ",\n");
  935. buffer_strcat(wb, "\t\"notification-methods\": ");
  936. analytics_get_data(analytics_data.netdata_notification_methods, wb);
  937. buffer_strcat(wb, ",\n");
  938. buffer_strcat(wb, "\t\"exporting-enabled\": ");
  939. analytics_get_data(analytics_data.netdata_config_exporting_enabled, wb);
  940. buffer_strcat(wb, ",\n");
  941. buffer_strcat(wb, "\t\"exporting-connectors\": ");
  942. analytics_get_data(analytics_data.netdata_exporting_connectors, wb);
  943. buffer_strcat(wb, ",\n");
  944. buffer_strcat(wb, "\t\"allmetrics-prometheus-used\": ");
  945. analytics_get_data(analytics_data.netdata_allmetrics_prometheus_used, wb);
  946. buffer_strcat(wb, ",\n");
  947. buffer_strcat(wb, "\t\"allmetrics-shell-used\": ");
  948. analytics_get_data(analytics_data.netdata_allmetrics_shell_used, wb);
  949. buffer_strcat(wb, ",\n");
  950. buffer_strcat(wb, "\t\"allmetrics-json-used\": ");
  951. analytics_get_data(analytics_data.netdata_allmetrics_json_used, wb);
  952. buffer_strcat(wb, ",\n");
  953. buffer_strcat(wb, "\t\"dashboard-used\": ");
  954. analytics_get_data(analytics_data.netdata_dashboard_used, wb);
  955. buffer_strcat(wb, ",\n");
  956. buffer_strcat(wb, "\t\"charts-count\": ");
  957. analytics_get_data(analytics_data.netdata_charts_count, wb);
  958. buffer_strcat(wb, ",\n");
  959. buffer_strcat(wb, "\t\"metrics-count\": ");
  960. analytics_get_data(analytics_data.netdata_metrics_count, wb);
  961. #if defined(ENABLE_ML)
  962. buffer_strcat(wb, ",\n");
  963. char *ml_info = ml_get_host_info(host);
  964. buffer_strcat(wb, "\t\"ml-info\": ");
  965. buffer_strcat(wb, ml_info);
  966. free(ml_info);
  967. #endif
  968. buffer_strcat(wb, "\n}");
  969. return 0;
  970. }
  971. #if defined(ENABLE_ML)
  972. int web_client_api_request_v1_anomaly_events(RRDHOST *host, struct web_client *w, char *url) {
  973. if (!netdata_ready)
  974. return HTTP_RESP_BACKEND_FETCH_FAILED;
  975. uint32_t after = 0, before = 0;
  976. while (url) {
  977. char *value = mystrsep(&url, "&");
  978. if (!value || !*value)
  979. continue;
  980. char *name = mystrsep(&value, "=");
  981. if (!name || !*name)
  982. continue;
  983. if (!value || !*value)
  984. continue;
  985. if (!strcmp(name, "after"))
  986. after = (uint32_t) (strtoul(value, NULL, 0) / 1000);
  987. else if (!strcmp(name, "before"))
  988. before = (uint32_t) (strtoul(value, NULL, 0) / 1000);
  989. }
  990. char *s;
  991. if (!before || !after)
  992. s = strdupz("{\"error\": \"missing after/before parameters\" }\n");
  993. else {
  994. s = ml_get_anomaly_events(host, "AD1", 1, after, before);
  995. if (!s)
  996. s = strdupz("{\"error\": \"json string is empty\" }\n");
  997. }
  998. BUFFER *wb = w->response.data;
  999. buffer_flush(wb);
  1000. wb->contenttype = CT_APPLICATION_JSON;
  1001. buffer_strcat(wb, s);
  1002. buffer_no_cacheable(wb);
  1003. freez(s);
  1004. return HTTP_RESP_OK;
  1005. }
  1006. int web_client_api_request_v1_anomaly_event_info(RRDHOST *host, struct web_client *w, char *url) {
  1007. if (!netdata_ready)
  1008. return HTTP_RESP_BACKEND_FETCH_FAILED;
  1009. uint32_t after = 0, before = 0;
  1010. while (url) {
  1011. char *value = mystrsep(&url, "&");
  1012. if (!value || !*value)
  1013. continue;
  1014. char *name = mystrsep(&value, "=");
  1015. if (!name || !*name)
  1016. continue;
  1017. if (!value || !*value)
  1018. continue;
  1019. if (!strcmp(name, "after"))
  1020. after = (uint32_t) strtoul(value, NULL, 0);
  1021. else if (!strcmp(name, "before"))
  1022. before = (uint32_t) strtoul(value, NULL, 0);
  1023. }
  1024. char *s;
  1025. if (!before || !after)
  1026. s = strdupz("{\"error\": \"missing after/before parameters\" }\n");
  1027. else {
  1028. s = ml_get_anomaly_event_info(host, "AD1", 1, after, before);
  1029. if (!s)
  1030. s = strdupz("{\"error\": \"json string is empty\" }\n");
  1031. }
  1032. BUFFER *wb = w->response.data;
  1033. buffer_flush(wb);
  1034. wb->contenttype = CT_APPLICATION_JSON;
  1035. buffer_strcat(wb, s);
  1036. buffer_no_cacheable(wb);
  1037. freez(s);
  1038. return HTTP_RESP_OK;
  1039. }
  1040. int web_client_api_request_v1_ml_info(RRDHOST *host, struct web_client *w, char *url) {
  1041. (void) url;
  1042. if (!netdata_ready)
  1043. return HTTP_RESP_BACKEND_FETCH_FAILED;
  1044. char *s = ml_get_host_runtime_info(host);
  1045. if (!s)
  1046. s = strdupz("{\"error\": \"json string is empty\" }\n");
  1047. BUFFER *wb = w->response.data;
  1048. buffer_flush(wb);
  1049. wb->contenttype = CT_APPLICATION_JSON;
  1050. buffer_strcat(wb, s);
  1051. buffer_no_cacheable(wb);
  1052. freez(s);
  1053. return HTTP_RESP_OK;
  1054. }
  1055. #endif // defined(ENABLE_ML)
  1056. inline int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, char *url) {
  1057. (void)url;
  1058. if (!netdata_ready) return HTTP_RESP_BACKEND_FETCH_FAILED;
  1059. BUFFER *wb = w->response.data;
  1060. buffer_flush(wb);
  1061. wb->contenttype = CT_APPLICATION_JSON;
  1062. web_client_api_request_v1_info_fill_buffer(host, wb);
  1063. buffer_no_cacheable(wb);
  1064. return HTTP_RESP_OK;
  1065. }
  1066. static int web_client_api_request_v1_aclk_state(RRDHOST *host, struct web_client *w, char *url) {
  1067. UNUSED(url);
  1068. UNUSED(host);
  1069. if (!netdata_ready) return HTTP_RESP_BACKEND_FETCH_FAILED;
  1070. BUFFER *wb = w->response.data;
  1071. buffer_flush(wb);
  1072. char *str = aclk_state_json();
  1073. buffer_strcat(wb, str);
  1074. freez(str);
  1075. wb->contenttype = CT_APPLICATION_JSON;
  1076. buffer_no_cacheable(wb);
  1077. return HTTP_RESP_OK;
  1078. }
  1079. static struct api_command {
  1080. const char *command;
  1081. uint32_t hash;
  1082. WEB_CLIENT_ACL acl;
  1083. int (*callback)(RRDHOST *host, struct web_client *w, char *url);
  1084. } api_commands[] = {
  1085. { "info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_info },
  1086. { "data", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_data },
  1087. { "chart", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_chart },
  1088. { "charts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_charts },
  1089. { "archivedcharts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_archivedcharts },
  1090. // registry checks the ACL by itself, so we allow everything
  1091. { "registry", 0, WEB_CLIENT_ACL_NOCHECK, web_client_api_request_v1_registry },
  1092. // badges can be fetched with both dashboard and badge permissions
  1093. { "badge.svg", 0, WEB_CLIENT_ACL_DASHBOARD|WEB_CLIENT_ACL_BADGE, web_client_api_request_v1_badge },
  1094. { "alarms", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarms },
  1095. { "alarms_values", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarms_values },
  1096. { "alarm_log", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_log },
  1097. { "alarm_variables", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_variables },
  1098. { "alarm_count", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_count },
  1099. { "allmetrics", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_allmetrics },
  1100. #if defined(ENABLE_ML)
  1101. { "anomaly_events", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_anomaly_events },
  1102. { "anomaly_event_info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_anomaly_event_info },
  1103. { "ml_info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_ml_info },
  1104. #endif
  1105. { "manage/health", 0, WEB_CLIENT_ACL_MGMT, web_client_api_request_v1_mgmt_health },
  1106. { "aclk", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_aclk_state },
  1107. // terminator
  1108. { NULL, 0, WEB_CLIENT_ACL_NONE, NULL },
  1109. };
  1110. inline int web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *url) {
  1111. static int initialized = 0;
  1112. int i;
  1113. if(unlikely(initialized == 0)) {
  1114. initialized = 1;
  1115. for(i = 0; api_commands[i].command ; i++)
  1116. api_commands[i].hash = simple_hash(api_commands[i].command);
  1117. }
  1118. // get the command
  1119. if(url) {
  1120. debug(D_WEB_CLIENT, "%llu: Searching for API v1 command '%s'.", w->id, url);
  1121. uint32_t hash = simple_hash(url);
  1122. for(i = 0; api_commands[i].command ;i++) {
  1123. if(unlikely(hash == api_commands[i].hash && !strcmp(url, api_commands[i].command))) {
  1124. if(unlikely(api_commands[i].acl != WEB_CLIENT_ACL_NOCHECK) && !(w->acl & api_commands[i].acl))
  1125. return web_client_permission_denied(w);
  1126. //return api_commands[i].callback(host, w, url);
  1127. return api_commands[i].callback(host, w, (w->decoded_query_string + 1));
  1128. }
  1129. }
  1130. buffer_flush(w->response.data);
  1131. buffer_strcat(w->response.data, "Unsupported v1 API command: ");
  1132. buffer_strcat_htmlescape(w->response.data, url);
  1133. return HTTP_RESP_NOT_FOUND;
  1134. }
  1135. else {
  1136. buffer_flush(w->response.data);
  1137. buffer_sprintf(w->response.data, "Which API v1 command?");
  1138. return HTTP_RESP_BAD_REQUEST;
  1139. }
  1140. }