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>
28 #include "configfile.h"
30 #include "utils_complain.h"
32 #include "filter_chain.h"
37 /* List of matches, used in fc_rule_t and for the global `match_list_head'
40 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; /* {{{ */
55 char name[DATA_MAX_NAME_LEN];
61 /* List of rules, used in fc_chain_t */
63 typedef struct fc_rule_s fc_rule_t; /* {{{ */
66 char name[DATA_MAX_NAME_LEN];
72 /* List of chains, used for `chain_list_head' */
73 struct fc_chain_s /* {{{ */
75 char name[DATA_MAX_NAME_LEN];
81 /* Writer configuration. */
83 typedef struct fc_writer_s fc_writer_t; /* {{{ */
87 c_complain_t complaint;
93 static fc_match_t *match_list_head;
94 static fc_target_t *target_list_head;
95 static fc_chain_t *chain_list_head;
100 static void fc_free_matches (fc_match_t *m) /* {{{ */
105 if (m->proc.destroy != NULL)
106 (*m->proc.destroy) (&m->user_data);
107 else if (m->user_data != NULL)
109 ERROR ("Filter subsystem: fc_free_matches: There is user data, but no "
110 "destroy functions has been specified. "
111 "Memory will probably be lost!");
115 fc_free_matches (m->next);
118 } /* }}} void fc_free_matches */
120 static void fc_free_targets (fc_target_t *t) /* {{{ */
125 if (t->proc.destroy != NULL)
126 (*t->proc.destroy) (&t->user_data);
127 else if (t->user_data != NULL)
129 ERROR ("Filter subsystem: fc_free_targets: There is user data, but no "
130 "destroy functions has been specified. "
131 "Memory will probably be lost!");
135 fc_free_targets (t->next);
138 } /* }}} void fc_free_targets */
140 static void fc_free_rules (fc_rule_t *r) /* {{{ */
145 fc_free_matches (r->matches);
146 fc_free_targets (r->targets);
149 fc_free_rules (r->next);
152 } /* }}} void fc_free_rules */
154 static void fc_free_chains (fc_chain_t *c) /* {{{ */
159 fc_free_rules (c->rules);
160 fc_free_targets (c->targets);
163 fc_free_chains (c->next);
166 } /* }}} void fc_free_chains */
168 static char *fc_strdup (const char *orig) /* {{{ */
176 sz = strlen (orig) + 1;
177 dest = (char *) malloc (sz);
181 memcpy (dest, orig, sz);
184 } /* }}} char *fc_strdup */
189 * The configuration looks somewhat like this:
195 * Type "^mysql_command$"
196 * TypeInstance "^show_"
207 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
214 if ((ci->values_num != 1)
215 || (ci->values[0].type != OCONFIG_TYPE_STRING))
217 WARNING ("Filter subsystem: `Match' blocks require "
218 "exactly one string argument.");
222 ptr = match_list_head;
225 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
232 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
233 "Did you load the appropriate plugin?",
234 ci->values[0].value.string);
238 m = (fc_match_t *) malloc (sizeof (*m));
241 ERROR ("fc_config_add_match: malloc failed.");
244 memset (m, 0, sizeof (*m));
246 sstrncpy (m->name, ptr->name, sizeof (m->name));
247 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
251 if (m->proc.create != NULL)
253 status = (*m->proc.create) (ci, &m->user_data);
256 WARNING ("Filter subsystem: Failed to create a %s match.",
263 if (*matches_head != NULL)
266 while (ptr->next != NULL)
277 } /* }}} int fc_config_add_match */
279 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
286 if ((ci->values_num != 1)
287 || (ci->values[0].type != OCONFIG_TYPE_STRING))
289 WARNING ("Filter subsystem: `Target' blocks require "
290 "exactly one string argument.");
294 ptr = target_list_head;
297 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
304 WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
305 "Did you load the appropriate plugin?",
306 ci->values[0].value.string);
310 t = (fc_target_t *) malloc (sizeof (*t));
313 ERROR ("fc_config_add_target: malloc failed.");
316 memset (t, 0, sizeof (*t));
318 sstrncpy (t->name, ptr->name, sizeof (t->name));
319 memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
323 if (t->proc.create != NULL)
325 status = (*t->proc.create) (ci, &t->user_data);
328 WARNING ("Filter subsystem: Failed to create a %s target.",
339 if (*targets_head != NULL)
342 while (ptr->next != NULL)
353 } /* }}} int fc_config_add_target */
355 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
359 char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
363 if (ci->values_num > 1)
365 WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
368 else if ((ci->values_num == 1)
369 && (ci->values[0].type != OCONFIG_TYPE_STRING))
371 WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
372 "or no argument at all.");
376 rule = (fc_rule_t *) malloc (sizeof (*rule));
379 ERROR ("fc_config_add_rule: malloc failed.");
382 memset (rule, 0, sizeof (*rule));
385 if (ci->values_num == 1)
387 sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
388 ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
389 ci->values[0].value.string);
392 for (i = 0; i < ci->children_num; i++)
394 oconfig_item_t *option = ci->children + i;
396 if (strcasecmp ("Match", option->key) == 0)
397 status = fc_config_add_match (&rule->matches, option);
398 else if (strcasecmp ("Target", option->key) == 0)
399 status = fc_config_add_target (&rule->targets, option);
402 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
403 "inside a <Rule> block.", rule_name, option->key);
409 } /* for (ci->children) */
411 /* Additional sanity checking. */
414 if (rule->targets == NULL)
416 WARNING ("Filter subsystem: %s: No target has been specified.",
423 } /* while (status == 0) */
427 fc_free_rules (rule);
431 if (chain->rules != NULL)
436 while (ptr->next != NULL)
447 } /* }}} int fc_config_add_rule */
449 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
451 fc_chain_t *chain = NULL;
456 if ((ci->values_num != 1)
457 || (ci->values[0].type != OCONFIG_TYPE_STRING))
459 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
464 if (chain_list_head != NULL)
466 if ((chain = fc_chain_get_by_name (ci->values[0].value.string)) != NULL)
472 chain = (fc_chain_t *) malloc (sizeof (*chain));
475 ERROR ("fc_config_add_chain: malloc failed.");
478 memset (chain, 0, sizeof (*chain));
479 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
481 chain->targets = NULL;
485 for (i = 0; i < ci->children_num; i++)
487 oconfig_item_t *option = ci->children + i;
489 if (strcasecmp ("Rule", option->key) == 0)
490 status = fc_config_add_rule (chain, option);
491 else if (strcasecmp ("Target", option->key) == 0)
492 status = fc_config_add_target (&chain->targets, option);
495 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
496 "inside a <Chain> block.", chain->name, option->key);
502 } /* for (ci->children) */
506 fc_free_chains (chain);
510 if (chain_list_head != NULL)
517 ptr = chain_list_head;
518 while (ptr->next != NULL)
525 chain_list_head = chain;
529 } /* }}} int fc_config_add_chain */
532 * Built-in target "jump"
534 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
536 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
539 oconfig_item_t *ci_chain;
541 if (ci->children_num != 1)
543 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
544 "one `Chain' argument!");
548 ci_chain = ci->children;
549 if (strcasecmp ("Chain", ci_chain->key) != 0)
551 ERROR ("Filter subsystem: The built-in target `jump' does not "
552 "support the configuration option `%s'.",
557 if ((ci_chain->values_num != 1)
558 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
560 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
561 "needs exactly one string argument.");
565 *user_data = fc_strdup (ci_chain->values[0].value.string);
566 if (*user_data == NULL)
568 ERROR ("fc_bit_jump_create: fc_strdup failed.");
573 } /* }}} int fc_bit_jump_create */
575 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
577 if (user_data != NULL)
584 } /* }}} int fc_bit_jump_destroy */
586 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
587 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
594 chain_name = *user_data;
596 for (chain = chain_list_head; chain != NULL; chain = chain->next)
597 if (strcasecmp (chain_name, chain->name) == 0)
602 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
603 "named `%s'.", chain_name);
607 status = fc_process_chain (ds, vl, chain);
610 else if (status == FC_TARGET_STOP)
611 return (FC_TARGET_STOP);
613 return (FC_TARGET_CONTINUE);
614 } /* }}} int fc_bit_jump_invoke */
616 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
617 value_list_t __attribute__((unused)) *vl,
618 notification_meta_t __attribute__((unused)) **meta,
619 void __attribute__((unused)) **user_data)
621 return (FC_TARGET_STOP);
622 } /* }}} int fc_bit_stop_invoke */
624 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
625 value_list_t __attribute__((unused)) *vl,
626 notification_meta_t __attribute__((unused)) **meta,
627 void __attribute__((unused)) **user_data)
629 return (FC_TARGET_RETURN);
630 } /* }}} int fc_bit_return_invoke */
632 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
637 fc_writer_t *plugin_list = NULL;
638 size_t plugin_list_len = 0;
640 for (i = 0; i < ci->children_num; i++)
642 oconfig_item_t *child = ci->children + i;
646 if (strcasecmp ("Plugin", child->key) != 0)
648 ERROR ("Filter subsystem: The built-in target `write' does not "
649 "support the configuration option `%s'.",
654 for (j = 0; j < child->values_num; j++)
658 if (child->values[j].type != OCONFIG_TYPE_STRING)
660 ERROR ("Filter subsystem: Built-in target `write': "
661 "The `Plugin' option accepts only string arguments.");
664 plugin = child->values[j].value.string;
666 temp = (fc_writer_t *) realloc (plugin_list, (plugin_list_len + 2)
667 * (sizeof (*plugin_list)));
670 ERROR ("fc_bit_write_create: realloc failed.");
675 plugin_list[plugin_list_len].plugin = fc_strdup (plugin);
676 if (plugin_list[plugin_list_len].plugin == NULL)
678 ERROR ("fc_bit_write_create: fc_strdup failed.");
681 C_COMPLAIN_INIT (&plugin_list[plugin_list_len].complaint);
683 plugin_list[plugin_list_len].plugin = NULL;
684 } /* for (j = 0; j < child->values_num; j++) */
685 } /* for (i = 0; i < ci->children_num; i++) */
687 *user_data = plugin_list;
690 } /* }}} int fc_bit_write_create */
692 static int fc_bit_write_destroy (void **user_data) /* {{{ */
694 fc_writer_t *plugin_list;
697 if ((user_data == NULL) || (*user_data == NULL))
700 plugin_list = *user_data;
702 for (i = 0; plugin_list[i].plugin != NULL; i++)
703 free (plugin_list[i].plugin);
707 } /* }}} int fc_bit_write_destroy */
709 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
710 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
713 fc_writer_t *plugin_list;
717 if (user_data != NULL)
718 plugin_list = *user_data;
720 if ((plugin_list == NULL) || (plugin_list[0].plugin == NULL))
722 static c_complain_t write_complaint = C_COMPLAIN_INIT_STATIC;
724 status = plugin_write (/* plugin = */ NULL, ds, vl);
725 if (status == ENOENT)
727 /* in most cases this is a permanent error, so use the complain
728 * mechanism rather than spamming the logs */
729 c_complain (LOG_INFO, &write_complaint,
730 "Filter subsystem: Built-in target `write': Dispatching value to "
731 "all write plugins failed with status %i (ENOENT). "
732 "Most likely this means you didn't load any write plugins.",
735 plugin_log_available_writers ();
737 else if (status != 0)
739 /* often, this is a permanent error (e.g. target system unavailable),
740 * so use the complain mechanism rather than spamming the logs */
741 c_complain (LOG_INFO, &write_complaint,
742 "Filter subsystem: Built-in target `write': Dispatching value to "
743 "all write plugins failed with status %i.", status);
747 assert (status == 0);
748 c_release (LOG_INFO, &write_complaint, "Filter subsystem: "
749 "Built-in target `write': Some write plugin is back to normal "
750 "operation. `write' succeeded.");
757 for (i = 0; plugin_list[i].plugin != NULL; i++)
759 status = plugin_write (plugin_list[i].plugin, ds, vl);
762 c_complain (LOG_INFO, &plugin_list[i].complaint,
763 "Filter subsystem: Built-in target `write': Dispatching value to "
764 "the `%s' plugin failed with status %i.",
765 plugin_list[i].plugin, status);
767 plugin_log_available_writers ();
771 c_release (LOG_INFO, &plugin_list[i].complaint,
772 "Filter subsystem: Built-in target `write': Plugin `%s' is back "
773 "to normal operation. `write' succeeded.", plugin_list[i].plugin);
775 } /* for (i = 0; plugin_list[i] != NULL; i++) */
778 return (FC_TARGET_CONTINUE);
779 } /* }}} int fc_bit_write_invoke */
781 static int fc_init_once (void) /* {{{ */
789 memset (&tproc, 0, sizeof (tproc));
790 tproc.create = fc_bit_jump_create;
791 tproc.destroy = fc_bit_jump_destroy;
792 tproc.invoke = fc_bit_jump_invoke;
793 fc_register_target ("jump", tproc);
795 memset (&tproc, 0, sizeof (tproc));
797 tproc.destroy = NULL;
798 tproc.invoke = fc_bit_stop_invoke;
799 fc_register_target ("stop", tproc);
801 memset (&tproc, 0, sizeof (tproc));
803 tproc.destroy = NULL;
804 tproc.invoke = fc_bit_return_invoke;
805 fc_register_target ("return", tproc);
807 memset (&tproc, 0, sizeof (tproc));
808 tproc.create = fc_bit_write_create;
809 tproc.destroy = fc_bit_write_destroy;
810 tproc.invoke = fc_bit_write_invoke;
811 fc_register_target ("write", tproc);
815 } /* }}} int fc_init_once */
820 /* Add a match to list of available matches. */
821 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
825 DEBUG ("fc_register_match (%s);", name);
827 m = (fc_match_t *) malloc (sizeof (*m));
830 memset (m, 0, sizeof (*m));
832 sstrncpy (m->name, name, sizeof (m->name));
833 memcpy (&m->proc, &proc, sizeof (m->proc));
836 if (match_list_head == NULL)
844 ptr = match_list_head;
845 while (ptr->next != NULL)
852 } /* }}} int fc_register_match */
854 /* Add a target to list of available targets. */
855 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
859 DEBUG ("fc_register_target (%s);", name);
861 t = (fc_target_t *) malloc (sizeof (*t));
864 memset (t, 0, sizeof (*t));
866 sstrncpy (t->name, name, sizeof (t->name));
867 memcpy (&t->proc, &proc, sizeof (t->proc));
870 if (target_list_head == NULL)
872 target_list_head = t;
878 ptr = target_list_head;
879 while (ptr->next != NULL)
886 } /* }}} int fc_register_target */
888 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
892 if (chain_name == NULL)
895 for (chain = chain_list_head; chain != NULL; chain = chain->next)
896 if (strcasecmp (chain_name, chain->name) == 0)
900 } /* }}} int fc_chain_get_by_name */
902 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
907 int status = FC_TARGET_CONTINUE;
912 DEBUG ("fc_process_chain (chain = %s);", chain->name);
914 for (rule = chain->rules; rule != NULL; rule = rule->next)
917 status = FC_TARGET_CONTINUE;
919 if (rule->name[0] != 0)
921 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
922 chain->name, rule->name);
925 /* N. B.: rule->matches may be NULL. */
926 for (match = rule->matches; match != NULL; match = match->next)
928 /* FIXME: Pass the meta-data to match targets here (when implemented). */
929 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
933 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
936 else if (status != FC_MATCH_MATCHES)
940 /* for-loop has been aborted: Either error or no match. */
943 status = FC_TARGET_CONTINUE;
947 if (rule->name[0] != 0)
949 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
950 chain->name, rule->name);
953 for (target = rule->targets; target != NULL; target = target->next)
955 /* If we get here, all matches have matched the value. Execute the
957 /* FIXME: Pass the meta-data to match targets here (when implemented). */
958 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
962 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
965 else if (status == FC_TARGET_CONTINUE)
967 else if (status == FC_TARGET_STOP)
969 else if (status == FC_TARGET_RETURN)
973 WARNING ("fc_process_chain (%s): Unknown return value "
974 "from target `%s': %i",
975 chain->name, target->name, status);
979 if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN))
981 if (rule->name[0] != 0)
983 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
985 chain->name, rule->name,
986 (status == FC_TARGET_STOP) ? "stop" : "return");
992 if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN))
995 DEBUG ("fc_process_chain (%s): Executing the default targets.",
998 status = FC_TARGET_CONTINUE;
999 for (target = chain->targets; target != NULL; target = target->next)
1001 /* If we get here, all matches have matched the value. Execute the
1003 /* FIXME: Pass the meta-data to match targets here (when implemented). */
1004 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
1005 &target->user_data);
1008 WARNING ("fc_process_chain (%s): The default target failed.",
1011 else if (status == FC_TARGET_CONTINUE)
1013 else if (status == FC_TARGET_STOP)
1015 else if (status == FC_TARGET_RETURN)
1019 WARNING ("fc_process_chain (%s): Unknown return value "
1020 "from target `%s': %i",
1021 chain->name, target->name, status);
1025 if ((status == FC_TARGET_STOP)
1026 || (status == FC_TARGET_RETURN))
1028 assert (target != NULL);
1029 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1030 "the %s condition.",
1031 chain->name, target->name,
1032 (status == FC_TARGET_STOP) ? "stop" : "return");
1033 if (status == FC_TARGET_STOP)
1034 return (FC_TARGET_STOP);
1036 return (FC_TARGET_CONTINUE);
1039 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1042 return (FC_TARGET_CONTINUE);
1043 } /* }}} int fc_process_chain */
1045 /* Iterate over all rules in the chain and execute all targets for which all
1047 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1049 /* FIXME: Pass the meta-data to match targets here (when implemented). */
1050 return (fc_bit_write_invoke (ds, vl,
1051 /* meta = */ NULL, /* user_data = */ NULL));
1052 } /* }}} int fc_default_action */
1054 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1061 if (strcasecmp ("Chain", ci->key) == 0)
1062 return (fc_config_add_chain (ci));
1064 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1068 } /* }}} int fc_configure */
1070 /* vim: set sw=2 sts=2 et fdm=marker : */