2 * collectd - src/filter_chain.c
3 * Copyright (C) 2008-2010 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
29 #include "configfile.h"
30 #include "filter_chain.h"
32 #include "utils/common/common.h"
33 #include "utils_complain.h"
38 /* List of matches, used in fc_rule_t and for the global `match_list_head'
41 typedef struct fc_match_s fc_match_t; /* {{{ */
43 char name[DATA_MAX_NAME_LEN];
49 /* List of targets, used in fc_rule_t and for the global `target_list_head'
52 typedef struct fc_target_s fc_target_t; /* {{{ */
54 char name[DATA_MAX_NAME_LEN];
60 /* List of rules, used in fc_chain_t */
62 typedef struct fc_rule_s fc_rule_t; /* {{{ */
64 char name[DATA_MAX_NAME_LEN];
70 /* List of chains, used for `chain_list_head' */
71 struct fc_chain_s /* {{{ */
73 char name[DATA_MAX_NAME_LEN];
79 /* Writer configuration. */
81 typedef struct fc_writer_s fc_writer_t; /* {{{ */
84 c_complain_t complaint;
90 static fc_match_t *match_list_head;
91 static fc_target_t *target_list_head;
92 static fc_chain_t *chain_list_head;
97 static void fc_free_matches(fc_match_t *m) /* {{{ */
102 if (m->proc.destroy != NULL)
103 (*m->proc.destroy)(&m->user_data);
104 else if (m->user_data != NULL) {
105 ERROR("Filter subsystem: fc_free_matches: There is user data, but no "
106 "destroy functions has been specified. "
107 "Memory will probably be lost!");
111 fc_free_matches(m->next);
114 } /* }}} void fc_free_matches */
116 static void fc_free_targets(fc_target_t *t) /* {{{ */
121 if (t->proc.destroy != NULL)
122 (*t->proc.destroy)(&t->user_data);
123 else if (t->user_data != NULL) {
124 ERROR("Filter subsystem: fc_free_targets: There is user data, but no "
125 "destroy functions has been specified. "
126 "Memory will probably be lost!");
130 fc_free_targets(t->next);
133 } /* }}} void fc_free_targets */
135 static void fc_free_rules(fc_rule_t *r) /* {{{ */
140 fc_free_matches(r->matches);
141 fc_free_targets(r->targets);
144 fc_free_rules(r->next);
147 } /* }}} void fc_free_rules */
149 static void fc_free_chains(fc_chain_t *c) /* {{{ */
154 fc_free_rules(c->rules);
155 fc_free_targets(c->targets);
158 fc_free_chains(c->next);
161 } /* }}} void fc_free_chains */
163 static char *fc_strdup(const char *orig) /* {{{ */
171 sz = strlen(orig) + 1;
176 memcpy(dest, orig, sz);
179 } /* }}} char *fc_strdup */
184 * The configuration looks somewhat like this:
190 * Type "^mysql_command$"
191 * TypeInstance "^show_"
202 static int fc_config_add_match(fc_match_t **matches_head, /* {{{ */
203 oconfig_item_t *ci) {
208 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
209 WARNING("Filter subsystem: `Match' blocks require "
210 "exactly one string argument.");
214 ptr = match_list_head;
215 while (ptr != NULL) {
216 if (strcasecmp(ptr->name, ci->values[0].value.string) == 0)
222 WARNING("Filter subsystem: Cannot find a \"%s\" match. "
223 "Did you load the appropriate plugin?",
224 ci->values[0].value.string);
228 m = calloc(1, sizeof(*m));
230 ERROR("fc_config_add_match: calloc failed.");
234 sstrncpy(m->name, ptr->name, sizeof(m->name));
235 memcpy(&m->proc, &ptr->proc, sizeof(m->proc));
239 if (m->proc.create != NULL) {
240 status = (*m->proc.create)(ci, &m->user_data);
242 WARNING("Filter subsystem: Failed to create a %s match.", m->name);
248 if (*matches_head != NULL) {
250 while (ptr->next != NULL)
259 } /* }}} int fc_config_add_match */
261 static int fc_config_add_target(fc_target_t **targets_head, /* {{{ */
262 oconfig_item_t *ci) {
267 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
268 WARNING("Filter subsystem: `Target' blocks require "
269 "exactly one string argument.");
273 ptr = target_list_head;
274 while (ptr != NULL) {
275 if (strcasecmp(ptr->name, ci->values[0].value.string) == 0)
281 WARNING("Filter subsystem: Cannot find a \"%s\" target. "
282 "Did you load the appropriate plugin?",
283 ci->values[0].value.string);
287 t = calloc(1, sizeof(*t));
289 ERROR("fc_config_add_target: calloc failed.");
293 sstrncpy(t->name, ptr->name, sizeof(t->name));
294 memcpy(&t->proc, &ptr->proc, sizeof(t->proc));
298 if (t->proc.create != NULL) {
299 status = (*t->proc.create)(ci, &t->user_data);
301 WARNING("Filter subsystem: Failed to create a %s target.", t->name);
309 if (*targets_head != NULL) {
311 while (ptr->next != NULL)
320 } /* }}} int fc_config_add_target */
322 static int fc_config_add_rule(fc_chain_t *chain, /* {{{ */
323 oconfig_item_t *ci) {
325 char rule_name[2 * DATA_MAX_NAME_LEN] = "Unnamed rule";
328 if (ci->values_num > 1) {
329 WARNING("Filter subsystem: `Rule' blocks have at most one argument.");
331 } else if ((ci->values_num == 1) &&
332 (ci->values[0].type != OCONFIG_TYPE_STRING)) {
333 WARNING("Filter subsystem: `Rule' blocks expect one string argument "
334 "or no argument at all.");
338 rule = calloc(1, sizeof(*rule));
340 ERROR("fc_config_add_rule: calloc failed.");
344 if (ci->values_num == 1) {
345 sstrncpy(rule->name, ci->values[0].value.string, sizeof(rule->name));
346 snprintf(rule_name, sizeof(rule_name), "Rule \"%s\"",
347 ci->values[0].value.string);
350 for (int i = 0; i < ci->children_num; i++) {
351 oconfig_item_t *option = ci->children + i;
353 if (strcasecmp("Match", option->key) == 0)
354 status = fc_config_add_match(&rule->matches, option);
355 else if (strcasecmp("Target", option->key) == 0)
356 status = fc_config_add_target(&rule->targets, option);
358 WARNING("Filter subsystem: %s: Option `%s' not allowed "
359 "inside a <Rule> block.",
360 rule_name, option->key);
366 } /* for (ci->children) */
368 /* Additional sanity checking. */
369 while (status == 0) {
370 if (rule->targets == NULL) {
371 WARNING("Filter subsystem: %s: No target has been specified.", rule_name);
377 } /* while (status == 0) */
384 if (chain->rules != NULL) {
388 while (ptr->next != NULL)
397 } /* }}} int fc_config_add_rule */
399 static int fc_config_add_chain(const oconfig_item_t *ci) /* {{{ */
401 fc_chain_t *chain = NULL;
405 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
406 WARNING("Filter subsystem: <Chain> blocks require exactly one "
411 if (chain_list_head != NULL) {
412 if ((chain = fc_chain_get_by_name(ci->values[0].value.string)) != NULL)
417 chain = calloc(1, sizeof(*chain));
419 ERROR("fc_config_add_chain: calloc failed.");
422 sstrncpy(chain->name, ci->values[0].value.string, sizeof(chain->name));
425 for (int i = 0; i < ci->children_num; i++) {
426 oconfig_item_t *option = ci->children + i;
428 if (strcasecmp("Rule", option->key) == 0)
429 status = fc_config_add_rule(chain, option);
430 else if (strcasecmp("Target", option->key) == 0)
431 status = fc_config_add_target(&chain->targets, option);
433 WARNING("Filter subsystem: Chain %s: Option `%s' not allowed "
434 "inside a <Chain> block.",
435 chain->name, option->key);
441 } /* for (ci->children) */
444 fc_free_chains(chain);
448 if (chain_list_head != NULL) {
454 ptr = chain_list_head;
455 while (ptr->next != NULL)
460 chain_list_head = chain;
464 } /* }}} int fc_config_add_chain */
467 * Built-in target "jump"
469 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
471 static int fc_bit_jump_create(const oconfig_item_t *ci, /* {{{ */
473 oconfig_item_t *ci_chain;
475 if (ci->children_num != 1) {
476 ERROR("Filter subsystem: The built-in target `jump' needs exactly "
477 "one `Chain' argument!");
481 ci_chain = ci->children;
482 if (strcasecmp("Chain", ci_chain->key) != 0) {
483 ERROR("Filter subsystem: The built-in target `jump' does not "
484 "support the configuration option `%s'.",
489 if ((ci_chain->values_num != 1) ||
490 (ci_chain->values[0].type != OCONFIG_TYPE_STRING)) {
491 ERROR("Filter subsystem: Built-in target `jump': The `Chain' option "
492 "needs exactly one string argument.");
496 *user_data = fc_strdup(ci_chain->values[0].value.string);
497 if (*user_data == NULL) {
498 ERROR("fc_bit_jump_create: fc_strdup failed.");
503 } /* }}} int fc_bit_jump_create */
505 static int fc_bit_jump_destroy(void **user_data) /* {{{ */
507 if (user_data != NULL) {
513 } /* }}} int fc_bit_jump_destroy */
515 static int fc_bit_jump_invoke(const data_set_t *ds, /* {{{ */
517 notification_meta_t __attribute__((unused)) *
524 chain_name = *user_data;
526 for (chain = chain_list_head; chain != NULL; chain = chain->next)
527 if (strcasecmp(chain_name, chain->name) == 0)
531 ERROR("Filter subsystem: Built-in target `jump': There is no chain "
537 status = fc_process_chain(ds, vl, chain);
540 else if (status == FC_TARGET_STOP)
541 return FC_TARGET_STOP;
543 return FC_TARGET_CONTINUE;
544 } /* }}} int fc_bit_jump_invoke */
547 fc_bit_stop_invoke(const data_set_t __attribute__((unused)) * ds, /* {{{ */
548 value_list_t __attribute__((unused)) * vl,
549 notification_meta_t __attribute__((unused)) * *meta,
550 void __attribute__((unused)) * *user_data) {
551 return FC_TARGET_STOP;
552 } /* }}} int fc_bit_stop_invoke */
555 fc_bit_return_invoke(const data_set_t __attribute__((unused)) * ds, /* {{{ */
556 value_list_t __attribute__((unused)) * vl,
557 notification_meta_t __attribute__((unused)) * *meta,
558 void __attribute__((unused)) * *user_data) {
559 return FC_TARGET_RETURN;
560 } /* }}} int fc_bit_return_invoke */
562 static int fc_bit_write_create(const oconfig_item_t *ci, /* {{{ */
564 fc_writer_t *plugin_list = NULL;
565 size_t plugin_list_len = 0;
567 for (int i = 0; i < ci->children_num; i++) {
568 oconfig_item_t *child = ci->children + i;
571 if (strcasecmp("Plugin", child->key) != 0) {
572 ERROR("Filter subsystem: The built-in target `write' does not "
573 "support the configuration option `%s'.",
578 for (int j = 0; j < child->values_num; j++) {
581 if (child->values[j].type != OCONFIG_TYPE_STRING) {
582 ERROR("Filter subsystem: Built-in target `write': "
583 "The `Plugin' option accepts only string arguments.");
586 plugin = child->values[j].value.string;
589 realloc(plugin_list, (plugin_list_len + 2) * (sizeof(*plugin_list)));
591 ERROR("fc_bit_write_create: realloc failed.");
596 plugin_list[plugin_list_len].plugin = fc_strdup(plugin);
597 if (plugin_list[plugin_list_len].plugin == NULL) {
598 ERROR("fc_bit_write_create: fc_strdup failed.");
601 C_COMPLAIN_INIT(&plugin_list[plugin_list_len].complaint);
603 plugin_list[plugin_list_len].plugin = NULL;
604 } /* for (j = 0; j < child->values_num; j++) */
605 } /* for (i = 0; i < ci->children_num; i++) */
607 *user_data = plugin_list;
610 } /* }}} int fc_bit_write_create */
612 static int fc_bit_write_destroy(void **user_data) /* {{{ */
614 fc_writer_t *plugin_list;
616 if ((user_data == NULL) || (*user_data == NULL))
619 plugin_list = *user_data;
621 for (size_t i = 0; plugin_list[i].plugin != NULL; i++)
622 free(plugin_list[i].plugin);
626 } /* }}} int fc_bit_write_destroy */
628 static int fc_bit_write_invoke(const data_set_t *ds, /* {{{ */
630 notification_meta_t __attribute__((unused)) *
633 fc_writer_t *plugin_list;
637 if (user_data != NULL)
638 plugin_list = *user_data;
640 if ((plugin_list == NULL) || (plugin_list[0].plugin == NULL)) {
641 static c_complain_t write_complaint = C_COMPLAIN_INIT_STATIC;
643 status = plugin_write(/* plugin = */ NULL, ds, vl);
644 if (status == ENOENT) {
645 /* in most cases this is a permanent error, so use the complain
646 * mechanism rather than spamming the logs */
648 LOG_INFO, &write_complaint,
649 "Filter subsystem: Built-in target `write': Dispatching value to "
650 "all write plugins failed with status %i (ENOENT). "
651 "Most likely this means you didn't load any write plugins.",
654 plugin_log_available_writers();
655 } else if (status != 0) {
656 /* often, this is a permanent error (e.g. target system unavailable),
657 * so use the complain mechanism rather than spamming the logs */
659 LOG_INFO, &write_complaint,
660 "Filter subsystem: Built-in target `write': Dispatching value to "
661 "all write plugins failed with status %i.",
665 c_release(LOG_INFO, &write_complaint,
667 "Built-in target `write': Some write plugin is back to normal "
668 "operation. `write' succeeded.");
671 for (size_t i = 0; plugin_list[i].plugin != NULL; i++) {
672 status = plugin_write(plugin_list[i].plugin, ds, vl);
675 LOG_INFO, &plugin_list[i].complaint,
676 "Filter subsystem: Built-in target `write': Dispatching value to "
677 "the `%s' plugin failed with status %i.",
678 plugin_list[i].plugin, status);
680 plugin_log_available_writers();
683 LOG_INFO, &plugin_list[i].complaint,
684 "Filter subsystem: Built-in target `write': Plugin `%s' is back "
685 "to normal operation. `write' succeeded.",
686 plugin_list[i].plugin);
688 } /* for (i = 0; plugin_list[i] != NULL; i++) */
691 return FC_TARGET_CONTINUE;
692 } /* }}} int fc_bit_write_invoke */
694 static int fc_init_once(void) /* {{{ */
697 target_proc_t tproc = {0};
702 tproc.create = fc_bit_jump_create;
703 tproc.destroy = fc_bit_jump_destroy;
704 tproc.invoke = fc_bit_jump_invoke;
705 fc_register_target("jump", tproc);
707 memset(&tproc, 0, sizeof(tproc));
709 tproc.destroy = NULL;
710 tproc.invoke = fc_bit_stop_invoke;
711 fc_register_target("stop", tproc);
713 memset(&tproc, 0, sizeof(tproc));
715 tproc.destroy = NULL;
716 tproc.invoke = fc_bit_return_invoke;
717 fc_register_target("return", tproc);
719 memset(&tproc, 0, sizeof(tproc));
720 tproc.create = fc_bit_write_create;
721 tproc.destroy = fc_bit_write_destroy;
722 tproc.invoke = fc_bit_write_invoke;
723 fc_register_target("write", tproc);
727 } /* }}} int fc_init_once */
732 /* Add a match to list of available matches. */
733 int fc_register_match(const char *name, match_proc_t proc) /* {{{ */
737 DEBUG("fc_register_match (%s);", name);
739 m = calloc(1, sizeof(*m));
743 sstrncpy(m->name, name, sizeof(m->name));
744 memcpy(&m->proc, &proc, sizeof(m->proc));
746 if (match_list_head == NULL) {
751 ptr = match_list_head;
752 while (ptr->next != NULL)
759 } /* }}} int fc_register_match */
761 /* Add a target to list of available targets. */
762 int fc_register_target(const char *name, target_proc_t proc) /* {{{ */
766 DEBUG("fc_register_target (%s);", name);
768 t = calloc(1, sizeof(*t));
772 sstrncpy(t->name, name, sizeof(t->name));
773 memcpy(&t->proc, &proc, sizeof(t->proc));
775 if (target_list_head == NULL) {
776 target_list_head = t;
780 ptr = target_list_head;
781 while (ptr->next != NULL)
788 } /* }}} int fc_register_target */
790 fc_chain_t *fc_chain_get_by_name(const char *chain_name) /* {{{ */
792 if (chain_name == NULL)
795 for (fc_chain_t *chain = chain_list_head; chain != NULL; chain = chain->next)
796 if (strcasecmp(chain_name, chain->name) == 0)
800 } /* }}} int fc_chain_get_by_name */
802 int fc_process_chain(const data_set_t *ds, value_list_t *vl, /* {{{ */
805 int status = FC_TARGET_CONTINUE;
810 DEBUG("fc_process_chain (chain = %s);", chain->name);
812 for (fc_rule_t *rule = chain->rules; rule != NULL; rule = rule->next) {
814 status = FC_TARGET_CONTINUE;
816 if (rule->name[0] != 0) {
817 DEBUG("fc_process_chain (%s): Testing the `%s' rule.", chain->name,
821 /* N. B.: rule->matches may be NULL. */
822 for (match = rule->matches; match != NULL; match = match->next) {
823 /* FIXME: Pass the meta-data to match targets here (when implemented). */
825 (*match->proc.match)(ds, vl, /* meta = */ NULL, &match->user_data);
827 WARNING("fc_process_chain (%s): A match failed.", chain->name);
829 } else if (status != FC_MATCH_MATCHES)
833 /* for-loop has been aborted: Either error or no match. */
835 status = FC_TARGET_CONTINUE;
839 if (rule->name[0] != 0) {
840 DEBUG("fc_process_chain (%s): Rule `%s' matches.", chain->name,
844 for (target = rule->targets; target != NULL; target = target->next) {
845 /* If we get here, all matches have matched the value. Execute the
847 /* FIXME: Pass the meta-data to match targets here (when implemented). */
849 (*target->proc.invoke)(ds, vl, /* meta = */ NULL, &target->user_data);
851 WARNING("fc_process_chain (%s): A target failed.", chain->name);
853 } else if (status == FC_TARGET_CONTINUE)
855 else if (status == FC_TARGET_STOP)
857 else if (status == FC_TARGET_RETURN)
860 WARNING("fc_process_chain (%s): Unknown return value "
861 "from target `%s': %i",
862 chain->name, target->name, status);
866 if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN)) {
867 if (rule->name[0] != 0) {
868 DEBUG("fc_process_chain (%s): Rule `%s' signaled "
870 chain->name, rule->name,
871 (status == FC_TARGET_STOP) ? "stop" : "return");
877 if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN))
880 DEBUG("fc_process_chain (%s): Executing the default targets.", chain->name);
882 status = FC_TARGET_CONTINUE;
883 for (target = chain->targets; target != NULL; target = target->next) {
884 /* If we get here, all matches have matched the value. Execute the
886 /* FIXME: Pass the meta-data to match targets here (when implemented). */
888 (*target->proc.invoke)(ds, vl, /* meta = */ NULL, &target->user_data);
890 WARNING("fc_process_chain (%s): The default target failed.", chain->name);
891 } else if (status == FC_TARGET_CONTINUE)
893 else if (status == FC_TARGET_STOP)
895 else if (status == FC_TARGET_RETURN)
898 WARNING("fc_process_chain (%s): Unknown return value "
899 "from target `%s': %i",
900 chain->name, target->name, status);
904 if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN)) {
905 assert(target != NULL);
906 DEBUG("fc_process_chain (%s): Default target `%s' signaled "
908 chain->name, target->name,
909 (status == FC_TARGET_STOP) ? "stop" : "return");
910 if (status == FC_TARGET_STOP)
911 return FC_TARGET_STOP;
913 return FC_TARGET_CONTINUE;
916 DEBUG("fc_process_chain (%s): Signaling `continue' at end of chain.",
919 return FC_TARGET_CONTINUE;
920 } /* }}} int fc_process_chain */
922 /* Iterate over all rules in the chain and execute all targets for which all
924 int fc_default_action(const data_set_t *ds, value_list_t *vl) /* {{{ */
926 /* FIXME: Pass the meta-data to match targets here (when implemented). */
927 return fc_bit_write_invoke(ds, vl, NULL, NULL);
928 } /* }}} int fc_default_action */
930 int fc_configure(const oconfig_item_t *ci) /* {{{ */
937 if (strcasecmp("Chain", ci->key) == 0)
938 return fc_config_add_chain(ci);
940 WARNING("Filter subsystem: Unknown top level config option `%s'.", ci->key);
943 } /* }}} int fc_configure */