web_api_v1.c 63 KB

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