rrdfunctions.c 76 KB

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