rrdfunctions.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #define NETDATA_RRDCOLLECTOR_INTERNALS
  4. #include "rrd.h"
  5. #define MAX_FUNCTION_LENGTH (PLUGINSD_LINE_MAX - 512) // we need some space for the rest of the line
  6. static unsigned char functions_allowed_chars[256] = {
  7. [0] = '\0', [1] = '_', [2] = '_', [3] = '_', [4] = '_', [5] = '_', [6] = '_', [7] = '_', [8] = '_',
  8. // control
  9. ['\t'] = ' ', ['\n'] = ' ', ['\v'] = ' ', [12] = ' ', ['\r'] = ' ',
  10. [14] = '_', [15] = '_', [16] = '_', [17] = '_', [18] = '_', [19] = '_', [20] = '_', [21] = '_',
  11. [22] = '_', [23] = '_', [24] = '_', [25] = '_', [26] = '_', [27] = '_', [28] = '_', [29] = '_',
  12. [30] = '_', [31] = '_',
  13. // symbols
  14. [' '] = ' ', ['!'] = '!', ['"'] = '"', ['#'] = '#', ['$'] = '$', ['%'] = '%', ['&'] = '&', ['\''] = '\'',
  15. ['('] = '(', [')'] = ')', ['*'] = '*', ['+'] = '+', [','] = ',', ['-'] = '-', ['.'] = '.', ['/'] = '/',
  16. // numbers
  17. ['0'] = '0', ['1'] = '1', ['2'] = '2', ['3'] = '3', ['4'] = '4', ['5'] = '5', ['6'] = '6', ['7'] = '7',
  18. ['8'] = '8', ['9'] = '9',
  19. // symbols
  20. [':'] = ':', [';'] = ';', ['<'] = '<', ['='] = '=', ['>'] = '>', ['?'] = '?', ['@'] = '@',
  21. // capitals
  22. ['A'] = 'A', ['B'] = 'B', ['C'] = 'C', ['D'] = 'D', ['E'] = 'E', ['F'] = 'F', ['G'] = 'G', ['H'] = 'H',
  23. ['I'] = 'I', ['J'] = 'J', ['K'] = 'K', ['L'] = 'L', ['M'] = 'M', ['N'] = 'N', ['O'] = 'O', ['P'] = 'P',
  24. ['Q'] = 'Q', ['R'] = 'R', ['S'] = 'S', ['T'] = 'T', ['U'] = 'U', ['V'] = 'V', ['W'] = 'W', ['X'] = 'X',
  25. ['Y'] = 'Y', ['Z'] = 'Z',
  26. // symbols
  27. ['['] = '[', ['\\'] = '\\', [']'] = ']', ['^'] = '^', ['_'] = '_', ['`'] = '`',
  28. // lower
  29. ['a'] = 'a', ['b'] = 'b', ['c'] = 'c', ['d'] = 'd', ['e'] = 'e', ['f'] = 'f', ['g'] = 'g', ['h'] = 'h',
  30. ['i'] = 'i', ['j'] = 'j', ['k'] = 'k', ['l'] = 'l', ['m'] = 'm', ['n'] = 'n', ['o'] = 'o', ['p'] = 'p',
  31. ['q'] = 'q', ['r'] = 'r', ['s'] = 's', ['t'] = 't', ['u'] = 'u', ['v'] = 'v', ['w'] = 'w', ['x'] = 'x',
  32. ['y'] = 'y', ['z'] = 'z',
  33. // symbols
  34. ['{'] = '{', ['|'] = '|', ['}'] = '}', ['~'] = '~',
  35. // rest
  36. [127] = '_', [128] = '_', [129] = '_', [130] = '_', [131] = '_', [132] = '_', [133] = '_', [134] = '_',
  37. [135] = '_', [136] = '_', [137] = '_', [138] = '_', [139] = '_', [140] = '_', [141] = '_', [142] = '_',
  38. [143] = '_', [144] = '_', [145] = '_', [146] = '_', [147] = '_', [148] = '_', [149] = '_', [150] = '_',
  39. [151] = '_', [152] = '_', [153] = '_', [154] = '_', [155] = '_', [156] = '_', [157] = '_', [158] = '_',
  40. [159] = '_', [160] = '_', [161] = '_', [162] = '_', [163] = '_', [164] = '_', [165] = '_', [166] = '_',
  41. [167] = '_', [168] = '_', [169] = '_', [170] = '_', [171] = '_', [172] = '_', [173] = '_', [174] = '_',
  42. [175] = '_', [176] = '_', [177] = '_', [178] = '_', [179] = '_', [180] = '_', [181] = '_', [182] = '_',
  43. [183] = '_', [184] = '_', [185] = '_', [186] = '_', [187] = '_', [188] = '_', [189] = '_', [190] = '_',
  44. [191] = '_', [192] = '_', [193] = '_', [194] = '_', [195] = '_', [196] = '_', [197] = '_', [198] = '_',
  45. [199] = '_', [200] = '_', [201] = '_', [202] = '_', [203] = '_', [204] = '_', [205] = '_', [206] = '_',
  46. [207] = '_', [208] = '_', [209] = '_', [210] = '_', [211] = '_', [212] = '_', [213] = '_', [214] = '_',
  47. [215] = '_', [216] = '_', [217] = '_', [218] = '_', [219] = '_', [220] = '_', [221] = '_', [222] = '_',
  48. [223] = '_', [224] = '_', [225] = '_', [226] = '_', [227] = '_', [228] = '_', [229] = '_', [230] = '_',
  49. [231] = '_', [232] = '_', [233] = '_', [234] = '_', [235] = '_', [236] = '_', [237] = '_', [238] = '_',
  50. [239] = '_', [240] = '_', [241] = '_', [242] = '_', [243] = '_', [244] = '_', [245] = '_', [246] = '_',
  51. [247] = '_', [248] = '_', [249] = '_', [250] = '_', [251] = '_', [252] = '_', [253] = '_', [254] = '_',
  52. [255] = '_'
  53. };
  54. static inline size_t sanitize_function_text(char *dst, const char *src, size_t dst_len) {
  55. return text_sanitize((unsigned char *)dst, (const unsigned char *)src, dst_len,
  56. functions_allowed_chars, true, "", NULL);
  57. }
  58. // we keep a dictionary per RRDSET with these functions
  59. // the dictionary is created on demand (only when a function is added to an RRDSET)
  60. typedef enum __attribute__((packed)) {
  61. RRD_FUNCTION_LOCAL = (1 << 0),
  62. RRD_FUNCTION_GLOBAL = (1 << 1),
  63. // this is 8-bit
  64. } RRD_FUNCTION_OPTIONS;
  65. // ----------------------------------------------------------------------------
  66. struct rrd_host_function {
  67. bool sync; // when true, the function is called synchronously
  68. RRD_FUNCTION_OPTIONS options; // RRD_FUNCTION_OPTIONS
  69. HTTP_ACCESS access;
  70. STRING *help;
  71. STRING *tags;
  72. int timeout; // the default timeout of the function
  73. int priority;
  74. rrd_function_execute_cb_t execute_cb;
  75. void *execute_cb_data;
  76. struct rrd_collector *collector;
  77. };
  78. struct rrd_function_inflight {
  79. bool used;
  80. RRDHOST *host;
  81. uuid_t transaction_uuid;
  82. const char *transaction;
  83. const char *cmd;
  84. const char *sanitized_cmd;
  85. size_t sanitized_cmd_length;
  86. int timeout;
  87. bool cancelled;
  88. usec_t stop_monotonic_ut;
  89. const DICTIONARY_ITEM *host_function_acquired;
  90. // the collector
  91. // we acquire this structure at the beginning,
  92. // and we release it at the end
  93. struct rrd_host_function *rdcf;
  94. struct {
  95. BUFFER *wb;
  96. // in async mode,
  97. // the function to call to send the result back
  98. rrd_function_result_callback_t cb;
  99. void *data;
  100. } result;
  101. struct {
  102. // to be called in sync mode
  103. // while the function is running
  104. // to check if the function has been canceled
  105. rrd_function_is_cancelled_cb_t cb;
  106. void *data;
  107. } is_cancelled;
  108. struct {
  109. // to be registered by the function itself
  110. // used to signal the function to cancel
  111. rrd_function_cancel_cb_t cb;
  112. void *data;
  113. } canceller;
  114. struct {
  115. // callback to receive progress reports from function
  116. rrd_function_progress_cb_t cb;
  117. void *data;
  118. } progress;
  119. struct {
  120. // to be registered by the function itself
  121. // used to send progress requests to function
  122. rrd_function_progresser_cb_t cb;
  123. void *data;
  124. } progresser;
  125. };
  126. static DICTIONARY *rrd_functions_inflight_requests = NULL;
  127. static void rrd_function_cancel_inflight(struct rrd_function_inflight *r);
  128. // ----------------------------------------------------------------------------
  129. static void rrd_functions_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *func, void *rrdhost) {
  130. RRDHOST *host = rrdhost; (void)host;
  131. struct rrd_host_function *rdcf = func;
  132. rrd_collector_started();
  133. rdcf->collector = rrd_collector_acquire_current_thread();
  134. if(!rdcf->priority)
  135. rdcf->priority = RRDFUNCTIONS_PRIORITY_DEFAULT;
  136. // internal_error(true, "FUNCTIONS: adding function '%s' on host '%s', collection tid %d, %s",
  137. // dictionary_acquired_item_name(item), rrdhost_hostname(host),
  138. // rdcf->collector->tid, rdcf->collector->running ? "running" : "NOT running");
  139. }
  140. static void rrd_functions_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *func,
  141. void *rrdhost __maybe_unused) {
  142. struct rrd_host_function *rdcf = func;
  143. rrd_collector_release(rdcf->collector);
  144. }
  145. static bool rrd_functions_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *func,
  146. void *new_func, void *rrdhost) {
  147. RRDHOST *host = rrdhost; (void)host;
  148. struct rrd_host_function *rdcf = func;
  149. struct rrd_host_function *new_rdcf = new_func;
  150. rrd_collector_started();
  151. bool changed = false;
  152. if(rdcf->collector != thread_rrd_collector) {
  153. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  154. "FUNCTIONS: function '%s' of host '%s' changed collector from %d to %d",
  155. dictionary_acquired_item_name(item), rrdhost_hostname(host),
  156. rrd_collector_tid(rdcf->collector), rrd_collector_tid(thread_rrd_collector));
  157. struct rrd_collector *old_rdc = rdcf->collector;
  158. rdcf->collector = rrd_collector_acquire_current_thread();
  159. rrd_collector_release(old_rdc);
  160. changed = true;
  161. }
  162. if(rdcf->execute_cb != new_rdcf->execute_cb) {
  163. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  164. "FUNCTIONS: function '%s' of host '%s' changed execute callback",
  165. dictionary_acquired_item_name(item), rrdhost_hostname(host));
  166. rdcf->execute_cb = new_rdcf->execute_cb;
  167. changed = true;
  168. }
  169. if(rdcf->help != new_rdcf->help) {
  170. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  171. "FUNCTIONS: function '%s' of host '%s' changed help text",
  172. dictionary_acquired_item_name(item), rrdhost_hostname(host));
  173. STRING *old = rdcf->help;
  174. rdcf->help = new_rdcf->help;
  175. string_freez(old);
  176. changed = true;
  177. }
  178. else
  179. string_freez(new_rdcf->help);
  180. if(rdcf->tags != new_rdcf->tags) {
  181. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  182. "FUNCTIONS: function '%s' of host '%s' changed tags",
  183. dictionary_acquired_item_name(item), rrdhost_hostname(host));
  184. STRING *old = rdcf->tags;
  185. rdcf->tags = new_rdcf->tags;
  186. string_freez(old);
  187. changed = true;
  188. }
  189. else
  190. string_freez(new_rdcf->tags);
  191. if(rdcf->timeout != new_rdcf->timeout) {
  192. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  193. "FUNCTIONS: function '%s' of host '%s' changed timeout",
  194. dictionary_acquired_item_name(item), rrdhost_hostname(host));
  195. rdcf->timeout = new_rdcf->timeout;
  196. changed = true;
  197. }
  198. if(rdcf->priority != new_rdcf->priority) {
  199. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  200. "FUNCTIONS: function '%s' of host '%s' changed priority",
  201. dictionary_acquired_item_name(item), rrdhost_hostname(host));
  202. rdcf->priority = new_rdcf->priority;
  203. changed = true;
  204. }
  205. if(rdcf->access != new_rdcf->access) {
  206. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  207. "FUNCTIONS: function '%s' of host '%s' changed access level",
  208. dictionary_acquired_item_name(item), rrdhost_hostname(host));
  209. rdcf->access = new_rdcf->access;
  210. changed = true;
  211. }
  212. if(rdcf->sync != new_rdcf->sync) {
  213. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  214. "FUNCTIONS: function '%s' of host '%s' changed sync/async mode",
  215. dictionary_acquired_item_name(item), rrdhost_hostname(host));
  216. rdcf->sync = new_rdcf->sync;
  217. changed = true;
  218. }
  219. if(rdcf->execute_cb_data != new_rdcf->execute_cb_data) {
  220. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  221. "FUNCTIONS: function '%s' of host '%s' changed execute callback data",
  222. dictionary_acquired_item_name(item), rrdhost_hostname(host));
  223. rdcf->execute_cb_data = new_rdcf->execute_cb_data;
  224. changed = true;
  225. }
  226. // internal_error(true, "FUNCTIONS: adding function '%s' on host '%s', collection tid %d, %s",
  227. // dictionary_acquired_item_name(item), rrdhost_hostname(host),
  228. // rdcf->collector->tid, rdcf->collector->running ? "running" : "NOT running");
  229. return changed;
  230. }
  231. void rrdfunctions_host_init(RRDHOST *host) {
  232. if(host->functions) return;
  233. host->functions = dictionary_create_advanced(DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  234. &dictionary_stats_category_functions, sizeof(struct rrd_host_function));
  235. dictionary_register_insert_callback(host->functions, rrd_functions_insert_callback, host);
  236. dictionary_register_delete_callback(host->functions, rrd_functions_delete_callback, host);
  237. dictionary_register_conflict_callback(host->functions, rrd_functions_conflict_callback, host);
  238. }
  239. void rrdfunctions_host_destroy(RRDHOST *host) {
  240. dictionary_destroy(host->functions);
  241. }
  242. // ----------------------------------------------------------------------------
  243. void rrd_function_add(RRDHOST *host, RRDSET *st, const char *name, int timeout, int priority, const char *help, const char *tags,
  244. HTTP_ACCESS access, bool sync, rrd_function_execute_cb_t execute_cb,
  245. void *execute_cb_data) {
  246. // RRDSET *st may be NULL in this function
  247. // to create a GLOBAL function
  248. if(!tags || !*tags) {
  249. if(strcmp(name, "systemd-journal") == 0)
  250. tags = "logs";
  251. else
  252. tags = "top";
  253. }
  254. if(st && !st->functions_view)
  255. st->functions_view = dictionary_create_view(host->functions);
  256. char key[PLUGINSD_LINE_MAX + 1];
  257. sanitize_function_text(key, name, PLUGINSD_LINE_MAX);
  258. struct rrd_host_function tmp = {
  259. .sync = sync,
  260. .timeout = timeout,
  261. .options = (st)?RRD_FUNCTION_LOCAL:RRD_FUNCTION_GLOBAL,
  262. .access = access,
  263. .execute_cb = execute_cb,
  264. .execute_cb_data = execute_cb_data,
  265. .help = string_strdupz(help),
  266. .tags = string_strdupz(tags),
  267. .priority = priority,
  268. };
  269. const DICTIONARY_ITEM *item = dictionary_set_and_acquire_item(host->functions, key, &tmp, sizeof(tmp));
  270. if(st)
  271. dictionary_view_set(st->functions_view, key, item);
  272. else
  273. rrdhost_flag_set(host, RRDHOST_FLAG_GLOBAL_FUNCTIONS_UPDATED);
  274. dictionary_acquired_item_release(host->functions, item);
  275. }
  276. void rrd_functions_expose_rrdpush(RRDSET *st, BUFFER *wb) {
  277. if(!st->functions_view)
  278. return;
  279. struct rrd_host_function *tmp;
  280. dfe_start_read(st->functions_view, tmp) {
  281. buffer_sprintf(wb
  282. , PLUGINSD_KEYWORD_FUNCTION " \"%s\" %d \"%s\" \"%s\" \"%s\" %d\n"
  283. , tmp_dfe.name
  284. , tmp->timeout
  285. , string2str(tmp->help)
  286. , string2str(tmp->tags)
  287. , http_id2access(tmp->access)
  288. , tmp->priority
  289. );
  290. }
  291. dfe_done(tmp);
  292. }
  293. void rrd_functions_expose_global_rrdpush(RRDHOST *host, BUFFER *wb) {
  294. rrdhost_flag_clear(host, RRDHOST_FLAG_GLOBAL_FUNCTIONS_UPDATED);
  295. struct rrd_host_function *tmp;
  296. dfe_start_read(host->functions, tmp) {
  297. if(!(tmp->options & RRD_FUNCTION_GLOBAL))
  298. continue;
  299. buffer_sprintf(wb
  300. , PLUGINSD_KEYWORD_FUNCTION " GLOBAL \"%s\" %d \"%s\" \"%s\" \"%s\" %d\n"
  301. , tmp_dfe.name
  302. , tmp->timeout
  303. , string2str(tmp->help)
  304. , string2str(tmp->tags)
  305. , http_id2access(tmp->access)
  306. , tmp->priority
  307. );
  308. }
  309. dfe_done(tmp);
  310. }
  311. struct {
  312. const char *format;
  313. HTTP_CONTENT_TYPE content_type;
  314. } function_formats[] = {
  315. { .format = "application/json", CT_APPLICATION_JSON },
  316. { .format = "text/plain", CT_TEXT_PLAIN },
  317. { .format = "application/xml", CT_APPLICATION_XML },
  318. { .format = "prometheus", CT_PROMETHEUS },
  319. { .format = "text", CT_TEXT_PLAIN },
  320. { .format = "txt", CT_TEXT_PLAIN },
  321. { .format = "json", CT_APPLICATION_JSON },
  322. { .format = "html", CT_TEXT_HTML },
  323. { .format = "text/html", CT_TEXT_HTML },
  324. { .format = "xml", CT_APPLICATION_XML },
  325. // terminator
  326. { .format = NULL, CT_TEXT_PLAIN },
  327. };
  328. uint8_t functions_format_to_content_type(const char *format) {
  329. if(format && *format) {
  330. for (int i = 0; function_formats[i].format; i++)
  331. if (strcmp(function_formats[i].format, format) == 0)
  332. return function_formats[i].content_type;
  333. }
  334. return CT_TEXT_PLAIN;
  335. }
  336. const char *functions_content_type_to_format(HTTP_CONTENT_TYPE content_type) {
  337. for (int i = 0; function_formats[i].format; i++)
  338. if (function_formats[i].content_type == content_type)
  339. return function_formats[i].format;
  340. return "text/plain";
  341. }
  342. int rrd_call_function_error(BUFFER *wb, const char *msg, int code) {
  343. char buffer[PLUGINSD_LINE_MAX];
  344. json_escape_string(buffer, msg, PLUGINSD_LINE_MAX);
  345. buffer_flush(wb);
  346. buffer_sprintf(wb, "{\"status\":%d,\"error_message\":\"%s\"}", code, buffer);
  347. wb->content_type = CT_APPLICATION_JSON;
  348. buffer_no_cacheable(wb);
  349. return code;
  350. }
  351. static int rrd_call_function_find(RRDHOST *host, BUFFER *wb, const char *name, size_t key_length, const DICTIONARY_ITEM **item) {
  352. char buffer[MAX_FUNCTION_LENGTH + 1];
  353. strncpyz(buffer, name, MAX_FUNCTION_LENGTH);
  354. char *s = NULL;
  355. bool found = false;
  356. *item = NULL;
  357. if(host->functions) {
  358. while (buffer[0]) {
  359. if((*item = dictionary_get_and_acquire_item(host->functions, buffer))) {
  360. found = true;
  361. struct rrd_host_function *rdcf = dictionary_acquired_item_value(*item);
  362. if(rrd_collector_running(rdcf->collector)) {
  363. break;
  364. }
  365. else {
  366. dictionary_acquired_item_release(host->functions, *item);
  367. *item = NULL;
  368. }
  369. }
  370. // if s == NULL, set it to the end of the buffer;
  371. // this should happen only the first time
  372. if (unlikely(!s))
  373. s = &buffer[key_length - 1];
  374. // skip a word from the end
  375. while (s >= buffer && !isspace(*s)) *s-- = '\0';
  376. // skip all spaces
  377. while (s >= buffer && isspace(*s)) *s-- = '\0';
  378. }
  379. }
  380. buffer_flush(wb);
  381. if(!(*item)) {
  382. if(found)
  383. return rrd_call_function_error(wb,
  384. "The collector that registered this function, is not currently running.",
  385. HTTP_RESP_SERVICE_UNAVAILABLE);
  386. else
  387. return rrd_call_function_error(wb,
  388. "No collector is supplying this function on this host at this time.",
  389. HTTP_RESP_NOT_FOUND);
  390. }
  391. return HTTP_RESP_OK;
  392. }
  393. // ----------------------------------------------------------------------------
  394. static void rrd_functions_inflight_delete_cb(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused) {
  395. struct rrd_function_inflight *r = value;
  396. // internal_error(true, "FUNCTIONS: transaction '%s' finished", r->transaction);
  397. freez((void *)r->transaction);
  398. freez((void *)r->cmd);
  399. freez((void *)r->sanitized_cmd);
  400. dictionary_acquired_item_release(r->host->functions, r->host_function_acquired);
  401. }
  402. void rrd_functions_inflight_init(void) {
  403. if(rrd_functions_inflight_requests)
  404. return;
  405. rrd_functions_inflight_requests = dictionary_create_advanced(DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE, NULL, sizeof(struct rrd_function_inflight));
  406. dictionary_register_delete_callback(rrd_functions_inflight_requests, rrd_functions_inflight_delete_cb, NULL);
  407. }
  408. void rrd_functions_inflight_destroy(void) {
  409. if(!rrd_functions_inflight_requests)
  410. return;
  411. dictionary_destroy(rrd_functions_inflight_requests);
  412. rrd_functions_inflight_requests = NULL;
  413. }
  414. static void rrd_inflight_async_function_register_canceller_cb(void *register_canceller_cb_data, rrd_function_cancel_cb_t canceller_cb, void *canceller_cb_data) {
  415. struct rrd_function_inflight *r = register_canceller_cb_data;
  416. r->canceller.cb = canceller_cb;
  417. r->canceller.data = canceller_cb_data;
  418. }
  419. static void rrd_inflight_async_function_register_progresser_cb(void *register_progresser_cb_data, rrd_function_progresser_cb_t progresser_cb, void *progresser_cb_data) {
  420. struct rrd_function_inflight *r = register_progresser_cb_data;
  421. r->progresser.cb = progresser_cb;
  422. r->progresser.data = progresser_cb_data;
  423. }
  424. // ----------------------------------------------------------------------------
  425. // waiting for async function completion
  426. struct rrd_function_call_wait {
  427. RRDHOST *host;
  428. const DICTIONARY_ITEM *host_function_acquired;
  429. char *transaction;
  430. bool free_with_signal;
  431. bool data_are_ready;
  432. netdata_mutex_t mutex;
  433. pthread_cond_t cond;
  434. int code;
  435. };
  436. static void rrd_inflight_function_cleanup(RRDHOST *host __maybe_unused,
  437. const char *transaction) {
  438. dictionary_del(rrd_functions_inflight_requests, transaction);
  439. dictionary_garbage_collect(rrd_functions_inflight_requests);
  440. }
  441. static void rrd_function_call_wait_free(struct rrd_function_call_wait *tmp) {
  442. rrd_inflight_function_cleanup(tmp->host, tmp->transaction);
  443. freez(tmp->transaction);
  444. pthread_cond_destroy(&tmp->cond);
  445. netdata_mutex_destroy(&tmp->mutex);
  446. freez(tmp);
  447. }
  448. static void rrd_async_function_signal_when_ready(BUFFER *temp_wb __maybe_unused, int code, void *callback_data) {
  449. struct rrd_function_call_wait *tmp = callback_data;
  450. bool we_should_free = false;
  451. netdata_mutex_lock(&tmp->mutex);
  452. // since we got the mutex,
  453. // the waiting thread is either in pthread_cond_timedwait()
  454. // or gave up and left.
  455. tmp->code = code;
  456. tmp->data_are_ready = true;
  457. if(tmp->free_with_signal)
  458. we_should_free = true;
  459. pthread_cond_signal(&tmp->cond);
  460. netdata_mutex_unlock(&tmp->mutex);
  461. if(we_should_free) {
  462. buffer_free(temp_wb);
  463. rrd_function_call_wait_free(tmp);
  464. }
  465. }
  466. static void rrd_inflight_async_function_nowait_finished(BUFFER *wb, int code, void *data) {
  467. struct rrd_function_inflight *r = data;
  468. if(r->result.cb)
  469. r->result.cb(wb, code, r->result.data);
  470. rrd_inflight_function_cleanup(r->host, r->transaction);
  471. }
  472. static bool rrd_inflight_async_function_is_cancelled(void *data) {
  473. struct rrd_function_inflight *r = data;
  474. return __atomic_load_n(&r->cancelled, __ATOMIC_RELAXED);
  475. }
  476. static inline int rrd_call_function_async_and_dont_wait(struct rrd_function_inflight *r) {
  477. int code = r->rdcf->execute_cb(&r->transaction_uuid, r->result.wb,
  478. &r->stop_monotonic_ut, r->sanitized_cmd, r->rdcf->execute_cb_data,
  479. rrd_inflight_async_function_nowait_finished, r,
  480. r->progress.cb, r->progress.data,
  481. rrd_inflight_async_function_is_cancelled, r,
  482. rrd_inflight_async_function_register_canceller_cb, r,
  483. rrd_inflight_async_function_register_progresser_cb, r);
  484. if(code != HTTP_RESP_OK) {
  485. if (!buffer_strlen(r->result.wb))
  486. rrd_call_function_error(r->result.wb, "Failed to send request to the collector.", code);
  487. rrd_inflight_function_cleanup(r->host, r->transaction);
  488. }
  489. return code;
  490. }
  491. static int rrd_call_function_async_and_wait(struct rrd_function_inflight *r) {
  492. struct rrd_function_call_wait *tmp = mallocz(sizeof(struct rrd_function_call_wait));
  493. tmp->free_with_signal = false;
  494. tmp->data_are_ready = false;
  495. tmp->host = r->host;
  496. tmp->host_function_acquired = r->host_function_acquired;
  497. tmp->transaction = strdupz(r->transaction);
  498. netdata_mutex_init(&tmp->mutex);
  499. pthread_cond_init(&tmp->cond, NULL);
  500. // we need a temporary BUFFER, because we may time out and the caller supplied one may vanish,
  501. // so we create a new one we guarantee will survive until the collector finishes...
  502. bool we_should_free = true;
  503. BUFFER *temp_wb = buffer_create(PLUGINSD_LINE_MAX + 1, &netdata_buffers_statistics.buffers_functions); // we need it because we may give up on it
  504. temp_wb->content_type = r->result.wb->content_type;
  505. int code = r->rdcf->execute_cb(&r->transaction_uuid, temp_wb, &r->stop_monotonic_ut,
  506. r->sanitized_cmd, r->rdcf->execute_cb_data,
  507. // we overwrite the result callbacks,
  508. // so that we can clean up the allocations made
  509. rrd_async_function_signal_when_ready, tmp,
  510. r->progress.cb, r->progress.data,
  511. rrd_inflight_async_function_is_cancelled, r,
  512. rrd_inflight_async_function_register_canceller_cb, r,
  513. rrd_inflight_async_function_register_progresser_cb, r);
  514. if (code == HTTP_RESP_OK) {
  515. netdata_mutex_lock(&tmp->mutex);
  516. bool cancelled = false;
  517. int rc = 0;
  518. while (rc == 0 && !cancelled && !tmp->data_are_ready) {
  519. usec_t now_mono_ut = now_monotonic_usec();
  520. usec_t stop_mono_ut = __atomic_load_n(&r->stop_monotonic_ut, __ATOMIC_RELAXED) + RRDFUNCTIONS_TIMEOUT_EXTENSION_UT;
  521. if(now_mono_ut > stop_mono_ut) {
  522. rc = ETIMEDOUT;
  523. break;
  524. }
  525. // wait for 10ms, and loop again...
  526. struct timespec tp;
  527. clock_gettime(CLOCK_REALTIME, &tp);
  528. tp.tv_nsec += 10 * NSEC_PER_MSEC;
  529. if(tp.tv_nsec > (long)(1 * NSEC_PER_SEC)) {
  530. tp.tv_sec++;
  531. tp.tv_nsec -= 1 * NSEC_PER_SEC;
  532. }
  533. // the mutex is unlocked within pthread_cond_timedwait()
  534. rc = pthread_cond_timedwait(&tmp->cond, &tmp->mutex, &tp);
  535. // the mutex is again ours
  536. if(rc == ETIMEDOUT) {
  537. // 10ms have passed
  538. rc = 0;
  539. if (!tmp->data_are_ready && r->is_cancelled.cb &&
  540. r->is_cancelled.cb(r->is_cancelled.data)) {
  541. // internal_error(true, "FUNCTIONS: transaction '%s' is cancelled while waiting for response",
  542. // r->transaction);
  543. cancelled = true;
  544. rrd_function_cancel_inflight(r);
  545. break;
  546. }
  547. }
  548. }
  549. if (tmp->data_are_ready) {
  550. // we have a response
  551. buffer_fast_strcat(r->result.wb, buffer_tostring(temp_wb), buffer_strlen(temp_wb));
  552. r->result.wb->content_type = temp_wb->content_type;
  553. r->result.wb->expires = temp_wb->expires;
  554. if(r->result.wb->expires)
  555. buffer_cacheable(r->result.wb);
  556. else
  557. buffer_no_cacheable(r->result.wb);
  558. code = tmp->code;
  559. }
  560. else if (rc == ETIMEDOUT || cancelled) {
  561. // timeout
  562. // we will go away and let the callback free the structure
  563. tmp->free_with_signal = true;
  564. we_should_free = false;
  565. if(cancelled)
  566. code = rrd_call_function_error(r->result.wb,
  567. "Request cancelled",
  568. HTTP_RESP_CLIENT_CLOSED_REQUEST);
  569. else
  570. code = rrd_call_function_error(r->result.wb,
  571. "Timeout while waiting for a response from the collector.",
  572. HTTP_RESP_GATEWAY_TIMEOUT);
  573. }
  574. else
  575. code = rrd_call_function_error(r->result.wb,
  576. "Internal error while communicating with the collector",
  577. HTTP_RESP_INTERNAL_SERVER_ERROR);
  578. netdata_mutex_unlock(&tmp->mutex);
  579. }
  580. else {
  581. if(!buffer_strlen(r->result.wb))
  582. rrd_call_function_error(r->result.wb, "The collector returned an error.", code);
  583. }
  584. if (we_should_free) {
  585. rrd_function_call_wait_free(tmp);
  586. buffer_free(temp_wb);
  587. }
  588. return code;
  589. }
  590. static inline int rrd_call_function_async(struct rrd_function_inflight *r, bool wait) {
  591. if(wait)
  592. return rrd_call_function_async_and_wait(r);
  593. else
  594. return rrd_call_function_async_and_dont_wait(r);
  595. }
  596. void call_virtual_function_async(BUFFER *wb, RRDHOST *host, const char *name, const char *payload, rrd_function_result_callback_t callback, void *callback_data);
  597. // ----------------------------------------------------------------------------
  598. int rrd_function_run(RRDHOST *host, BUFFER *result_wb, int timeout_s, HTTP_ACCESS access, const char *cmd,
  599. bool wait, const char *transaction,
  600. rrd_function_result_callback_t result_cb, void *result_cb_data,
  601. rrd_function_progress_cb_t progress_cb, void *progress_cb_data,
  602. rrd_function_is_cancelled_cb_t is_cancelled_cb, void *is_cancelled_cb_data, const char *payload) {
  603. int code;
  604. char sanitized_cmd[PLUGINSD_LINE_MAX + 1];
  605. const DICTIONARY_ITEM *host_function_acquired = NULL;
  606. // ------------------------------------------------------------------------
  607. // find the function
  608. size_t sanitized_cmd_length = sanitize_function_text(sanitized_cmd, cmd, PLUGINSD_LINE_MAX);
  609. if (is_dyncfg_function(sanitized_cmd, DYNCFG_FUNCTION_TYPE_ALL)) {
  610. call_virtual_function_async(result_wb, host, sanitized_cmd, payload, result_cb, result_cb_data);
  611. return HTTP_RESP_OK;
  612. }
  613. code = rrd_call_function_find(host, result_wb, sanitized_cmd, sanitized_cmd_length, &host_function_acquired);
  614. if(code != HTTP_RESP_OK)
  615. return code;
  616. struct rrd_host_function *rdcf = dictionary_acquired_item_value(host_function_acquired);
  617. if(access != HTTP_ACCESS_ADMINS && rdcf->access != HTTP_ACCESS_ANY && access > rdcf->access) {
  618. if(!aclk_connected)
  619. rrd_call_function_error(result_wb, "This Netdata must be connected to Netdata Cloud to access this function.", HTTP_RESP_PRECOND_FAIL);
  620. else if(access >= HTTP_ACCESS_ANY)
  621. rrd_call_function_error(result_wb, "You need to login to the Netdata Cloud space this agent is claimed to, to access this function.", HTTP_RESP_PRECOND_FAIL);
  622. else /* if(access < HTTP_ACCESS_ANY && rdcf->access < access) */
  623. rrd_call_function_error(result_wb, "To access this function you need to be an admin in this Netdata Cloud space.", HTTP_RESP_PRECOND_FAIL);
  624. dictionary_acquired_item_release(host->functions, host_function_acquired);
  625. return HTTP_RESP_PRECOND_FAIL;
  626. }
  627. if(timeout_s <= 0)
  628. timeout_s = rdcf->timeout;
  629. // ------------------------------------------------------------------------
  630. // validate and parse the transaction, or generate a new transaction id
  631. char uuid_str[UUID_COMPACT_STR_LEN];
  632. uuid_t uuid;
  633. if(!transaction || !*transaction || uuid_parse_flexi(transaction, uuid) != 0)
  634. uuid_generate_random(uuid);
  635. uuid_unparse_lower_compact(uuid, uuid_str);
  636. transaction = uuid_str;
  637. // ------------------------------------------------------------------------
  638. // the function can only be executed in async mode
  639. // put the function into the inflight requests
  640. struct rrd_function_inflight t = {
  641. .used = false,
  642. .host = host,
  643. .cmd = strdupz(cmd),
  644. .sanitized_cmd = strdupz(sanitized_cmd),
  645. .sanitized_cmd_length = sanitized_cmd_length,
  646. .transaction = strdupz(transaction),
  647. .timeout = timeout_s,
  648. .cancelled = false,
  649. .stop_monotonic_ut = now_monotonic_usec() + timeout_s * USEC_PER_SEC,
  650. .host_function_acquired = host_function_acquired,
  651. .rdcf = rdcf,
  652. .result = {
  653. .wb = result_wb,
  654. .cb = result_cb,
  655. .data = result_cb_data,
  656. },
  657. .is_cancelled = {
  658. .cb = is_cancelled_cb,
  659. .data = is_cancelled_cb_data,
  660. },
  661. .progress = {
  662. .cb = progress_cb,
  663. .data = progress_cb_data,
  664. },
  665. };
  666. uuid_copy(t.transaction_uuid, uuid);
  667. struct rrd_function_inflight *r = dictionary_set(rrd_functions_inflight_requests, transaction, &t, sizeof(t));
  668. if(r->used) {
  669. nd_log(NDLS_DAEMON, NDLP_NOTICE,
  670. "FUNCTIONS: duplicate transaction '%s', function: '%s'",
  671. t.transaction, t.cmd);
  672. code = rrd_call_function_error(result_wb, "duplicate transaction", HTTP_RESP_BAD_REQUEST);
  673. freez((void *)t.transaction);
  674. freez((void *)t.cmd);
  675. freez((void *)t.sanitized_cmd);
  676. dictionary_acquired_item_release(r->host->functions, t.host_function_acquired);
  677. return code;
  678. }
  679. r->used = true;
  680. // internal_error(true, "FUNCTIONS: transaction '%s' started", r->transaction);
  681. if(r->rdcf->sync) {
  682. // the caller has to wait
  683. code = r->rdcf->execute_cb(&r->transaction_uuid, r->result.wb,
  684. &r->stop_monotonic_ut, r->sanitized_cmd, r->rdcf->execute_cb_data,
  685. r->result.cb, r->result.data,
  686. r->progress.cb, r->progress.data,
  687. r->is_cancelled.cb, r->is_cancelled.data, // it is ok to pass these, we block the caller
  688. NULL, NULL, // no need to register canceller, we will wait
  689. NULL, NULL // ?? do we need a progresser in this case?
  690. );
  691. if(code != HTTP_RESP_OK && !buffer_strlen(result_wb))
  692. rrd_call_function_error(result_wb, "Collector reported error.", code);
  693. rrd_inflight_function_cleanup(host, r->transaction);
  694. return code;
  695. }
  696. return rrd_call_function_async(r, wait);
  697. }
  698. static void rrd_function_cancel_inflight(struct rrd_function_inflight *r) {
  699. if(!r)
  700. return;
  701. bool cancelled = __atomic_load_n(&r->cancelled, __ATOMIC_RELAXED);
  702. if(cancelled) {
  703. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  704. "FUNCTIONS: received a CANCEL request for transaction '%s', but it is already cancelled.",
  705. r->transaction);
  706. return;
  707. }
  708. __atomic_store_n(&r->cancelled, true, __ATOMIC_RELAXED);
  709. if(!rrd_collector_dispatcher_acquire(r->rdcf->collector)) {
  710. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  711. "FUNCTIONS: received a CANCEL request for transaction '%s', but the collector is not running.",
  712. r->transaction);
  713. return;
  714. }
  715. if(r->canceller.cb)
  716. r->canceller.cb(r->canceller.data);
  717. rrd_collector_dispatcher_release(r->rdcf->collector);
  718. }
  719. void rrd_function_cancel(const char *transaction) {
  720. // internal_error(true, "FUNCTIONS: request to cancel transaction '%s'", transaction);
  721. const DICTIONARY_ITEM *item = dictionary_get_and_acquire_item(rrd_functions_inflight_requests, transaction);
  722. if(!item) {
  723. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  724. "FUNCTIONS: received a CANCEL request for transaction '%s', but the transaction is not running.",
  725. transaction);
  726. return;
  727. }
  728. struct rrd_function_inflight *r = dictionary_acquired_item_value(item);
  729. rrd_function_cancel_inflight(r);
  730. dictionary_acquired_item_release(rrd_functions_inflight_requests, item);
  731. }
  732. void rrd_function_progress(const char *transaction) {
  733. const DICTIONARY_ITEM *item = dictionary_get_and_acquire_item(rrd_functions_inflight_requests, transaction);
  734. if(!item) {
  735. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  736. "FUNCTIONS: received a PROGRESS request for transaction '%s', but the transaction is not running.",
  737. transaction);
  738. return;
  739. }
  740. struct rrd_function_inflight *r = dictionary_acquired_item_value(item);
  741. if(!rrd_collector_dispatcher_acquire(r->rdcf->collector)) {
  742. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  743. "FUNCTIONS: received a PROGRESS request for transaction '%s', but the collector is not running.",
  744. transaction);
  745. goto cleanup;
  746. }
  747. functions_stop_monotonic_update_on_progress(&r->stop_monotonic_ut);
  748. if(r->progresser.cb)
  749. r->progresser.cb(r->progresser.data);
  750. rrd_collector_dispatcher_release(r->rdcf->collector);
  751. cleanup:
  752. dictionary_acquired_item_release(rrd_functions_inflight_requests, item);
  753. }
  754. void rrd_function_call_progresser(uuid_t *transaction) {
  755. char str[UUID_COMPACT_STR_LEN];
  756. uuid_unparse_lower_compact(*transaction, str);
  757. rrd_function_progress(str);
  758. }
  759. // ----------------------------------------------------------------------------
  760. static void functions2json(DICTIONARY *functions, BUFFER *wb)
  761. {
  762. struct rrd_host_function *t;
  763. dfe_start_read(functions, t)
  764. {
  765. if (!rrd_collector_running(t->collector))
  766. continue;
  767. buffer_json_member_add_object(wb, t_dfe.name);
  768. {
  769. buffer_json_member_add_string_or_empty(wb, "help", string2str(t->help));
  770. buffer_json_member_add_int64(wb, "timeout", (int64_t) t->timeout);
  771. char options[65];
  772. snprintfz(
  773. options, 64
  774. , "%s%s"
  775. , (t->options & RRD_FUNCTION_LOCAL) ? "LOCAL " : ""
  776. , (t->options & RRD_FUNCTION_GLOBAL) ? "GLOBAL" : ""
  777. );
  778. buffer_json_member_add_string_or_empty(wb, "options", options);
  779. buffer_json_member_add_string_or_empty(wb, "tags", string2str(t->tags));
  780. buffer_json_member_add_string(wb, "access", http_id2access(t->access));
  781. buffer_json_member_add_uint64(wb, "priority", t->priority);
  782. }
  783. buffer_json_object_close(wb);
  784. }
  785. dfe_done(t);
  786. }
  787. void chart_functions2json(RRDSET *st, BUFFER *wb) {
  788. if(!st || !st->functions_view) return;
  789. functions2json(st->functions_view, wb);
  790. }
  791. void host_functions2json(RRDHOST *host, BUFFER *wb) {
  792. if(!host || !host->functions) return;
  793. buffer_json_member_add_object(wb, "functions");
  794. struct rrd_host_function *t;
  795. dfe_start_read(host->functions, t) {
  796. if(!rrd_collector_running(t->collector)) continue;
  797. buffer_json_member_add_object(wb, t_dfe.name);
  798. {
  799. buffer_json_member_add_string(wb, "help", string2str(t->help));
  800. buffer_json_member_add_int64(wb, "timeout", t->timeout);
  801. buffer_json_member_add_array(wb, "options");
  802. {
  803. if (t->options & RRD_FUNCTION_GLOBAL)
  804. buffer_json_add_array_item_string(wb, "GLOBAL");
  805. if (t->options & RRD_FUNCTION_LOCAL)
  806. buffer_json_add_array_item_string(wb, "LOCAL");
  807. }
  808. buffer_json_array_close(wb);
  809. buffer_json_member_add_string(wb, "tags", string2str(t->tags));
  810. buffer_json_member_add_string(wb, "access", http_id2access(t->access));
  811. buffer_json_member_add_uint64(wb, "priority", t->priority);
  812. }
  813. buffer_json_object_close(wb);
  814. }
  815. dfe_done(t);
  816. buffer_json_object_close(wb);
  817. }
  818. void chart_functions_to_dict(DICTIONARY *rrdset_functions_view, DICTIONARY *dst, void *value, size_t value_size) {
  819. if(!rrdset_functions_view || !dst) return;
  820. struct rrd_host_function *t;
  821. dfe_start_read(rrdset_functions_view, t) {
  822. if(!rrd_collector_running(t->collector)) continue;
  823. dictionary_set(dst, t_dfe.name, value, value_size);
  824. }
  825. dfe_done(t);
  826. }
  827. void host_functions_to_dict(RRDHOST *host, DICTIONARY *dst, void *value, size_t value_size, STRING **help, STRING **tags, HTTP_ACCESS *access, int *priority) {
  828. if(!host || !host->functions || !dictionary_entries(host->functions) || !dst) return;
  829. struct rrd_host_function *t;
  830. dfe_start_read(host->functions, t) {
  831. if(!rrd_collector_running(t->collector)) continue;
  832. if(help)
  833. *help = t->help;
  834. if(tags)
  835. *tags = t->tags;
  836. if(access)
  837. *access = t->access;
  838. if(priority)
  839. *priority = t->priority;
  840. dictionary_set(dst, t_dfe.name, value, value_size);
  841. }
  842. dfe_done(t);
  843. }
  844. // ----------------------------------------------------------------------------
  845. int rrdhost_function_progress(uuid_t *transaction __maybe_unused, BUFFER *wb,
  846. usec_t *stop_monotonic_ut __maybe_unused, const char *function __maybe_unused,
  847. void *collector_data __maybe_unused,
  848. rrd_function_result_callback_t result_cb, void *result_cb_data,
  849. rrd_function_progress_cb_t progress_cb __maybe_unused, void *progress_cb_data __maybe_unused,
  850. rrd_function_is_cancelled_cb_t is_cancelled_cb, void *is_cancelled_cb_data,
  851. rrd_function_register_canceller_cb_t register_canceller_cb __maybe_unused,
  852. void *register_canceller_cb_data __maybe_unused,
  853. rrd_function_register_progresser_cb_t register_progresser_cb __maybe_unused,
  854. void *register_progresser_cb_data __maybe_unused) {
  855. int response = progress_function_result(wb, rrdhost_hostname(localhost));
  856. if(is_cancelled_cb && is_cancelled_cb(is_cancelled_cb_data)) {
  857. buffer_flush(wb);
  858. response = HTTP_RESP_CLIENT_CLOSED_REQUEST;
  859. }
  860. if(result_cb)
  861. result_cb(wb, response, result_cb_data);
  862. return response;
  863. }
  864. int rrdhost_function_streaming(uuid_t *transaction __maybe_unused, BUFFER *wb,
  865. usec_t *stop_monotonic_ut __maybe_unused, const char *function __maybe_unused,
  866. void *collector_data __maybe_unused,
  867. rrd_function_result_callback_t result_cb, void *result_cb_data,
  868. rrd_function_progress_cb_t progress_cb __maybe_unused, void *progress_cb_data __maybe_unused,
  869. rrd_function_is_cancelled_cb_t is_cancelled_cb, void *is_cancelled_cb_data,
  870. rrd_function_register_canceller_cb_t register_canceller_cb __maybe_unused,
  871. void *register_canceller_cb_data __maybe_unused,
  872. rrd_function_register_progresser_cb_t register_progresser_cb __maybe_unused,
  873. void *register_progresser_cb_data __maybe_unused) {
  874. time_t now = now_realtime_sec();
  875. buffer_flush(wb);
  876. wb->content_type = CT_APPLICATION_JSON;
  877. buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_DEFAULT);
  878. buffer_json_member_add_string(wb, "hostname", rrdhost_hostname(localhost));
  879. buffer_json_member_add_uint64(wb, "status", HTTP_RESP_OK);
  880. buffer_json_member_add_string(wb, "type", "table");
  881. buffer_json_member_add_time_t(wb, "update_every", 1);
  882. buffer_json_member_add_string(wb, "help", RRDFUNCTIONS_STREAMING_HELP);
  883. buffer_json_member_add_array(wb, "data");
  884. size_t max_sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_MAX] = { 0 };
  885. size_t max_db_metrics = 0, max_db_instances = 0, max_db_contexts = 0;
  886. size_t max_collection_replication_instances = 0, max_streaming_replication_instances = 0;
  887. size_t max_ml_anomalous = 0, max_ml_normal = 0, max_ml_trained = 0, max_ml_pending = 0, max_ml_silenced = 0;
  888. {
  889. RRDHOST *host;
  890. dfe_start_read(rrdhost_root_index, host) {
  891. RRDHOST_STATUS s;
  892. rrdhost_status(host, now, &s);
  893. buffer_json_add_array_item_array(wb);
  894. if(s.db.metrics > max_db_metrics)
  895. max_db_metrics = s.db.metrics;
  896. if(s.db.instances > max_db_instances)
  897. max_db_instances = s.db.instances;
  898. if(s.db.contexts > max_db_contexts)
  899. max_db_contexts = s.db.contexts;
  900. if(s.ingest.replication.instances > max_collection_replication_instances)
  901. max_collection_replication_instances = s.ingest.replication.instances;
  902. if(s.stream.replication.instances > max_streaming_replication_instances)
  903. max_streaming_replication_instances = s.stream.replication.instances;
  904. for(int i = 0; i < STREAM_TRAFFIC_TYPE_MAX ;i++) {
  905. if (s.stream.sent_bytes_on_this_connection_per_type[i] >
  906. max_sent_bytes_on_this_connection_per_type[i])
  907. max_sent_bytes_on_this_connection_per_type[i] =
  908. s.stream.sent_bytes_on_this_connection_per_type[i];
  909. }
  910. // retention
  911. buffer_json_add_array_item_string(wb, rrdhost_hostname(s.host)); // Node
  912. buffer_json_add_array_item_uint64(wb, s.db.first_time_s * MSEC_PER_SEC); // dbFrom
  913. buffer_json_add_array_item_uint64(wb, s.db.last_time_s * MSEC_PER_SEC); // dbTo
  914. if(s.db.first_time_s && s.db.last_time_s && s.db.last_time_s > s.db.first_time_s)
  915. buffer_json_add_array_item_uint64(wb, s.db.last_time_s - s.db.first_time_s); // dbDuration
  916. else
  917. buffer_json_add_array_item_string(wb, NULL); // dbDuration
  918. buffer_json_add_array_item_uint64(wb, s.db.metrics); // dbMetrics
  919. buffer_json_add_array_item_uint64(wb, s.db.instances); // dbInstances
  920. buffer_json_add_array_item_uint64(wb, s.db.contexts); // dbContexts
  921. // statuses
  922. buffer_json_add_array_item_string(wb, rrdhost_ingest_status_to_string(s.ingest.status)); // InStatus
  923. buffer_json_add_array_item_string(wb, rrdhost_streaming_status_to_string(s.stream.status)); // OutStatus
  924. buffer_json_add_array_item_string(wb, rrdhost_ml_status_to_string(s.ml.status)); // MLStatus
  925. // collection
  926. if(s.ingest.since) {
  927. buffer_json_add_array_item_uint64(wb, s.ingest.since * MSEC_PER_SEC); // InSince
  928. buffer_json_add_array_item_time_t(wb, s.now - s.ingest.since); // InAge
  929. }
  930. else {
  931. buffer_json_add_array_item_string(wb, NULL); // InSince
  932. buffer_json_add_array_item_string(wb, NULL); // InAge
  933. }
  934. buffer_json_add_array_item_string(wb, stream_handshake_error_to_string(s.ingest.reason)); // InReason
  935. buffer_json_add_array_item_uint64(wb, s.ingest.hops); // InHops
  936. buffer_json_add_array_item_double(wb, s.ingest.replication.completion); // InReplCompletion
  937. buffer_json_add_array_item_uint64(wb, s.ingest.replication.instances); // InReplInstances
  938. buffer_json_add_array_item_string(wb, s.ingest.peers.local.ip); // InLocalIP
  939. buffer_json_add_array_item_uint64(wb, s.ingest.peers.local.port); // InLocalPort
  940. buffer_json_add_array_item_string(wb, s.ingest.peers.peer.ip); // InRemoteIP
  941. buffer_json_add_array_item_uint64(wb, s.ingest.peers.peer.port); // InRemotePort
  942. buffer_json_add_array_item_string(wb, s.ingest.ssl ? "SSL" : "PLAIN"); // InSSL
  943. stream_capabilities_to_json_array(wb, s.ingest.capabilities, NULL); // InCapabilities
  944. // streaming
  945. if(s.stream.since) {
  946. buffer_json_add_array_item_uint64(wb, s.stream.since * MSEC_PER_SEC); // OutSince
  947. buffer_json_add_array_item_time_t(wb, s.now - s.stream.since); // OutAge
  948. }
  949. else {
  950. buffer_json_add_array_item_string(wb, NULL); // OutSince
  951. buffer_json_add_array_item_string(wb, NULL); // OutAge
  952. }
  953. buffer_json_add_array_item_string(wb, stream_handshake_error_to_string(s.stream.reason)); // OutReason
  954. buffer_json_add_array_item_uint64(wb, s.stream.hops); // OutHops
  955. buffer_json_add_array_item_double(wb, s.stream.replication.completion); // OutReplCompletion
  956. buffer_json_add_array_item_uint64(wb, s.stream.replication.instances); // OutReplInstances
  957. buffer_json_add_array_item_string(wb, s.stream.peers.local.ip); // OutLocalIP
  958. buffer_json_add_array_item_uint64(wb, s.stream.peers.local.port); // OutLocalPort
  959. buffer_json_add_array_item_string(wb, s.stream.peers.peer.ip); // OutRemoteIP
  960. buffer_json_add_array_item_uint64(wb, s.stream.peers.peer.port); // OutRemotePort
  961. buffer_json_add_array_item_string(wb, s.stream.ssl ? "SSL" : "PLAIN"); // OutSSL
  962. buffer_json_add_array_item_string(wb, s.stream.compression ? "COMPRESSED" : "UNCOMPRESSED"); // OutCompression
  963. stream_capabilities_to_json_array(wb, s.stream.capabilities, NULL); // OutCapabilities
  964. buffer_json_add_array_item_uint64(wb, s.stream.sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_DATA]);
  965. buffer_json_add_array_item_uint64(wb, s.stream.sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_METADATA]);
  966. buffer_json_add_array_item_uint64(wb, s.stream.sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_REPLICATION]);
  967. buffer_json_add_array_item_uint64(wb, s.stream.sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_FUNCTIONS]);
  968. buffer_json_add_array_item_array(wb); // OutAttemptHandshake
  969. time_t last_attempt = 0;
  970. for(struct rrdpush_destinations *d = host->destinations; d ; d = d->next) {
  971. if(d->since > last_attempt)
  972. last_attempt = d->since;
  973. buffer_json_add_array_item_string(wb, stream_handshake_error_to_string(d->reason));
  974. }
  975. buffer_json_array_close(wb); // // OutAttemptHandshake
  976. if(!last_attempt) {
  977. buffer_json_add_array_item_string(wb, NULL); // OutAttemptSince
  978. buffer_json_add_array_item_string(wb, NULL); // OutAttemptAge
  979. }
  980. else {
  981. buffer_json_add_array_item_uint64(wb, last_attempt * 1000); // OutAttemptSince
  982. buffer_json_add_array_item_time_t(wb, s.now - last_attempt); // OutAttemptAge
  983. }
  984. // ML
  985. if(s.ml.status == RRDHOST_ML_STATUS_RUNNING) {
  986. buffer_json_add_array_item_uint64(wb, s.ml.metrics.anomalous); // MlAnomalous
  987. buffer_json_add_array_item_uint64(wb, s.ml.metrics.normal); // MlNormal
  988. buffer_json_add_array_item_uint64(wb, s.ml.metrics.trained); // MlTrained
  989. buffer_json_add_array_item_uint64(wb, s.ml.metrics.pending); // MlPending
  990. buffer_json_add_array_item_uint64(wb, s.ml.metrics.silenced); // MlSilenced
  991. if(s.ml.metrics.anomalous > max_ml_anomalous)
  992. max_ml_anomalous = s.ml.metrics.anomalous;
  993. if(s.ml.metrics.normal > max_ml_normal)
  994. max_ml_normal = s.ml.metrics.normal;
  995. if(s.ml.metrics.trained > max_ml_trained)
  996. max_ml_trained = s.ml.metrics.trained;
  997. if(s.ml.metrics.pending > max_ml_pending)
  998. max_ml_pending = s.ml.metrics.pending;
  999. if(s.ml.metrics.silenced > max_ml_silenced)
  1000. max_ml_silenced = s.ml.metrics.silenced;
  1001. }
  1002. else {
  1003. buffer_json_add_array_item_string(wb, NULL); // MlAnomalous
  1004. buffer_json_add_array_item_string(wb, NULL); // MlNormal
  1005. buffer_json_add_array_item_string(wb, NULL); // MlTrained
  1006. buffer_json_add_array_item_string(wb, NULL); // MlPending
  1007. buffer_json_add_array_item_string(wb, NULL); // MlSilenced
  1008. }
  1009. // close
  1010. buffer_json_array_close(wb);
  1011. }
  1012. dfe_done(host);
  1013. }
  1014. buffer_json_array_close(wb); // data
  1015. buffer_json_member_add_object(wb, "columns");
  1016. {
  1017. size_t field_id = 0;
  1018. // Node
  1019. buffer_rrdf_table_add_field(wb, field_id++, "Node", "Node's Hostname",
  1020. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1021. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1022. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1023. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY | RRDF_FIELD_OPTS_STICKY,
  1024. NULL);
  1025. buffer_rrdf_table_add_field(wb, field_id++, "dbFrom", "DB Data Retention From",
  1026. RRDF_FIELD_TYPE_TIMESTAMP, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DATETIME_MS,
  1027. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1028. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1029. RRDF_FIELD_OPTS_NONE, NULL);
  1030. buffer_rrdf_table_add_field(wb, field_id++, "dbTo", "DB Data Retention To",
  1031. RRDF_FIELD_TYPE_TIMESTAMP, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DATETIME_MS,
  1032. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1033. RRDF_FIELD_SUMMARY_MAX, RRDF_FIELD_FILTER_RANGE,
  1034. RRDF_FIELD_OPTS_NONE, NULL);
  1035. buffer_rrdf_table_add_field(wb, field_id++, "dbDuration", "DB Data Retention Duration",
  1036. RRDF_FIELD_TYPE_DURATION, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DURATION_S,
  1037. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1038. RRDF_FIELD_SUMMARY_MAX, RRDF_FIELD_FILTER_RANGE,
  1039. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1040. buffer_rrdf_table_add_field(wb, field_id++, "dbMetrics", "Time-series Metrics in the DB",
  1041. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1042. 0, NULL, (double)max_db_metrics, RRDF_FIELD_SORT_DESCENDING, NULL,
  1043. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1044. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1045. buffer_rrdf_table_add_field(wb, field_id++, "dbInstances", "Instances in the DB",
  1046. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1047. 0, NULL, (double)max_db_instances, RRDF_FIELD_SORT_DESCENDING, NULL,
  1048. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1049. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1050. buffer_rrdf_table_add_field(wb, field_id++, "dbContexts", "Contexts in the DB",
  1051. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1052. 0, NULL, (double)max_db_contexts, RRDF_FIELD_SORT_DESCENDING, NULL,
  1053. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1054. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1055. // --- statuses ---
  1056. buffer_rrdf_table_add_field(wb, field_id++, "InStatus", "Data Collection Online Status",
  1057. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1058. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1059. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1060. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1061. buffer_rrdf_table_add_field(wb, field_id++, "OutStatus", "Streaming Online Status",
  1062. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1063. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1064. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1065. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1066. buffer_rrdf_table_add_field(wb, field_id++, "MlStatus", "ML Status",
  1067. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1068. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1069. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1070. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1071. // --- collection ---
  1072. buffer_rrdf_table_add_field(wb, field_id++, "InSince", "Last Data Collection Status Change",
  1073. RRDF_FIELD_TYPE_TIMESTAMP, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DATETIME_MS,
  1074. 0, NULL, NAN, RRDF_FIELD_SORT_DESCENDING, NULL,
  1075. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1076. RRDF_FIELD_OPTS_NONE, NULL);
  1077. buffer_rrdf_table_add_field(wb, field_id++, "InAge", "Last Data Collection Online Status Change Age",
  1078. RRDF_FIELD_TYPE_DURATION, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DURATION_S,
  1079. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1080. RRDF_FIELD_SUMMARY_MAX, RRDF_FIELD_FILTER_RANGE,
  1081. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1082. buffer_rrdf_table_add_field(wb, field_id++, "InReason", "Data Collection Online Status Reason",
  1083. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1084. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1085. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1086. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1087. buffer_rrdf_table_add_field(wb, field_id++, "InHops", "Data Collection Distance Hops from Origin Node",
  1088. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1089. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1090. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1091. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1092. buffer_rrdf_table_add_field(wb, field_id++, "InReplCompletion", "Inbound Replication Completion",
  1093. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER,
  1094. 1, "%", 100.0, RRDF_FIELD_SORT_DESCENDING, NULL,
  1095. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1096. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1097. buffer_rrdf_table_add_field(wb, field_id++, "InReplInstances", "Inbound Replicating Instances",
  1098. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1099. 0, "instances", (double)max_collection_replication_instances, RRDF_FIELD_SORT_DESCENDING,
  1100. NULL,
  1101. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1102. RRDF_FIELD_OPTS_NONE, NULL);
  1103. buffer_rrdf_table_add_field(wb, field_id++, "InLocalIP", "Inbound Local IP",
  1104. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1105. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1106. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1107. RRDF_FIELD_OPTS_NONE, NULL);
  1108. buffer_rrdf_table_add_field(wb, field_id++, "InLocalPort", "Inbound Local Port",
  1109. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1110. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1111. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_RANGE,
  1112. RRDF_FIELD_OPTS_NONE, NULL);
  1113. buffer_rrdf_table_add_field(wb, field_id++, "InRemoteIP", "Inbound Remote IP",
  1114. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1115. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1116. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1117. RRDF_FIELD_OPTS_NONE, NULL);
  1118. buffer_rrdf_table_add_field(wb, field_id++, "InRemotePort", "Inbound Remote Port",
  1119. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1120. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1121. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_RANGE,
  1122. RRDF_FIELD_OPTS_NONE, NULL);
  1123. buffer_rrdf_table_add_field(wb, field_id++, "InSSL", "Inbound SSL Connection",
  1124. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1125. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1126. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1127. RRDF_FIELD_OPTS_NONE, NULL);
  1128. buffer_rrdf_table_add_field(wb, field_id++, "InCapabilities", "Inbound Connection Capabilities",
  1129. RRDF_FIELD_TYPE_ARRAY, RRDF_FIELD_VISUAL_PILL, RRDF_FIELD_TRANSFORM_NONE,
  1130. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1131. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1132. RRDF_FIELD_OPTS_NONE, NULL);
  1133. // --- streaming ---
  1134. buffer_rrdf_table_add_field(wb, field_id++, "OutSince", "Last Streaming Status Change",
  1135. RRDF_FIELD_TYPE_TIMESTAMP, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DATETIME_MS,
  1136. 0, NULL, NAN, RRDF_FIELD_SORT_DESCENDING, NULL,
  1137. RRDF_FIELD_SUMMARY_MAX, RRDF_FIELD_FILTER_RANGE,
  1138. RRDF_FIELD_OPTS_NONE, NULL);
  1139. buffer_rrdf_table_add_field(wb, field_id++, "OutAge", "Last Streaming Status Change Age",
  1140. RRDF_FIELD_TYPE_DURATION, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DURATION_S,
  1141. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1142. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1143. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1144. buffer_rrdf_table_add_field(wb, field_id++, "OutReason", "Streaming Status Reason",
  1145. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1146. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1147. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1148. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1149. buffer_rrdf_table_add_field(wb, field_id++, "OutHops", "Streaming Distance Hops from Origin Node",
  1150. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1151. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1152. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1153. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1154. buffer_rrdf_table_add_field(wb, field_id++, "OutReplCompletion", "Outbound Replication Completion",
  1155. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER,
  1156. 1, "%", 100.0, RRDF_FIELD_SORT_DESCENDING, NULL,
  1157. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1158. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1159. buffer_rrdf_table_add_field(wb, field_id++, "OutReplInstances", "Outbound Replicating Instances",
  1160. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1161. 0, "instances", (double)max_streaming_replication_instances, RRDF_FIELD_SORT_DESCENDING,
  1162. NULL,
  1163. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1164. RRDF_FIELD_OPTS_NONE, NULL);
  1165. buffer_rrdf_table_add_field(wb, field_id++, "OutLocalIP", "Outbound Local IP",
  1166. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1167. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1168. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1169. RRDF_FIELD_OPTS_NONE, NULL);
  1170. buffer_rrdf_table_add_field(wb, field_id++, "OutLocalPort", "Outbound Local Port",
  1171. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1172. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1173. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_RANGE,
  1174. RRDF_FIELD_OPTS_NONE, NULL);
  1175. buffer_rrdf_table_add_field(wb, field_id++, "OutRemoteIP", "Outbound Remote IP",
  1176. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1177. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1178. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1179. RRDF_FIELD_OPTS_NONE, NULL);
  1180. buffer_rrdf_table_add_field(wb, field_id++, "OutRemotePort", "Outbound Remote Port",
  1181. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1182. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1183. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_RANGE,
  1184. RRDF_FIELD_OPTS_NONE, NULL);
  1185. buffer_rrdf_table_add_field(wb, field_id++, "OutSSL", "Outbound SSL Connection",
  1186. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1187. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1188. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1189. RRDF_FIELD_OPTS_NONE, NULL);
  1190. buffer_rrdf_table_add_field(wb, field_id++, "OutCompression", "Outbound Compressed Connection",
  1191. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1192. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1193. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1194. RRDF_FIELD_OPTS_NONE, NULL);
  1195. buffer_rrdf_table_add_field(wb, field_id++, "OutCapabilities", "Outbound Connection Capabilities",
  1196. RRDF_FIELD_TYPE_ARRAY, RRDF_FIELD_VISUAL_PILL, RRDF_FIELD_TRANSFORM_NONE,
  1197. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1198. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1199. RRDF_FIELD_OPTS_NONE, NULL);
  1200. buffer_rrdf_table_add_field(wb, field_id++, "OutTrafficData", "Outbound Metric Data Traffic",
  1201. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1202. 0, "bytes", (double)max_sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_DATA],
  1203. RRDF_FIELD_SORT_DESCENDING, NULL,
  1204. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1205. RRDF_FIELD_OPTS_NONE, NULL);
  1206. buffer_rrdf_table_add_field(wb, field_id++, "OutTrafficMetadata", "Outbound Metric Metadata Traffic",
  1207. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1208. 0, "bytes",
  1209. (double)max_sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_METADATA],
  1210. RRDF_FIELD_SORT_DESCENDING, NULL,
  1211. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1212. RRDF_FIELD_OPTS_NONE, NULL);
  1213. buffer_rrdf_table_add_field(wb, field_id++, "OutTrafficReplication", "Outbound Metric Replication Traffic",
  1214. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1215. 0, "bytes",
  1216. (double)max_sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_REPLICATION],
  1217. RRDF_FIELD_SORT_DESCENDING, NULL,
  1218. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1219. RRDF_FIELD_OPTS_NONE, NULL);
  1220. buffer_rrdf_table_add_field(wb, field_id++, "OutTrafficFunctions", "Outbound Metric Functions Traffic",
  1221. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1222. 0, "bytes",
  1223. (double)max_sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_FUNCTIONS],
  1224. RRDF_FIELD_SORT_DESCENDING, NULL,
  1225. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1226. RRDF_FIELD_OPTS_NONE, NULL);
  1227. buffer_rrdf_table_add_field(wb, field_id++, "OutAttemptHandshake",
  1228. "Outbound Connection Attempt Handshake Status",
  1229. RRDF_FIELD_TYPE_ARRAY, RRDF_FIELD_VISUAL_PILL, RRDF_FIELD_TRANSFORM_NONE,
  1230. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1231. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1232. RRDF_FIELD_OPTS_NONE, NULL);
  1233. buffer_rrdf_table_add_field(wb, field_id++, "OutAttemptSince",
  1234. "Last Outbound Connection Attempt Status Change Time",
  1235. RRDF_FIELD_TYPE_TIMESTAMP, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DATETIME_MS,
  1236. 0, NULL, NAN, RRDF_FIELD_SORT_DESCENDING, NULL,
  1237. RRDF_FIELD_SUMMARY_MAX, RRDF_FIELD_FILTER_RANGE,
  1238. RRDF_FIELD_OPTS_NONE, NULL);
  1239. buffer_rrdf_table_add_field(wb, field_id++, "OutAttemptAge",
  1240. "Last Outbound Connection Attempt Status Change Age",
  1241. RRDF_FIELD_TYPE_DURATION, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DURATION_S,
  1242. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1243. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1244. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1245. // --- ML ---
  1246. buffer_rrdf_table_add_field(wb, field_id++, "MlAnomalous", "Number of Anomalous Metrics",
  1247. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1248. 0, "metrics",
  1249. (double)max_ml_anomalous,
  1250. RRDF_FIELD_SORT_DESCENDING, NULL,
  1251. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1252. RRDF_FIELD_OPTS_NONE, NULL);
  1253. buffer_rrdf_table_add_field(wb, field_id++, "MlNormal", "Number of Not Anomalous Metrics",
  1254. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1255. 0, "metrics",
  1256. (double)max_ml_normal,
  1257. RRDF_FIELD_SORT_DESCENDING, NULL,
  1258. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1259. RRDF_FIELD_OPTS_NONE, NULL);
  1260. buffer_rrdf_table_add_field(wb, field_id++, "MlTrained", "Number of Trained Metrics",
  1261. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1262. 0, "metrics",
  1263. (double)max_ml_trained,
  1264. RRDF_FIELD_SORT_DESCENDING, NULL,
  1265. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1266. RRDF_FIELD_OPTS_NONE, NULL);
  1267. buffer_rrdf_table_add_field(wb, field_id++, "MlPending", "Number of Pending Metrics",
  1268. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1269. 0, "metrics",
  1270. (double)max_ml_pending,
  1271. RRDF_FIELD_SORT_DESCENDING, NULL,
  1272. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1273. RRDF_FIELD_OPTS_NONE, NULL);
  1274. buffer_rrdf_table_add_field(wb, field_id++, "MlSilenced", "Number of Silenced Metrics",
  1275. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1276. 0, "metrics",
  1277. (double)max_ml_silenced,
  1278. RRDF_FIELD_SORT_DESCENDING, NULL,
  1279. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1280. RRDF_FIELD_OPTS_NONE, NULL);
  1281. }
  1282. buffer_json_object_close(wb); // columns
  1283. buffer_json_member_add_string(wb, "default_sort_column", "Node");
  1284. buffer_json_member_add_object(wb, "charts");
  1285. {
  1286. // Data Collection Age chart
  1287. buffer_json_member_add_object(wb, "InAge");
  1288. {
  1289. buffer_json_member_add_string(wb, "name", "Data Collection Age");
  1290. buffer_json_member_add_string(wb, "type", "stacked-bar");
  1291. buffer_json_member_add_array(wb, "columns");
  1292. {
  1293. buffer_json_add_array_item_string(wb, "InAge");
  1294. }
  1295. buffer_json_array_close(wb);
  1296. }
  1297. buffer_json_object_close(wb);
  1298. // Streaming Age chart
  1299. buffer_json_member_add_object(wb, "OutAge");
  1300. {
  1301. buffer_json_member_add_string(wb, "name", "Streaming Age");
  1302. buffer_json_member_add_string(wb, "type", "stacked-bar");
  1303. buffer_json_member_add_array(wb, "columns");
  1304. {
  1305. buffer_json_add_array_item_string(wb, "OutAge");
  1306. }
  1307. buffer_json_array_close(wb);
  1308. }
  1309. buffer_json_object_close(wb);
  1310. // DB Duration
  1311. buffer_json_member_add_object(wb, "dbDuration");
  1312. {
  1313. buffer_json_member_add_string(wb, "name", "Retention Duration");
  1314. buffer_json_member_add_string(wb, "type", "stacked-bar");
  1315. buffer_json_member_add_array(wb, "columns");
  1316. {
  1317. buffer_json_add_array_item_string(wb, "dbDuration");
  1318. }
  1319. buffer_json_array_close(wb);
  1320. }
  1321. buffer_json_object_close(wb);
  1322. }
  1323. buffer_json_object_close(wb); // charts
  1324. buffer_json_member_add_array(wb, "default_charts");
  1325. {
  1326. buffer_json_add_array_item_array(wb);
  1327. buffer_json_add_array_item_string(wb, "InAge");
  1328. buffer_json_add_array_item_string(wb, "Node");
  1329. buffer_json_array_close(wb);
  1330. buffer_json_add_array_item_array(wb);
  1331. buffer_json_add_array_item_string(wb, "OutAge");
  1332. buffer_json_add_array_item_string(wb, "Node");
  1333. buffer_json_array_close(wb);
  1334. }
  1335. buffer_json_array_close(wb);
  1336. buffer_json_member_add_object(wb, "group_by");
  1337. {
  1338. buffer_json_member_add_object(wb, "Node");
  1339. {
  1340. buffer_json_member_add_string(wb, "name", "Node");
  1341. buffer_json_member_add_array(wb, "columns");
  1342. {
  1343. buffer_json_add_array_item_string(wb, "Node");
  1344. }
  1345. buffer_json_array_close(wb);
  1346. }
  1347. buffer_json_object_close(wb);
  1348. buffer_json_member_add_object(wb, "InStatus");
  1349. {
  1350. buffer_json_member_add_string(wb, "name", "Nodes by Collection Status");
  1351. buffer_json_member_add_array(wb, "columns");
  1352. {
  1353. buffer_json_add_array_item_string(wb, "InStatus");
  1354. }
  1355. buffer_json_array_close(wb);
  1356. }
  1357. buffer_json_object_close(wb);
  1358. buffer_json_member_add_object(wb, "OutStatus");
  1359. {
  1360. buffer_json_member_add_string(wb, "name", "Nodes by Streaming Status");
  1361. buffer_json_member_add_array(wb, "columns");
  1362. {
  1363. buffer_json_add_array_item_string(wb, "OutStatus");
  1364. }
  1365. buffer_json_array_close(wb);
  1366. }
  1367. buffer_json_object_close(wb);
  1368. buffer_json_member_add_object(wb, "MlStatus");
  1369. {
  1370. buffer_json_member_add_string(wb, "name", "Nodes by ML Status");
  1371. buffer_json_member_add_array(wb, "columns");
  1372. {
  1373. buffer_json_add_array_item_string(wb, "MlStatus");
  1374. }
  1375. buffer_json_array_close(wb);
  1376. }
  1377. buffer_json_object_close(wb);
  1378. buffer_json_member_add_object(wb, "InRemoteIP");
  1379. {
  1380. buffer_json_member_add_string(wb, "name", "Nodes by Inbound IP");
  1381. buffer_json_member_add_array(wb, "columns");
  1382. {
  1383. buffer_json_add_array_item_string(wb, "InRemoteIP");
  1384. }
  1385. buffer_json_array_close(wb);
  1386. }
  1387. buffer_json_object_close(wb);
  1388. buffer_json_member_add_object(wb, "OutRemoteIP");
  1389. {
  1390. buffer_json_member_add_string(wb, "name", "Nodes by Outbound IP");
  1391. buffer_json_member_add_array(wb, "columns");
  1392. {
  1393. buffer_json_add_array_item_string(wb, "OutRemoteIP");
  1394. }
  1395. buffer_json_array_close(wb);
  1396. }
  1397. buffer_json_object_close(wb);
  1398. }
  1399. buffer_json_object_close(wb); // group_by
  1400. buffer_json_member_add_time_t(wb, "expires", now_realtime_sec() + 1);
  1401. buffer_json_finalize(wb);
  1402. int response = HTTP_RESP_OK;
  1403. if(is_cancelled_cb && is_cancelled_cb(is_cancelled_cb_data)) {
  1404. buffer_flush(wb);
  1405. response = HTTP_RESP_CLIENT_CLOSED_REQUEST;
  1406. }
  1407. if(result_cb)
  1408. result_cb(wb, response, result_cb_data);
  1409. return response;
  1410. }