rrdfunctions.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. #define NETDATA_RRD_INTERNALS
  2. #include "rrd.h"
  3. #define MAX_FUNCTION_LENGTH (PLUGINSD_LINE_MAX - 512) // we need some space for the rest of the line
  4. static unsigned char functions_allowed_chars[256] = {
  5. [0] = '\0', //
  6. [1] = '_', //
  7. [2] = '_', //
  8. [3] = '_', //
  9. [4] = '_', //
  10. [5] = '_', //
  11. [6] = '_', //
  12. [7] = '_', //
  13. [8] = '_', //
  14. [9] = ' ', // Horizontal Tab
  15. [10] = ' ', // Line Feed
  16. [11] = ' ', // Vertical Tab
  17. [12] = ' ', // Form Feed
  18. [13] = ' ', // Carriage Return
  19. [14] = '_', //
  20. [15] = '_', //
  21. [16] = '_', //
  22. [17] = '_', //
  23. [18] = '_', //
  24. [19] = '_', //
  25. [20] = '_', //
  26. [21] = '_', //
  27. [22] = '_', //
  28. [23] = '_', //
  29. [24] = '_', //
  30. [25] = '_', //
  31. [26] = '_', //
  32. [27] = '_', //
  33. [28] = '_', //
  34. [29] = '_', //
  35. [30] = '_', //
  36. [31] = '_', //
  37. [32] = ' ', // SPACE keep
  38. [33] = '_', // !
  39. [34] = '_', // "
  40. [35] = '_', // #
  41. [36] = '_', // $
  42. [37] = '_', // %
  43. [38] = '_', // &
  44. [39] = '_', // '
  45. [40] = '_', // (
  46. [41] = '_', // )
  47. [42] = '_', // *
  48. [43] = '_', // +
  49. [44] = ',', // , keep
  50. [45] = '-', // - keep
  51. [46] = '.', // . keep
  52. [47] = '/', // / keep
  53. [48] = '0', // 0 keep
  54. [49] = '1', // 1 keep
  55. [50] = '2', // 2 keep
  56. [51] = '3', // 3 keep
  57. [52] = '4', // 4 keep
  58. [53] = '5', // 5 keep
  59. [54] = '6', // 6 keep
  60. [55] = '7', // 7 keep
  61. [56] = '8', // 8 keep
  62. [57] = '9', // 9 keep
  63. [58] = ':', // : keep
  64. [59] = ':', // ; convert ; to :
  65. [60] = '_', // <
  66. [61] = ':', // = convert = to :
  67. [62] = '_', // >
  68. [63] = '_', // ?
  69. [64] = '_', // @
  70. [65] = 'A', // A keep
  71. [66] = 'B', // B keep
  72. [67] = 'C', // C keep
  73. [68] = 'D', // D keep
  74. [69] = 'E', // E keep
  75. [70] = 'F', // F keep
  76. [71] = 'G', // G keep
  77. [72] = 'H', // H keep
  78. [73] = 'I', // I keep
  79. [74] = 'J', // J keep
  80. [75] = 'K', // K keep
  81. [76] = 'L', // L keep
  82. [77] = 'M', // M keep
  83. [78] = 'N', // N keep
  84. [79] = 'O', // O keep
  85. [80] = 'P', // P keep
  86. [81] = 'Q', // Q keep
  87. [82] = 'R', // R keep
  88. [83] = 'S', // S keep
  89. [84] = 'T', // T keep
  90. [85] = 'U', // U keep
  91. [86] = 'V', // V keep
  92. [87] = 'W', // W keep
  93. [88] = 'X', // X keep
  94. [89] = 'Y', // Y keep
  95. [90] = 'Z', // Z keep
  96. [91] = '_', // [
  97. [92] = '/', // backslash convert \ to /
  98. [93] = '_', // ]
  99. [94] = '_', // ^
  100. [95] = '_', // _ keep
  101. [96] = '_', // `
  102. [97] = 'a', // a keep
  103. [98] = 'b', // b keep
  104. [99] = 'c', // c keep
  105. [100] = 'd', // d keep
  106. [101] = 'e', // e keep
  107. [102] = 'f', // f keep
  108. [103] = 'g', // g keep
  109. [104] = 'h', // h keep
  110. [105] = 'i', // i keep
  111. [106] = 'j', // j keep
  112. [107] = 'k', // k keep
  113. [108] = 'l', // l keep
  114. [109] = 'm', // m keep
  115. [110] = 'n', // n keep
  116. [111] = 'o', // o keep
  117. [112] = 'p', // p keep
  118. [113] = 'q', // q keep
  119. [114] = 'r', // r keep
  120. [115] = 's', // s keep
  121. [116] = 't', // t keep
  122. [117] = 'u', // u keep
  123. [118] = 'v', // v keep
  124. [119] = 'w', // w keep
  125. [120] = 'x', // x keep
  126. [121] = 'y', // y keep
  127. [122] = 'z', // z keep
  128. [123] = '_', // {
  129. [124] = '_', // |
  130. [125] = '_', // }
  131. [126] = '_', // ~
  132. [127] = '_', //
  133. [128] = '_', //
  134. [129] = '_', //
  135. [130] = '_', //
  136. [131] = '_', //
  137. [132] = '_', //
  138. [133] = '_', //
  139. [134] = '_', //
  140. [135] = '_', //
  141. [136] = '_', //
  142. [137] = '_', //
  143. [138] = '_', //
  144. [139] = '_', //
  145. [140] = '_', //
  146. [141] = '_', //
  147. [142] = '_', //
  148. [143] = '_', //
  149. [144] = '_', //
  150. [145] = '_', //
  151. [146] = '_', //
  152. [147] = '_', //
  153. [148] = '_', //
  154. [149] = '_', //
  155. [150] = '_', //
  156. [151] = '_', //
  157. [152] = '_', //
  158. [153] = '_', //
  159. [154] = '_', //
  160. [155] = '_', //
  161. [156] = '_', //
  162. [157] = '_', //
  163. [158] = '_', //
  164. [159] = '_', //
  165. [160] = '_', //
  166. [161] = '_', //
  167. [162] = '_', //
  168. [163] = '_', //
  169. [164] = '_', //
  170. [165] = '_', //
  171. [166] = '_', //
  172. [167] = '_', //
  173. [168] = '_', //
  174. [169] = '_', //
  175. [170] = '_', //
  176. [171] = '_', //
  177. [172] = '_', //
  178. [173] = '_', //
  179. [174] = '_', //
  180. [175] = '_', //
  181. [176] = '_', //
  182. [177] = '_', //
  183. [178] = '_', //
  184. [179] = '_', //
  185. [180] = '_', //
  186. [181] = '_', //
  187. [182] = '_', //
  188. [183] = '_', //
  189. [184] = '_', //
  190. [185] = '_', //
  191. [186] = '_', //
  192. [187] = '_', //
  193. [188] = '_', //
  194. [189] = '_', //
  195. [190] = '_', //
  196. [191] = '_', //
  197. [192] = '_', //
  198. [193] = '_', //
  199. [194] = '_', //
  200. [195] = '_', //
  201. [196] = '_', //
  202. [197] = '_', //
  203. [198] = '_', //
  204. [199] = '_', //
  205. [200] = '_', //
  206. [201] = '_', //
  207. [202] = '_', //
  208. [203] = '_', //
  209. [204] = '_', //
  210. [205] = '_', //
  211. [206] = '_', //
  212. [207] = '_', //
  213. [208] = '_', //
  214. [209] = '_', //
  215. [210] = '_', //
  216. [211] = '_', //
  217. [212] = '_', //
  218. [213] = '_', //
  219. [214] = '_', //
  220. [215] = '_', //
  221. [216] = '_', //
  222. [217] = '_', //
  223. [218] = '_', //
  224. [219] = '_', //
  225. [220] = '_', //
  226. [221] = '_', //
  227. [222] = '_', //
  228. [223] = '_', //
  229. [224] = '_', //
  230. [225] = '_', //
  231. [226] = '_', //
  232. [227] = '_', //
  233. [228] = '_', //
  234. [229] = '_', //
  235. [230] = '_', //
  236. [231] = '_', //
  237. [232] = '_', //
  238. [233] = '_', //
  239. [234] = '_', //
  240. [235] = '_', //
  241. [236] = '_', //
  242. [237] = '_', //
  243. [238] = '_', //
  244. [239] = '_', //
  245. [240] = '_', //
  246. [241] = '_', //
  247. [242] = '_', //
  248. [243] = '_', //
  249. [244] = '_', //
  250. [245] = '_', //
  251. [246] = '_', //
  252. [247] = '_', //
  253. [248] = '_', //
  254. [249] = '_', //
  255. [250] = '_', //
  256. [251] = '_', //
  257. [252] = '_', //
  258. [253] = '_', //
  259. [254] = '_', //
  260. [255] = '_' //
  261. };
  262. static inline size_t sanitize_function_text(char *dst, const char *src, size_t dst_len) {
  263. return text_sanitize((unsigned char *)dst, (const unsigned char *)src, dst_len,
  264. functions_allowed_chars, true, "", NULL);
  265. }
  266. // we keep a dictionary per RRDSET with these functions
  267. // the dictionary is created on demand (only when a function is added to an RRDSET)
  268. typedef enum {
  269. RRD_FUNCTION_LOCAL = (1 << 0),
  270. RRD_FUNCTION_GLOBAL = (1 << 1),
  271. // this is 8-bit
  272. } RRD_FUNCTION_OPTIONS;
  273. struct rrd_collector_function {
  274. bool sync; // when true, the function is called synchronously
  275. uint8_t options; // RRD_FUNCTION_OPTIONS
  276. STRING *help;
  277. int timeout; // the default timeout of the function
  278. int (*function)(BUFFER *wb, int timeout, const char *function, void *collector_data,
  279. function_data_ready_callback callback, void *callback_data);
  280. void *collector_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. pid_t tid;
  292. bool running;
  293. };
  294. // Each thread that adds RRDSET functions, has to call
  295. // rrdset_collector_started() and rrdset_collector_finished()
  296. // to create the collector structure.
  297. static __thread struct rrd_collector *thread_rrd_collector = NULL;
  298. static void rrd_collector_free(struct rrd_collector *rdc) {
  299. int32_t expected = 0;
  300. if(likely(!__atomic_compare_exchange_n(&rdc->refcount, &expected, -1, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))) {
  301. // the collector is still referenced by charts.
  302. // leave it hanging there, the last chart will actually free it.
  303. return;
  304. }
  305. // we can free it now
  306. freez(rdc);
  307. }
  308. // called once per collector
  309. void rrd_collector_started(void) {
  310. if(likely(thread_rrd_collector)) return;
  311. thread_rrd_collector = callocz(1, sizeof(struct rrd_collector));
  312. thread_rrd_collector->tid = gettid();
  313. thread_rrd_collector->running = true;
  314. }
  315. // called once per collector
  316. void rrd_collector_finished(void) {
  317. if(!thread_rrd_collector)
  318. return;
  319. thread_rrd_collector->running = false;
  320. rrd_collector_free(thread_rrd_collector);
  321. thread_rrd_collector = NULL;
  322. }
  323. static struct rrd_collector *rrd_collector_acquire(void) {
  324. __atomic_add_fetch(&thread_rrd_collector->refcount, 1, __ATOMIC_SEQ_CST);
  325. return thread_rrd_collector;
  326. }
  327. static void rrd_collector_release(struct rrd_collector *rdc) {
  328. if(unlikely(!rdc)) return;
  329. int32_t refcount = __atomic_sub_fetch(&rdc->refcount, 1, __ATOMIC_SEQ_CST);
  330. if(refcount == 0 && !rdc->running)
  331. rrd_collector_free(rdc);
  332. }
  333. static void rrd_functions_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *func __maybe_unused,
  334. void *rrdhost __maybe_unused) {
  335. struct rrd_collector_function *rdcf = func;
  336. if(!thread_rrd_collector)
  337. fatal("RRDSET_COLLECTOR: called %s() for function '%s' without calling rrd_collector_started() first.",
  338. __FUNCTION__, dictionary_acquired_item_name(item));
  339. rdcf->collector = rrd_collector_acquire();
  340. }
  341. static void rrd_functions_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *func __maybe_unused,
  342. void *rrdhost __maybe_unused) {
  343. struct rrd_collector_function *rdcf = func;
  344. rrd_collector_release(rdcf->collector);
  345. }
  346. static bool rrd_functions_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *func __maybe_unused,
  347. void *new_func __maybe_unused, void *rrdhost __maybe_unused) {
  348. struct rrd_collector_function *rdcf = func;
  349. struct rrd_collector_function *new_rdcf = new_func;
  350. if(!thread_rrd_collector)
  351. fatal("RRDSET_COLLECTOR: called %s() for function '%s' without calling rrd_collector_started() first.",
  352. __FUNCTION__, dictionary_acquired_item_name(item));
  353. bool changed = false;
  354. if(rdcf->collector != thread_rrd_collector) {
  355. struct rrd_collector *old_rdc = rdcf->collector;
  356. rdcf->collector = rrd_collector_acquire();
  357. rrd_collector_release(old_rdc);
  358. changed = true;
  359. }
  360. if(rdcf->function != new_rdcf->function) {
  361. rdcf->function = new_rdcf->function;
  362. changed = true;
  363. }
  364. if(rdcf->help != new_rdcf->help) {
  365. STRING *old = rdcf->help;
  366. rdcf->help = new_rdcf->help;
  367. string_freez(old);
  368. changed = true;
  369. }
  370. else
  371. string_freez(new_rdcf->help);
  372. if(rdcf->timeout != new_rdcf->timeout) {
  373. rdcf->timeout = new_rdcf->timeout;
  374. changed = true;
  375. }
  376. if(rdcf->sync != new_rdcf->sync) {
  377. rdcf->sync = new_rdcf->sync;
  378. changed = true;
  379. }
  380. if(rdcf->collector_data != new_rdcf->collector_data) {
  381. rdcf->collector_data = new_rdcf->collector_data;
  382. changed = true;
  383. }
  384. return changed;
  385. }
  386. void rrdfunctions_init(RRDHOST *host) {
  387. if(host->functions) return;
  388. host->functions = dictionary_create_advanced(DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  389. &dictionary_stats_category_functions, sizeof(struct rrd_collector_function));
  390. dictionary_register_insert_callback(host->functions, rrd_functions_insert_callback, host);
  391. dictionary_register_delete_callback(host->functions, rrd_functions_delete_callback, host);
  392. dictionary_register_conflict_callback(host->functions, rrd_functions_conflict_callback, host);
  393. }
  394. void rrdfunctions_destroy(RRDHOST *host) {
  395. dictionary_destroy(host->functions);
  396. }
  397. void rrd_collector_add_function(RRDHOST *host, RRDSET *st, const char *name, int timeout, const char *help,
  398. bool sync, function_execute_at_collector function, void *collector_data) {
  399. // RRDSET *st may be NULL in this function
  400. // to create a GLOBAL function
  401. if(st && !st->functions_view)
  402. st->functions_view = dictionary_create_view(host->functions);
  403. char key[PLUGINSD_LINE_MAX + 1];
  404. sanitize_function_text(key, name, PLUGINSD_LINE_MAX);
  405. struct rrd_collector_function tmp = {
  406. .sync = sync,
  407. .timeout = timeout,
  408. .options = (st)?RRD_FUNCTION_LOCAL:RRD_FUNCTION_GLOBAL,
  409. .function = function,
  410. .collector_data = collector_data,
  411. .help = string_strdupz(help),
  412. };
  413. const DICTIONARY_ITEM *item = dictionary_set_and_acquire_item(host->functions, key, &tmp, sizeof(tmp));
  414. if(st)
  415. dictionary_view_set(st->functions_view, key, item);
  416. dictionary_acquired_item_release(host->functions, item);
  417. }
  418. void rrd_functions_expose_rrdpush(RRDSET *st, BUFFER *wb) {
  419. if(!st->functions_view)
  420. return;
  421. struct rrd_collector_function *tmp;
  422. dfe_start_read(st->functions_view, tmp) {
  423. buffer_sprintf(wb
  424. , PLUGINSD_KEYWORD_FUNCTION " \"%s\" %d \"%s\"\n"
  425. , tmp_dfe.name
  426. , tmp->timeout
  427. , string2str(tmp->help)
  428. );
  429. }
  430. dfe_done(tmp);
  431. }
  432. struct rrd_function_call_wait {
  433. bool free_with_signal;
  434. bool data_are_ready;
  435. netdata_mutex_t mutex;
  436. pthread_cond_t cond;
  437. int code;
  438. };
  439. static void rrd_function_call_wait_free(struct rrd_function_call_wait *tmp) {
  440. pthread_cond_destroy(&tmp->cond);
  441. netdata_mutex_destroy(&tmp->mutex);
  442. freez(tmp);
  443. }
  444. struct {
  445. const char *format;
  446. HTTP_CONTENT_TYPE content_type;
  447. } function_formats[] = {
  448. { .format = "application/json", CT_APPLICATION_JSON },
  449. { .format = "text/plain", CT_TEXT_PLAIN },
  450. { .format = "application/xml", CT_APPLICATION_XML },
  451. { .format = "prometheus", CT_PROMETHEUS },
  452. { .format = "text", CT_TEXT_PLAIN },
  453. { .format = "txt", CT_TEXT_PLAIN },
  454. { .format = "json", CT_APPLICATION_JSON },
  455. { .format = "html", CT_TEXT_HTML },
  456. { .format = "text/html", CT_TEXT_HTML },
  457. { .format = "xml", CT_APPLICATION_XML },
  458. // terminator
  459. { .format = NULL, CT_TEXT_PLAIN },
  460. };
  461. uint8_t functions_format_to_content_type(const char *format) {
  462. if(format && *format) {
  463. for (int i = 0; function_formats[i].format; i++)
  464. if (strcmp(function_formats[i].format, format) == 0)
  465. return function_formats[i].content_type;
  466. }
  467. return CT_TEXT_PLAIN;
  468. }
  469. const char *functions_content_type_to_format(HTTP_CONTENT_TYPE content_type) {
  470. for (int i = 0; function_formats[i].format; i++)
  471. if (function_formats[i].content_type == content_type)
  472. return function_formats[i].format;
  473. return "text/plain";
  474. }
  475. int rrd_call_function_error(BUFFER *wb, const char *msg, int code) {
  476. char buffer[PLUGINSD_LINE_MAX];
  477. json_escape_string(buffer, msg, PLUGINSD_LINE_MAX);
  478. buffer_flush(wb);
  479. buffer_sprintf(wb, "{\"status\":%d,\"error_message\":\"%s\"}", code, buffer);
  480. wb->content_type = CT_APPLICATION_JSON;
  481. buffer_no_cacheable(wb);
  482. return code;
  483. }
  484. static int rrd_call_function_find(RRDHOST *host, BUFFER *wb, const char *name, size_t key_length, struct rrd_collector_function **rdcf) {
  485. char buffer[MAX_FUNCTION_LENGTH + 1];
  486. strncpyz(buffer, name, MAX_FUNCTION_LENGTH);
  487. char *s = NULL;
  488. *rdcf = NULL;
  489. while(!(*rdcf) && buffer[0]) {
  490. *rdcf = dictionary_get(host->functions, buffer);
  491. if(*rdcf) break;
  492. // if s == NULL, set it to the end of the buffer
  493. // this should happen only the first time
  494. if(unlikely(!s))
  495. s = &buffer[key_length - 1];
  496. // skip a word from the end
  497. while(s >= buffer && !isspace(*s)) *s-- = '\0';
  498. // skip all spaces
  499. while(s >= buffer && isspace(*s)) *s-- = '\0';
  500. }
  501. buffer_flush(wb);
  502. if(!(*rdcf))
  503. return rrd_call_function_error(wb, "No collector is supplying this function on this host at this time.", HTTP_RESP_NOT_FOUND);
  504. if(!(*rdcf)->collector->running)
  505. return rrd_call_function_error(wb, "The collector that registered this function, is not currently running.", HTTP_RESP_BACKEND_FETCH_FAILED);
  506. return HTTP_RESP_OK;
  507. }
  508. static void rrd_call_function_signal_when_ready(BUFFER *temp_wb __maybe_unused, int code, void *callback_data) {
  509. struct rrd_function_call_wait *tmp = callback_data;
  510. bool we_should_free = false;
  511. netdata_mutex_lock(&tmp->mutex);
  512. // since we got the mutex,
  513. // the waiting thread is either in pthread_cond_timedwait()
  514. // or gave up and left.
  515. tmp->code = code;
  516. tmp->data_are_ready = true;
  517. if(tmp->free_with_signal)
  518. we_should_free = true;
  519. pthread_cond_signal(&tmp->cond);
  520. netdata_mutex_unlock(&tmp->mutex);
  521. if(we_should_free) {
  522. buffer_free(temp_wb);
  523. rrd_function_call_wait_free(tmp);
  524. }
  525. }
  526. int rrd_call_function_and_wait(RRDHOST *host, BUFFER *wb, int timeout, const char *name) {
  527. int code;
  528. struct rrd_collector_function *rdcf = NULL;
  529. char key[PLUGINSD_LINE_MAX + 1];
  530. size_t key_length = sanitize_function_text(key, name, PLUGINSD_LINE_MAX);
  531. code = rrd_call_function_find(host, wb, key, key_length, &rdcf);
  532. if(code != HTTP_RESP_OK)
  533. return code;
  534. if(timeout <= 0)
  535. timeout = rdcf->timeout;
  536. struct timespec tp;
  537. clock_gettime(CLOCK_REALTIME, &tp);
  538. tp.tv_sec += (time_t)timeout;
  539. if(rdcf->sync) {
  540. code = rdcf->function(wb, timeout, key, rdcf->collector_data, NULL, NULL);
  541. }
  542. else {
  543. struct rrd_function_call_wait *tmp = mallocz(sizeof(struct rrd_function_call_wait));
  544. tmp->free_with_signal = false;
  545. tmp->data_are_ready = false;
  546. netdata_mutex_init(&tmp->mutex);
  547. pthread_cond_init(&tmp->cond, NULL);
  548. bool we_should_free = true;
  549. BUFFER *temp_wb = buffer_create(PLUGINSD_LINE_MAX + 1, &netdata_buffers_statistics.buffers_functions); // we need it because we may give up on it
  550. temp_wb->content_type = wb->content_type;
  551. code = rdcf->function(temp_wb, timeout, key, rdcf->collector_data, rrd_call_function_signal_when_ready, tmp);
  552. if (code == HTTP_RESP_OK) {
  553. netdata_mutex_lock(&tmp->mutex);
  554. int rc = 0;
  555. while (rc == 0 && !tmp->data_are_ready) {
  556. // the mutex is unlocked within pthread_cond_timedwait()
  557. rc = pthread_cond_timedwait(&tmp->cond, &tmp->mutex, &tp);
  558. // the mutex is again ours
  559. }
  560. if (tmp->data_are_ready) {
  561. // we have a response
  562. buffer_fast_strcat(wb, buffer_tostring(temp_wb), buffer_strlen(temp_wb));
  563. wb->content_type = temp_wb->content_type;
  564. wb->expires = temp_wb->expires;
  565. if(wb->expires)
  566. buffer_cacheable(wb);
  567. else
  568. buffer_no_cacheable(wb);
  569. code = tmp->code;
  570. }
  571. else if (rc == ETIMEDOUT) {
  572. // timeout
  573. // we will go away and let the callback free the structure
  574. tmp->free_with_signal = true;
  575. we_should_free = false;
  576. code = rrd_call_function_error(wb, "Timeout while waiting for a response from the collector.", HTTP_RESP_GATEWAY_TIMEOUT);
  577. }
  578. else
  579. code = rrd_call_function_error(wb, "Failed to get the response from the collector.", HTTP_RESP_INTERNAL_SERVER_ERROR);
  580. netdata_mutex_unlock(&tmp->mutex);
  581. }
  582. else {
  583. if(!buffer_strlen(wb))
  584. rrd_call_function_error(wb, "Failed to send request to the collector.", code);
  585. }
  586. if (we_should_free) {
  587. rrd_function_call_wait_free(tmp);
  588. buffer_free(temp_wb);
  589. }
  590. }
  591. return code;
  592. }
  593. int rrd_call_function_async(RRDHOST *host, BUFFER *wb, int timeout, const char *name,
  594. rrd_call_function_async_callback callback, void *callback_data) {
  595. int code;
  596. struct rrd_collector_function *rdcf = NULL;
  597. char key[PLUGINSD_LINE_MAX + 1];
  598. size_t key_length = sanitize_function_text(key, name, PLUGINSD_LINE_MAX);
  599. code = rrd_call_function_find(host, wb, key, key_length, &rdcf);
  600. if(code != HTTP_RESP_OK)
  601. return code;
  602. if(timeout <= 0)
  603. timeout = rdcf->timeout;
  604. code = rdcf->function(wb, timeout, key, rdcf->collector_data, callback, callback_data);
  605. if(code != HTTP_RESP_OK) {
  606. if (!buffer_strlen(wb))
  607. rrd_call_function_error(wb, "Failed to send request to the collector.", code);
  608. }
  609. return code;
  610. }
  611. static void functions2json(DICTIONARY *functions, BUFFER *wb, const char *ident, const char *kq, const char *sq) {
  612. struct rrd_collector_function *t;
  613. dfe_start_read(functions, t) {
  614. if(!t->collector->running) continue;
  615. if(t_dfe.counter)
  616. buffer_strcat(wb, ",\n");
  617. buffer_sprintf(wb, "%s%s%s%s: {", ident, kq, t_dfe.name, kq);
  618. buffer_sprintf(wb, "\n\t%s%shelp%s: %s%s%s", ident, kq, kq, sq, string2str(t->help), sq);
  619. buffer_sprintf(wb, ",\n\t%s%stimeout%s: %d", ident, kq, kq, t->timeout);
  620. buffer_sprintf(wb, ",\n\t%s%soptions%s: \"%s%s\"", ident, kq, kq
  621. , (t->options & RRD_FUNCTION_LOCAL)?"LOCAL ":""
  622. , (t->options & RRD_FUNCTION_GLOBAL)?"GLOBAL ":""
  623. );
  624. buffer_sprintf(wb, "\n%s}", ident);
  625. }
  626. dfe_done(t);
  627. buffer_strcat(wb, "\n");
  628. }
  629. void chart_functions2json(RRDSET *st, BUFFER *wb, int tabs, const char *kq, const char *sq) {
  630. if(!st || !st->functions_view) return;
  631. char ident[tabs + 1];
  632. ident[tabs] = '\0';
  633. while(tabs) ident[--tabs] = '\t';
  634. functions2json(st->functions_view, wb, ident, kq, sq);
  635. }
  636. void host_functions2json(RRDHOST *host, BUFFER *wb) {
  637. if(!host || !host->functions) return;
  638. buffer_json_member_add_object(wb, "functions");
  639. struct rrd_collector_function *t;
  640. dfe_start_read(host->functions, t) {
  641. if(!t->collector->running) continue;
  642. buffer_json_member_add_object(wb, t_dfe.name);
  643. buffer_json_member_add_string(wb, "help", string2str(t->help));
  644. buffer_json_member_add_int64(wb, "timeout", t->timeout);
  645. buffer_json_member_add_array(wb, "options");
  646. if(t->options & RRD_FUNCTION_GLOBAL)
  647. buffer_json_add_array_item_string(wb, "GLOBAL");
  648. if(t->options & RRD_FUNCTION_LOCAL)
  649. buffer_json_add_array_item_string(wb, "LOCAL");
  650. buffer_json_array_close(wb);
  651. buffer_json_object_close(wb);
  652. }
  653. dfe_done(t);
  654. buffer_json_object_close(wb);
  655. }
  656. void chart_functions_to_dict(DICTIONARY *rrdset_functions_view, DICTIONARY *dst) {
  657. if(!rrdset_functions_view || !dst) return;
  658. struct rrd_collector_function *t;
  659. dfe_start_read(rrdset_functions_view, t) {
  660. if(!t->collector->running) continue;
  661. dictionary_set(dst, t_dfe.name, NULL, 0);
  662. }
  663. dfe_done(t);
  664. }