pluginsd_parser.c 23 KB

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