pluginsd_parser.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. sql_store_host_labels(host);
  163. return PARSER_RC_OK;
  164. }
  165. PARSER_RC pluginsd_set(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  166. {
  167. char *dimension = words[1];
  168. char *value = words[2];
  169. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  170. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  171. if (unlikely(!dimension || !*dimension)) {
  172. error("requested a SET on chart '%s' of host '%s', without a dimension. Disabling it.", st->id, host->hostname);
  173. goto disable;
  174. }
  175. if (unlikely(!value || !*value))
  176. value = NULL;
  177. if (unlikely(!st)) {
  178. error(
  179. "requested a SET on dimension %s with value %s on host '%s', without a BEGIN. Disabling it.", dimension,
  180. value ? value : "<nothing>", host->hostname);
  181. goto disable;
  182. }
  183. if (unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  184. debug(D_PLUGINSD, "is setting dimension %s/%s to %s", st->id, dimension, value ? value : "<nothing>");
  185. if (value) {
  186. RRDDIM *rd = rrddim_find(st, dimension);
  187. if (unlikely(!rd)) {
  188. error(
  189. "requested a SET to dimension with id '%s' on stats '%s' (%s) on host '%s', which does not exist. Disabling it.",
  190. dimension, st->name, st->id, st->rrdhost->hostname);
  191. goto disable;
  192. } else {
  193. if (plugins_action->set_action) {
  194. return plugins_action->set_action(
  195. user, st, rd, strtoll(value, NULL, 0));
  196. }
  197. }
  198. }
  199. return PARSER_RC_OK;
  200. disable:
  201. ((PARSER_USER_OBJECT *) user)->enabled = 0;
  202. return PARSER_RC_ERROR;
  203. }
  204. PARSER_RC pluginsd_begin(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  205. {
  206. char *id = words[1];
  207. char *microseconds_txt = words[2];
  208. RRDSET *st = NULL;
  209. RRDHOST *host = ((PARSER_USER_OBJECT *)user)->host;
  210. if (unlikely(!id)) {
  211. error("requested a BEGIN without a chart id for host '%s'. Disabling it.", host->hostname);
  212. goto disable;
  213. }
  214. st = rrdset_find(host, id);
  215. if (unlikely(!st)) {
  216. error("requested a BEGIN on chart '%s', which does not exist on host '%s'. Disabling it.", id, host->hostname);
  217. goto disable;
  218. }
  219. ((PARSER_USER_OBJECT *)user)->st = st;
  220. usec_t microseconds = 0;
  221. if (microseconds_txt && *microseconds_txt)
  222. microseconds = str2ull(microseconds_txt);
  223. if (plugins_action->begin_action) {
  224. return plugins_action->begin_action(user, st, microseconds,
  225. ((PARSER_USER_OBJECT *)user)->trust_durations);
  226. }
  227. return PARSER_RC_OK;
  228. disable:
  229. ((PARSER_USER_OBJECT *)user)->enabled = 0;
  230. return PARSER_RC_ERROR;
  231. }
  232. PARSER_RC pluginsd_end(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  233. {
  234. UNUSED(words);
  235. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  236. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  237. if (unlikely(!st)) {
  238. error("requested an END, without a BEGIN on host '%s'. Disabling it.", host->hostname);
  239. ((PARSER_USER_OBJECT *) user)->enabled = 0;
  240. return PARSER_RC_ERROR;
  241. }
  242. if (unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  243. debug(D_PLUGINSD, "requested an END on chart %s", st->id);
  244. ((PARSER_USER_OBJECT *) user)->st = NULL;
  245. ((PARSER_USER_OBJECT *) user)->count++;
  246. if (plugins_action->end_action) {
  247. return plugins_action->end_action(user, st);
  248. }
  249. return PARSER_RC_OK;
  250. }
  251. PARSER_RC pluginsd_chart(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  252. {
  253. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  254. if (unlikely(!host && !((PARSER_USER_OBJECT *) user)->host_exists)) {
  255. debug(D_PLUGINSD, "Ignoring chart belonging to missing or ignored host.");
  256. return PARSER_RC_OK;
  257. }
  258. char *type = words[1];
  259. char *name = words[2];
  260. char *title = words[3];
  261. char *units = words[4];
  262. char *family = words[5];
  263. char *context = words[6];
  264. char *chart = words[7];
  265. char *priority_s = words[8];
  266. char *update_every_s = words[9];
  267. char *options = words[10];
  268. char *plugin = words[11];
  269. char *module = words[12];
  270. int have_action = ((plugins_action->chart_action) != NULL);
  271. // parse the id from type
  272. char *id = NULL;
  273. if (likely(type && (id = strchr(type, '.')))) {
  274. *id = '\0';
  275. id++;
  276. }
  277. // make sure we have the required variables
  278. if (unlikely((!type || !*type || !id || !*id))) {
  279. if (likely(host))
  280. error("requested a CHART, without a type.id, on host '%s'. Disabling it.", host->hostname);
  281. else
  282. error("requested a CHART, without a type.id. Disabling it.");
  283. ((PARSER_USER_OBJECT *) user)->enabled = 0;
  284. return PARSER_RC_ERROR;
  285. }
  286. // parse the name, and make sure it does not include 'type.'
  287. if (unlikely(name && *name)) {
  288. // when data are streamed from child nodes
  289. // name will be type.name
  290. // so we have to remove 'type.' from name too
  291. size_t len = strlen(type);
  292. if (strncmp(type, name, len) == 0 && name[len] == '.')
  293. name = &name[len + 1];
  294. // if the name is the same with the id,
  295. // or is just 'NULL', clear it.
  296. if (unlikely(strcmp(name, id) == 0 || strcasecmp(name, "NULL") == 0 || strcasecmp(name, "(NULL)") == 0))
  297. name = NULL;
  298. }
  299. int priority = 1000;
  300. if (likely(priority_s && *priority_s))
  301. priority = str2i(priority_s);
  302. int update_every = ((PARSER_USER_OBJECT *) user)->cd->update_every;
  303. if (likely(update_every_s && *update_every_s))
  304. update_every = str2i(update_every_s);
  305. if (unlikely(!update_every))
  306. update_every = ((PARSER_USER_OBJECT *) user)->cd->update_every;
  307. RRDSET_TYPE chart_type = RRDSET_TYPE_LINE;
  308. if (unlikely(chart))
  309. chart_type = rrdset_type_id(chart);
  310. if (unlikely(name && !*name))
  311. name = NULL;
  312. if (unlikely(family && !*family))
  313. family = NULL;
  314. if (unlikely(context && !*context))
  315. context = NULL;
  316. if (unlikely(!title))
  317. title = "";
  318. if (unlikely(!units))
  319. units = "unknown";
  320. debug(
  321. D_PLUGINSD,
  322. "creating chart type='%s', id='%s', name='%s', family='%s', context='%s', chart='%s', priority=%d, update_every=%d",
  323. type, id, name ? name : "", family ? family : "", context ? context : "", rrdset_type_name(chart_type),
  324. priority, update_every);
  325. if (have_action) {
  326. return plugins_action->chart_action(
  327. user, type, id, name, family, context, title, units,
  328. (plugin && *plugin) ? plugin : ((PARSER_USER_OBJECT *)user)->cd->filename, module, priority, update_every,
  329. chart_type, options);
  330. }
  331. return PARSER_RC_OK;
  332. }
  333. PARSER_RC pluginsd_dimension(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  334. {
  335. char *id = words[1];
  336. char *name = words[2];
  337. char *algorithm = words[3];
  338. char *multiplier_s = words[4];
  339. char *divisor_s = words[5];
  340. char *options = words[6];
  341. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  342. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  343. if (unlikely(!host && !((PARSER_USER_OBJECT *) user)->host_exists)) {
  344. debug(D_PLUGINSD, "Ignoring dimension belonging to missing or ignored host.");
  345. return PARSER_RC_OK;
  346. }
  347. if (unlikely(!id)) {
  348. error(
  349. "requested a DIMENSION, without an id, host '%s' and chart '%s'. Disabling it.", host->hostname,
  350. st ? st->id : "UNSET");
  351. goto disable;
  352. }
  353. if (unlikely(!st && !((PARSER_USER_OBJECT *) user)->st_exists)) {
  354. error("requested a DIMENSION, without a CHART, on host '%s'. Disabling it.", host->hostname);
  355. goto disable;
  356. }
  357. long multiplier = 1;
  358. if (multiplier_s && *multiplier_s) {
  359. multiplier = strtol(multiplier_s, NULL, 0);
  360. if (unlikely(!multiplier))
  361. multiplier = 1;
  362. }
  363. long divisor = 1;
  364. if (likely(divisor_s && *divisor_s)) {
  365. divisor = strtol(divisor_s, NULL, 0);
  366. if (unlikely(!divisor))
  367. divisor = 1;
  368. }
  369. if (unlikely(!algorithm || !*algorithm))
  370. algorithm = "absolute";
  371. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  372. debug(
  373. D_PLUGINSD,
  374. "creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'",
  375. st->id, id, name ? name : "", rrd_algorithm_name(rrd_algorithm_id(algorithm)), multiplier, divisor,
  376. options ? options : "");
  377. if (plugins_action->dimension_action) {
  378. return plugins_action->dimension_action(
  379. user, st, id, name, algorithm,
  380. multiplier, divisor, (options && *options)?options:NULL, rrd_algorithm_id(algorithm));
  381. }
  382. return PARSER_RC_OK;
  383. disable:
  384. ((PARSER_USER_OBJECT *)user)->enabled = 0;
  385. return PARSER_RC_ERROR;
  386. }
  387. PARSER_RC pluginsd_variable(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  388. {
  389. char *name = words[1];
  390. char *value = words[2];
  391. NETDATA_DOUBLE v;
  392. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  393. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  394. int global = (st) ? 0 : 1;
  395. if (name && *name) {
  396. if ((strcmp(name, "GLOBAL") == 0 || strcmp(name, "HOST") == 0)) {
  397. global = 1;
  398. name = words[2];
  399. value = words[3];
  400. } else if ((strcmp(name, "LOCAL") == 0 || strcmp(name, "CHART") == 0)) {
  401. global = 0;
  402. name = words[2];
  403. value = words[3];
  404. }
  405. }
  406. if (unlikely(!name || !*name)) {
  407. error("requested a VARIABLE on host '%s', without a variable name. Disabling it.", host->hostname);
  408. ((PARSER_USER_OBJECT *)user)->enabled = 0;
  409. return PARSER_RC_ERROR;
  410. }
  411. if (unlikely(!value || !*value))
  412. value = NULL;
  413. if (unlikely(!value)) {
  414. error("cannot set %s VARIABLE '%s' on host '%s' to an empty value", (global) ? "HOST" : "CHART", name,
  415. host->hostname);
  416. return PARSER_RC_OK;
  417. }
  418. if (!global && !st) {
  419. error("cannot find/create CHART VARIABLE '%s' on host '%s' without a chart", name, host->hostname);
  420. return PARSER_RC_OK;
  421. }
  422. char *endptr = NULL;
  423. v = (NETDATA_DOUBLE)str2ndd(value, &endptr);
  424. if (unlikely(endptr && *endptr)) {
  425. if (endptr == value)
  426. error(
  427. "the value '%s' of VARIABLE '%s' on host '%s' cannot be parsed as a number", value, name,
  428. host->hostname);
  429. else
  430. error(
  431. "the value '%s' of VARIABLE '%s' on host '%s' has leftovers: '%s'", value, name, host->hostname,
  432. endptr);
  433. }
  434. if (plugins_action->variable_action) {
  435. return plugins_action->variable_action(user, host, st, name, global, v);
  436. }
  437. return PARSER_RC_OK;
  438. }
  439. PARSER_RC pluginsd_flush(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  440. {
  441. UNUSED(words);
  442. debug(D_PLUGINSD, "requested a FLUSH");
  443. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  444. ((PARSER_USER_OBJECT *) user)->st = NULL;
  445. if (plugins_action->flush_action) {
  446. return plugins_action->flush_action(user, st);
  447. }
  448. return PARSER_RC_OK;
  449. }
  450. PARSER_RC pluginsd_disable(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  451. {
  452. UNUSED(user);
  453. UNUSED(words);
  454. if (plugins_action->disable_action) {
  455. return plugins_action->disable_action(user);
  456. }
  457. return PARSER_RC_ERROR;
  458. }
  459. PARSER_RC pluginsd_label(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  460. {
  461. char *store;
  462. if (!words[1] || !words[2] || !words[3]) {
  463. error("Ignoring malformed or empty LABEL command.");
  464. return PARSER_RC_OK;
  465. }
  466. if (!words[4])
  467. store = words[3];
  468. else {
  469. store = callocz(PLUGINSD_LINE_MAX + 1, sizeof(char));
  470. size_t remaining = PLUGINSD_LINE_MAX;
  471. char *move = store;
  472. int i = 3;
  473. while (i < PLUGINSD_MAX_WORDS) {
  474. size_t length = strlen(words[i]);
  475. if ((length + 1) >= remaining)
  476. break;
  477. remaining -= (length + 1);
  478. memcpy(move, words[i], length);
  479. move += length;
  480. *move++ = ' ';
  481. i++;
  482. if (!words[i])
  483. break;
  484. }
  485. }
  486. if (plugins_action->label_action) {
  487. PARSER_RC rc = plugins_action->label_action(user, words[1], store, strtol(words[2], NULL, 10));
  488. if (store != words[3])
  489. freez(store);
  490. return rc;
  491. }
  492. if (store != words[3])
  493. freez(store);
  494. return PARSER_RC_OK;
  495. }
  496. PARSER_RC pluginsd_clabel(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  497. {
  498. if (!words[1] || !words[2] || !words[3]) {
  499. error("Ignoring malformed or empty CHART LABEL command.");
  500. return PARSER_RC_OK;
  501. }
  502. if (plugins_action->clabel_action) {
  503. PARSER_RC rc = plugins_action->clabel_action(user, words[1], words[2], strtol(words[3], NULL, 10));
  504. return rc;
  505. }
  506. return PARSER_RC_OK;
  507. }
  508. PARSER_RC pluginsd_clabel_commit(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  509. {
  510. UNUSED(words);
  511. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  512. debug(D_PLUGINSD, "requested to commit chart labels");
  513. PARSER_RC rc = PARSER_RC_OK;
  514. if (plugins_action->clabel_commit_action)
  515. rc = plugins_action->clabel_commit_action(user, host, ((PARSER_USER_OBJECT *)user)->new_chart_labels);
  516. rrdlabels_destroy(((PARSER_USER_OBJECT *)user)->new_chart_labels);
  517. ((PARSER_USER_OBJECT *)user)->new_chart_labels = NULL;
  518. return rc;
  519. }
  520. PARSER_RC pluginsd_overwrite(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  521. {
  522. UNUSED(words);
  523. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  524. debug(D_PLUGINSD, "requested to OVERWRITE host labels");
  525. PARSER_RC rc = PARSER_RC_OK;
  526. if (plugins_action->overwrite_action)
  527. rc = plugins_action->overwrite_action(user, host, ((PARSER_USER_OBJECT *)user)->new_host_labels);
  528. rrdlabels_destroy(((PARSER_USER_OBJECT *)user)->new_host_labels);
  529. ((PARSER_USER_OBJECT *)user)->new_host_labels = NULL;
  530. return rc;
  531. }
  532. PARSER_RC pluginsd_guid(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  533. {
  534. char *uuid_str = words[1];
  535. uuid_t uuid;
  536. if (unlikely(!uuid_str)) {
  537. error("requested a GUID, without a uuid.");
  538. return PARSER_RC_ERROR;
  539. }
  540. if (unlikely(strlen(uuid_str) != GUID_LEN || uuid_parse(uuid_str, uuid) == -1)) {
  541. error("requested a GUID, without a valid uuid string.");
  542. return PARSER_RC_ERROR;
  543. }
  544. debug(D_PLUGINSD, "Parsed uuid=%s", uuid_str);
  545. if (plugins_action->guid_action) {
  546. return plugins_action->guid_action(user, &uuid);
  547. }
  548. return PARSER_RC_OK;
  549. }
  550. PARSER_RC pluginsd_context(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  551. {
  552. char *uuid_str = words[1];
  553. uuid_t uuid;
  554. if (unlikely(!uuid_str)) {
  555. error("requested a CONTEXT, without a uuid.");
  556. return PARSER_RC_ERROR;
  557. }
  558. if (unlikely(strlen(uuid_str) != GUID_LEN || uuid_parse(uuid_str, uuid) == -1)) {
  559. error("requested a CONTEXT, without a valid uuid string.");
  560. return PARSER_RC_ERROR;
  561. }
  562. debug(D_PLUGINSD, "Parsed uuid=%s", uuid_str);
  563. if (plugins_action->context_action) {
  564. return plugins_action->context_action(user, &uuid);
  565. }
  566. return PARSER_RC_OK;
  567. }
  568. PARSER_RC pluginsd_tombstone(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  569. {
  570. char *uuid_str = words[1];
  571. uuid_t uuid;
  572. if (unlikely(!uuid_str)) {
  573. error("requested a TOMBSTONE, without a uuid.");
  574. return PARSER_RC_ERROR;
  575. }
  576. if (unlikely(strlen(uuid_str) != GUID_LEN || uuid_parse(uuid_str, uuid) == -1)) {
  577. error("requested a TOMBSTONE, without a valid uuid string.");
  578. return PARSER_RC_ERROR;
  579. }
  580. debug(D_PLUGINSD, "Parsed uuid=%s", uuid_str);
  581. if (plugins_action->tombstone_action) {
  582. return plugins_action->tombstone_action(user, &uuid);
  583. }
  584. return PARSER_RC_OK;
  585. }
  586. PARSER_RC metalog_pluginsd_host(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  587. {
  588. char *machine_guid = words[1];
  589. char *hostname = words[2];
  590. char *registry_hostname = words[3];
  591. char *update_every_s = words[4];
  592. char *os = words[5];
  593. char *timezone = words[6];
  594. char *tags = words[7];
  595. int update_every = 1;
  596. if (likely(update_every_s && *update_every_s))
  597. update_every = str2i(update_every_s);
  598. if (unlikely(!update_every))
  599. update_every = 1;
  600. debug(D_PLUGINSD, "HOST PARSED: guid=%s, hostname=%s, reg_host=%s, update=%d, os=%s, timezone=%s, tags=%s",
  601. machine_guid, hostname, registry_hostname, update_every, os, timezone, tags);
  602. if (plugins_action->host_action) {
  603. return plugins_action->host_action(
  604. user, machine_guid, hostname, registry_hostname, update_every, os, timezone, tags);
  605. }
  606. return PARSER_RC_OK;
  607. }
  608. static void pluginsd_process_thread_cleanup(void *ptr) {
  609. PARSER *parser = (PARSER *)ptr;
  610. parser_destroy(parser);
  611. }
  612. // New plugins.d parser
  613. inline size_t pluginsd_process(RRDHOST *host, struct plugind *cd, FILE *fp, int trust_durations)
  614. {
  615. int enabled = cd->enabled;
  616. if (!fp || !enabled) {
  617. cd->enabled = 0;
  618. return 0;
  619. }
  620. if (unlikely(fileno(fp) == -1)) {
  621. error("file descriptor given is not a valid stream");
  622. cd->serial_failures++;
  623. return 0;
  624. }
  625. clearerr(fp);
  626. PARSER_USER_OBJECT user = {
  627. .enabled = cd->enabled,
  628. .host = host,
  629. .cd = cd,
  630. .trust_durations = trust_durations
  631. };
  632. PARSER *parser = parser_init(host, &user, fp, PARSER_INPUT_SPLIT);
  633. // this keeps the parser with its current value
  634. // so, parser needs to be allocated before pushing it
  635. netdata_thread_cleanup_push(pluginsd_process_thread_cleanup, parser);
  636. parser->plugins_action->begin_action = &pluginsd_begin_action;
  637. parser->plugins_action->flush_action = &pluginsd_flush_action;
  638. parser->plugins_action->end_action = &pluginsd_end_action;
  639. parser->plugins_action->disable_action = &pluginsd_disable_action;
  640. parser->plugins_action->variable_action = &pluginsd_variable_action;
  641. parser->plugins_action->dimension_action = &pluginsd_dimension_action;
  642. parser->plugins_action->label_action = &pluginsd_label_action;
  643. parser->plugins_action->overwrite_action = &pluginsd_overwrite_action;
  644. parser->plugins_action->chart_action = &pluginsd_chart_action;
  645. parser->plugins_action->set_action = &pluginsd_set_action;
  646. parser->plugins_action->clabel_commit_action = &pluginsd_clabel_commit_action;
  647. parser->plugins_action->clabel_action = &pluginsd_clabel_action;
  648. user.parser = parser;
  649. while (likely(!parser_next(parser))) {
  650. if (unlikely(netdata_exit || parser_action(parser, NULL)))
  651. break;
  652. }
  653. // free parser with the pop function
  654. netdata_thread_cleanup_pop(1);
  655. cd->enabled = user.enabled;
  656. size_t count = user.count;
  657. if (likely(count)) {
  658. cd->successful_collections += count;
  659. cd->serial_failures = 0;
  660. }
  661. else
  662. cd->serial_failures++;
  663. return count;
  664. }