2 * collectd - src/filter_chain.h
3 * Copyright (C) 2008,2009 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 * First tell the compiler to stick to the C99 and POSIX standards as close as
26 #ifndef __STRICT_ANSI__ /* {{{ */
27 # define __STRICT_ANSI__
30 #ifndef _ISOC99_SOURCE
31 # define _ISOC99_SOURCE
34 #ifdef _POSIX_C_SOURCE
35 # undef _POSIX_C_SOURCE
37 #define _POSIX_C_SOURCE 200112L
42 #define _XOPEN_SOURCE 600
58 #include "configfile.h"
61 #include "filter_chain.h"
66 /* List of matches, used in fc_rule_t and for the global `match_list_head'
69 typedef struct fc_match_s fc_match_t; /* {{{ */
72 char name[DATA_MAX_NAME_LEN];
78 /* List of targets, used in fc_rule_t and for the global `target_list_head'
81 typedef struct fc_target_s fc_target_t; /* {{{ */
84 char name[DATA_MAX_NAME_LEN];
90 /* List of rules, used in fc_chain_t */
92 typedef struct fc_rule_s fc_rule_t; /* {{{ */
95 char name[DATA_MAX_NAME_LEN];
101 /* List of chains, used for `chain_list_head' */
102 struct fc_chain_s /* {{{ */
104 char name[DATA_MAX_NAME_LEN];
106 fc_target_t *targets;
113 static fc_match_t *match_list_head;
114 static fc_target_t *target_list_head;
115 static fc_chain_t *chain_list_head;
120 static void fc_free_matches (fc_match_t *m) /* {{{ */
125 if (m->proc.destroy != NULL)
126 (*m->proc.destroy) (&m->user_data);
127 else if (m->user_data != NULL)
129 ERROR ("Filter sybsystem: fc_free_matches: There is user data, but no "
130 "destroy functions has been specified. "
131 "Memory will probably be lost!");
135 fc_free_matches (m->next);
138 } /* }}} void fc_free_matches */
140 static void fc_free_targets (fc_target_t *t) /* {{{ */
145 if (t->proc.destroy != NULL)
146 (*t->proc.destroy) (&t->user_data);
147 else if (t->user_data != NULL)
149 ERROR ("Filter sybsystem: fc_free_targets: There is user data, but no "
150 "destroy functions has been specified. "
151 "Memory will probably be lost!");
155 fc_free_targets (t->next);
158 } /* }}} void fc_free_targets */
160 static void fc_free_rules (fc_rule_t *r) /* {{{ */
165 fc_free_matches (r->matches);
166 fc_free_targets (r->targets);
169 fc_free_rules (r->next);
172 } /* }}} void fc_free_rules */
174 static void fc_free_chains (fc_chain_t *c) /* {{{ */
179 fc_free_rules (c->rules);
180 fc_free_targets (c->targets);
183 fc_free_chains (c->next);
186 } /* }}} void fc_free_chains */
188 static char *fc_strdup (const char *orig) /* {{{ */
196 sz = strlen (orig) + 1;
197 dest = (char *) malloc (sz);
201 memcpy (dest, orig, sz);
204 } /* }}} char *fc_strdup */
209 * The configuration looks somewhat like this:
215 * Type "^mysql_command$"
216 * TypeInstance "^show_"
227 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
234 if ((ci->values_num != 1)
235 || (ci->values[0].type != OCONFIG_TYPE_STRING))
237 WARNING ("Filter subsystem: `Match' blocks require "
238 "exactly one string argument.");
242 ptr = match_list_head;
245 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
252 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
253 "Did you load the appropriate plugin?",
254 ci->values[0].value.string);
258 m = (fc_match_t *) malloc (sizeof (*m));
261 ERROR ("fc_config_add_match: malloc failed.");
264 memset (m, 0, sizeof (*m));
266 sstrncpy (m->name, ptr->name, sizeof (m->name));
267 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
271 if (m->proc.create != NULL)
273 status = (*m->proc.create) (ci, &m->user_data);
276 WARNING ("Filter subsystem: Failed to create a %s match.",
283 if (*matches_head != NULL)
286 while (ptr->next != NULL)
297 } /* }}} int fc_config_add_match */
299 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
306 if ((ci->values_num != 1)
307 || (ci->values[0].type != OCONFIG_TYPE_STRING))
309 WARNING ("Filter subsystem: `Target' blocks require "
310 "exactly one string argument.");
314 ptr = target_list_head;
317 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
324 WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
325 "Did you load the appropriate plugin?",
326 ci->values[0].value.string);
330 t = (fc_target_t *) malloc (sizeof (*t));
333 ERROR ("fc_config_add_target: malloc failed.");
336 memset (t, 0, sizeof (*t));
338 sstrncpy (t->name, ptr->name, sizeof (t->name));
339 memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
343 if (t->proc.create != NULL)
345 status = (*t->proc.create) (ci, &t->user_data);
348 WARNING ("Filter subsystem: Failed to create a %s target.",
359 if (*targets_head != NULL)
362 while (ptr->next != NULL)
373 } /* }}} int fc_config_add_target */
375 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
379 char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
383 if (ci->values_num > 1)
385 WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
388 else if ((ci->values_num == 1)
389 && (ci->values[0].type != OCONFIG_TYPE_STRING))
391 WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
392 "or no argument at all.");
396 rule = (fc_rule_t *) malloc (sizeof (*rule));
399 ERROR ("fc_config_add_rule: malloc failed.");
402 memset (rule, 0, sizeof (*rule));
405 if (ci->values_num == 1)
407 sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
408 ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
409 ci->values[0].value.string);
412 for (i = 0; i < ci->children_num; i++)
414 oconfig_item_t *option = ci->children + i;
417 if (strcasecmp ("Match", option->key) == 0)
418 status = fc_config_add_match (&rule->matches, option);
419 else if (strcasecmp ("Target", option->key) == 0)
420 status = fc_config_add_target (&rule->targets, option);
423 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
424 "inside a <Rule> block.", rule_name, option->key);
430 } /* for (ci->children) */
432 /* Additional sanity checking. */
435 if (rule->targets == NULL)
437 WARNING ("Filter subsystem: %s: No target has been specified.",
444 } /* while (status == 0) */
448 fc_free_rules (rule);
452 if (chain->rules != NULL)
457 while (ptr->next != NULL)
468 } /* }}} int fc_config_add_rule */
470 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
476 if ((ci->values_num != 1)
477 || (ci->values[0].type != OCONFIG_TYPE_STRING))
479 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
484 chain = (fc_chain_t *) malloc (sizeof (*chain));
487 ERROR ("fc_config_add_chain: malloc failed.");
490 memset (chain, 0, sizeof (*chain));
491 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
493 chain->targets = NULL;
496 for (i = 0; i < ci->children_num; i++)
498 oconfig_item_t *option = ci->children + i;
501 if (strcasecmp ("Rule", option->key) == 0)
502 status = fc_config_add_rule (chain, option);
503 else if (strcasecmp ("Target", option->key) == 0)
504 status = fc_config_add_target (&chain->targets, option);
507 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
508 "inside a <Chain> block.", chain->name, option->key);
514 } /* for (ci->children) */
518 fc_free_chains (chain);
522 if (chain_list_head != NULL)
526 ptr = chain_list_head;
527 while (ptr->next != NULL)
534 chain_list_head = chain;
538 } /* }}} int fc_config_add_chain */
541 * Built-in target "jump"
543 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
545 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
548 oconfig_item_t *ci_chain;
550 if (ci->children_num != 1)
552 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
553 "one `Chain' argument!");
557 ci_chain = ci->children;
558 if (strcasecmp ("Chain", ci_chain->key) != 0)
560 ERROR ("Filter subsystem: The built-in target `jump' does not "
561 "support the configuration option `%s'.",
566 if ((ci_chain->values_num != 1)
567 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
569 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
570 "needs exactly one string argument.");
574 *user_data = fc_strdup (ci_chain->values[0].value.string);
575 if (*user_data == NULL)
577 ERROR ("fc_bit_jump_create: fc_strdup failed.");
582 } /* }}} int fc_bit_jump_create */
584 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
586 if (user_data != NULL)
593 } /* }}} int fc_bit_jump_destroy */
595 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
596 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
603 chain_name = *user_data;
605 for (chain = chain_list_head; chain != NULL; chain = chain->next)
606 if (strcasecmp (chain_name, chain->name) == 0)
611 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
612 "named `%s'.", chain_name);
616 status = fc_process_chain (ds, vl, chain);
619 else if (status == FC_TARGET_STOP)
620 return (FC_TARGET_STOP);
622 return (FC_TARGET_CONTINUE);
623 } /* }}} int fc_bit_jump_invoke */
625 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
626 value_list_t __attribute__((unused)) *vl,
627 notification_meta_t __attribute__((unused)) **meta,
628 void __attribute__((unused)) **user_data)
630 return (FC_TARGET_STOP);
631 } /* }}} int fc_bit_stop_invoke */
633 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
634 value_list_t __attribute__((unused)) *vl,
635 notification_meta_t __attribute__((unused)) **meta,
636 void __attribute__((unused)) **user_data)
638 return (FC_TARGET_RETURN);
639 } /* }}} int fc_bit_return_invoke */
641 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
647 size_t plugin_list_len;
652 for (i = 0; i < ci->children_num; i++)
654 oconfig_item_t *child = ci->children + i;
658 if (strcasecmp ("Plugin", child->key) != 0)
660 ERROR ("Filter subsystem: The built-in target `write' does not "
661 "support the configuration option `%s'.",
666 for (j = 0; j < child->values_num; j++)
668 if (child->values[j].type != OCONFIG_TYPE_STRING)
670 ERROR ("Filter subsystem: Built-in target `write': "
671 "The `Plugin' option accepts only string arguments.");
675 temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
676 * (sizeof (*plugin_list)));
679 ERROR ("fc_bit_write_create: realloc failed.");
684 plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
685 if (plugin_list[plugin_list_len] == NULL)
687 ERROR ("fc_bit_write_create: fc_strdup failed.");
691 plugin_list[plugin_list_len] = NULL;
692 } /* for (j = 0; j < child->values_num; j++) */
693 } /* for (i = 0; i < ci->children_num; i++) */
695 *user_data = plugin_list;
698 } /* }}} int fc_bit_write_create */
700 static int fc_bit_write_destroy (void **user_data) /* {{{ */
705 if ((user_data == NULL) || (*user_data == NULL))
708 plugin_list = *user_data;
710 for (i = 0; plugin_list[i] != NULL; i++)
711 free (plugin_list[i]);
715 } /* }}} int fc_bit_write_destroy */
717 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
718 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
725 if (user_data != NULL)
726 plugin_list = *user_data;
728 if ((plugin_list == NULL) || (plugin_list[0] == NULL))
730 status = plugin_write (/* plugin = */ NULL, ds, vl);
733 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
734 "all write plugins failed with status %i.", status);
741 for (i = 0; plugin_list[i] != NULL; i++)
743 status = plugin_write (plugin_list[i], ds, vl);
746 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
747 "the `%s' plugin failed with status %i.", plugin_list[i], status);
749 } /* for (i = 0; plugin_list[i] != NULL; i++) */
752 return (FC_TARGET_CONTINUE);
753 } /* }}} int fc_bit_write_invoke */
755 static int fc_init_once (void) /* {{{ */
763 memset (&tproc, 0, sizeof (tproc));
764 tproc.create = fc_bit_jump_create;
765 tproc.destroy = fc_bit_jump_destroy;
766 tproc.invoke = fc_bit_jump_invoke;
767 fc_register_target ("jump", tproc);
769 memset (&tproc, 0, sizeof (tproc));
771 tproc.destroy = NULL;
772 tproc.invoke = fc_bit_stop_invoke;
773 fc_register_target ("stop", tproc);
775 memset (&tproc, 0, sizeof (tproc));
777 tproc.destroy = NULL;
778 tproc.invoke = fc_bit_return_invoke;
779 fc_register_target ("return", tproc);
781 memset (&tproc, 0, sizeof (tproc));
782 tproc.create = fc_bit_write_create;
783 tproc.destroy = fc_bit_write_destroy;
784 tproc.invoke = fc_bit_write_invoke;
785 fc_register_target ("write", tproc);
789 } /* }}} int fc_init_once */
794 /* Add a match to list of available matches. */
795 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
799 DEBUG ("fc_register_match (%s);", name);
801 m = (fc_match_t *) malloc (sizeof (*m));
804 memset (m, 0, sizeof (*m));
806 sstrncpy (m->name, name, sizeof (m->name));
807 memcpy (&m->proc, &proc, sizeof (m->proc));
810 if (match_list_head == NULL)
818 ptr = match_list_head;
819 while (ptr->next != NULL)
826 } /* }}} int fc_register_match */
828 /* Add a target to list of available targets. */
829 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
833 DEBUG ("fc_register_target (%s);", name);
835 t = (fc_target_t *) malloc (sizeof (*t));
838 memset (t, 0, sizeof (*t));
840 sstrncpy (t->name, name, sizeof (t->name));
841 memcpy (&t->proc, &proc, sizeof (t->proc));
844 if (target_list_head == NULL)
846 target_list_head = t;
852 ptr = target_list_head;
853 while (ptr->next != NULL)
860 } /* }}} int fc_register_target */
862 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
866 if (chain_name == NULL)
869 for (chain = chain_list_head; chain != NULL; chain = chain->next)
870 if (strcasecmp (chain_name, chain->name) == 0)
874 } /* }}} int fc_chain_get_by_name */
876 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
886 DEBUG ("fc_process_chain (chain = %s);", chain->name);
888 status = FC_TARGET_CONTINUE;
889 for (rule = chain->rules; rule != NULL; rule = rule->next)
893 if (rule->name[0] != 0)
895 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
896 chain->name, rule->name);
899 /* N. B.: rule->matches may be NULL. */
900 for (match = rule->matches; match != NULL; match = match->next)
902 /* FIXME: Pass the meta-data to match targets here (when implemented). */
903 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
907 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
910 else if (status != FC_MATCH_MATCHES)
914 /* for-loop has been aborted: Either error or no match. */
917 status = FC_TARGET_CONTINUE;
921 if (rule->name[0] != 0)
923 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
924 chain->name, rule->name);
927 for (target = rule->targets; target != NULL; target = target->next)
929 /* If we get here, all matches have matched the value. Execute the
931 /* FIXME: Pass the meta-data to match targets here (when implemented). */
932 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
936 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
939 else if (status == FC_TARGET_CONTINUE)
941 else if (status == FC_TARGET_STOP)
943 else if (status == FC_TARGET_RETURN)
947 WARNING ("fc_process_chain (%s): Unknown return value "
948 "from target `%s': %i",
949 chain->name, target->name, status);
953 if ((status == FC_TARGET_STOP)
954 || (status == FC_TARGET_RETURN))
956 if (rule->name[0] != 0)
958 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
960 chain->name, rule->name,
961 (status == FC_TARGET_STOP) ? "stop" : "return");
967 status = FC_TARGET_CONTINUE;
971 if (status == FC_TARGET_STOP)
972 return (FC_TARGET_STOP);
973 else if (status == FC_TARGET_RETURN)
974 return (FC_TARGET_CONTINUE);
976 /* for-loop has been aborted: A target returned `FC_TARGET_STOP' */
978 return (FC_TARGET_CONTINUE);
980 DEBUG ("fc_process_chain (%s): Executing the default targets.",
983 status = FC_TARGET_CONTINUE;
984 for (target = chain->targets; target != NULL; target = target->next)
986 /* If we get here, all matches have matched the value. Execute the
988 /* FIXME: Pass the meta-data to match targets here (when implemented). */
989 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
993 WARNING ("fc_process_chain (%s): The default target failed.",
996 else if (status == FC_TARGET_CONTINUE)
998 else if (status == FC_TARGET_STOP)
1000 else if (status == FC_TARGET_RETURN)
1004 WARNING ("fc_process_chain (%s): Unknown return value "
1005 "from target `%s': %i",
1006 chain->name, target->name, status);
1010 if ((status == FC_TARGET_STOP)
1011 || (status == FC_TARGET_RETURN))
1013 assert (target != NULL);
1014 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1015 "the %s condition.",
1016 chain->name, target->name,
1017 (status == FC_TARGET_STOP) ? "stop" : "return");
1018 if (status == FC_TARGET_STOP)
1019 return (FC_TARGET_STOP);
1021 return (FC_TARGET_CONTINUE);
1024 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1027 return (FC_TARGET_CONTINUE);
1028 } /* }}} int fc_process_chain */
1030 /* Iterate over all rules in the chain and execute all targets for which all
1032 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1034 /* FIXME: Pass the meta-data to match targets here (when implemented). */
1035 return (fc_bit_write_invoke (ds, vl,
1036 /* meta = */ NULL, /* user_data = */ NULL));
1037 } /* }}} int fc_default_action */
1039 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1046 if (strcasecmp ("Chain", ci->key) == 0)
1047 return (fc_config_add_chain (ci));
1049 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1053 } /* }}} int fc_configure */
1055 /* vim: set sw=2 sts=2 et fdm=marker : */