rrdfunctions.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  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. result_cb, result_cb_data,
  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_COMPACT_STR_LEN];
  843. if(!transaction) {
  844. uuid_t uuid;
  845. uuid_generate_random(uuid);
  846. uuid_unparse_lower_compact(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)
  919. {
  920. struct rrd_host_function *t;
  921. dfe_start_read(functions, t)
  922. {
  923. if (!rrd_collector_running(t->collector))
  924. continue;
  925. buffer_json_member_add_object(wb, t_dfe.name);
  926. buffer_json_member_add_string_or_empty(wb, "help", string2str(t->help));
  927. buffer_json_member_add_int64(wb, "timeout", (int64_t)t->timeout);
  928. char options[65];
  929. snprintfz(
  930. options,
  931. 64,
  932. "%s%s",
  933. (t->options & RRD_FUNCTION_LOCAL) ? "LOCAL " : "",
  934. (t->options & RRD_FUNCTION_GLOBAL) ? "GLOBAL" : "");
  935. buffer_json_member_add_string_or_empty(wb, "options", options);
  936. buffer_json_object_close(wb);
  937. }
  938. dfe_done(t);
  939. }
  940. void chart_functions2json(RRDSET *st, BUFFER *wb) {
  941. if(!st || !st->functions_view) return;
  942. functions2json(st->functions_view, wb);
  943. }
  944. void host_functions2json(RRDHOST *host, BUFFER *wb) {
  945. if(!host || !host->functions) return;
  946. buffer_json_member_add_object(wb, "functions");
  947. struct rrd_host_function *t;
  948. dfe_start_read(host->functions, t) {
  949. if(!rrd_collector_running(t->collector)) continue;
  950. buffer_json_member_add_object(wb, t_dfe.name);
  951. buffer_json_member_add_string(wb, "help", string2str(t->help));
  952. buffer_json_member_add_int64(wb, "timeout", t->timeout);
  953. buffer_json_member_add_array(wb, "options");
  954. if(t->options & RRD_FUNCTION_GLOBAL)
  955. buffer_json_add_array_item_string(wb, "GLOBAL");
  956. if(t->options & RRD_FUNCTION_LOCAL)
  957. buffer_json_add_array_item_string(wb, "LOCAL");
  958. buffer_json_array_close(wb);
  959. buffer_json_object_close(wb);
  960. }
  961. dfe_done(t);
  962. buffer_json_object_close(wb);
  963. }
  964. void chart_functions_to_dict(DICTIONARY *rrdset_functions_view, DICTIONARY *dst, void *value, size_t value_size) {
  965. if(!rrdset_functions_view || !dst) return;
  966. struct rrd_host_function *t;
  967. dfe_start_read(rrdset_functions_view, t) {
  968. if(!rrd_collector_running(t->collector)) continue;
  969. dictionary_set(dst, t_dfe.name, value, value_size);
  970. }
  971. dfe_done(t);
  972. }
  973. void host_functions_to_dict(RRDHOST *host, DICTIONARY *dst, void *value, size_t value_size, STRING **help) {
  974. if(!host || !host->functions || !dictionary_entries(host->functions) || !dst) return;
  975. struct rrd_host_function *t;
  976. dfe_start_read(host->functions, t) {
  977. if(!rrd_collector_running(t->collector)) continue;
  978. if(help)
  979. *help = t->help;
  980. dictionary_set(dst, t_dfe.name, value, value_size);
  981. }
  982. dfe_done(t);
  983. }
  984. // ----------------------------------------------------------------------------
  985. int rrdhost_function_streaming(BUFFER *wb, int timeout __maybe_unused, const char *function __maybe_unused,
  986. void *collector_data __maybe_unused,
  987. rrd_function_result_callback_t result_cb, void *result_cb_data,
  988. rrd_function_is_cancelled_cb_t is_cancelled_cb, void *is_cancelled_cb_data,
  989. rrd_function_register_canceller_cb_t register_canceller_cb __maybe_unused,
  990. void *register_canceller_cb_data __maybe_unused) {
  991. time_t now = now_realtime_sec();
  992. buffer_flush(wb);
  993. wb->content_type = CT_APPLICATION_JSON;
  994. buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_DEFAULT);
  995. buffer_json_member_add_string(wb, "hostname", rrdhost_hostname(localhost));
  996. buffer_json_member_add_uint64(wb, "status", HTTP_RESP_OK);
  997. buffer_json_member_add_string(wb, "type", "table");
  998. buffer_json_member_add_time_t(wb, "update_every", 1);
  999. buffer_json_member_add_string(wb, "help", RRDFUNCTIONS_STREAMING_HELP);
  1000. buffer_json_member_add_array(wb, "data");
  1001. size_t max_sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_MAX];
  1002. size_t max_db_metrics = 0, max_db_instances = 0, max_db_contexts = 0;
  1003. size_t max_collection_replication_instances = 0, max_streaming_replication_instances = 0;
  1004. size_t max_ml_anomalous = 0, max_ml_normal = 0, max_ml_trained = 0, max_ml_pending = 0, max_ml_silenced = 0;
  1005. {
  1006. RRDHOST *host;
  1007. dfe_start_read(rrdhost_root_index, host) {
  1008. RRDHOST_STATUS s;
  1009. rrdhost_status(host, now, &s);
  1010. buffer_json_add_array_item_array(wb);
  1011. if(s.db.metrics > max_db_metrics)
  1012. max_db_metrics = s.db.metrics;
  1013. if(s.db.instances > max_db_instances)
  1014. max_db_instances = s.db.instances;
  1015. if(s.db.contexts > max_db_contexts)
  1016. max_db_contexts = s.db.contexts;
  1017. if(s.ingest.replication.instances > max_collection_replication_instances)
  1018. max_collection_replication_instances = s.ingest.replication.instances;
  1019. if(s.stream.replication.instances > max_streaming_replication_instances)
  1020. max_streaming_replication_instances = s.stream.replication.instances;
  1021. for(int i = 0; i < STREAM_TRAFFIC_TYPE_MAX ;i++) {
  1022. if (s.stream.sent_bytes_on_this_connection_per_type[i] >
  1023. max_sent_bytes_on_this_connection_per_type[i])
  1024. max_sent_bytes_on_this_connection_per_type[i] =
  1025. s.stream.sent_bytes_on_this_connection_per_type[i];
  1026. }
  1027. // retention
  1028. buffer_json_add_array_item_string(wb, rrdhost_hostname(s.host)); // Node
  1029. buffer_json_add_array_item_uint64(wb, s.db.first_time_s * MSEC_PER_SEC); // dbFrom
  1030. buffer_json_add_array_item_uint64(wb, s.db.last_time_s * MSEC_PER_SEC); // dbTo
  1031. if(s.db.first_time_s && s.db.last_time_s && s.db.last_time_s > s.db.first_time_s)
  1032. buffer_json_add_array_item_uint64(wb, s.db.last_time_s - s.db.first_time_s); // dbDuration
  1033. else
  1034. buffer_json_add_array_item_string(wb, NULL); // dbDuration
  1035. buffer_json_add_array_item_uint64(wb, s.db.metrics); // dbMetrics
  1036. buffer_json_add_array_item_uint64(wb, s.db.instances); // dbInstances
  1037. buffer_json_add_array_item_uint64(wb, s.db.contexts); // dbContexts
  1038. // statuses
  1039. buffer_json_add_array_item_string(wb, rrdhost_ingest_status_to_string(s.ingest.status)); // InStatus
  1040. buffer_json_add_array_item_string(wb, rrdhost_streaming_status_to_string(s.stream.status)); // OutStatus
  1041. buffer_json_add_array_item_string(wb, rrdhost_ml_status_to_string(s.ml.status)); // MLStatus
  1042. // collection
  1043. if(s.ingest.since) {
  1044. buffer_json_add_array_item_uint64(wb, s.ingest.since * MSEC_PER_SEC); // InSince
  1045. buffer_json_add_array_item_time_t(wb, s.now - s.ingest.since); // InAge
  1046. }
  1047. else {
  1048. buffer_json_add_array_item_string(wb, NULL); // InSince
  1049. buffer_json_add_array_item_string(wb, NULL); // InAge
  1050. }
  1051. buffer_json_add_array_item_string(wb, stream_handshake_error_to_string(s.ingest.reason)); // InReason
  1052. buffer_json_add_array_item_uint64(wb, s.ingest.hops); // InHops
  1053. buffer_json_add_array_item_double(wb, s.ingest.replication.completion); // InReplCompletion
  1054. buffer_json_add_array_item_uint64(wb, s.ingest.replication.instances); // InReplInstances
  1055. buffer_json_add_array_item_string(wb, s.ingest.peers.local.ip); // InLocalIP
  1056. buffer_json_add_array_item_uint64(wb, s.ingest.peers.local.port); // InLocalPort
  1057. buffer_json_add_array_item_string(wb, s.ingest.peers.peer.ip); // InRemoteIP
  1058. buffer_json_add_array_item_uint64(wb, s.ingest.peers.peer.port); // InRemotePort
  1059. buffer_json_add_array_item_string(wb, s.ingest.ssl ? "SSL" : "PLAIN"); // InSSL
  1060. stream_capabilities_to_json_array(wb, s.ingest.capabilities, NULL); // InCapabilities
  1061. // streaming
  1062. if(s.stream.since) {
  1063. buffer_json_add_array_item_uint64(wb, s.stream.since * MSEC_PER_SEC); // OutSince
  1064. buffer_json_add_array_item_time_t(wb, s.now - s.stream.since); // OutAge
  1065. }
  1066. else {
  1067. buffer_json_add_array_item_string(wb, NULL); // OutSince
  1068. buffer_json_add_array_item_string(wb, NULL); // OutAge
  1069. }
  1070. buffer_json_add_array_item_string(wb, stream_handshake_error_to_string(s.stream.reason)); // OutReason
  1071. buffer_json_add_array_item_uint64(wb, s.stream.hops); // OutHops
  1072. buffer_json_add_array_item_double(wb, s.stream.replication.completion); // OutReplCompletion
  1073. buffer_json_add_array_item_uint64(wb, s.stream.replication.instances); // OutReplInstances
  1074. buffer_json_add_array_item_string(wb, s.stream.peers.local.ip); // OutLocalIP
  1075. buffer_json_add_array_item_uint64(wb, s.stream.peers.local.port); // OutLocalPort
  1076. buffer_json_add_array_item_string(wb, s.stream.peers.peer.ip); // OutRemoteIP
  1077. buffer_json_add_array_item_uint64(wb, s.stream.peers.peer.port); // OutRemotePort
  1078. buffer_json_add_array_item_string(wb, s.stream.ssl ? "SSL" : "PLAIN"); // OutSSL
  1079. buffer_json_add_array_item_string(wb, s.stream.compression ? "COMPRESSED" : "UNCOMPRESSED"); // OutCompression
  1080. stream_capabilities_to_json_array(wb, s.stream.capabilities, NULL); // OutCapabilities
  1081. buffer_json_add_array_item_uint64(wb, s.stream.sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_DATA]);
  1082. buffer_json_add_array_item_uint64(wb, s.stream.sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_METADATA]);
  1083. buffer_json_add_array_item_uint64(wb, s.stream.sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_REPLICATION]);
  1084. buffer_json_add_array_item_uint64(wb, s.stream.sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_FUNCTIONS]);
  1085. buffer_json_add_array_item_array(wb); // OutAttemptHandshake
  1086. time_t last_attempt = 0;
  1087. for(struct rrdpush_destinations *d = host->destinations; d ; d = d->next) {
  1088. if(d->since > last_attempt)
  1089. last_attempt = d->since;
  1090. buffer_json_add_array_item_string(wb, stream_handshake_error_to_string(d->reason));
  1091. }
  1092. buffer_json_array_close(wb); // // OutAttemptHandshake
  1093. if(!last_attempt) {
  1094. buffer_json_add_array_item_string(wb, NULL); // OutAttemptSince
  1095. buffer_json_add_array_item_string(wb, NULL); // OutAttemptAge
  1096. }
  1097. else {
  1098. buffer_json_add_array_item_uint64(wb, last_attempt * 1000); // OutAttemptSince
  1099. buffer_json_add_array_item_time_t(wb, s.now - last_attempt); // OutAttemptAge
  1100. }
  1101. // ML
  1102. if(s.ml.status == RRDHOST_ML_STATUS_RUNNING) {
  1103. buffer_json_add_array_item_uint64(wb, s.ml.metrics.anomalous); // MlAnomalous
  1104. buffer_json_add_array_item_uint64(wb, s.ml.metrics.normal); // MlNormal
  1105. buffer_json_add_array_item_uint64(wb, s.ml.metrics.trained); // MlTrained
  1106. buffer_json_add_array_item_uint64(wb, s.ml.metrics.pending); // MlPending
  1107. buffer_json_add_array_item_uint64(wb, s.ml.metrics.silenced); // MlSilenced
  1108. if(s.ml.metrics.anomalous > max_ml_anomalous)
  1109. max_ml_anomalous = s.ml.metrics.anomalous;
  1110. if(s.ml.metrics.normal > max_ml_normal)
  1111. max_ml_normal = s.ml.metrics.normal;
  1112. if(s.ml.metrics.trained > max_ml_trained)
  1113. max_ml_trained = s.ml.metrics.trained;
  1114. if(s.ml.metrics.pending > max_ml_pending)
  1115. max_ml_pending = s.ml.metrics.pending;
  1116. if(s.ml.metrics.silenced > max_ml_silenced)
  1117. max_ml_silenced = s.ml.metrics.silenced;
  1118. }
  1119. else {
  1120. buffer_json_add_array_item_string(wb, NULL); // MlAnomalous
  1121. buffer_json_add_array_item_string(wb, NULL); // MlNormal
  1122. buffer_json_add_array_item_string(wb, NULL); // MlTrained
  1123. buffer_json_add_array_item_string(wb, NULL); // MlPending
  1124. buffer_json_add_array_item_string(wb, NULL); // MlSilenced
  1125. }
  1126. // close
  1127. buffer_json_array_close(wb);
  1128. }
  1129. dfe_done(host);
  1130. }
  1131. buffer_json_array_close(wb); // data
  1132. buffer_json_member_add_object(wb, "columns");
  1133. {
  1134. size_t field_id = 0;
  1135. // Node
  1136. buffer_rrdf_table_add_field(wb, field_id++, "Node", "Node's Hostname",
  1137. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1138. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1139. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1140. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY | RRDF_FIELD_OPTS_STICKY,
  1141. NULL);
  1142. buffer_rrdf_table_add_field(wb, field_id++, "dbFrom", "DB Data Retention From",
  1143. RRDF_FIELD_TYPE_TIMESTAMP, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DATETIME_MS,
  1144. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1145. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1146. RRDF_FIELD_OPTS_NONE, NULL);
  1147. buffer_rrdf_table_add_field(wb, field_id++, "dbTo", "DB Data Retention To",
  1148. RRDF_FIELD_TYPE_TIMESTAMP, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DATETIME_MS,
  1149. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1150. RRDF_FIELD_SUMMARY_MAX, RRDF_FIELD_FILTER_RANGE,
  1151. RRDF_FIELD_OPTS_NONE, NULL);
  1152. buffer_rrdf_table_add_field(wb, field_id++, "dbDuration", "DB Data Retention Duration",
  1153. RRDF_FIELD_TYPE_DURATION, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DURATION_S,
  1154. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1155. RRDF_FIELD_SUMMARY_MAX, RRDF_FIELD_FILTER_RANGE,
  1156. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1157. buffer_rrdf_table_add_field(wb, field_id++, "dbMetrics", "Time-series Metrics in the DB",
  1158. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1159. 0, NULL, max_db_metrics, RRDF_FIELD_SORT_DESCENDING, NULL,
  1160. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1161. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1162. buffer_rrdf_table_add_field(wb, field_id++, "dbInstances", "Instances in the DB",
  1163. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1164. 0, NULL, max_db_instances, RRDF_FIELD_SORT_DESCENDING, NULL,
  1165. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1166. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1167. buffer_rrdf_table_add_field(wb, field_id++, "dbContexts", "Contexts in the DB",
  1168. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1169. 0, NULL, max_db_contexts, RRDF_FIELD_SORT_DESCENDING, NULL,
  1170. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1171. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1172. // --- statuses ---
  1173. buffer_rrdf_table_add_field(wb, field_id++, "InStatus", "Data Collection Online Status",
  1174. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1175. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1176. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1177. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1178. buffer_rrdf_table_add_field(wb, field_id++, "OutStatus", "Streaming Online Status",
  1179. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1180. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1181. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1182. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1183. buffer_rrdf_table_add_field(wb, field_id++, "MlStatus", "ML Status",
  1184. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1185. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1186. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1187. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1188. // --- collection ---
  1189. buffer_rrdf_table_add_field(wb, field_id++, "InSince", "Last Data Collection Status Change",
  1190. RRDF_FIELD_TYPE_TIMESTAMP, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DATETIME_MS,
  1191. 0, NULL, NAN, RRDF_FIELD_SORT_DESCENDING, NULL,
  1192. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1193. RRDF_FIELD_OPTS_NONE, NULL);
  1194. buffer_rrdf_table_add_field(wb, field_id++, "InAge", "Last Data Collection Online Status Change Age",
  1195. RRDF_FIELD_TYPE_DURATION, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DURATION_S,
  1196. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1197. RRDF_FIELD_SUMMARY_MAX, RRDF_FIELD_FILTER_RANGE,
  1198. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1199. buffer_rrdf_table_add_field(wb, field_id++, "InReason", "Data Collection Online Status Reason",
  1200. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1201. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1202. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1203. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1204. buffer_rrdf_table_add_field(wb, field_id++, "InHops", "Data Collection Distance Hops from Origin Node",
  1205. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1206. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1207. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1208. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1209. buffer_rrdf_table_add_field(wb, field_id++, "InReplCompletion", "Inbound Replication Completion",
  1210. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER,
  1211. 1, "%", 100.0, RRDF_FIELD_SORT_DESCENDING, NULL,
  1212. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1213. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1214. buffer_rrdf_table_add_field(wb, field_id++, "InReplInstances", "Inbound Replicating Instances",
  1215. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1216. 0, "instances", max_collection_replication_instances, RRDF_FIELD_SORT_DESCENDING,
  1217. 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++, "InLocalIP", "Inbound Local IP",
  1221. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1222. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1223. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1224. RRDF_FIELD_OPTS_NONE, NULL);
  1225. buffer_rrdf_table_add_field(wb, field_id++, "InLocalPort", "Inbound Local Port",
  1226. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1227. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1228. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_RANGE,
  1229. RRDF_FIELD_OPTS_NONE, NULL);
  1230. buffer_rrdf_table_add_field(wb, field_id++, "InRemoteIP", "Inbound Remote IP",
  1231. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1232. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1233. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1234. RRDF_FIELD_OPTS_NONE, NULL);
  1235. buffer_rrdf_table_add_field(wb, field_id++, "InRemotePort", "Inbound Remote Port",
  1236. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1237. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1238. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_RANGE,
  1239. RRDF_FIELD_OPTS_NONE, NULL);
  1240. buffer_rrdf_table_add_field(wb, field_id++, "InSSL", "Inbound SSL Connection",
  1241. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1242. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1243. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1244. RRDF_FIELD_OPTS_NONE, NULL);
  1245. buffer_rrdf_table_add_field(wb, field_id++, "InCapabilities", "Inbound Connection Capabilities",
  1246. RRDF_FIELD_TYPE_ARRAY, RRDF_FIELD_VISUAL_PILL, RRDF_FIELD_TRANSFORM_NONE,
  1247. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1248. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1249. RRDF_FIELD_OPTS_NONE, NULL);
  1250. // --- streaming ---
  1251. buffer_rrdf_table_add_field(wb, field_id++, "OutSince", "Last Streaming Status Change",
  1252. RRDF_FIELD_TYPE_TIMESTAMP, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DATETIME_MS,
  1253. 0, NULL, NAN, RRDF_FIELD_SORT_DESCENDING, NULL,
  1254. RRDF_FIELD_SUMMARY_MAX, RRDF_FIELD_FILTER_RANGE,
  1255. RRDF_FIELD_OPTS_NONE, NULL);
  1256. buffer_rrdf_table_add_field(wb, field_id++, "OutAge", "Last Streaming Status Change Age",
  1257. RRDF_FIELD_TYPE_DURATION, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DURATION_S,
  1258. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1259. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1260. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1261. buffer_rrdf_table_add_field(wb, field_id++, "OutReason", "Streaming Status Reason",
  1262. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1263. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1264. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1265. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1266. buffer_rrdf_table_add_field(wb, field_id++, "OutHops", "Streaming Distance Hops from Origin Node",
  1267. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1268. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1269. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1270. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1271. buffer_rrdf_table_add_field(wb, field_id++, "OutReplCompletion", "Outbound Replication Completion",
  1272. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_BAR, RRDF_FIELD_TRANSFORM_NUMBER,
  1273. 1, "%", 100.0, RRDF_FIELD_SORT_DESCENDING, NULL,
  1274. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1275. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1276. buffer_rrdf_table_add_field(wb, field_id++, "OutReplInstances", "Outbound Replicating Instances",
  1277. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1278. 0, "instances", max_streaming_replication_instances, RRDF_FIELD_SORT_DESCENDING,
  1279. NULL,
  1280. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1281. RRDF_FIELD_OPTS_NONE, NULL);
  1282. buffer_rrdf_table_add_field(wb, field_id++, "OutLocalIP", "Outbound Local IP",
  1283. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1284. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1285. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1286. RRDF_FIELD_OPTS_NONE, NULL);
  1287. buffer_rrdf_table_add_field(wb, field_id++, "OutLocalPort", "Outbound Local Port",
  1288. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1289. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1290. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_RANGE,
  1291. RRDF_FIELD_OPTS_NONE, NULL);
  1292. buffer_rrdf_table_add_field(wb, field_id++, "OutRemoteIP", "Outbound Remote IP",
  1293. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1294. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1295. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1296. RRDF_FIELD_OPTS_NONE, NULL);
  1297. buffer_rrdf_table_add_field(wb, field_id++, "OutRemotePort", "Outbound Remote Port",
  1298. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1299. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1300. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_RANGE,
  1301. RRDF_FIELD_OPTS_NONE, NULL);
  1302. buffer_rrdf_table_add_field(wb, field_id++, "OutSSL", "Outbound SSL Connection",
  1303. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1304. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1305. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1306. RRDF_FIELD_OPTS_NONE, NULL);
  1307. buffer_rrdf_table_add_field(wb, field_id++, "OutCompression", "Outbound Compressed Connection",
  1308. RRDF_FIELD_TYPE_STRING, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE,
  1309. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1310. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1311. RRDF_FIELD_OPTS_NONE, NULL);
  1312. buffer_rrdf_table_add_field(wb, field_id++, "OutCapabilities", "Outbound Connection Capabilities",
  1313. RRDF_FIELD_TYPE_ARRAY, RRDF_FIELD_VISUAL_PILL, RRDF_FIELD_TRANSFORM_NONE,
  1314. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1315. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1316. RRDF_FIELD_OPTS_NONE, NULL);
  1317. buffer_rrdf_table_add_field(wb, field_id++, "OutTrafficData", "Outbound Metric Data Traffic",
  1318. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1319. 0, "bytes", max_sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_DATA],
  1320. RRDF_FIELD_SORT_DESCENDING, NULL,
  1321. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1322. RRDF_FIELD_OPTS_NONE, NULL);
  1323. buffer_rrdf_table_add_field(wb, field_id++, "OutTrafficMetadata", "Outbound Metric Metadata Traffic",
  1324. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1325. 0, "bytes",
  1326. max_sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_METADATA],
  1327. RRDF_FIELD_SORT_DESCENDING, NULL,
  1328. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1329. RRDF_FIELD_OPTS_NONE, NULL);
  1330. buffer_rrdf_table_add_field(wb, field_id++, "OutTrafficReplication", "Outbound Metric Replication Traffic",
  1331. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1332. 0, "bytes",
  1333. max_sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_REPLICATION],
  1334. RRDF_FIELD_SORT_DESCENDING, NULL,
  1335. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1336. RRDF_FIELD_OPTS_NONE, NULL);
  1337. buffer_rrdf_table_add_field(wb, field_id++, "OutTrafficFunctions", "Outbound Metric Functions Traffic",
  1338. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1339. 0, "bytes",
  1340. max_sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_FUNCTIONS],
  1341. RRDF_FIELD_SORT_DESCENDING, NULL,
  1342. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1343. RRDF_FIELD_OPTS_NONE, NULL);
  1344. buffer_rrdf_table_add_field(wb, field_id++, "OutAttemptHandshake",
  1345. "Outbound Connection Attempt Handshake Status",
  1346. RRDF_FIELD_TYPE_ARRAY, RRDF_FIELD_VISUAL_PILL, RRDF_FIELD_TRANSFORM_NONE,
  1347. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1348. RRDF_FIELD_SUMMARY_COUNT, RRDF_FIELD_FILTER_MULTISELECT,
  1349. RRDF_FIELD_OPTS_NONE, NULL);
  1350. buffer_rrdf_table_add_field(wb, field_id++, "OutAttemptSince",
  1351. "Last Outbound Connection Attempt Status Change Time",
  1352. RRDF_FIELD_TYPE_TIMESTAMP, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DATETIME_MS,
  1353. 0, NULL, NAN, RRDF_FIELD_SORT_DESCENDING, NULL,
  1354. RRDF_FIELD_SUMMARY_MAX, RRDF_FIELD_FILTER_RANGE,
  1355. RRDF_FIELD_OPTS_NONE, NULL);
  1356. buffer_rrdf_table_add_field(wb, field_id++, "OutAttemptAge",
  1357. "Last Outbound Connection Attempt Status Change Age",
  1358. RRDF_FIELD_TYPE_DURATION, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_DURATION_S,
  1359. 0, NULL, NAN, RRDF_FIELD_SORT_ASCENDING, NULL,
  1360. RRDF_FIELD_SUMMARY_MIN, RRDF_FIELD_FILTER_RANGE,
  1361. RRDF_FIELD_OPTS_VISIBLE, NULL);
  1362. // --- ML ---
  1363. buffer_rrdf_table_add_field(wb, field_id++, "MlAnomalous", "Number of Anomalous Metrics",
  1364. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1365. 0, "metrics",
  1366. max_ml_anomalous,
  1367. RRDF_FIELD_SORT_DESCENDING, NULL,
  1368. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1369. RRDF_FIELD_OPTS_NONE, NULL);
  1370. buffer_rrdf_table_add_field(wb, field_id++, "MlNormal", "Number of Not Anomalous Metrics",
  1371. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1372. 0, "metrics",
  1373. max_ml_normal,
  1374. RRDF_FIELD_SORT_DESCENDING, NULL,
  1375. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1376. RRDF_FIELD_OPTS_NONE, NULL);
  1377. buffer_rrdf_table_add_field(wb, field_id++, "MlTrained", "Number of Trained Metrics",
  1378. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1379. 0, "metrics",
  1380. max_ml_trained,
  1381. RRDF_FIELD_SORT_DESCENDING, NULL,
  1382. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1383. RRDF_FIELD_OPTS_NONE, NULL);
  1384. buffer_rrdf_table_add_field(wb, field_id++, "MlPending", "Number of Pending Metrics",
  1385. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1386. 0, "metrics",
  1387. max_ml_pending,
  1388. RRDF_FIELD_SORT_DESCENDING, NULL,
  1389. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1390. RRDF_FIELD_OPTS_NONE, NULL);
  1391. buffer_rrdf_table_add_field(wb, field_id++, "MlSilenced", "Number of Silenced Metrics",
  1392. RRDF_FIELD_TYPE_INTEGER, RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER,
  1393. 0, "metrics",
  1394. max_ml_silenced,
  1395. RRDF_FIELD_SORT_DESCENDING, NULL,
  1396. RRDF_FIELD_SUMMARY_SUM, RRDF_FIELD_FILTER_RANGE,
  1397. RRDF_FIELD_OPTS_NONE, NULL);
  1398. }
  1399. buffer_json_object_close(wb); // columns
  1400. buffer_json_member_add_string(wb, "default_sort_column", "Node");
  1401. buffer_json_member_add_object(wb, "charts");
  1402. {
  1403. // Data Collection Age chart
  1404. buffer_json_member_add_object(wb, "InAge");
  1405. {
  1406. buffer_json_member_add_string(wb, "name", "Data Collection Age");
  1407. buffer_json_member_add_string(wb, "type", "stacked-bar");
  1408. buffer_json_member_add_array(wb, "columns");
  1409. {
  1410. buffer_json_add_array_item_string(wb, "InAge");
  1411. }
  1412. buffer_json_array_close(wb);
  1413. }
  1414. buffer_json_object_close(wb);
  1415. // Streaming Age chart
  1416. buffer_json_member_add_object(wb, "OutAge");
  1417. {
  1418. buffer_json_member_add_string(wb, "name", "Streaming Age");
  1419. buffer_json_member_add_string(wb, "type", "stacked-bar");
  1420. buffer_json_member_add_array(wb, "columns");
  1421. {
  1422. buffer_json_add_array_item_string(wb, "OutAge");
  1423. }
  1424. buffer_json_array_close(wb);
  1425. }
  1426. buffer_json_object_close(wb);
  1427. // DB Duration
  1428. buffer_json_member_add_object(wb, "dbDuration");
  1429. {
  1430. buffer_json_member_add_string(wb, "name", "Retention Duration");
  1431. buffer_json_member_add_string(wb, "type", "stacked-bar");
  1432. buffer_json_member_add_array(wb, "columns");
  1433. {
  1434. buffer_json_add_array_item_string(wb, "dbDuration");
  1435. }
  1436. buffer_json_array_close(wb);
  1437. }
  1438. buffer_json_object_close(wb);
  1439. }
  1440. buffer_json_object_close(wb); // charts
  1441. buffer_json_member_add_array(wb, "default_charts");
  1442. {
  1443. buffer_json_add_array_item_array(wb);
  1444. buffer_json_add_array_item_string(wb, "InAge");
  1445. buffer_json_add_array_item_string(wb, "Node");
  1446. buffer_json_array_close(wb);
  1447. buffer_json_add_array_item_array(wb);
  1448. buffer_json_add_array_item_string(wb, "OutAge");
  1449. buffer_json_add_array_item_string(wb, "Node");
  1450. buffer_json_array_close(wb);
  1451. }
  1452. buffer_json_array_close(wb);
  1453. buffer_json_member_add_object(wb, "group_by");
  1454. {
  1455. buffer_json_member_add_object(wb, "Node");
  1456. {
  1457. buffer_json_member_add_string(wb, "name", "Node");
  1458. buffer_json_member_add_array(wb, "columns");
  1459. {
  1460. buffer_json_add_array_item_string(wb, "Node");
  1461. }
  1462. buffer_json_array_close(wb);
  1463. }
  1464. buffer_json_object_close(wb);
  1465. buffer_json_member_add_object(wb, "InStatus");
  1466. {
  1467. buffer_json_member_add_string(wb, "name", "Nodes by Collection Status");
  1468. buffer_json_member_add_array(wb, "columns");
  1469. {
  1470. buffer_json_add_array_item_string(wb, "InStatus");
  1471. }
  1472. buffer_json_array_close(wb);
  1473. }
  1474. buffer_json_object_close(wb);
  1475. buffer_json_member_add_object(wb, "OutStatus");
  1476. {
  1477. buffer_json_member_add_string(wb, "name", "Nodes by Streaming Status");
  1478. buffer_json_member_add_array(wb, "columns");
  1479. {
  1480. buffer_json_add_array_item_string(wb, "OutStatus");
  1481. }
  1482. buffer_json_array_close(wb);
  1483. }
  1484. buffer_json_object_close(wb);
  1485. buffer_json_member_add_object(wb, "MlStatus");
  1486. {
  1487. buffer_json_member_add_string(wb, "name", "Nodes by ML Status");
  1488. buffer_json_member_add_array(wb, "columns");
  1489. {
  1490. buffer_json_add_array_item_string(wb, "MlStatus");
  1491. }
  1492. buffer_json_array_close(wb);
  1493. }
  1494. buffer_json_object_close(wb);
  1495. buffer_json_member_add_object(wb, "InRemoteIP");
  1496. {
  1497. buffer_json_member_add_string(wb, "name", "Nodes by Inbound IP");
  1498. buffer_json_member_add_array(wb, "columns");
  1499. {
  1500. buffer_json_add_array_item_string(wb, "InRemoteIP");
  1501. }
  1502. buffer_json_array_close(wb);
  1503. }
  1504. buffer_json_object_close(wb);
  1505. buffer_json_member_add_object(wb, "OutRemoteIP");
  1506. {
  1507. buffer_json_member_add_string(wb, "name", "Nodes by Outbound IP");
  1508. buffer_json_member_add_array(wb, "columns");
  1509. {
  1510. buffer_json_add_array_item_string(wb, "OutRemoteIP");
  1511. }
  1512. buffer_json_array_close(wb);
  1513. }
  1514. buffer_json_object_close(wb);
  1515. }
  1516. buffer_json_object_close(wb); // group_by
  1517. buffer_json_member_add_time_t(wb, "expires", now_realtime_sec() + 1);
  1518. buffer_json_finalize(wb);
  1519. int response = HTTP_RESP_OK;
  1520. if(is_cancelled_cb && is_cancelled_cb(is_cancelled_cb_data)) {
  1521. buffer_flush(wb);
  1522. response = HTTP_RESP_CLIENT_CLOSED_REQUEST;
  1523. }
  1524. if(result_cb)
  1525. result_cb(wb, response, result_cb_data);
  1526. return response;
  1527. }