pluginsd_parser.c 24 KB

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