PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/target_replace.c

http://github.com/octo/collectd
C | 565 lines | 431 code | 98 blank | 36 comment | 157 complexity | dccb948d2a38769d9a67e8bbbd3c2e30 MD5 | raw file
Possible License(s): GPL-2.0
  1. /**
  2. * collectd - src/target_replace.c
  3. * Copyright (C) 2008 Florian Forster
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Florian Forster <octo at collectd.org>
  25. **/
  26. #include "collectd.h"
  27. #include "filter_chain.h"
  28. #include "utils/common/common.h"
  29. #include "utils_subst.h"
  30. #include <regex.h>
  31. struct tr_action_s;
  32. typedef struct tr_action_s tr_action_t;
  33. struct tr_action_s {
  34. regex_t re;
  35. char *replacement;
  36. bool may_be_empty;
  37. tr_action_t *next;
  38. };
  39. struct tr_meta_data_action_s;
  40. typedef struct tr_meta_data_action_s tr_meta_data_action_t;
  41. struct tr_meta_data_action_s {
  42. char *key;
  43. regex_t re;
  44. char *replacement;
  45. tr_meta_data_action_t *next;
  46. };
  47. struct tr_data_s {
  48. tr_action_t *host;
  49. tr_action_t *plugin;
  50. tr_action_t *plugin_instance;
  51. /* tr_action_t *type; */
  52. tr_action_t *type_instance;
  53. tr_meta_data_action_t *meta;
  54. };
  55. typedef struct tr_data_s tr_data_t;
  56. static char *tr_strdup(const char *orig) /* {{{ */
  57. {
  58. size_t sz;
  59. char *dest;
  60. if (orig == NULL)
  61. return NULL;
  62. sz = strlen(orig) + 1;
  63. dest = malloc(sz);
  64. if (dest == NULL)
  65. return NULL;
  66. memcpy(dest, orig, sz);
  67. return dest;
  68. } /* }}} char *tr_strdup */
  69. static void tr_action_destroy(tr_action_t *act) /* {{{ */
  70. {
  71. if (act == NULL)
  72. return;
  73. regfree(&act->re);
  74. sfree(act->replacement);
  75. if (act->next != NULL)
  76. tr_action_destroy(act->next);
  77. sfree(act);
  78. } /* }}} void tr_action_destroy */
  79. static void tr_meta_data_action_destroy(tr_meta_data_action_t *act) /* {{{ */
  80. {
  81. if (act == NULL)
  82. return;
  83. sfree(act->key);
  84. regfree(&act->re);
  85. sfree(act->replacement);
  86. if (act->next != NULL)
  87. tr_meta_data_action_destroy(act->next);
  88. sfree(act);
  89. } /* }}} void tr_meta_data_action_destroy */
  90. static int tr_config_add_action(tr_action_t **dest, /* {{{ */
  91. const oconfig_item_t *ci, bool may_be_empty) {
  92. tr_action_t *act;
  93. int status;
  94. if (dest == NULL)
  95. return -EINVAL;
  96. if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
  97. (ci->values[1].type != OCONFIG_TYPE_STRING)) {
  98. ERROR("Target `replace': The `%s' option requires exactly two string "
  99. "arguments.",
  100. ci->key);
  101. return -1;
  102. }
  103. act = calloc(1, sizeof(*act));
  104. if (act == NULL) {
  105. ERROR("tr_config_add_action: calloc failed.");
  106. return -ENOMEM;
  107. }
  108. act->replacement = NULL;
  109. act->may_be_empty = may_be_empty;
  110. status = regcomp(&act->re, ci->values[0].value.string, REG_EXTENDED);
  111. if (status != 0) {
  112. char errbuf[1024] = "";
  113. /* regerror assures null termination. */
  114. regerror(status, &act->re, errbuf, sizeof(errbuf));
  115. ERROR("Target `replace': Compiling the regular expression `%s' "
  116. "failed: %s.",
  117. ci->values[0].value.string, errbuf);
  118. sfree(act);
  119. return -EINVAL;
  120. }
  121. act->replacement = tr_strdup(ci->values[1].value.string);
  122. if (act->replacement == NULL) {
  123. ERROR("tr_config_add_action: tr_strdup failed.");
  124. tr_action_destroy(act);
  125. return -ENOMEM;
  126. }
  127. /* Insert action at end of list. */
  128. if (*dest == NULL)
  129. *dest = act;
  130. else {
  131. tr_action_t *prev;
  132. prev = *dest;
  133. while (prev->next != NULL)
  134. prev = prev->next;
  135. prev->next = act;
  136. }
  137. return 0;
  138. } /* }}} int tr_config_add_action */
  139. static int tr_config_add_meta_action(tr_meta_data_action_t **dest, /* {{{ */
  140. const oconfig_item_t *ci,
  141. bool should_delete) {
  142. tr_meta_data_action_t *act;
  143. int status;
  144. if (dest == NULL)
  145. return -EINVAL;
  146. if (should_delete) {
  147. if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
  148. (ci->values[1].type != OCONFIG_TYPE_STRING)) {
  149. ERROR("Target `replace': The `%s' option requires exactly two string "
  150. "arguments.",
  151. ci->key);
  152. return -1;
  153. }
  154. } else {
  155. if ((ci->values_num != 3) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
  156. (ci->values[1].type != OCONFIG_TYPE_STRING) ||
  157. (ci->values[2].type != OCONFIG_TYPE_STRING)) {
  158. ERROR("Target `replace': The `%s' option requires exactly three string "
  159. "arguments.",
  160. ci->key);
  161. return -1;
  162. }
  163. }
  164. if (strlen(ci->values[0].value.string) == 0) {
  165. ERROR("Target `replace': The `%s' option does not accept empty string as "
  166. "first argument.",
  167. ci->key);
  168. return -1;
  169. }
  170. act = calloc(1, sizeof(*act));
  171. if (act == NULL) {
  172. ERROR("tr_config_add_meta_action: calloc failed.");
  173. return -ENOMEM;
  174. }
  175. act->key = NULL;
  176. act->replacement = NULL;
  177. status = regcomp(&act->re, ci->values[1].value.string, REG_EXTENDED);
  178. if (status != 0) {
  179. char errbuf[1024] = "";
  180. /* regerror assures null termination. */
  181. regerror(status, &act->re, errbuf, sizeof(errbuf));
  182. ERROR("Target `replace': Compiling the regular expression `%s' "
  183. "failed: %s.",
  184. ci->values[1].value.string, errbuf);
  185. sfree(act->key);
  186. sfree(act);
  187. return -EINVAL;
  188. }
  189. act->key = tr_strdup(ci->values[0].value.string);
  190. if (act->key == NULL) {
  191. ERROR("tr_config_add_meta_action: tr_strdup failed.");
  192. tr_meta_data_action_destroy(act);
  193. return -ENOMEM;
  194. }
  195. if (!should_delete) {
  196. act->replacement = tr_strdup(ci->values[2].value.string);
  197. if (act->replacement == NULL) {
  198. ERROR("tr_config_add_meta_action: tr_strdup failed.");
  199. tr_meta_data_action_destroy(act);
  200. return -ENOMEM;
  201. }
  202. }
  203. /* Insert action at end of list. */
  204. if (*dest == NULL)
  205. *dest = act;
  206. else {
  207. tr_meta_data_action_t *prev;
  208. prev = *dest;
  209. while (prev->next != NULL)
  210. prev = prev->next;
  211. prev->next = act;
  212. }
  213. return 0;
  214. } /* }}} int tr_config_add_meta_action */
  215. static int tr_action_invoke(tr_action_t *act_head, /* {{{ */
  216. char *buffer_in, size_t buffer_in_size,
  217. bool may_be_empty) {
  218. int status;
  219. char buffer[DATA_MAX_NAME_LEN];
  220. regmatch_t matches[8] = {[0] = {0}};
  221. if (act_head == NULL)
  222. return -EINVAL;
  223. sstrncpy(buffer, buffer_in, sizeof(buffer));
  224. DEBUG("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
  225. for (tr_action_t *act = act_head; act != NULL; act = act->next) {
  226. char temp[DATA_MAX_NAME_LEN];
  227. char *subst_status;
  228. status = regexec(&act->re, buffer, STATIC_ARRAY_SIZE(matches), matches,
  229. /* flags = */ 0);
  230. if (status == REG_NOMATCH)
  231. continue;
  232. else if (status != 0) {
  233. char errbuf[1024] = "";
  234. regerror(status, &act->re, errbuf, sizeof(errbuf));
  235. ERROR("Target `replace': Executing a regular expression failed: %s.",
  236. errbuf);
  237. continue;
  238. }
  239. subst_status = subst(temp, sizeof(temp), buffer, (size_t)matches[0].rm_so,
  240. (size_t)matches[0].rm_eo, act->replacement);
  241. if (subst_status == NULL) {
  242. ERROR("Target `replace': subst (buffer = %s, start = %" PRIsz
  243. ", end = %" PRIsz ", "
  244. "replacement = %s) failed.",
  245. buffer, (size_t)matches[0].rm_so, (size_t)matches[0].rm_eo,
  246. act->replacement);
  247. continue;
  248. }
  249. sstrncpy(buffer, temp, sizeof(buffer));
  250. DEBUG("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
  251. } /* for (act = act_head; act != NULL; act = act->next) */
  252. if ((may_be_empty == false) && (buffer[0] == 0)) {
  253. WARNING("Target `replace': Replacement resulted in an empty string, "
  254. "which is not allowed for this buffer (`host' or `plugin').");
  255. return 0;
  256. }
  257. DEBUG("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
  258. sstrncpy(buffer_in, buffer, buffer_in_size);
  259. return 0;
  260. } /* }}} int tr_action_invoke */
  261. static int tr_meta_data_action_invoke(/* {{{ */
  262. tr_meta_data_action_t *act_head,
  263. meta_data_t **dest) {
  264. int status;
  265. regmatch_t matches[8] = {[0] = {0}};
  266. if (act_head == NULL)
  267. return -EINVAL;
  268. if ((*dest) == NULL) /* nothing to do */
  269. return 0;
  270. for (tr_meta_data_action_t *act = act_head; act != NULL; act = act->next) {
  271. char temp[DATA_MAX_NAME_LEN];
  272. char *subst_status;
  273. int value_type;
  274. int meta_data_status;
  275. char *value;
  276. meta_data_t *result;
  277. value_type = meta_data_type(*dest, act->key);
  278. if (value_type == 0) /* not found */
  279. continue;
  280. if (value_type != MD_TYPE_STRING) {
  281. WARNING("Target `replace': Attempting replace on metadata key `%s', "
  282. "which isn't a string.",
  283. act->key);
  284. continue;
  285. }
  286. meta_data_status = meta_data_get_string(*dest, act->key, &value);
  287. if (meta_data_status != 0) {
  288. ERROR("Target `replace': Unable to retrieve metadata value for `%s'.",
  289. act->key);
  290. return meta_data_status;
  291. }
  292. DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' "
  293. "old value = `%s'",
  294. act->key, value);
  295. status = regexec(&act->re, value, STATIC_ARRAY_SIZE(matches), matches,
  296. /* flags = */ 0);
  297. if (status == REG_NOMATCH) {
  298. sfree(value);
  299. continue;
  300. } else if (status != 0) {
  301. char errbuf[1024] = "";
  302. regerror(status, &act->re, errbuf, sizeof(errbuf));
  303. ERROR("Target `replace': Executing a regular expression failed: %s.",
  304. errbuf);
  305. sfree(value);
  306. continue;
  307. }
  308. if (act->replacement == NULL) {
  309. /* no replacement; delete the key */
  310. DEBUG("target_replace plugin: tr_meta_data_action_invoke: "
  311. "deleting `%s'",
  312. act->key);
  313. meta_data_delete(*dest, act->key);
  314. sfree(value);
  315. continue;
  316. }
  317. subst_status = subst(temp, sizeof(temp), value, (size_t)matches[0].rm_so,
  318. (size_t)matches[0].rm_eo, act->replacement);
  319. if (subst_status == NULL) {
  320. ERROR("Target `replace': subst (value = %s, start = %" PRIsz
  321. ", end = %" PRIsz ", "
  322. "replacement = %s) failed.",
  323. value, (size_t)matches[0].rm_so, (size_t)matches[0].rm_eo,
  324. act->replacement);
  325. sfree(value);
  326. continue;
  327. }
  328. DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' "
  329. "value `%s' -> `%s'",
  330. act->key, value, temp);
  331. if ((result = meta_data_create()) == NULL) {
  332. ERROR("Target `replace': failed to create metadata for `%s'.", act->key);
  333. sfree(value);
  334. return -ENOMEM;
  335. }
  336. meta_data_status = meta_data_add_string(result, act->key, temp);
  337. if (meta_data_status != 0) {
  338. ERROR("Target `replace': Unable to set metadata value for `%s'.",
  339. act->key);
  340. meta_data_destroy(result);
  341. sfree(value);
  342. return meta_data_status;
  343. }
  344. meta_data_clone_merge(dest, result);
  345. meta_data_destroy(result);
  346. sfree(value);
  347. } /* for (act = act_head; act != NULL; act = act->next) */
  348. return 0;
  349. } /* }}} int tr_meta_data_action_invoke */
  350. static int tr_destroy(void **user_data) /* {{{ */
  351. {
  352. tr_data_t *data;
  353. if (user_data == NULL)
  354. return -EINVAL;
  355. data = *user_data;
  356. if (data == NULL)
  357. return 0;
  358. tr_action_destroy(data->host);
  359. tr_action_destroy(data->plugin);
  360. tr_action_destroy(data->plugin_instance);
  361. /* tr_action_destroy (data->type); */
  362. tr_action_destroy(data->type_instance);
  363. tr_meta_data_action_destroy(data->meta);
  364. sfree(data);
  365. return 0;
  366. } /* }}} int tr_destroy */
  367. static int tr_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
  368. {
  369. tr_data_t *data;
  370. int status;
  371. data = calloc(1, sizeof(*data));
  372. if (data == NULL) {
  373. ERROR("tr_create: calloc failed.");
  374. return -ENOMEM;
  375. }
  376. data->host = NULL;
  377. data->plugin = NULL;
  378. data->plugin_instance = NULL;
  379. /* data->type = NULL; */
  380. data->type_instance = NULL;
  381. data->meta = NULL;
  382. status = 0;
  383. for (int i = 0; i < ci->children_num; i++) {
  384. oconfig_item_t *child = ci->children + i;
  385. if ((strcasecmp("Host", child->key) == 0) ||
  386. (strcasecmp("Hostname", child->key) == 0))
  387. status = tr_config_add_action(&data->host, child,
  388. /* may be empty = */ false);
  389. else if (strcasecmp("Plugin", child->key) == 0)
  390. status = tr_config_add_action(&data->plugin, child,
  391. /* may be empty = */ false);
  392. else if (strcasecmp("PluginInstance", child->key) == 0)
  393. status = tr_config_add_action(&data->plugin_instance, child,
  394. /* may be empty = */ true);
  395. #if 0
  396. else if (strcasecmp ("Type", child->key) == 0)
  397. status = tr_config_add_action (&data->type, child,
  398. /* may be empty = */ 0);
  399. #endif
  400. else if (strcasecmp("TypeInstance", child->key) == 0)
  401. status = tr_config_add_action(&data->type_instance, child,
  402. /* may be empty = */ true);
  403. else if (strcasecmp("MetaData", child->key) == 0)
  404. status = tr_config_add_meta_action(&data->meta, child,
  405. /* should delete = */ false);
  406. else if (strcasecmp("DeleteMetaData", child->key) == 0)
  407. status = tr_config_add_meta_action(&data->meta, child,
  408. /* should delete = */ true);
  409. else {
  410. ERROR("Target `replace': The `%s' configuration option is not understood "
  411. "and will be ignored.",
  412. child->key);
  413. status = 0;
  414. }
  415. if (status != 0)
  416. break;
  417. }
  418. /* Additional sanity-checking */
  419. while (status == 0) {
  420. if ((data->host == NULL) && (data->plugin == NULL) &&
  421. (data->plugin_instance == NULL)
  422. /* && (data->type == NULL) */
  423. && (data->type_instance == NULL) && (data->meta == NULL)) {
  424. ERROR("Target `replace': You need to set at least one of `Host', "
  425. "`Plugin', `PluginInstance' or `TypeInstance'.");
  426. status = -1;
  427. }
  428. break;
  429. }
  430. if (status != 0) {
  431. tr_destroy((void *)&data);
  432. return status;
  433. }
  434. *user_data = data;
  435. return 0;
  436. } /* }}} int tr_create */
  437. static int tr_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */
  438. notification_meta_t __attribute__((unused)) * *meta,
  439. void **user_data) {
  440. tr_data_t *data;
  441. if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
  442. return -EINVAL;
  443. data = *user_data;
  444. if (data == NULL) {
  445. ERROR("Target `replace': Invoke: `data' is NULL.");
  446. return -EINVAL;
  447. }
  448. if (data->meta != NULL) {
  449. tr_meta_data_action_invoke(data->meta, &(vl->meta));
  450. }
  451. #define HANDLE_FIELD(f, e) \
  452. if (data->f != NULL) \
  453. tr_action_invoke(data->f, vl->f, sizeof(vl->f), e)
  454. HANDLE_FIELD(host, false);
  455. HANDLE_FIELD(plugin, false);
  456. HANDLE_FIELD(plugin_instance, true);
  457. /* HANDLE_FIELD (type, false); */
  458. HANDLE_FIELD(type_instance, true);
  459. return FC_TARGET_CONTINUE;
  460. } /* }}} int tr_invoke */
  461. void module_register(void) {
  462. target_proc_t tproc = {0};
  463. tproc.create = tr_create;
  464. tproc.destroy = tr_destroy;
  465. tproc.invoke = tr_invoke;
  466. fc_register_target("replace", tproc);
  467. } /* module_register */