2 * collectd - src/filter_chain.h
3 * Copyright (C) 2008-2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
23 #include "configfile.h"
25 #include "utils_complain.h"
27 #include "filter_chain.h"
32 /* List of matches, used in fc_rule_t and for the global `match_list_head'
35 typedef struct fc_match_s fc_match_t; /* {{{ */
38 char name[DATA_MAX_NAME_LEN];
44 /* List of targets, used in fc_rule_t and for the global `target_list_head'
47 typedef struct fc_target_s fc_target_t; /* {{{ */
50 char name[DATA_MAX_NAME_LEN];
56 /* List of rules, used in fc_chain_t */
58 typedef struct fc_rule_s fc_rule_t; /* {{{ */
61 char name[DATA_MAX_NAME_LEN];
67 /* List of chains, used for `chain_list_head' */
68 struct fc_chain_s /* {{{ */
70 char name[DATA_MAX_NAME_LEN];
79 static fc_match_t *match_list_head;
80 static fc_target_t *target_list_head;
81 static fc_chain_t *chain_list_head;
86 static void fc_free_matches (fc_match_t *m) /* {{{ */
91 if (m->proc.destroy != NULL)
92 (*m->proc.destroy) (&m->user_data);
93 else if (m->user_data != NULL)
95 ERROR ("Filter subsystem: fc_free_matches: There is user data, but no "
96 "destroy functions has been specified. "
97 "Memory will probably be lost!");
101 fc_free_matches (m->next);
104 } /* }}} void fc_free_matches */
106 static void fc_free_targets (fc_target_t *t) /* {{{ */
111 if (t->proc.destroy != NULL)
112 (*t->proc.destroy) (&t->user_data);
113 else if (t->user_data != NULL)
115 ERROR ("Filter subsystem: fc_free_targets: There is user data, but no "
116 "destroy functions has been specified. "
117 "Memory will probably be lost!");
121 fc_free_targets (t->next);
124 } /* }}} void fc_free_targets */
126 static void fc_free_rules (fc_rule_t *r) /* {{{ */
131 fc_free_matches (r->matches);
132 fc_free_targets (r->targets);
135 fc_free_rules (r->next);
138 } /* }}} void fc_free_rules */
140 static void fc_free_chains (fc_chain_t *c) /* {{{ */
145 fc_free_rules (c->rules);
146 fc_free_targets (c->targets);
149 fc_free_chains (c->next);
152 } /* }}} void fc_free_chains */
154 static char *fc_strdup (const char *orig) /* {{{ */
162 sz = strlen (orig) + 1;
163 dest = (char *) malloc (sz);
167 memcpy (dest, orig, sz);
170 } /* }}} char *fc_strdup */
175 * The configuration looks somewhat like this:
181 * Type "^mysql_command$"
182 * TypeInstance "^show_"
193 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
200 if ((ci->values_num != 1)
201 || (ci->values[0].type != OCONFIG_TYPE_STRING))
203 WARNING ("Filter subsystem: `Match' blocks require "
204 "exactly one string argument.");
208 ptr = match_list_head;
211 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
218 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
219 "Did you load the appropriate plugin?",
220 ci->values[0].value.string);
224 m = (fc_match_t *) malloc (sizeof (*m));
227 ERROR ("fc_config_add_match: malloc failed.");
230 memset (m, 0, sizeof (*m));
232 sstrncpy (m->name, ptr->name, sizeof (m->name));
233 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
237 if (m->proc.create != NULL)
239 status = (*m->proc.create) (ci, &m->user_data);
242 WARNING ("Filter subsystem: Failed to create a %s match.",
249 if (*matches_head != NULL)
252 while (ptr->next != NULL)
263 } /* }}} int fc_config_add_match */
265 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
272 if ((ci->values_num != 1)
273 || (ci->values[0].type != OCONFIG_TYPE_STRING))
275 WARNING ("Filter subsystem: `Target' blocks require "
276 "exactly one string argument.");
280 ptr = target_list_head;
283 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
290 WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
291 "Did you load the appropriate plugin?",
292 ci->values[0].value.string);
296 t = (fc_target_t *) malloc (sizeof (*t));
299 ERROR ("fc_config_add_target: malloc failed.");
302 memset (t, 0, sizeof (*t));
304 sstrncpy (t->name, ptr->name, sizeof (t->name));
305 memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
309 if (t->proc.create != NULL)
311 status = (*t->proc.create) (ci, &t->user_data);
314 WARNING ("Filter subsystem: Failed to create a %s target.",
325 if (*targets_head != NULL)
328 while (ptr->next != NULL)
339 } /* }}} int fc_config_add_target */
341 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
345 char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
349 if (ci->values_num > 1)
351 WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
354 else if ((ci->values_num == 1)
355 && (ci->values[0].type != OCONFIG_TYPE_STRING))
357 WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
358 "or no argument at all.");
362 rule = (fc_rule_t *) malloc (sizeof (*rule));
365 ERROR ("fc_config_add_rule: malloc failed.");
368 memset (rule, 0, sizeof (*rule));
371 if (ci->values_num == 1)
373 sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
374 ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
375 ci->values[0].value.string);
378 for (i = 0; i < ci->children_num; i++)
380 oconfig_item_t *option = ci->children + i;
383 if (strcasecmp ("Match", option->key) == 0)
384 status = fc_config_add_match (&rule->matches, option);
385 else if (strcasecmp ("Target", option->key) == 0)
386 status = fc_config_add_target (&rule->targets, option);
389 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
390 "inside a <Rule> block.", rule_name, option->key);
396 } /* for (ci->children) */
398 /* Additional sanity checking. */
401 if (rule->targets == NULL)
403 WARNING ("Filter subsystem: %s: No target has been specified.",
410 } /* while (status == 0) */
414 fc_free_rules (rule);
418 if (chain->rules != NULL)
423 while (ptr->next != NULL)
434 } /* }}} int fc_config_add_rule */
436 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
438 fc_chain_t *chain = NULL;
443 if ((ci->values_num != 1)
444 || (ci->values[0].type != OCONFIG_TYPE_STRING))
446 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
451 if (chain_list_head != NULL)
453 if ((chain = fc_chain_get_by_name (ci->values[0].value.string)) != NULL)
459 chain = (fc_chain_t *) malloc (sizeof (*chain));
462 ERROR ("fc_config_add_chain: malloc failed.");
465 memset (chain, 0, sizeof (*chain));
466 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
468 chain->targets = NULL;
472 for (i = 0; i < ci->children_num; i++)
474 oconfig_item_t *option = ci->children + i;
477 if (strcasecmp ("Rule", option->key) == 0)
478 status = fc_config_add_rule (chain, option);
479 else if (strcasecmp ("Target", option->key) == 0)
480 status = fc_config_add_target (&chain->targets, option);
483 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
484 "inside a <Chain> block.", chain->name, option->key);
490 } /* for (ci->children) */
494 fc_free_chains (chain);
498 if (chain_list_head != NULL)
505 ptr = chain_list_head;
506 while (ptr->next != NULL)
513 chain_list_head = chain;
517 } /* }}} int fc_config_add_chain */
520 * Built-in target "jump"
522 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
524 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
527 oconfig_item_t *ci_chain;
529 if (ci->children_num != 1)
531 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
532 "one `Chain' argument!");
536 ci_chain = ci->children;
537 if (strcasecmp ("Chain", ci_chain->key) != 0)
539 ERROR ("Filter subsystem: The built-in target `jump' does not "
540 "support the configuration option `%s'.",
545 if ((ci_chain->values_num != 1)
546 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
548 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
549 "needs exactly one string argument.");
553 *user_data = fc_strdup (ci_chain->values[0].value.string);
554 if (*user_data == NULL)
556 ERROR ("fc_bit_jump_create: fc_strdup failed.");
561 } /* }}} int fc_bit_jump_create */
563 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
565 if (user_data != NULL)
572 } /* }}} int fc_bit_jump_destroy */
574 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
575 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
582 chain_name = *user_data;
584 for (chain = chain_list_head; chain != NULL; chain = chain->next)
585 if (strcasecmp (chain_name, chain->name) == 0)
590 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
591 "named `%s'.", chain_name);
595 status = fc_process_chain (ds, vl, chain);
598 else if (status == FC_TARGET_STOP)
599 return (FC_TARGET_STOP);
601 return (FC_TARGET_CONTINUE);
602 } /* }}} int fc_bit_jump_invoke */
604 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
605 value_list_t __attribute__((unused)) *vl,
606 notification_meta_t __attribute__((unused)) **meta,
607 void __attribute__((unused)) **user_data)
609 return (FC_TARGET_STOP);
610 } /* }}} int fc_bit_stop_invoke */
612 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
613 value_list_t __attribute__((unused)) *vl,
614 notification_meta_t __attribute__((unused)) **meta,
615 void __attribute__((unused)) **user_data)
617 return (FC_TARGET_RETURN);
618 } /* }}} int fc_bit_return_invoke */
620 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
626 size_t plugin_list_len;
631 for (i = 0; i < ci->children_num; i++)
633 oconfig_item_t *child = ci->children + i;
637 if (strcasecmp ("Plugin", child->key) != 0)
639 ERROR ("Filter subsystem: The built-in target `write' does not "
640 "support the configuration option `%s'.",
645 for (j = 0; j < child->values_num; j++)
647 if (child->values[j].type != OCONFIG_TYPE_STRING)
649 ERROR ("Filter subsystem: Built-in target `write': "
650 "The `Plugin' option accepts only string arguments.");
654 temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
655 * (sizeof (*plugin_list)));
658 ERROR ("fc_bit_write_create: realloc failed.");
663 plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
664 if (plugin_list[plugin_list_len] == NULL)
666 ERROR ("fc_bit_write_create: fc_strdup failed.");
670 plugin_list[plugin_list_len] = NULL;
671 } /* for (j = 0; j < child->values_num; j++) */
672 } /* for (i = 0; i < ci->children_num; i++) */
674 *user_data = plugin_list;
677 } /* }}} int fc_bit_write_create */
679 static int fc_bit_write_destroy (void **user_data) /* {{{ */
684 if ((user_data == NULL) || (*user_data == NULL))
687 plugin_list = *user_data;
689 for (i = 0; plugin_list[i] != NULL; i++)
690 free (plugin_list[i]);
694 } /* }}} int fc_bit_write_destroy */
696 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
697 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
704 if (user_data != NULL)
705 plugin_list = *user_data;
707 if ((plugin_list == NULL) || (plugin_list[0] == NULL))
709 static c_complain_t enoent_complaint = C_COMPLAIN_INIT_STATIC;
711 status = plugin_write (/* plugin = */ NULL, ds, vl);
712 if (status == ENOENT)
714 /* in most cases this is a permanent error, so use the complain
715 * mechanism rather than spamming the logs */
716 c_complain (LOG_INFO, &enoent_complaint,
717 "Filter subsystem: Built-in target `write': Dispatching value to "
718 "all write plugins failed with status %i (ENOENT). "
719 "Most likely this means you didn't load any write plugins.",
722 else if (status != 0)
724 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
725 "all write plugins failed with status %i.", status);
729 assert (status == 0);
730 c_release (LOG_INFO, &enoent_complaint, "Filter subsystem: "
731 "Built-in target `write': Some write plugin is back to normal "
732 "operation. `write' succeeded.");
739 for (i = 0; plugin_list[i] != NULL; i++)
741 status = plugin_write (plugin_list[i], ds, vl);
744 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
745 "the `%s' plugin failed with status %i.", plugin_list[i], status);
747 } /* for (i = 0; plugin_list[i] != NULL; i++) */
750 return (FC_TARGET_CONTINUE);
751 } /* }}} int fc_bit_write_invoke */
753 static int fc_init_once (void) /* {{{ */
761 memset (&tproc, 0, sizeof (tproc));
762 tproc.create = fc_bit_jump_create;
763 tproc.destroy = fc_bit_jump_destroy;
764 tproc.invoke = fc_bit_jump_invoke;
765 fc_register_target ("jump", tproc);
767 memset (&tproc, 0, sizeof (tproc));
769 tproc.destroy = NULL;
770 tproc.invoke = fc_bit_stop_invoke;
771 fc_register_target ("stop", tproc);
773 memset (&tproc, 0, sizeof (tproc));
775 tproc.destroy = NULL;
776 tproc.invoke = fc_bit_return_invoke;
777 fc_register_target ("return", tproc);
779 memset (&tproc, 0, sizeof (tproc));
780 tproc.create = fc_bit_write_create;
781 tproc.destroy = fc_bit_write_destroy;
782 tproc.invoke = fc_bit_write_invoke;
783 fc_register_target ("write", tproc);
787 } /* }}} int fc_init_once */
792 /* Add a match to list of available matches. */
793 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
797 DEBUG ("fc_register_match (%s);", name);
799 m = (fc_match_t *) malloc (sizeof (*m));
802 memset (m, 0, sizeof (*m));
804 sstrncpy (m->name, name, sizeof (m->name));
805 memcpy (&m->proc, &proc, sizeof (m->proc));
808 if (match_list_head == NULL)
816 ptr = match_list_head;
817 while (ptr->next != NULL)
824 } /* }}} int fc_register_match */
826 /* Add a target to list of available targets. */
827 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
831 DEBUG ("fc_register_target (%s);", name);
833 t = (fc_target_t *) malloc (sizeof (*t));
836 memset (t, 0, sizeof (*t));
838 sstrncpy (t->name, name, sizeof (t->name));
839 memcpy (&t->proc, &proc, sizeof (t->proc));
842 if (target_list_head == NULL)
844 target_list_head = t;
850 ptr = target_list_head;
851 while (ptr->next != NULL)
858 } /* }}} int fc_register_target */
860 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
864 if (chain_name == NULL)
867 for (chain = chain_list_head; chain != NULL; chain = chain->next)
868 if (strcasecmp (chain_name, chain->name) == 0)
872 } /* }}} int fc_chain_get_by_name */
874 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
884 DEBUG ("fc_process_chain (chain = %s);", chain->name);
886 status = FC_TARGET_CONTINUE;
887 for (rule = chain->rules; rule != NULL; rule = rule->next)
891 if (rule->name[0] != 0)
893 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
894 chain->name, rule->name);
897 /* N. B.: rule->matches may be NULL. */
898 for (match = rule->matches; match != NULL; match = match->next)
900 /* FIXME: Pass the meta-data to match targets here (when implemented). */
901 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
905 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
908 else if (status != FC_MATCH_MATCHES)
912 /* for-loop has been aborted: Either error or no match. */
915 status = FC_TARGET_CONTINUE;
919 if (rule->name[0] != 0)
921 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
922 chain->name, rule->name);
925 for (target = rule->targets; target != NULL; target = target->next)
927 /* If we get here, all matches have matched the value. Execute the
929 /* FIXME: Pass the meta-data to match targets here (when implemented). */
930 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
934 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
937 else if (status == FC_TARGET_CONTINUE)
939 else if (status == FC_TARGET_STOP)
941 else if (status == FC_TARGET_RETURN)
945 WARNING ("fc_process_chain (%s): Unknown return value "
946 "from target `%s': %i",
947 chain->name, target->name, status);
951 if ((status == FC_TARGET_STOP)
952 || (status == FC_TARGET_RETURN))
954 if (rule->name[0] != 0)
956 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
958 chain->name, rule->name,
959 (status == FC_TARGET_STOP) ? "stop" : "return");
965 status = FC_TARGET_CONTINUE;
969 if (status == FC_TARGET_STOP)
970 return (FC_TARGET_STOP);
971 else if (status == FC_TARGET_RETURN)
972 return (FC_TARGET_CONTINUE);
974 /* for-loop has been aborted: A target returned `FC_TARGET_STOP' */
976 return (FC_TARGET_CONTINUE);
978 DEBUG ("fc_process_chain (%s): Executing the default targets.",
981 status = FC_TARGET_CONTINUE;
982 for (target = chain->targets; target != NULL; target = target->next)
984 /* If we get here, all matches have matched the value. Execute the
986 /* FIXME: Pass the meta-data to match targets here (when implemented). */
987 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
991 WARNING ("fc_process_chain (%s): The default target failed.",
994 else if (status == FC_TARGET_CONTINUE)
996 else if (status == FC_TARGET_STOP)
998 else if (status == FC_TARGET_RETURN)
1002 WARNING ("fc_process_chain (%s): Unknown return value "
1003 "from target `%s': %i",
1004 chain->name, target->name, status);
1008 if ((status == FC_TARGET_STOP)
1009 || (status == FC_TARGET_RETURN))
1011 assert (target != NULL);
1012 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1013 "the %s condition.",
1014 chain->name, target->name,
1015 (status == FC_TARGET_STOP) ? "stop" : "return");
1016 if (status == FC_TARGET_STOP)
1017 return (FC_TARGET_STOP);
1019 return (FC_TARGET_CONTINUE);
1022 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1025 return (FC_TARGET_CONTINUE);
1026 } /* }}} int fc_process_chain */
1028 /* Iterate over all rules in the chain and execute all targets for which all
1030 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1032 /* FIXME: Pass the meta-data to match targets here (when implemented). */
1033 return (fc_bit_write_invoke (ds, vl,
1034 /* meta = */ NULL, /* user_data = */ NULL));
1035 } /* }}} int fc_default_action */
1037 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1044 if (strcasecmp ("Chain", ci->key) == 0)
1045 return (fc_config_add_chain (ci));
1047 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1051 } /* }}} int fc_configure */
1053 /* vim: set sw=2 sts=2 et fdm=marker : */