pluginsd_parser.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "pluginsd_parser.h"
  3. #define LOG_FUNCTIONS false
  4. PARSER_RC pluginsd_set(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  5. {
  6. char *dimension = words[1];
  7. char *value = words[2];
  8. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  9. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  10. if (unlikely(!dimension || !*dimension)) {
  11. error("requested a SET on chart '%s' of host '%s', without a dimension. Disabling it.", rrdset_id(st), rrdhost_hostname(host));
  12. goto disable;
  13. }
  14. if (unlikely(!value || !*value))
  15. value = NULL;
  16. if (unlikely(!st)) {
  17. error(
  18. "requested a SET on dimension %s with value %s on host '%s', without a BEGIN. Disabling it.", dimension,
  19. value ? value : "<nothing>", rrdhost_hostname(host));
  20. goto disable;
  21. }
  22. if (unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  23. debug(D_PLUGINSD, "is setting dimension '%s'/'%s' to '%s'", rrdset_id(st), dimension, value ? value : "<nothing>");
  24. if (value) {
  25. RRDDIM *rd = rrddim_find(st, dimension);
  26. if (unlikely(!rd)) {
  27. error(
  28. "requested a SET to dimension with id '%s' on stats '%s' (%s) on host '%s', which does not exist. Disabling it.",
  29. dimension, rrdset_name(st), rrdset_id(st), rrdhost_hostname(st->rrdhost));
  30. goto disable;
  31. } else
  32. rrddim_set_by_pointer(st, rd, strtoll(value, NULL, 0));
  33. }
  34. return PARSER_RC_OK;
  35. disable:
  36. ((PARSER_USER_OBJECT *) user)->enabled = 0;
  37. return PARSER_RC_ERROR;
  38. }
  39. PARSER_RC pluginsd_begin(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  40. {
  41. char *id = words[1];
  42. char *microseconds_txt = words[2];
  43. RRDSET *st = NULL;
  44. RRDHOST *host = ((PARSER_USER_OBJECT *)user)->host;
  45. if (unlikely(!id)) {
  46. error("requested a BEGIN without a chart id for host '%s'. Disabling it.", rrdhost_hostname(host));
  47. goto disable;
  48. }
  49. st = rrdset_find(host, id);
  50. if (unlikely(!st)) {
  51. error("requested a BEGIN on chart '%s', which does not exist on host '%s'. Disabling it.", id, rrdhost_hostname(host));
  52. goto disable;
  53. }
  54. ((PARSER_USER_OBJECT *)user)->st = st;
  55. usec_t microseconds = 0;
  56. if (microseconds_txt && *microseconds_txt)
  57. microseconds = str2ull(microseconds_txt);
  58. if (likely(st->counter_done)) {
  59. if (likely(microseconds)) {
  60. if (((PARSER_USER_OBJECT *)user)->trust_durations)
  61. rrdset_next_usec_unfiltered(st, microseconds);
  62. else
  63. rrdset_next_usec(st, microseconds);
  64. } else
  65. rrdset_next(st);
  66. }
  67. return PARSER_RC_OK;
  68. disable:
  69. ((PARSER_USER_OBJECT *)user)->enabled = 0;
  70. return PARSER_RC_ERROR;
  71. }
  72. PARSER_RC pluginsd_end(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  73. {
  74. UNUSED(words);
  75. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  76. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  77. if (unlikely(!st)) {
  78. error("requested an END, without a BEGIN on host '%s'. Disabling it.", rrdhost_hostname(host));
  79. ((PARSER_USER_OBJECT *) user)->enabled = 0;
  80. return PARSER_RC_ERROR;
  81. }
  82. if (unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  83. debug(D_PLUGINSD, "requested an END on chart '%s'", rrdset_id(st));
  84. ((PARSER_USER_OBJECT *) user)->st = NULL;
  85. ((PARSER_USER_OBJECT *) user)->count++;
  86. rrdset_done(st);
  87. return PARSER_RC_OK;
  88. }
  89. PARSER_RC pluginsd_chart(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  90. {
  91. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  92. if (unlikely(!host && !((PARSER_USER_OBJECT *) user)->host_exists)) {
  93. debug(D_PLUGINSD, "Ignoring chart belonging to missing or ignored host.");
  94. return PARSER_RC_OK;
  95. }
  96. char *type = words[1];
  97. char *name = words[2];
  98. char *title = words[3];
  99. char *units = words[4];
  100. char *family = words[5];
  101. char *context = words[6];
  102. char *chart = words[7];
  103. char *priority_s = words[8];
  104. char *update_every_s = words[9];
  105. char *options = words[10];
  106. char *plugin = words[11];
  107. char *module = words[12];
  108. // parse the id from type
  109. char *id = NULL;
  110. if (likely(type && (id = strchr(type, '.')))) {
  111. *id = '\0';
  112. id++;
  113. }
  114. // make sure we have the required variables
  115. if (unlikely((!type || !*type || !id || !*id))) {
  116. if (likely(host))
  117. error("requested a CHART, without a type.id, on host '%s'. Disabling it.", rrdhost_hostname(host));
  118. else
  119. error("requested a CHART, without a type.id. Disabling it.");
  120. ((PARSER_USER_OBJECT *) user)->enabled = 0;
  121. return PARSER_RC_ERROR;
  122. }
  123. // parse the name, and make sure it does not include 'type.'
  124. if (unlikely(name && *name)) {
  125. // when data are streamed from child nodes
  126. // name will be type.name
  127. // so we have to remove 'type.' from name too
  128. size_t len = strlen(type);
  129. if (strncmp(type, name, len) == 0 && name[len] == '.')
  130. name = &name[len + 1];
  131. // if the name is the same with the id,
  132. // or is just 'NULL', clear it.
  133. if (unlikely(strcmp(name, id) == 0 || strcasecmp(name, "NULL") == 0 || strcasecmp(name, "(NULL)") == 0))
  134. name = NULL;
  135. }
  136. int priority = 1000;
  137. if (likely(priority_s && *priority_s))
  138. priority = str2i(priority_s);
  139. int update_every = ((PARSER_USER_OBJECT *) user)->cd->update_every;
  140. if (likely(update_every_s && *update_every_s))
  141. update_every = str2i(update_every_s);
  142. if (unlikely(!update_every))
  143. update_every = ((PARSER_USER_OBJECT *) user)->cd->update_every;
  144. RRDSET_TYPE chart_type = RRDSET_TYPE_LINE;
  145. if (unlikely(chart))
  146. chart_type = rrdset_type_id(chart);
  147. if (unlikely(name && !*name))
  148. name = NULL;
  149. if (unlikely(family && !*family))
  150. family = NULL;
  151. if (unlikely(context && !*context))
  152. context = NULL;
  153. if (unlikely(!title))
  154. title = "";
  155. if (unlikely(!units))
  156. units = "unknown";
  157. debug(
  158. D_PLUGINSD,
  159. "creating chart type='%s', id='%s', name='%s', family='%s', context='%s', chart='%s', priority=%d, update_every=%d",
  160. type, id, name ? name : "", family ? family : "", context ? context : "", rrdset_type_name(chart_type),
  161. priority, update_every);
  162. RRDSET *st = NULL;
  163. st = rrdset_create(
  164. host, type, id, name, family, context, title, units,
  165. (plugin && *plugin) ? plugin : ((PARSER_USER_OBJECT *)user)->cd->filename,
  166. module, priority, update_every,
  167. chart_type);
  168. if (likely(st)) {
  169. if (options && *options) {
  170. if (strstr(options, "obsolete"))
  171. rrdset_is_obsolete(st);
  172. else
  173. rrdset_isnot_obsolete(st);
  174. if (strstr(options, "detail"))
  175. rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
  176. else
  177. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  178. if (strstr(options, "hidden"))
  179. rrdset_flag_set(st, RRDSET_FLAG_HIDDEN);
  180. else
  181. rrdset_flag_clear(st, RRDSET_FLAG_HIDDEN);
  182. if (strstr(options, "store_first"))
  183. rrdset_flag_set(st, RRDSET_FLAG_STORE_FIRST);
  184. else
  185. rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
  186. } else {
  187. rrdset_isnot_obsolete(st);
  188. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  189. rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
  190. }
  191. }
  192. ((PARSER_USER_OBJECT *)user)->st = st;
  193. return PARSER_RC_OK;
  194. }
  195. PARSER_RC pluginsd_dimension(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  196. {
  197. char *id = words[1];
  198. char *name = words[2];
  199. char *algorithm = words[3];
  200. char *multiplier_s = words[4];
  201. char *divisor_s = words[5];
  202. char *options = words[6];
  203. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  204. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  205. if (unlikely(!host && !((PARSER_USER_OBJECT *) user)->host_exists)) {
  206. debug(D_PLUGINSD, "Ignoring dimension belonging to missing or ignored host.");
  207. return PARSER_RC_OK;
  208. }
  209. if (unlikely(!id)) {
  210. error(
  211. "requested a DIMENSION, without an id, host '%s' and chart '%s'. Disabling it.", rrdhost_hostname(host),
  212. st ? rrdset_id(st) : "UNSET");
  213. goto disable;
  214. }
  215. if (unlikely(!st && !((PARSER_USER_OBJECT *) user)->st_exists)) {
  216. error("requested a DIMENSION, without a CHART, on host '%s'. Disabling it.", rrdhost_hostname(host));
  217. goto disable;
  218. }
  219. long multiplier = 1;
  220. if (multiplier_s && *multiplier_s) {
  221. multiplier = strtol(multiplier_s, NULL, 0);
  222. if (unlikely(!multiplier))
  223. multiplier = 1;
  224. }
  225. long divisor = 1;
  226. if (likely(divisor_s && *divisor_s)) {
  227. divisor = strtol(divisor_s, NULL, 0);
  228. if (unlikely(!divisor))
  229. divisor = 1;
  230. }
  231. if (unlikely(!algorithm || !*algorithm))
  232. algorithm = "absolute";
  233. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  234. debug(
  235. D_PLUGINSD,
  236. "creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'",
  237. rrdset_id(st), id, name ? name : "", rrd_algorithm_name(rrd_algorithm_id(algorithm)), multiplier, divisor,
  238. options ? options : "");
  239. RRDDIM *rd = rrddim_add(st, id, name, multiplier, divisor, rrd_algorithm_id(algorithm));
  240. int unhide_dimension = 1;
  241. rrddim_option_clear(rd, RRDDIM_OPTION_DONT_DETECT_RESETS_OR_OVERFLOWS);
  242. if (options && *options) {
  243. if (strstr(options, "obsolete") != NULL)
  244. rrddim_is_obsolete(st, rd);
  245. else
  246. rrddim_isnot_obsolete(st, rd);
  247. unhide_dimension = !strstr(options, "hidden");
  248. if (strstr(options, "noreset") != NULL)
  249. rrddim_option_set(rd, RRDDIM_OPTION_DONT_DETECT_RESETS_OR_OVERFLOWS);
  250. if (strstr(options, "nooverflow") != NULL)
  251. rrddim_option_set(rd, RRDDIM_OPTION_DONT_DETECT_RESETS_OR_OVERFLOWS);
  252. } else
  253. rrddim_isnot_obsolete(st, rd);
  254. if (likely(unhide_dimension)) {
  255. rrddim_option_clear(rd, RRDDIM_OPTION_HIDDEN);
  256. if (rrddim_flag_check(rd, RRDDIM_FLAG_META_HIDDEN)) {
  257. rrddim_flag_clear(rd, RRDDIM_FLAG_META_HIDDEN);
  258. metaqueue_dimension_update_flags(rd);
  259. }
  260. } else {
  261. rrddim_option_set(rd, RRDDIM_OPTION_HIDDEN);
  262. if (!rrddim_flag_check(rd, RRDDIM_FLAG_META_HIDDEN)) {
  263. rrddim_flag_set(rd, RRDDIM_FLAG_META_HIDDEN);
  264. metaqueue_dimension_update_flags(rd);
  265. }
  266. }
  267. return PARSER_RC_OK;
  268. disable:
  269. ((PARSER_USER_OBJECT *)user)->enabled = 0;
  270. return PARSER_RC_ERROR;
  271. }
  272. // ----------------------------------------------------------------------------
  273. // execution of functions
  274. struct inflight_function {
  275. int code;
  276. int timeout;
  277. BUFFER *destination_wb;
  278. STRING *function;
  279. void (*callback)(BUFFER *wb, int code, void *callback_data);
  280. void *callback_data;
  281. usec_t timeout_ut;
  282. usec_t started_ut;
  283. usec_t sent_ut;
  284. };
  285. static void inflight_functions_insert_callback(const DICTIONARY_ITEM *item, void *func, void *parser_ptr) {
  286. struct inflight_function *pf = func;
  287. PARSER *parser = parser_ptr;
  288. FILE *fp = parser->output;
  289. // leave this code as default, so that when the dictionary is destroyed this will be sent back to the caller
  290. pf->code = HTTP_RESP_GATEWAY_TIMEOUT;
  291. // send the command to the plugin
  292. int ret = fprintf(fp, "FUNCTION %s %d \"%s\"\n",
  293. dictionary_acquired_item_name(item),
  294. pf->timeout,
  295. string2str(pf->function));
  296. pf->sent_ut = now_realtime_usec();
  297. if(ret < 0) {
  298. error("FUNCTION: failed to send function to plugin, fprintf() returned error %d", ret);
  299. rrd_call_function_error(pf->destination_wb, "Failed to communicate with collector", HTTP_RESP_BACKEND_FETCH_FAILED);
  300. }
  301. else {
  302. fflush(fp);
  303. internal_error(LOG_FUNCTIONS,
  304. "FUNCTION '%s' with transaction '%s' sent to collector (%d bytes, fd %d, in %llu usec)",
  305. string2str(pf->function), dictionary_acquired_item_name(item), ret, fileno(fp),
  306. pf->sent_ut - pf->started_ut);
  307. }
  308. }
  309. static bool inflight_functions_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *func __maybe_unused, void *new_func, void *parser_ptr __maybe_unused) {
  310. struct inflight_function *pf = new_func;
  311. error("PLUGINSD_PARSER: duplicate UUID on pending function '%s' detected. Ignoring the second one.", string2str(pf->function));
  312. pf->code = rrd_call_function_error(pf->destination_wb, "This request is already in progress", HTTP_RESP_BAD_REQUEST);
  313. pf->callback(pf->destination_wb, pf->code, pf->callback_data);
  314. string_freez(pf->function);
  315. return false;
  316. }
  317. static void inflight_functions_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *func, void *parser_ptr __maybe_unused) {
  318. struct inflight_function *pf = func;
  319. internal_error(LOG_FUNCTIONS,
  320. "FUNCTION '%s' result of transaction '%s' received from collector (%zu bytes, request %llu usec, response %llu usec)",
  321. string2str(pf->function), dictionary_acquired_item_name(item),
  322. buffer_strlen(pf->destination_wb), pf->sent_ut - pf->started_ut, now_realtime_usec() - pf->sent_ut);
  323. pf->callback(pf->destination_wb, pf->code, pf->callback_data);
  324. string_freez(pf->function);
  325. }
  326. void inflight_functions_init(PARSER *parser) {
  327. parser->inflight.functions = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE);
  328. dictionary_register_insert_callback(parser->inflight.functions, inflight_functions_insert_callback, parser);
  329. dictionary_register_delete_callback(parser->inflight.functions, inflight_functions_delete_callback, parser);
  330. dictionary_register_conflict_callback(parser->inflight.functions, inflight_functions_conflict_callback, parser);
  331. }
  332. static void inflight_functions_garbage_collect(PARSER *parser, usec_t now) {
  333. parser->inflight.smaller_timeout = 0;
  334. struct inflight_function *pf;
  335. dfe_start_write(parser->inflight.functions, pf) {
  336. if (pf->timeout_ut < now) {
  337. internal_error(true,
  338. "FUNCTION '%s' removing expired transaction '%s', after %llu usec.",
  339. string2str(pf->function), pf_dfe.name, now - pf->started_ut);
  340. if(!buffer_strlen(pf->destination_wb) || pf->code == HTTP_RESP_OK)
  341. pf->code = rrd_call_function_error(pf->destination_wb,
  342. "Timeout waiting for collector response.",
  343. HTTP_RESP_GATEWAY_TIMEOUT);
  344. dictionary_del(parser->inflight.functions, pf_dfe.name);
  345. }
  346. else if(!parser->inflight.smaller_timeout || pf->timeout_ut < parser->inflight.smaller_timeout)
  347. parser->inflight.smaller_timeout = pf->timeout_ut;
  348. }
  349. dfe_done(pf);
  350. }
  351. // this is the function that is called from
  352. // rrd_call_function_and_wait() and rrd_call_function_async()
  353. static int pluginsd_execute_function_callback(BUFFER *destination_wb, int timeout, const char *function, void *collector_data, void (*callback)(BUFFER *wb, int code, void *callback_data), void *callback_data) {
  354. PARSER *parser = collector_data;
  355. usec_t now = now_realtime_usec();
  356. struct inflight_function tmp = {
  357. .started_ut = now,
  358. .timeout_ut = now + timeout * USEC_PER_SEC,
  359. .destination_wb = destination_wb,
  360. .timeout = timeout,
  361. .function = string_strdupz(function),
  362. .callback = callback,
  363. .callback_data = callback_data,
  364. };
  365. uuid_t uuid;
  366. uuid_generate_time(uuid);
  367. char key[UUID_STR_LEN];
  368. uuid_unparse_lower(uuid, key);
  369. dictionary_write_lock(parser->inflight.functions);
  370. // if there is any error, our dictionary callbacks will call the caller callback to notify
  371. // the caller about the error - no need for error handling here.
  372. dictionary_set(parser->inflight.functions, key, &tmp, sizeof(struct inflight_function));
  373. if(!parser->inflight.smaller_timeout || tmp.timeout_ut < parser->inflight.smaller_timeout)
  374. parser->inflight.smaller_timeout = tmp.timeout_ut;
  375. // garbage collect stale inflight functions
  376. if(parser->inflight.smaller_timeout < now)
  377. inflight_functions_garbage_collect(parser, now);
  378. dictionary_write_unlock(parser->inflight.functions);
  379. return HTTP_RESP_OK;
  380. }
  381. PARSER_RC pluginsd_function(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  382. {
  383. bool global = false;
  384. int i = 1;
  385. if(strcmp(words[i], "GLOBAL") == 0) {
  386. i++;
  387. global = true;
  388. }
  389. char *name = words[i++];
  390. char *timeout_s = words[i++];
  391. char *help = words[i++];
  392. RRDSET *st = (global)?NULL:((PARSER_USER_OBJECT *) user)->st;
  393. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  394. if (unlikely(!host || !timeout_s || !name || !help || (!global && !st))) {
  395. error("requested a FUNCTION, without providing the required data (global = '%s', name = '%s', timeout = '%s', help = '%s'), host '%s', chart '%s'. Ignoring it.",
  396. global?"yes":"no",
  397. name?name:"(unset)",
  398. timeout_s?timeout_s:"(unset)",
  399. help?help:"(unset)",
  400. host?rrdhost_hostname(host):"(unset)",
  401. st?rrdset_id(st):"(unset)");
  402. return PARSER_RC_OK;
  403. }
  404. int timeout = PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT;
  405. if (timeout_s && *timeout_s) {
  406. timeout = str2i(timeout_s);
  407. if (unlikely(timeout <= 0))
  408. timeout = PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT;
  409. }
  410. PARSER *parser = ((PARSER_USER_OBJECT *) user)->parser;
  411. rrd_collector_add_function(host, st, name, timeout, help, false, pluginsd_execute_function_callback, parser);
  412. return PARSER_RC_OK;
  413. }
  414. static void pluginsd_function_result_end(struct parser *parser, void *action_data) {
  415. STRING *key = action_data;
  416. if(key)
  417. dictionary_del(parser->inflight.functions, string2str(key));
  418. string_freez(key);
  419. }
  420. PARSER_RC pluginsd_function_result_begin(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  421. {
  422. char *key = words[1];
  423. char *status = words[2];
  424. char *format = words[3];
  425. char *expires = words[4];
  426. if (unlikely(!key || !*key || !status || !*status || !format || !*format || !expires || !*expires)) {
  427. error("got a " PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN " without providing the required data (key = '%s', status = '%s', format = '%s', expires = '%s')."
  428. , key ? key : "(unset)"
  429. , status ? status : "(unset)"
  430. , format ? format : "(unset)"
  431. , expires ? expires : "(unset)"
  432. );
  433. }
  434. int code = (status && *status) ? str2i(status) : 0;
  435. if (code <= 0)
  436. code = HTTP_RESP_BACKEND_RESPONSE_INVALID;
  437. time_t expiration = (expires && *expires) ? str2l(expires) : 0;
  438. PARSER *parser = ((PARSER_USER_OBJECT *) user)->parser;
  439. struct inflight_function *pf = NULL;
  440. if(key && *key)
  441. pf = (struct inflight_function *)dictionary_get(parser->inflight.functions, key);
  442. if(!pf) {
  443. error("got a " PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN " for transaction '%s', but the transaction is not found.", key?key:"(unset)");
  444. }
  445. else {
  446. if(format && *format)
  447. pf->destination_wb->contenttype = functions_format_to_content_type(format);
  448. pf->code = code;
  449. pf->destination_wb->expires = expiration;
  450. if(expiration <= now_realtime_sec())
  451. buffer_no_cacheable(pf->destination_wb);
  452. else
  453. buffer_cacheable(pf->destination_wb);
  454. }
  455. parser->defer.response = (pf) ? pf->destination_wb : NULL;
  456. parser->defer.end_keyword = PLUGINSD_KEYWORD_FUNCTION_RESULT_END;
  457. parser->defer.action = pluginsd_function_result_end;
  458. parser->defer.action_data = string_strdupz(key); // it is ok is key is NULL
  459. parser->flags |= PARSER_DEFER_UNTIL_KEYWORD;
  460. return PARSER_RC_OK;
  461. }
  462. // ----------------------------------------------------------------------------
  463. PARSER_RC pluginsd_variable(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  464. {
  465. char *name = words[1];
  466. char *value = words[2];
  467. NETDATA_DOUBLE v;
  468. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  469. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  470. int global = (st) ? 0 : 1;
  471. if (name && *name) {
  472. if ((strcmp(name, "GLOBAL") == 0 || strcmp(name, "HOST") == 0)) {
  473. global = 1;
  474. name = words[2];
  475. value = words[3];
  476. } else if ((strcmp(name, "LOCAL") == 0 || strcmp(name, "CHART") == 0)) {
  477. global = 0;
  478. name = words[2];
  479. value = words[3];
  480. }
  481. }
  482. if (unlikely(!name || !*name)) {
  483. error("requested a VARIABLE on host '%s', without a variable name. Disabling it.", rrdhost_hostname(host));
  484. ((PARSER_USER_OBJECT *)user)->enabled = 0;
  485. return PARSER_RC_ERROR;
  486. }
  487. if (unlikely(!value || !*value))
  488. value = NULL;
  489. if (unlikely(!value)) {
  490. error("cannot set %s VARIABLE '%s' on host '%s' to an empty value", (global) ? "HOST" : "CHART", name,
  491. rrdhost_hostname(host));
  492. return PARSER_RC_OK;
  493. }
  494. if (!global && !st) {
  495. error("cannot find/create CHART VARIABLE '%s' on host '%s' without a chart", name, rrdhost_hostname(host));
  496. return PARSER_RC_OK;
  497. }
  498. char *endptr = NULL;
  499. v = (NETDATA_DOUBLE)str2ndd(value, &endptr);
  500. if (unlikely(endptr && *endptr)) {
  501. if (endptr == value)
  502. error(
  503. "the value '%s' of VARIABLE '%s' on host '%s' cannot be parsed as a number", value, name,
  504. rrdhost_hostname(host));
  505. else
  506. error(
  507. "the value '%s' of VARIABLE '%s' on host '%s' has leftovers: '%s'", value, name, rrdhost_hostname(host),
  508. endptr);
  509. }
  510. if (global) {
  511. const RRDVAR_ACQUIRED *rva = rrdvar_custom_host_variable_add_and_acquire(host, name);
  512. if (rva) {
  513. rrdvar_custom_host_variable_set(host, rva, v);
  514. rrdvar_custom_host_variable_release(host, rva);
  515. }
  516. else
  517. error("cannot find/create HOST VARIABLE '%s' on host '%s'", name, rrdhost_hostname(host));
  518. } else {
  519. const RRDSETVAR_ACQUIRED *rsa = rrdsetvar_custom_chart_variable_add_and_acquire(st, name);
  520. if (rsa) {
  521. rrdsetvar_custom_chart_variable_set(st, rsa, v);
  522. rrdsetvar_custom_chart_variable_release(st, rsa);
  523. }
  524. else
  525. error("cannot find/create CHART VARIABLE '%s' on host '%s', chart '%s'", name, rrdhost_hostname(host), rrdset_id(st));
  526. }
  527. return PARSER_RC_OK;
  528. }
  529. PARSER_RC pluginsd_flush(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  530. {
  531. UNUSED(words);
  532. debug(D_PLUGINSD, "requested a FLUSH");
  533. ((PARSER_USER_OBJECT *) user)->st = NULL;
  534. return PARSER_RC_OK;
  535. }
  536. PARSER_RC pluginsd_disable(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  537. {
  538. UNUSED(user);
  539. UNUSED(words);
  540. info("called DISABLE. Disabling it.");
  541. ((PARSER_USER_OBJECT *) user)->enabled = 0;
  542. return PARSER_RC_ERROR;
  543. }
  544. PARSER_RC pluginsd_label(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  545. {
  546. char *store;
  547. if (!words[1] || !words[2] || !words[3]) {
  548. error("Ignoring malformed or empty LABEL command.");
  549. return PARSER_RC_OK;
  550. }
  551. if (!words[4])
  552. store = words[3];
  553. else {
  554. store = callocz(PLUGINSD_LINE_MAX + 1, sizeof(char));
  555. size_t remaining = PLUGINSD_LINE_MAX;
  556. char *move = store;
  557. int i = 3;
  558. while (i < PLUGINSD_MAX_WORDS) {
  559. size_t length = strlen(words[i]);
  560. if ((length + 1) >= remaining)
  561. break;
  562. remaining -= (length + 1);
  563. memcpy(move, words[i], length);
  564. move += length;
  565. *move++ = ' ';
  566. i++;
  567. if (!words[i])
  568. break;
  569. }
  570. }
  571. if(unlikely(!((PARSER_USER_OBJECT *) user)->new_host_labels))
  572. ((PARSER_USER_OBJECT *) user)->new_host_labels = rrdlabels_create();
  573. rrdlabels_add(((PARSER_USER_OBJECT *)user)->new_host_labels, words[1], store, strtol(words[2], NULL, 10));
  574. if (store != words[3])
  575. freez(store);
  576. return PARSER_RC_OK;
  577. }
  578. PARSER_RC pluginsd_overwrite(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  579. {
  580. UNUSED(words);
  581. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  582. debug(D_PLUGINSD, "requested to OVERWRITE host labels");
  583. if(!host->rrdlabels)
  584. host->rrdlabels = rrdlabels_create();
  585. rrdlabels_migrate_to_these(host->rrdlabels, (DICTIONARY *) (((PARSER_USER_OBJECT *)user)->new_host_labels));
  586. metaqueue_store_host_labels(host->machine_guid);
  587. rrdlabels_destroy(((PARSER_USER_OBJECT *)user)->new_host_labels);
  588. ((PARSER_USER_OBJECT *)user)->new_host_labels = NULL;
  589. return PARSER_RC_OK;
  590. }
  591. PARSER_RC pluginsd_clabel(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  592. {
  593. if (!words[1] || !words[2] || !words[3]) {
  594. error("Ignoring malformed or empty CHART LABEL command.");
  595. return PARSER_RC_OK;
  596. }
  597. if(unlikely(!((PARSER_USER_OBJECT *) user)->chart_rrdlabels_linked_temporarily)) {
  598. ((PARSER_USER_OBJECT *)user)->chart_rrdlabels_linked_temporarily = ((PARSER_USER_OBJECT *)user)->st->rrdlabels;
  599. rrdlabels_unmark_all(((PARSER_USER_OBJECT *)user)->chart_rrdlabels_linked_temporarily);
  600. }
  601. rrdlabels_add(((PARSER_USER_OBJECT *)user)->chart_rrdlabels_linked_temporarily, words[1], words[2], strtol(words[3], NULL, 10));
  602. return PARSER_RC_OK;
  603. }
  604. PARSER_RC pluginsd_clabel_commit(char **words, void *user, PLUGINSD_ACTION *plugins_action __maybe_unused)
  605. {
  606. UNUSED(words);
  607. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  608. RRDSET *st = ((PARSER_USER_OBJECT *)user)->st;
  609. if (unlikely(!st))
  610. return PARSER_RC_OK;
  611. debug(D_PLUGINSD, "requested to commit chart labels");
  612. if(!((PARSER_USER_OBJECT *)user)->chart_rrdlabels_linked_temporarily) {
  613. error("requested CLABEL_COMMIT on host '%s', without a BEGIN, ignoring it.", rrdhost_hostname(host));
  614. return PARSER_RC_OK;
  615. }
  616. rrdlabels_remove_all_unmarked(((PARSER_USER_OBJECT *)user)->chart_rrdlabels_linked_temporarily);
  617. rrdset_flag_set(st, RRDSET_FLAG_METADATA_UPDATE);
  618. rrdhost_flag_set(st->rrdhost, RRDHOST_FLAG_METADATA_UPDATE);
  619. ((PARSER_USER_OBJECT *)user)->chart_rrdlabels_linked_temporarily = NULL;
  620. return PARSER_RC_OK;
  621. }
  622. PARSER_RC pluginsd_guid(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  623. {
  624. char *uuid_str = words[1];
  625. uuid_t uuid;
  626. if (unlikely(!uuid_str)) {
  627. error("requested a GUID, without a uuid.");
  628. return PARSER_RC_ERROR;
  629. }
  630. if (unlikely(strlen(uuid_str) != GUID_LEN || uuid_parse(uuid_str, uuid) == -1)) {
  631. error("requested a GUID, without a valid uuid string.");
  632. return PARSER_RC_ERROR;
  633. }
  634. debug(D_PLUGINSD, "Parsed uuid=%s", uuid_str);
  635. if (plugins_action->guid_action) {
  636. return plugins_action->guid_action(user, &uuid);
  637. }
  638. return PARSER_RC_OK;
  639. }
  640. PARSER_RC pluginsd_context(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  641. {
  642. char *uuid_str = words[1];
  643. uuid_t uuid;
  644. if (unlikely(!uuid_str)) {
  645. error("requested a CONTEXT, without a uuid.");
  646. return PARSER_RC_ERROR;
  647. }
  648. if (unlikely(strlen(uuid_str) != GUID_LEN || uuid_parse(uuid_str, uuid) == -1)) {
  649. error("requested a CONTEXT, without a valid uuid string.");
  650. return PARSER_RC_ERROR;
  651. }
  652. debug(D_PLUGINSD, "Parsed uuid=%s", uuid_str);
  653. if (plugins_action->context_action) {
  654. return plugins_action->context_action(user, &uuid);
  655. }
  656. return PARSER_RC_OK;
  657. }
  658. PARSER_RC pluginsd_tombstone(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  659. {
  660. char *uuid_str = words[1];
  661. uuid_t uuid;
  662. if (unlikely(!uuid_str)) {
  663. error("requested a TOMBSTONE, without a uuid.");
  664. return PARSER_RC_ERROR;
  665. }
  666. if (unlikely(strlen(uuid_str) != GUID_LEN || uuid_parse(uuid_str, uuid) == -1)) {
  667. error("requested a TOMBSTONE, without a valid uuid string.");
  668. return PARSER_RC_ERROR;
  669. }
  670. debug(D_PLUGINSD, "Parsed uuid=%s", uuid_str);
  671. if (plugins_action->tombstone_action) {
  672. return plugins_action->tombstone_action(user, &uuid);
  673. }
  674. return PARSER_RC_OK;
  675. }
  676. PARSER_RC metalog_pluginsd_host(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  677. {
  678. char *machine_guid = words[1];
  679. char *hostname = words[2];
  680. char *registry_hostname = words[3];
  681. char *update_every_s = words[4];
  682. char *os = words[5];
  683. char *timezone = words[6];
  684. char *tags = words[7];
  685. int update_every = 1;
  686. if (likely(update_every_s && *update_every_s))
  687. update_every = str2i(update_every_s);
  688. if (unlikely(!update_every))
  689. update_every = 1;
  690. debug(D_PLUGINSD, "HOST PARSED: guid=%s, hostname=%s, reg_host=%s, update=%d, os=%s, timezone=%s, tags=%s",
  691. machine_guid, hostname, registry_hostname, update_every, os, timezone, tags);
  692. if (plugins_action->host_action) {
  693. return plugins_action->host_action(
  694. user, machine_guid, hostname, registry_hostname, update_every, os, timezone, tags);
  695. }
  696. return PARSER_RC_OK;
  697. }
  698. static void pluginsd_process_thread_cleanup(void *ptr) {
  699. PARSER *parser = (PARSER *)ptr;
  700. rrd_collector_finished();
  701. parser_destroy(parser);
  702. }
  703. // New plugins.d parser
  704. inline size_t pluginsd_process(RRDHOST *host, struct plugind *cd, FILE *fp_plugin_input, FILE *fp_plugin_output, int trust_durations)
  705. {
  706. int enabled = cd->enabled;
  707. if (!fp_plugin_input || !fp_plugin_output || !enabled) {
  708. cd->enabled = 0;
  709. return 0;
  710. }
  711. if (unlikely(fileno(fp_plugin_input) == -1)) {
  712. error("input file descriptor given is not a valid stream");
  713. cd->serial_failures++;
  714. return 0;
  715. }
  716. if (unlikely(fileno(fp_plugin_output) == -1)) {
  717. error("output file descriptor given is not a valid stream");
  718. cd->serial_failures++;
  719. return 0;
  720. }
  721. clearerr(fp_plugin_input);
  722. clearerr(fp_plugin_output);
  723. PARSER_USER_OBJECT user = {
  724. .enabled = cd->enabled,
  725. .host = host,
  726. .cd = cd,
  727. .trust_durations = trust_durations
  728. };
  729. // fp_plugin_output = our input; fp_plugin_input = our output
  730. PARSER *parser = parser_init(host, &user, fp_plugin_output, fp_plugin_input, PARSER_INPUT_SPLIT);
  731. rrd_collector_started();
  732. // this keeps the parser with its current value
  733. // so, parser needs to be allocated before pushing it
  734. netdata_thread_cleanup_push(pluginsd_process_thread_cleanup, parser);
  735. user.parser = parser;
  736. while (likely(!parser_next(parser))) {
  737. if (unlikely(netdata_exit || parser_action(parser, NULL)))
  738. break;
  739. }
  740. // free parser with the pop function
  741. netdata_thread_cleanup_pop(1);
  742. cd->enabled = user.enabled;
  743. size_t count = user.count;
  744. if (likely(count)) {
  745. cd->successful_collections += count;
  746. cd->serial_failures = 0;
  747. }
  748. else
  749. cd->serial_failures++;
  750. return count;
  751. }