pluginsd_parser.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "pluginsd_parser.h"
  3. /*
  4. * This is the action defined for the FLUSH command
  5. */
  6. PARSER_RC pluginsd_set_action(void *user, RRDSET *st, RRDDIM *rd, long long int value)
  7. {
  8. UNUSED(user);
  9. rrddim_set_by_pointer(st, rd, value);
  10. return PARSER_RC_OK;
  11. }
  12. PARSER_RC pluginsd_flush_action(void *user, RRDSET *st)
  13. {
  14. UNUSED(user);
  15. UNUSED(st);
  16. return PARSER_RC_OK;
  17. }
  18. PARSER_RC pluginsd_begin_action(void *user, RRDSET *st, usec_t microseconds, int trust_durations)
  19. {
  20. UNUSED(user);
  21. if (likely(st->counter_done)) {
  22. if (likely(microseconds)) {
  23. if (trust_durations)
  24. rrdset_next_usec_unfiltered(st, microseconds);
  25. else
  26. rrdset_next_usec(st, microseconds);
  27. } else
  28. rrdset_next(st);
  29. }
  30. return PARSER_RC_OK;
  31. }
  32. PARSER_RC pluginsd_end_action(void *user, RRDSET *st)
  33. {
  34. UNUSED(user);
  35. rrdset_done(st);
  36. return PARSER_RC_OK;
  37. }
  38. PARSER_RC pluginsd_chart_action(void *user, char *type, char *id, char *name, char *family, char *context, char *title, char *units, char *plugin,
  39. char *module, int priority, int update_every, RRDSET_TYPE chart_type, char *options)
  40. {
  41. RRDSET *st = NULL;
  42. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  43. st = rrdset_create(
  44. host, type, id, name, family, context, title, units,
  45. plugin, module, priority, update_every,
  46. chart_type);
  47. if (options && *options) {
  48. if (strstr(options, "obsolete"))
  49. rrdset_is_obsolete(st);
  50. else
  51. rrdset_isnot_obsolete(st);
  52. if (strstr(options, "detail"))
  53. rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
  54. else
  55. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  56. if (strstr(options, "hidden"))
  57. rrdset_flag_set(st, RRDSET_FLAG_HIDDEN);
  58. else
  59. rrdset_flag_clear(st, RRDSET_FLAG_HIDDEN);
  60. if (strstr(options, "store_first"))
  61. rrdset_flag_set(st, RRDSET_FLAG_STORE_FIRST);
  62. else
  63. rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
  64. } else {
  65. rrdset_isnot_obsolete(st);
  66. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  67. rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
  68. }
  69. ((PARSER_USER_OBJECT *)user)->st = st;
  70. return PARSER_RC_OK;
  71. }
  72. PARSER_RC pluginsd_disable_action(void *user)
  73. {
  74. UNUSED(user);
  75. info("called DISABLE. Disabling it.");
  76. ((PARSER_USER_OBJECT *) user)->enabled = 0;
  77. return PARSER_RC_ERROR;
  78. }
  79. PARSER_RC pluginsd_variable_action(void *user, RRDHOST *host, RRDSET *st, char *name, int global, NETDATA_DOUBLE value)
  80. {
  81. UNUSED(user);
  82. if (global) {
  83. RRDVAR *rv = rrdvar_custom_host_variable_create(host, name);
  84. if (rv)
  85. rrdvar_custom_host_variable_set(host, rv, value);
  86. else
  87. error("cannot find/create HOST VARIABLE '%s' on host '%s'", name, host->hostname);
  88. } else {
  89. RRDSETVAR *rs = rrdsetvar_custom_chart_variable_create(st, name);
  90. if (rs)
  91. rrdsetvar_custom_chart_variable_set(rs, value);
  92. else
  93. error("cannot find/create CHART VARIABLE '%s' on host '%s', chart '%s'", name, host->hostname, st->id);
  94. }
  95. return PARSER_RC_OK;
  96. }
  97. PARSER_RC pluginsd_dimension_action(void *user, RRDSET *st, char *id, char *name, char *algorithm, long multiplier, long divisor, char *options,
  98. RRD_ALGORITHM algorithm_type)
  99. {
  100. UNUSED(user);
  101. UNUSED(algorithm);
  102. RRDDIM *rd = rrddim_add(st, id, name, multiplier, divisor, algorithm_type);
  103. int unhide_dimension = 1;
  104. rrddim_flag_clear(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
  105. if (options && *options) {
  106. if (strstr(options, "obsolete") != NULL)
  107. rrddim_is_obsolete(st, rd);
  108. else
  109. rrddim_isnot_obsolete(st, rd);
  110. unhide_dimension = !strstr(options, "hidden");
  111. if (strstr(options, "noreset") != NULL)
  112. rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
  113. if (strstr(options, "nooverflow") != NULL)
  114. rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
  115. } else
  116. rrddim_isnot_obsolete(st, rd);
  117. if (likely(unhide_dimension)) {
  118. rrddim_flag_clear(rd, RRDDIM_FLAG_HIDDEN);
  119. if (rrddim_flag_check(rd, RRDDIM_FLAG_META_HIDDEN)) {
  120. (void)sql_set_dimension_option(&rd->metric_uuid, NULL);
  121. rrddim_flag_clear(rd, RRDDIM_FLAG_META_HIDDEN);
  122. }
  123. } else {
  124. rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
  125. if (!rrddim_flag_check(rd, RRDDIM_FLAG_META_HIDDEN)) {
  126. (void)sql_set_dimension_option(&rd->metric_uuid, "hidden");
  127. rrddim_flag_set(rd, RRDDIM_FLAG_META_HIDDEN);
  128. }
  129. }
  130. return PARSER_RC_OK;
  131. }
  132. PARSER_RC pluginsd_label_action(void *user, char *key, char *value, RRDLABEL_SRC source)
  133. {
  134. if(unlikely(!((PARSER_USER_OBJECT *) user)->new_host_labels))
  135. ((PARSER_USER_OBJECT *) user)->new_host_labels = rrdlabels_create();
  136. rrdlabels_add(((PARSER_USER_OBJECT *)user)->new_host_labels, key, value, source);
  137. return PARSER_RC_OK;
  138. }
  139. PARSER_RC pluginsd_clabel_action(void *user, char *key, char *value, RRDLABEL_SRC source)
  140. {
  141. if(unlikely(!((PARSER_USER_OBJECT *) user)->new_chart_labels))
  142. ((PARSER_USER_OBJECT *) user)->new_chart_labels = rrdlabels_create();
  143. rrdlabels_add(((PARSER_USER_OBJECT *)user)->new_chart_labels, key, value, source);
  144. return PARSER_RC_OK;
  145. }
  146. PARSER_RC pluginsd_clabel_commit_action(void *user, RRDHOST *host, DICTIONARY *new_chart_labels)
  147. {
  148. RRDSET *st = ((PARSER_USER_OBJECT *)user)->st;
  149. if (unlikely(!st)) {
  150. error("requested CLABEL_COMMIT on host '%s', without a BEGIN, ignoring it.", host->hostname);
  151. return PARSER_RC_OK;
  152. }
  153. rrdset_update_rrdlabels(st, new_chart_labels);
  154. return PARSER_RC_OK;
  155. }
  156. PARSER_RC pluginsd_overwrite_action(void *user, RRDHOST *host, DICTIONARY *new_host_labels)
  157. {
  158. UNUSED(user);
  159. if(!host->host_labels)
  160. host->host_labels = rrdlabels_create();
  161. rrdlabels_migrate_to_these(host->host_labels, new_host_labels);
  162. return PARSER_RC_OK;
  163. }
  164. PARSER_RC pluginsd_set(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  165. {
  166. char *dimension = words[1];
  167. char *value = words[2];
  168. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  169. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  170. if (unlikely(!dimension || !*dimension)) {
  171. error("requested a SET on chart '%s' of host '%s', without a dimension. Disabling it.", st->id, host->hostname);
  172. goto disable;
  173. }
  174. if (unlikely(!value || !*value))
  175. value = NULL;
  176. if (unlikely(!st)) {
  177. error(
  178. "requested a SET on dimension %s with value %s on host '%s', without a BEGIN. Disabling it.", dimension,
  179. value ? value : "<nothing>", host->hostname);
  180. goto disable;
  181. }
  182. if (unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  183. debug(D_PLUGINSD, "is setting dimension %s/%s to %s", st->id, dimension, value ? value : "<nothing>");
  184. if (value) {
  185. RRDDIM *rd = rrddim_find(st, dimension);
  186. if (unlikely(!rd)) {
  187. error(
  188. "requested a SET to dimension with id '%s' on stats '%s' (%s) on host '%s', which does not exist. Disabling it.",
  189. dimension, st->name, st->id, st->rrdhost->hostname);
  190. goto disable;
  191. } else {
  192. if (plugins_action->set_action) {
  193. return plugins_action->set_action(
  194. user, st, rd, strtoll(value, NULL, 0));
  195. }
  196. }
  197. }
  198. return PARSER_RC_OK;
  199. disable:
  200. ((PARSER_USER_OBJECT *) user)->enabled = 0;
  201. return PARSER_RC_ERROR;
  202. }
  203. PARSER_RC pluginsd_begin(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  204. {
  205. char *id = words[1];
  206. char *microseconds_txt = words[2];
  207. RRDSET *st = NULL;
  208. RRDHOST *host = ((PARSER_USER_OBJECT *)user)->host;
  209. if (unlikely(!id)) {
  210. error("requested a BEGIN without a chart id for host '%s'. Disabling it.", host->hostname);
  211. goto disable;
  212. }
  213. st = rrdset_find(host, id);
  214. if (unlikely(!st)) {
  215. error("requested a BEGIN on chart '%s', which does not exist on host '%s'. Disabling it.", id, host->hostname);
  216. goto disable;
  217. }
  218. ((PARSER_USER_OBJECT *)user)->st = st;
  219. usec_t microseconds = 0;
  220. if (microseconds_txt && *microseconds_txt)
  221. microseconds = str2ull(microseconds_txt);
  222. if (plugins_action->begin_action) {
  223. return plugins_action->begin_action(user, st, microseconds,
  224. ((PARSER_USER_OBJECT *)user)->trust_durations);
  225. }
  226. return PARSER_RC_OK;
  227. disable:
  228. ((PARSER_USER_OBJECT *)user)->enabled = 0;
  229. return PARSER_RC_ERROR;
  230. }
  231. PARSER_RC pluginsd_end(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  232. {
  233. UNUSED(words);
  234. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  235. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  236. if (unlikely(!st)) {
  237. error("requested an END, without a BEGIN on host '%s'. Disabling it.", host->hostname);
  238. ((PARSER_USER_OBJECT *) user)->enabled = 0;
  239. return PARSER_RC_ERROR;
  240. }
  241. if (unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  242. debug(D_PLUGINSD, "requested an END on chart %s", st->id);
  243. ((PARSER_USER_OBJECT *) user)->st = NULL;
  244. ((PARSER_USER_OBJECT *) user)->count++;
  245. if (plugins_action->end_action) {
  246. return plugins_action->end_action(user, st);
  247. }
  248. return PARSER_RC_OK;
  249. }
  250. PARSER_RC pluginsd_chart(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  251. {
  252. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  253. if (unlikely(!host && !((PARSER_USER_OBJECT *) user)->host_exists)) {
  254. debug(D_PLUGINSD, "Ignoring chart belonging to missing or ignored host.");
  255. return PARSER_RC_OK;
  256. }
  257. char *type = words[1];
  258. char *name = words[2];
  259. char *title = words[3];
  260. char *units = words[4];
  261. char *family = words[5];
  262. char *context = words[6];
  263. char *chart = words[7];
  264. char *priority_s = words[8];
  265. char *update_every_s = words[9];
  266. char *options = words[10];
  267. char *plugin = words[11];
  268. char *module = words[12];
  269. int have_action = ((plugins_action->chart_action) != NULL);
  270. // parse the id from type
  271. char *id = NULL;
  272. if (likely(type && (id = strchr(type, '.')))) {
  273. *id = '\0';
  274. id++;
  275. }
  276. // make sure we have the required variables
  277. if (unlikely((!type || !*type || !id || !*id))) {
  278. if (likely(host))
  279. error("requested a CHART, without a type.id, on host '%s'. Disabling it.", host->hostname);
  280. else
  281. error("requested a CHART, without a type.id. Disabling it.");
  282. ((PARSER_USER_OBJECT *) user)->enabled = 0;
  283. return PARSER_RC_ERROR;
  284. }
  285. // parse the name, and make sure it does not include 'type.'
  286. if (unlikely(name && *name)) {
  287. // when data are streamed from child nodes
  288. // name will be type.name
  289. // so we have to remove 'type.' from name too
  290. size_t len = strlen(type);
  291. if (strncmp(type, name, len) == 0 && name[len] == '.')
  292. name = &name[len + 1];
  293. // if the name is the same with the id,
  294. // or is just 'NULL', clear it.
  295. if (unlikely(strcmp(name, id) == 0 || strcasecmp(name, "NULL") == 0 || strcasecmp(name, "(NULL)") == 0))
  296. name = NULL;
  297. }
  298. int priority = 1000;
  299. if (likely(priority_s && *priority_s))
  300. priority = str2i(priority_s);
  301. int update_every = ((PARSER_USER_OBJECT *) user)->cd->update_every;
  302. if (likely(update_every_s && *update_every_s))
  303. update_every = str2i(update_every_s);
  304. if (unlikely(!update_every))
  305. update_every = ((PARSER_USER_OBJECT *) user)->cd->update_every;
  306. RRDSET_TYPE chart_type = RRDSET_TYPE_LINE;
  307. if (unlikely(chart))
  308. chart_type = rrdset_type_id(chart);
  309. if (unlikely(name && !*name))
  310. name = NULL;
  311. if (unlikely(family && !*family))
  312. family = NULL;
  313. if (unlikely(context && !*context))
  314. context = NULL;
  315. if (unlikely(!title))
  316. title = "";
  317. if (unlikely(!units))
  318. units = "unknown";
  319. debug(
  320. D_PLUGINSD,
  321. "creating chart type='%s', id='%s', name='%s', family='%s', context='%s', chart='%s', priority=%d, update_every=%d",
  322. type, id, name ? name : "", family ? family : "", context ? context : "", rrdset_type_name(chart_type),
  323. priority, update_every);
  324. if (have_action) {
  325. return plugins_action->chart_action(
  326. user, type, id, name, family, context, title, units,
  327. (plugin && *plugin) ? plugin : ((PARSER_USER_OBJECT *)user)->cd->filename, module, priority, update_every,
  328. chart_type, options);
  329. }
  330. return PARSER_RC_OK;
  331. }
  332. PARSER_RC pluginsd_dimension(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  333. {
  334. char *id = words[1];
  335. char *name = words[2];
  336. char *algorithm = words[3];
  337. char *multiplier_s = words[4];
  338. char *divisor_s = words[5];
  339. char *options = words[6];
  340. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  341. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  342. if (unlikely(!host && !((PARSER_USER_OBJECT *) user)->host_exists)) {
  343. debug(D_PLUGINSD, "Ignoring dimension belonging to missing or ignored host.");
  344. return PARSER_RC_OK;
  345. }
  346. if (unlikely(!id)) {
  347. error(
  348. "requested a DIMENSION, without an id, host '%s' and chart '%s'. Disabling it.", host->hostname,
  349. st ? st->id : "UNSET");
  350. goto disable;
  351. }
  352. if (unlikely(!st && !((PARSER_USER_OBJECT *) user)->st_exists)) {
  353. error("requested a DIMENSION, without a CHART, on host '%s'. Disabling it.", host->hostname);
  354. goto disable;
  355. }
  356. long multiplier = 1;
  357. if (multiplier_s && *multiplier_s) {
  358. multiplier = strtol(multiplier_s, NULL, 0);
  359. if (unlikely(!multiplier))
  360. multiplier = 1;
  361. }
  362. long divisor = 1;
  363. if (likely(divisor_s && *divisor_s)) {
  364. divisor = strtol(divisor_s, NULL, 0);
  365. if (unlikely(!divisor))
  366. divisor = 1;
  367. }
  368. if (unlikely(!algorithm || !*algorithm))
  369. algorithm = "absolute";
  370. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  371. debug(
  372. D_PLUGINSD,
  373. "creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'",
  374. st->id, id, name ? name : "", rrd_algorithm_name(rrd_algorithm_id(algorithm)), multiplier, divisor,
  375. options ? options : "");
  376. if (plugins_action->dimension_action) {
  377. return plugins_action->dimension_action(
  378. user, st, id, name, algorithm,
  379. multiplier, divisor, (options && *options)?options:NULL, rrd_algorithm_id(algorithm));
  380. }
  381. return PARSER_RC_OK;
  382. disable:
  383. ((PARSER_USER_OBJECT *)user)->enabled = 0;
  384. return PARSER_RC_ERROR;
  385. }
  386. PARSER_RC pluginsd_variable(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  387. {
  388. char *name = words[1];
  389. char *value = words[2];
  390. NETDATA_DOUBLE v;
  391. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  392. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  393. int global = (st) ? 0 : 1;
  394. if (name && *name) {
  395. if ((strcmp(name, "GLOBAL") == 0 || strcmp(name, "HOST") == 0)) {
  396. global = 1;
  397. name = words[2];
  398. value = words[3];
  399. } else if ((strcmp(name, "LOCAL") == 0 || strcmp(name, "CHART") == 0)) {
  400. global = 0;
  401. name = words[2];
  402. value = words[3];
  403. }
  404. }
  405. if (unlikely(!name || !*name)) {
  406. error("requested a VARIABLE on host '%s', without a variable name. Disabling it.", host->hostname);
  407. ((PARSER_USER_OBJECT *)user)->enabled = 0;
  408. return PARSER_RC_ERROR;
  409. }
  410. if (unlikely(!value || !*value))
  411. value = NULL;
  412. if (unlikely(!value)) {
  413. error("cannot set %s VARIABLE '%s' on host '%s' to an empty value", (global) ? "HOST" : "CHART", name,
  414. host->hostname);
  415. return PARSER_RC_OK;
  416. }
  417. if (!global && !st) {
  418. error("cannot find/create CHART VARIABLE '%s' on host '%s' without a chart", name, host->hostname);
  419. return PARSER_RC_OK;
  420. }
  421. char *endptr = NULL;
  422. v = (NETDATA_DOUBLE)str2ndd(value, &endptr);
  423. if (unlikely(endptr && *endptr)) {
  424. if (endptr == value)
  425. error(
  426. "the value '%s' of VARIABLE '%s' on host '%s' cannot be parsed as a number", value, name,
  427. host->hostname);
  428. else
  429. error(
  430. "the value '%s' of VARIABLE '%s' on host '%s' has leftovers: '%s'", value, name, host->hostname,
  431. endptr);
  432. }
  433. if (plugins_action->variable_action) {
  434. return plugins_action->variable_action(user, host, st, name, global, v);
  435. }
  436. return PARSER_RC_OK;
  437. }
  438. PARSER_RC pluginsd_flush(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  439. {
  440. UNUSED(words);
  441. debug(D_PLUGINSD, "requested a FLUSH");
  442. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  443. ((PARSER_USER_OBJECT *) user)->st = NULL;
  444. if (plugins_action->flush_action) {
  445. return plugins_action->flush_action(user, st);
  446. }
  447. return PARSER_RC_OK;
  448. }
  449. PARSER_RC pluginsd_disable(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  450. {
  451. UNUSED(user);
  452. UNUSED(words);
  453. if (plugins_action->disable_action) {
  454. return plugins_action->disable_action(user);
  455. }
  456. return PARSER_RC_ERROR;
  457. }
  458. PARSER_RC pluginsd_label(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  459. {
  460. char *store;
  461. if (!words[1] || !words[2] || !words[3]) {
  462. error("Ignoring malformed or empty LABEL command.");
  463. return PARSER_RC_OK;
  464. }
  465. if (!words[4])
  466. store = words[3];
  467. else {
  468. store = callocz(PLUGINSD_LINE_MAX + 1, sizeof(char));
  469. size_t remaining = PLUGINSD_LINE_MAX;
  470. char *move = store;
  471. int i = 3;
  472. while (i < PLUGINSD_MAX_WORDS) {
  473. size_t length = strlen(words[i]);
  474. if ((length + 1) >= remaining)
  475. break;
  476. remaining -= (length + 1);
  477. memcpy(move, words[i], length);
  478. move += length;
  479. *move++ = ' ';
  480. i++;
  481. if (!words[i])
  482. break;
  483. }
  484. }
  485. if (plugins_action->label_action) {
  486. PARSER_RC rc = plugins_action->label_action(user, words[1], store, strtol(words[2], NULL, 10));
  487. if (store != words[3])
  488. freez(store);
  489. return rc;
  490. }
  491. if (store != words[3])
  492. freez(store);
  493. return PARSER_RC_OK;
  494. }
  495. PARSER_RC pluginsd_clabel(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  496. {
  497. if (!words[1] || !words[2] || !words[3]) {
  498. error("Ignoring malformed or empty CHART LABEL command.");
  499. return PARSER_RC_OK;
  500. }
  501. if (plugins_action->clabel_action) {
  502. PARSER_RC rc = plugins_action->clabel_action(user, words[1], words[2], strtol(words[3], NULL, 10));
  503. return rc;
  504. }
  505. return PARSER_RC_OK;
  506. }
  507. PARSER_RC pluginsd_clabel_commit(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  508. {
  509. UNUSED(words);
  510. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  511. debug(D_PLUGINSD, "requested to commit chart labels");
  512. PARSER_RC rc = PARSER_RC_OK;
  513. if (plugins_action->clabel_commit_action)
  514. rc = plugins_action->clabel_commit_action(user, host, ((PARSER_USER_OBJECT *)user)->new_chart_labels);
  515. rrdlabels_destroy(((PARSER_USER_OBJECT *)user)->new_chart_labels);
  516. ((PARSER_USER_OBJECT *)user)->new_chart_labels = NULL;
  517. return rc;
  518. }
  519. PARSER_RC pluginsd_overwrite(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  520. {
  521. UNUSED(words);
  522. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  523. debug(D_PLUGINSD, "requested to OVERWRITE host labels");
  524. PARSER_RC rc = PARSER_RC_OK;
  525. if (plugins_action->overwrite_action)
  526. rc = plugins_action->overwrite_action(user, host, ((PARSER_USER_OBJECT *)user)->new_host_labels);
  527. rrdlabels_destroy(((PARSER_USER_OBJECT *)user)->new_host_labels);
  528. ((PARSER_USER_OBJECT *)user)->new_host_labels = NULL;
  529. return rc;
  530. }
  531. PARSER_RC pluginsd_guid(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  532. {
  533. char *uuid_str = words[1];
  534. uuid_t uuid;
  535. if (unlikely(!uuid_str)) {
  536. error("requested a GUID, without a uuid.");
  537. return PARSER_RC_ERROR;
  538. }
  539. if (unlikely(strlen(uuid_str) != GUID_LEN || uuid_parse(uuid_str, uuid) == -1)) {
  540. error("requested a GUID, without a valid uuid string.");
  541. return PARSER_RC_ERROR;
  542. }
  543. debug(D_PLUGINSD, "Parsed uuid=%s", uuid_str);
  544. if (plugins_action->guid_action) {
  545. return plugins_action->guid_action(user, &uuid);
  546. }
  547. return PARSER_RC_OK;
  548. }
  549. PARSER_RC pluginsd_context(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  550. {
  551. char *uuid_str = words[1];
  552. uuid_t uuid;
  553. if (unlikely(!uuid_str)) {
  554. error("requested a CONTEXT, without a uuid.");
  555. return PARSER_RC_ERROR;
  556. }
  557. if (unlikely(strlen(uuid_str) != GUID_LEN || uuid_parse(uuid_str, uuid) == -1)) {
  558. error("requested a CONTEXT, without a valid uuid string.");
  559. return PARSER_RC_ERROR;
  560. }
  561. debug(D_PLUGINSD, "Parsed uuid=%s", uuid_str);
  562. if (plugins_action->context_action) {
  563. return plugins_action->context_action(user, &uuid);
  564. }
  565. return PARSER_RC_OK;
  566. }
  567. PARSER_RC pluginsd_tombstone(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  568. {
  569. char *uuid_str = words[1];
  570. uuid_t uuid;
  571. if (unlikely(!uuid_str)) {
  572. error("requested a TOMBSTONE, without a uuid.");
  573. return PARSER_RC_ERROR;
  574. }
  575. if (unlikely(strlen(uuid_str) != GUID_LEN || uuid_parse(uuid_str, uuid) == -1)) {
  576. error("requested a TOMBSTONE, without a valid uuid string.");
  577. return PARSER_RC_ERROR;
  578. }
  579. debug(D_PLUGINSD, "Parsed uuid=%s", uuid_str);
  580. if (plugins_action->tombstone_action) {
  581. return plugins_action->tombstone_action(user, &uuid);
  582. }
  583. return PARSER_RC_OK;
  584. }
  585. PARSER_RC metalog_pluginsd_host(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  586. {
  587. char *machine_guid = words[1];
  588. char *hostname = words[2];
  589. char *registry_hostname = words[3];
  590. char *update_every_s = words[4];
  591. char *os = words[5];
  592. char *timezone = words[6];
  593. char *tags = words[7];
  594. int update_every = 1;
  595. if (likely(update_every_s && *update_every_s))
  596. update_every = str2i(update_every_s);
  597. if (unlikely(!update_every))
  598. update_every = 1;
  599. debug(D_PLUGINSD, "HOST PARSED: guid=%s, hostname=%s, reg_host=%s, update=%d, os=%s, timezone=%s, tags=%s",
  600. machine_guid, hostname, registry_hostname, update_every, os, timezone, tags);
  601. if (plugins_action->host_action) {
  602. return plugins_action->host_action(
  603. user, machine_guid, hostname, registry_hostname, update_every, os, timezone, tags);
  604. }
  605. return PARSER_RC_OK;
  606. }
  607. static void pluginsd_process_thread_cleanup(void *ptr) {
  608. PARSER *parser = (PARSER *)ptr;
  609. parser_destroy(parser);
  610. }
  611. // New plugins.d parser
  612. inline size_t pluginsd_process(RRDHOST *host, struct plugind *cd, FILE *fp, int trust_durations)
  613. {
  614. int enabled = cd->enabled;
  615. if (!fp || !enabled) {
  616. cd->enabled = 0;
  617. return 0;
  618. }
  619. if (unlikely(fileno(fp) == -1)) {
  620. error("file descriptor given is not a valid stream");
  621. cd->serial_failures++;
  622. return 0;
  623. }
  624. clearerr(fp);
  625. PARSER_USER_OBJECT user = {
  626. .enabled = cd->enabled,
  627. .host = host,
  628. .cd = cd,
  629. .trust_durations = trust_durations
  630. };
  631. PARSER *parser = parser_init(host, &user, fp, PARSER_INPUT_SPLIT);
  632. // this keeps the parser with its current value
  633. // so, parser needs to be allocated before pushing it
  634. netdata_thread_cleanup_push(pluginsd_process_thread_cleanup, parser);
  635. parser->plugins_action->begin_action = &pluginsd_begin_action;
  636. parser->plugins_action->flush_action = &pluginsd_flush_action;
  637. parser->plugins_action->end_action = &pluginsd_end_action;
  638. parser->plugins_action->disable_action = &pluginsd_disable_action;
  639. parser->plugins_action->variable_action = &pluginsd_variable_action;
  640. parser->plugins_action->dimension_action = &pluginsd_dimension_action;
  641. parser->plugins_action->label_action = &pluginsd_label_action;
  642. parser->plugins_action->overwrite_action = &pluginsd_overwrite_action;
  643. parser->plugins_action->chart_action = &pluginsd_chart_action;
  644. parser->plugins_action->set_action = &pluginsd_set_action;
  645. parser->plugins_action->clabel_commit_action = &pluginsd_clabel_commit_action;
  646. parser->plugins_action->clabel_action = &pluginsd_clabel_action;
  647. user.parser = parser;
  648. while (likely(!parser_next(parser))) {
  649. if (unlikely(netdata_exit || parser_action(parser, NULL)))
  650. break;
  651. }
  652. // free parser with the pop function
  653. netdata_thread_cleanup_pop(1);
  654. cd->enabled = user.enabled;
  655. size_t count = user.count;
  656. if (likely(count)) {
  657. cd->successful_collections += count;
  658. cd->serial_failures = 0;
  659. }
  660. else
  661. cd->serial_failures++;
  662. return count;
  663. }