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];
84 static fc_match_t *match_list_head;
85 static fc_target_t *target_list_head;
86 static fc_chain_t *chain_list_head;
91 static void fc_free_matches (fc_match_t *m) /* {{{ */
96 if (m->proc.destroy != NULL)
97 (*m->proc.destroy) (&m->user_data);
98 else if (m->user_data != NULL)
100 ERROR ("Filter subsystem: fc_free_matches: There is user data, but no "
101 "destroy functions has been specified. "
102 "Memory will probably be lost!");
106 fc_free_matches (m->next);
109 } /* }}} void fc_free_matches */
111 static void fc_free_targets (fc_target_t *t) /* {{{ */
116 if (t->proc.destroy != NULL)
117 (*t->proc.destroy) (&t->user_data);
118 else if (t->user_data != NULL)
120 ERROR ("Filter subsystem: fc_free_targets: There is user data, but no "
121 "destroy functions has been specified. "
122 "Memory will probably be lost!");
126 fc_free_targets (t->next);
129 } /* }}} void fc_free_targets */
131 static void fc_free_rules (fc_rule_t *r) /* {{{ */
136 fc_free_matches (r->matches);
137 fc_free_targets (r->targets);
140 fc_free_rules (r->next);
143 } /* }}} void fc_free_rules */
145 static void fc_free_chains (fc_chain_t *c) /* {{{ */
150 fc_free_rules (c->rules);
151 fc_free_targets (c->targets);
154 fc_free_chains (c->next);
157 } /* }}} void fc_free_chains */
159 static char *fc_strdup (const char *orig) /* {{{ */
167 sz = strlen (orig) + 1;
168 dest = (char *) malloc (sz);
172 memcpy (dest, orig, sz);
175 } /* }}} char *fc_strdup */
180 * The configuration looks somewhat like this:
186 * Type "^mysql_command$"
187 * TypeInstance "^show_"
198 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
205 if ((ci->values_num != 1)
206 || (ci->values[0].type != OCONFIG_TYPE_STRING))
208 WARNING ("Filter subsystem: `Match' blocks require "
209 "exactly one string argument.");
213 ptr = match_list_head;
216 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
223 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
224 "Did you load the appropriate plugin?",
225 ci->values[0].value.string);
229 m = (fc_match_t *) malloc (sizeof (*m));
232 ERROR ("fc_config_add_match: malloc failed.");
235 memset (m, 0, sizeof (*m));
237 sstrncpy (m->name, ptr->name, sizeof (m->name));
238 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
242 if (m->proc.create != NULL)
244 status = (*m->proc.create) (ci, &m->user_data);
247 WARNING ("Filter subsystem: Failed to create a %s match.",
254 if (*matches_head != NULL)
257 while (ptr->next != NULL)
268 } /* }}} int fc_config_add_match */
270 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
277 if ((ci->values_num != 1)
278 || (ci->values[0].type != OCONFIG_TYPE_STRING))
280 WARNING ("Filter subsystem: `Target' blocks require "
281 "exactly one string argument.");
285 ptr = target_list_head;
288 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
295 WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
296 "Did you load the appropriate plugin?",
297 ci->values[0].value.string);
301 t = (fc_target_t *) malloc (sizeof (*t));
304 ERROR ("fc_config_add_target: malloc failed.");
307 memset (t, 0, sizeof (*t));
309 sstrncpy (t->name, ptr->name, sizeof (t->name));
310 memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
314 if (t->proc.create != NULL)
316 status = (*t->proc.create) (ci, &t->user_data);
319 WARNING ("Filter subsystem: Failed to create a %s target.",
330 if (*targets_head != NULL)
333 while (ptr->next != NULL)
344 } /* }}} int fc_config_add_target */
346 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
350 char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
354 if (ci->values_num > 1)
356 WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
359 else if ((ci->values_num == 1)
360 && (ci->values[0].type != OCONFIG_TYPE_STRING))
362 WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
363 "or no argument at all.");
367 rule = (fc_rule_t *) malloc (sizeof (*rule));
370 ERROR ("fc_config_add_rule: malloc failed.");
373 memset (rule, 0, sizeof (*rule));
376 if (ci->values_num == 1)
378 sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
379 ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
380 ci->values[0].value.string);
383 for (i = 0; i < ci->children_num; i++)
385 oconfig_item_t *option = ci->children + i;
388 if (strcasecmp ("Match", option->key) == 0)
389 status = fc_config_add_match (&rule->matches, option);
390 else if (strcasecmp ("Target", option->key) == 0)
391 status = fc_config_add_target (&rule->targets, option);
394 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
395 "inside a <Rule> block.", rule_name, option->key);
401 } /* for (ci->children) */
403 /* Additional sanity checking. */
406 if (rule->targets == NULL)
408 WARNING ("Filter subsystem: %s: No target has been specified.",
415 } /* while (status == 0) */
419 fc_free_rules (rule);
423 if (chain->rules != NULL)
428 while (ptr->next != NULL)
439 } /* }}} int fc_config_add_rule */
441 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
443 fc_chain_t *chain = NULL;
448 if ((ci->values_num != 1)
449 || (ci->values[0].type != OCONFIG_TYPE_STRING))
451 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
456 if (chain_list_head != NULL)
458 if ((chain = fc_chain_get_by_name (ci->values[0].value.string)) != NULL)
464 chain = (fc_chain_t *) malloc (sizeof (*chain));
467 ERROR ("fc_config_add_chain: malloc failed.");
470 memset (chain, 0, sizeof (*chain));
471 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
473 chain->targets = NULL;
477 for (i = 0; i < ci->children_num; i++)
479 oconfig_item_t *option = ci->children + i;
482 if (strcasecmp ("Rule", option->key) == 0)
483 status = fc_config_add_rule (chain, option);
484 else if (strcasecmp ("Target", option->key) == 0)
485 status = fc_config_add_target (&chain->targets, option);
488 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
489 "inside a <Chain> block.", chain->name, option->key);
495 } /* for (ci->children) */
499 fc_free_chains (chain);
503 if (chain_list_head != NULL)
510 ptr = chain_list_head;
511 while (ptr->next != NULL)
518 chain_list_head = chain;
522 } /* }}} int fc_config_add_chain */
525 * Built-in target "jump"
527 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
529 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
532 oconfig_item_t *ci_chain;
534 if (ci->children_num != 1)
536 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
537 "one `Chain' argument!");
541 ci_chain = ci->children;
542 if (strcasecmp ("Chain", ci_chain->key) != 0)
544 ERROR ("Filter subsystem: The built-in target `jump' does not "
545 "support the configuration option `%s'.",
550 if ((ci_chain->values_num != 1)
551 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
553 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
554 "needs exactly one string argument.");
558 *user_data = fc_strdup (ci_chain->values[0].value.string);
559 if (*user_data == NULL)
561 ERROR ("fc_bit_jump_create: fc_strdup failed.");
566 } /* }}} int fc_bit_jump_create */
568 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
570 if (user_data != NULL)
577 } /* }}} int fc_bit_jump_destroy */
579 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
580 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
587 chain_name = *user_data;
589 for (chain = chain_list_head; chain != NULL; chain = chain->next)
590 if (strcasecmp (chain_name, chain->name) == 0)
595 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
596 "named `%s'.", chain_name);
600 status = fc_process_chain (ds, vl, chain);
603 else if (status == FC_TARGET_STOP)
604 return (FC_TARGET_STOP);
606 return (FC_TARGET_CONTINUE);
607 } /* }}} int fc_bit_jump_invoke */
609 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
610 value_list_t __attribute__((unused)) *vl,
611 notification_meta_t __attribute__((unused)) **meta,
612 void __attribute__((unused)) **user_data)
614 return (FC_TARGET_STOP);
615 } /* }}} int fc_bit_stop_invoke */
617 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
618 value_list_t __attribute__((unused)) *vl,
619 notification_meta_t __attribute__((unused)) **meta,
620 void __attribute__((unused)) **user_data)
622 return (FC_TARGET_RETURN);
623 } /* }}} int fc_bit_return_invoke */
625 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
631 size_t plugin_list_len;
636 for (i = 0; i < ci->children_num; i++)
638 oconfig_item_t *child = ci->children + i;
642 if (strcasecmp ("Plugin", child->key) != 0)
644 ERROR ("Filter subsystem: The built-in target `write' does not "
645 "support the configuration option `%s'.",
650 for (j = 0; j < child->values_num; j++)
652 if (child->values[j].type != OCONFIG_TYPE_STRING)
654 ERROR ("Filter subsystem: Built-in target `write': "
655 "The `Plugin' option accepts only string arguments.");
659 temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
660 * (sizeof (*plugin_list)));
663 ERROR ("fc_bit_write_create: realloc failed.");
668 plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
669 if (plugin_list[plugin_list_len] == NULL)
671 ERROR ("fc_bit_write_create: fc_strdup failed.");
675 plugin_list[plugin_list_len] = NULL;
676 } /* for (j = 0; j < child->values_num; j++) */
677 } /* for (i = 0; i < ci->children_num; i++) */
679 *user_data = plugin_list;
682 } /* }}} int fc_bit_write_create */
684 static int fc_bit_write_destroy (void **user_data) /* {{{ */
689 if ((user_data == NULL) || (*user_data == NULL))
692 plugin_list = *user_data;
694 for (i = 0; plugin_list[i] != NULL; i++)
695 free (plugin_list[i]);
699 } /* }}} int fc_bit_write_destroy */
701 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
702 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
709 if (user_data != NULL)
710 plugin_list = *user_data;
712 if ((plugin_list == NULL) || (plugin_list[0] == NULL))
714 static c_complain_t enoent_complaint = C_COMPLAIN_INIT_STATIC;
716 status = plugin_write (/* plugin = */ NULL, ds, vl);
717 if (status == ENOENT)
719 /* in most cases this is a permanent error, so use the complain
720 * mechanism rather than spamming the logs */
721 c_complain (LOG_INFO, &enoent_complaint,
722 "Filter subsystem: Built-in target `write': Dispatching value to "
723 "all write plugins failed with status %i (ENOENT). "
724 "Most likely this means you didn't load any write plugins.",
727 else if (status != 0)
729 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
730 "all write plugins failed with status %i.", status);
734 assert (status == 0);
735 c_release (LOG_INFO, &enoent_complaint, "Filter subsystem: "
736 "Built-in target `write': Some write plugin is back to normal "
737 "operation. `write' succeeded.");
744 for (i = 0; plugin_list[i] != NULL; i++)
746 status = plugin_write (plugin_list[i], ds, vl);
749 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
750 "the `%s' plugin failed with status %i.", plugin_list[i], status);
752 } /* for (i = 0; plugin_list[i] != NULL; i++) */
755 return (FC_TARGET_CONTINUE);
756 } /* }}} int fc_bit_write_invoke */
758 static int fc_init_once (void) /* {{{ */
766 memset (&tproc, 0, sizeof (tproc));
767 tproc.create = fc_bit_jump_create;
768 tproc.destroy = fc_bit_jump_destroy;
769 tproc.invoke = fc_bit_jump_invoke;
770 fc_register_target ("jump", tproc);
772 memset (&tproc, 0, sizeof (tproc));
774 tproc.destroy = NULL;
775 tproc.invoke = fc_bit_stop_invoke;
776 fc_register_target ("stop", tproc);
778 memset (&tproc, 0, sizeof (tproc));
780 tproc.destroy = NULL;
781 tproc.invoke = fc_bit_return_invoke;
782 fc_register_target ("return", tproc);
784 memset (&tproc, 0, sizeof (tproc));
785 tproc.create = fc_bit_write_create;
786 tproc.destroy = fc_bit_write_destroy;
787 tproc.invoke = fc_bit_write_invoke;
788 fc_register_target ("write", tproc);
792 } /* }}} int fc_init_once */
797 /* Add a match to list of available matches. */
798 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
802 DEBUG ("fc_register_match (%s);", name);
804 m = (fc_match_t *) malloc (sizeof (*m));
807 memset (m, 0, sizeof (*m));
809 sstrncpy (m->name, name, sizeof (m->name));
810 memcpy (&m->proc, &proc, sizeof (m->proc));
813 if (match_list_head == NULL)
821 ptr = match_list_head;
822 while (ptr->next != NULL)
829 } /* }}} int fc_register_match */
831 /* Add a target to list of available targets. */
832 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
836 DEBUG ("fc_register_target (%s);", name);
838 t = (fc_target_t *) malloc (sizeof (*t));
841 memset (t, 0, sizeof (*t));
843 sstrncpy (t->name, name, sizeof (t->name));
844 memcpy (&t->proc, &proc, sizeof (t->proc));
847 if (target_list_head == NULL)
849 target_list_head = t;
855 ptr = target_list_head;
856 while (ptr->next != NULL)
863 } /* }}} int fc_register_target */
865 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
869 if (chain_name == NULL)
872 for (chain = chain_list_head; chain != NULL; chain = chain->next)
873 if (strcasecmp (chain_name, chain->name) == 0)
877 } /* }}} int fc_chain_get_by_name */
879 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
889 DEBUG ("fc_process_chain (chain = %s);", chain->name);
891 status = FC_TARGET_CONTINUE;
892 for (rule = chain->rules; rule != NULL; rule = rule->next)
896 if (rule->name[0] != 0)
898 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
899 chain->name, rule->name);
902 /* N. B.: rule->matches may be NULL. */
903 for (match = rule->matches; match != NULL; match = match->next)
905 /* FIXME: Pass the meta-data to match targets here (when implemented). */
906 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
910 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
913 else if (status != FC_MATCH_MATCHES)
917 /* for-loop has been aborted: Either error or no match. */
920 status = FC_TARGET_CONTINUE;
924 if (rule->name[0] != 0)
926 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
927 chain->name, rule->name);
930 for (target = rule->targets; target != NULL; target = target->next)
932 /* If we get here, all matches have matched the value. Execute the
934 /* FIXME: Pass the meta-data to match targets here (when implemented). */
935 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
939 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
942 else if (status == FC_TARGET_CONTINUE)
944 else if (status == FC_TARGET_STOP)
946 else if (status == FC_TARGET_RETURN)
950 WARNING ("fc_process_chain (%s): Unknown return value "
951 "from target `%s': %i",
952 chain->name, target->name, status);
956 if ((status == FC_TARGET_STOP)
957 || (status == FC_TARGET_RETURN))
959 if (rule->name[0] != 0)
961 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
963 chain->name, rule->name,
964 (status == FC_TARGET_STOP) ? "stop" : "return");
970 status = FC_TARGET_CONTINUE;
974 if (status == FC_TARGET_STOP)
975 return (FC_TARGET_STOP);
976 else if (status == FC_TARGET_RETURN)
977 return (FC_TARGET_CONTINUE);
979 /* for-loop has been aborted: A target returned `FC_TARGET_STOP' */
981 return (FC_TARGET_CONTINUE);
983 DEBUG ("fc_process_chain (%s): Executing the default targets.",
986 status = FC_TARGET_CONTINUE;
987 for (target = chain->targets; target != NULL; target = target->next)
989 /* If we get here, all matches have matched the value. Execute the
991 /* FIXME: Pass the meta-data to match targets here (when implemented). */
992 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
996 WARNING ("fc_process_chain (%s): The default target failed.",
999 else if (status == FC_TARGET_CONTINUE)
1001 else if (status == FC_TARGET_STOP)
1003 else if (status == FC_TARGET_RETURN)
1007 WARNING ("fc_process_chain (%s): Unknown return value "
1008 "from target `%s': %i",
1009 chain->name, target->name, status);
1013 if ((status == FC_TARGET_STOP)
1014 || (status == FC_TARGET_RETURN))
1016 assert (target != NULL);
1017 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1018 "the %s condition.",
1019 chain->name, target->name,
1020 (status == FC_TARGET_STOP) ? "stop" : "return");
1021 if (status == FC_TARGET_STOP)
1022 return (FC_TARGET_STOP);
1024 return (FC_TARGET_CONTINUE);
1027 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1030 return (FC_TARGET_CONTINUE);
1031 } /* }}} int fc_process_chain */
1033 /* Iterate over all rules in the chain and execute all targets for which all
1035 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1037 /* FIXME: Pass the meta-data to match targets here (when implemented). */
1038 return (fc_bit_write_invoke (ds, vl,
1039 /* meta = */ NULL, /* user_data = */ NULL));
1040 } /* }}} int fc_default_action */
1042 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1049 if (strcasecmp ("Chain", ci->key) == 0)
1050 return (fc_config_add_chain (ci));
1052 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1056 } /* }}} int fc_configure */
1058 /* vim: set sw=2 sts=2 et fdm=marker : */