web_api_v1.c 35 KB

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