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
40 /* Single UNIX needed for strdup. */
44 #define _XOPEN_SOURCE 500
61 #include "configfile.h"
64 #include "filter_chain.h"
69 /* List of matches, used in fc_rule_t and for the global `match_list_head'
72 typedef struct fc_match_s fc_match_t; /* {{{ */
75 char name[DATA_MAX_NAME_LEN];
81 /* List of targets, used in fc_rule_t and for the global `target_list_head'
84 typedef struct fc_target_s fc_target_t; /* {{{ */
87 char name[DATA_MAX_NAME_LEN];
93 /* List of rules, used in fc_chain_t */
95 typedef struct fc_rule_s fc_rule_t; /* {{{ */
98 char name[DATA_MAX_NAME_LEN];
100 fc_target_t *targets;
104 /* List of chains, used for `chain_list_head' */
105 struct fc_chain_s /* {{{ */
107 char name[DATA_MAX_NAME_LEN];
109 fc_target_t *targets;
116 static fc_match_t *match_list_head;
117 static fc_target_t *target_list_head;
118 static fc_chain_t *chain_list_head;
123 static void fc_free_matches (fc_match_t *m) /* {{{ */
128 if (m->proc.destroy != NULL)
129 (*m->proc.destroy) (&m->user_data);
130 else if (m->user_data != NULL)
132 ERROR ("Filter sybsystem: fc_free_matches: There is user data, but no "
133 "destroy functions has been specified. "
134 "Memory will probably be lost!");
138 fc_free_matches (m->next);
141 } /* }}} void fc_free_matches */
143 static void fc_free_targets (fc_target_t *t) /* {{{ */
148 if (t->proc.destroy != NULL)
149 (*t->proc.destroy) (&t->user_data);
150 else if (t->user_data != NULL)
152 ERROR ("Filter sybsystem: fc_free_targets: There is user data, but no "
153 "destroy functions has been specified. "
154 "Memory will probably be lost!");
158 fc_free_targets (t->next);
161 } /* }}} void fc_free_targets */
163 static void fc_free_rules (fc_rule_t *r) /* {{{ */
168 fc_free_matches (r->matches);
169 fc_free_targets (r->targets);
172 fc_free_rules (r->next);
175 } /* }}} void fc_free_rules */
177 static void fc_free_chains (fc_chain_t *c) /* {{{ */
182 fc_free_rules (c->rules);
183 fc_free_targets (c->targets);
186 fc_free_chains (c->next);
189 } /* }}} void fc_free_chains */
191 static char *fc_strdup (const char *orig) /* {{{ */
199 sz = strlen (orig) + 1;
200 dest = (char *) malloc (sz);
204 memcpy (dest, orig, sz);
207 } /* }}} char *fc_strdup */
212 * The configuration looks somewhat like this:
218 * Type "^mysql_command$"
219 * TypeInstance "^show_"
230 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
237 if ((ci->values_num != 1)
238 || (ci->values[0].type != OCONFIG_TYPE_STRING))
240 WARNING ("Filter subsystem: `Match' blocks require "
241 "exactly one string argument.");
245 ptr = match_list_head;
248 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
255 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
256 "Did you load the appropriate plugin?",
257 ci->values[0].value.string);
261 m = (fc_match_t *) malloc (sizeof (*m));
264 ERROR ("fc_config_add_match: malloc failed.");
267 memset (m, 0, sizeof (*m));
269 sstrncpy (m->name, ptr->name, sizeof (m->name));
270 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
271 assert (m->proc.create != NULL);
275 status = (*m->proc.create) (ci, &m->user_data);
278 WARNING ("Filter subsystem: Failed to create a %s match.",
284 if (*matches_head != NULL)
287 while (ptr->next != NULL)
298 } /* }}} int fc_config_add_match */
300 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
307 if ((ci->values_num != 1)
308 || (ci->values[0].type != OCONFIG_TYPE_STRING))
310 WARNING ("Filter subsystem: `Target' blocks require "
311 "exactly one string argument.");
315 ptr = target_list_head;
318 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
325 WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
326 "Did you load the appropriate plugin?",
327 ci->values[0].value.string);
331 t = (fc_target_t *) malloc (sizeof (*t));
334 ERROR ("fc_config_add_match: malloc failed.");
337 memset (t, 0, sizeof (*t));
339 sstrncpy (t->name, ptr->name, sizeof (t->name));
340 memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
344 if (t->proc.create != NULL)
346 status = (*t->proc.create) (ci, &t->user_data);
349 WARNING ("Filter subsystem: Failed to create a %s match.",
360 if (*targets_head != NULL)
363 while (ptr->next != NULL)
374 } /* }}} int fc_config_add_target */
376 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
380 char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
384 if (ci->values_num > 1)
386 WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
389 else if ((ci->values_num == 1)
390 && (ci->values[0].type != OCONFIG_TYPE_STRING))
392 WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
393 "or no argument at all.");
397 rule = (fc_rule_t *) malloc (sizeof (*rule));
400 ERROR ("fc_config_add_rule: malloc failed.");
403 memset (rule, 0, sizeof (*rule));
406 if (ci->values_num == 1)
408 sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
409 ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
410 ci->values[0].value.string);
413 for (i = 0; i < ci->children_num; i++)
415 oconfig_item_t *option = ci->children + i;
418 if (strcasecmp ("Match", option->key) == 0)
419 status = fc_config_add_match (&rule->matches, option);
420 else if (strcasecmp ("Target", option->key) == 0)
421 status = fc_config_add_target (&rule->targets, option);
424 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
425 "inside a <Rule> block.", rule_name, option->key);
431 } /* for (ci->children) */
433 /* Additional sanity checking. */
436 if (rule->targets == NULL)
438 WARNING ("Filter subsystem: %s: No target has been specified.",
445 } /* while (status == 0) */
449 fc_free_rules (rule);
453 if (chain->rules != NULL)
458 while (ptr->next != NULL)
469 } /* }}} int fc_config_add_rule */
471 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
477 if ((ci->values_num != 1)
478 || (ci->values[0].type != OCONFIG_TYPE_STRING))
480 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
485 chain = (fc_chain_t *) malloc (sizeof (*chain));
488 ERROR ("fc_config_add_chain: malloc failed.");
491 memset (chain, 0, sizeof (*chain));
492 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
494 chain->targets = NULL;
497 for (i = 0; i < ci->children_num; i++)
499 oconfig_item_t *option = ci->children + i;
502 if (strcasecmp ("Rule", option->key) == 0)
503 status = fc_config_add_rule (chain, option);
504 else if (strcasecmp ("Target", option->key) == 0)
505 status = fc_config_add_target (&chain->targets, option);
508 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
509 "inside a <Chain> block.", chain->name, option->key);
515 } /* for (ci->children) */
517 /* Additional sanity checking. */
520 if (chain->targets == NULL)
522 WARNING ("Filter subsystem: Chain %s: No default target has been "
523 "specified. Please make sure that there is a <Target> block within "
524 "the <Chain> block!", chain->name);
530 } /* while (status == 0) */
534 fc_free_chains (chain);
538 if (chain_list_head != NULL)
542 ptr = chain_list_head;
543 while (ptr->next != NULL)
550 chain_list_head = chain;
554 } /* }}} int fc_config_add_chain */
557 * Built-in target "jump"
559 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
561 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
564 oconfig_item_t *ci_chain;
566 if (ci->children_num != 1)
568 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
569 "one `Chain' argument!");
573 ci_chain = ci->children;
574 if (strcasecmp ("Chain", ci_chain->key) != 0)
576 ERROR ("Filter subsystem: The built-in target `jump' does not "
577 "support the configuration option `%s'.",
582 if ((ci_chain->values_num != 1)
583 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
585 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
586 "needs exactly one string argument.");
590 *user_data = fc_strdup (ci_chain->values[0].value.string);
591 if (*user_data == NULL)
593 ERROR ("fc_bit_jump_create: fc_strdup failed.");
598 } /* }}} int fc_bit_jump_create */
600 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
602 if (user_data != NULL)
609 } /* }}} int fc_bit_jump_destroy */
611 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
612 value_list_t *vl, notification_meta_t **meta, void **user_data)
618 chain_name = *user_data;
620 for (chain = chain_list_head; chain != NULL; chain = chain->next)
621 if (strcasecmp (chain_name, chain->name) == 0)
626 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
627 "named `%s'.", chain_name);
631 status = fc_process_chain (ds, vl, chain);
634 else if (status == FC_TARGET_STOP)
635 return (FC_TARGET_STOP);
637 return (FC_TARGET_CONTINUE);
638 } /* }}} int fc_bit_jump_invoke */
640 static int fc_bit_stop_invoke (const data_set_t *ds, /* {{{ */
641 value_list_t *vl, notification_meta_t **meta, void **user_data)
643 return (FC_TARGET_STOP);
644 } /* }}} int fc_bit_stop_invoke */
646 static int fc_bit_return_invoke (const data_set_t *ds, /* {{{ */
647 value_list_t *vl, notification_meta_t **meta, void **user_data)
649 return (FC_TARGET_RETURN);
650 } /* }}} int fc_bit_return_invoke */
652 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
658 size_t plugin_list_len;
663 for (i = 0; i < ci->children_num; i++)
665 oconfig_item_t *child = ci->children + i;
669 if (strcasecmp ("Plugin", child->key) != 0)
671 ERROR ("Filter subsystem: The built-in target `write' does not "
672 "support the configuration option `%s'.",
677 for (j = 0; j < child->values_num; j++)
679 if (child->values[j].type != OCONFIG_TYPE_STRING)
681 ERROR ("Filter subsystem: Built-in target `write': "
682 "The `Plugin' option accepts only string arguments.");
686 temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
687 * (sizeof (*plugin_list)));
690 ERROR ("fc_bit_write_create: realloc failed.");
695 plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
696 if (plugin_list[plugin_list_len] == NULL)
698 ERROR ("fc_bit_write_create: fc_strdup failed.");
702 plugin_list[plugin_list_len] = NULL;
703 } /* for (j = 0; j < child->values_num; j++) */
704 } /* for (i = 0; i < ci->children_num; i++) */
706 *user_data = plugin_list;
709 } /* }}} int fc_bit_write_create */
711 static int fc_bit_write_destroy (void **user_data) /* {{{ */
716 if ((user_data == NULL) || (*user_data == NULL))
719 plugin_list = *user_data;
721 for (i = 0; plugin_list[i] != NULL; i++)
722 free (plugin_list[i]);
726 } /* }}} int fc_bit_write_destroy */
728 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
729 value_list_t *vl, notification_meta_t **meta, void **user_data)
735 if (user_data != NULL)
736 plugin_list = *user_data;
738 if ((plugin_list == NULL) || (plugin_list[0] == NULL))
740 status = plugin_write (/* plugin = */ NULL, ds, vl);
743 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
744 "all write plugins failed with status %i.", status);
751 for (i = 0; plugin_list[i] != NULL; i++)
753 status = plugin_write (plugin_list[i], ds, vl);
756 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
757 "the `%s' plugin failed with status %i.", plugin_list[i], status);
759 } /* for (i = 0; plugin_list[i] != NULL; i++) */
762 return (FC_TARGET_CONTINUE);
763 } /* }}} int fc_bit_write_invoke */
765 static int fc_init_once (void) /* {{{ */
773 memset (&tproc, 0, sizeof (tproc));
774 tproc.create = fc_bit_jump_create;
775 tproc.destroy = fc_bit_jump_destroy;
776 tproc.invoke = fc_bit_jump_invoke;
777 fc_register_target ("jump", tproc);
779 memset (&tproc, 0, sizeof (tproc));
781 tproc.destroy = NULL;
782 tproc.invoke = fc_bit_stop_invoke;
783 fc_register_target ("stop", tproc);
785 memset (&tproc, 0, sizeof (tproc));
787 tproc.destroy = NULL;
788 tproc.invoke = fc_bit_return_invoke;
789 fc_register_target ("return", tproc);
791 memset (&tproc, 0, sizeof (tproc));
792 tproc.create = fc_bit_write_create;
793 tproc.destroy = fc_bit_write_destroy;
794 tproc.invoke = fc_bit_write_invoke;
795 fc_register_target ("write", tproc);
799 } /* }}} int fc_init_once */
804 /* Add a match to list of available matches. */
805 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
809 DEBUG ("fc_register_match (%s);", name);
811 m = (fc_match_t *) malloc (sizeof (*m));
814 memset (m, 0, sizeof (*m));
816 sstrncpy (m->name, name, sizeof (m->name));
817 memcpy (&m->proc, &proc, sizeof (m->proc));
820 if (match_list_head == NULL)
828 ptr = match_list_head;
829 while (ptr->next != NULL)
836 } /* }}} int fc_register_match */
838 /* Add a target to list of available targets. */
839 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
843 DEBUG ("fc_register_target (%s);", name);
845 t = (fc_target_t *) malloc (sizeof (*t));
848 memset (t, 0, sizeof (*t));
850 sstrncpy (t->name, name, sizeof (t->name));
851 memcpy (&t->proc, &proc, sizeof (t->proc));
854 if (target_list_head == NULL)
856 target_list_head = t;
862 ptr = target_list_head;
863 while (ptr->next != NULL)
870 } /* }}} int fc_register_target */
872 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
876 if (chain_name == NULL)
879 for (chain = chain_list_head; chain != NULL; chain = chain->next)
880 if (strcasecmp (chain_name, chain->name) == 0)
884 } /* }}} int fc_chain_get_by_name */
886 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
896 DEBUG ("fc_process_chain (chain = %s);", chain->name);
898 status = FC_TARGET_CONTINUE;
899 for (rule = chain->rules; rule != NULL; rule = rule->next)
903 if (rule->name[0] != 0)
905 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
906 chain->name, rule->name);
909 /* N. B.: rule->matches may be NULL. */
910 for (match = rule->matches; match != NULL; match = match->next)
912 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
916 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
919 else if (status != FC_MATCH_MATCHES)
923 /* for-loop has been aborted: Either error or no match. */
926 status = FC_TARGET_CONTINUE;
930 if (rule->name[0] != 0)
932 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
933 chain->name, rule->name);
936 for (target = rule->targets; target != NULL; target = target->next)
938 /* If we get here, all matches have matched the value. Execute the
940 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
944 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
947 else if (status == FC_TARGET_CONTINUE)
949 else if (status == FC_TARGET_STOP)
951 else if (status == FC_TARGET_RETURN)
955 WARNING ("fc_process_chain (%s): Unknown return value "
956 "from target `%s': %i",
957 chain->name, target->name, status);
961 if ((status == FC_TARGET_STOP)
962 || (status == FC_TARGET_RETURN))
964 if (rule->name[0] != 0)
966 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
968 chain->name, rule->name,
969 (status == FC_TARGET_STOP) ? "stop" : "return");
975 status = FC_TARGET_CONTINUE;
979 if (status == FC_TARGET_STOP)
980 return (FC_TARGET_STOP);
981 else if (status == FC_TARGET_RETURN)
982 return (FC_TARGET_CONTINUE);
984 /* for-loop has been aborted: A target returned `FC_TARGET_STOP' */
986 return (FC_TARGET_CONTINUE);
988 DEBUG ("fc_process_chain (%s): Executing the default targets.",
991 status = FC_TARGET_CONTINUE;
992 for (target = chain->targets; target != NULL; target = target->next)
994 /* If we get here, all matches have matched the value. Execute the
996 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
1000 WARNING ("fc_process_chain (%s): The default target failed.",
1003 else if (status == FC_TARGET_CONTINUE)
1005 else if (status == FC_TARGET_STOP)
1007 else if (status == FC_TARGET_RETURN)
1011 WARNING ("fc_process_chain (%s): Unknown return value "
1012 "from target `%s': %i",
1013 chain->name, target->name, status);
1017 if ((status == FC_TARGET_STOP)
1018 || (status == FC_TARGET_RETURN))
1020 assert (target != NULL);
1021 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1022 "the %s condition.",
1023 chain->name, target->name,
1024 (status == FC_TARGET_STOP) ? "stop" : "return");
1025 if (status == FC_TARGET_STOP)
1026 return (FC_TARGET_STOP);
1028 return (FC_TARGET_CONTINUE);
1031 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1034 return (FC_TARGET_CONTINUE);
1035 } /* }}} int fc_process_chain */
1037 /* Iterate over all rules in the chain and execute all targets for which all
1039 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1041 return (fc_bit_write_invoke (ds, vl,
1042 /* meta = */ NULL, /* user_data = */ NULL));
1043 } /* }}} int fc_default_action */
1045 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1052 if (strcasecmp ("Chain", ci->key) == 0)
1053 return (fc_config_add_chain (ci));
1055 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1059 } /* }}} int fc_configure */
1061 /* vim: set sw=2 sts=2 et fdm=marker : */