2 * collectd - src/filter_chain.h
3 * Copyright (C) 2008 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_fule_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' */
106 typedef struct fc_chain_s fc_chain_t; /* {{{ */
109 char name[DATA_MAX_NAME_LEN];
111 fc_target_t *targets;
118 static fc_match_t *match_list_head;
119 static fc_target_t *target_list_head;
120 static fc_chain_t *chain_list_head;
125 static void fc_free_matches (fc_match_t *m) /* {{{ */
130 if (m->proc.destroy != NULL)
131 (*m->proc.destroy) (&m->user_data);
132 else if (m->user_data != NULL)
134 ERROR ("Filter sybsystem: fc_free_matches: There is user data, but no "
135 "destroy functions has been specified. "
136 "Memory will probably be lost!");
140 fc_free_matches (m->next);
143 } /* }}} void fc_free_matches */
145 static void fc_free_targets (fc_target_t *t) /* {{{ */
150 if (t->proc.destroy != NULL)
151 (*t->proc.destroy) (&t->user_data);
152 else if (t->user_data != NULL)
154 ERROR ("Filter sybsystem: fc_free_targets: There is user data, but no "
155 "destroy functions has been specified. "
156 "Memory will probably be lost!");
160 fc_free_targets (t->next);
163 } /* }}} void fc_free_targets */
165 static void fc_free_rules (fc_rule_t *r) /* {{{ */
170 fc_free_matches (r->matches);
171 fc_free_targets (r->targets);
174 fc_free_rules (r->next);
177 } /* }}} void fc_free_rules */
179 static void fc_free_chains (fc_chain_t *c) /* {{{ */
184 fc_free_rules (c->rules);
185 fc_free_targets (c->targets);
188 fc_free_chains (c->next);
191 } /* }}} void fc_free_chains */
193 static char *fc_strdup (const char *orig) /* {{{ */
201 sz = strlen (orig) + 1;
202 dest = (char *) malloc (sz);
206 memcpy (dest, orig, sz);
209 } /* }}} char *fc_strdup */
214 * The configuration looks somewhat like this:
220 * Type "^mysql_command$"
221 * TypeInstance "^show_"
232 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
239 if ((ci->values_num != 1)
240 || (ci->values[0].type != OCONFIG_TYPE_STRING))
242 WARNING ("Filter subsystem: `Match' blocks require "
243 "exactly one string argument.");
247 ptr = match_list_head;
250 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
257 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
258 "Did you load the appropriate plugin?",
259 ci->values[0].value.string);
263 m = (fc_match_t *) malloc (sizeof (*m));
266 ERROR ("fc_config_add_match: malloc failed.");
269 memset (m, 0, sizeof (*m));
271 sstrncpy (m->name, ptr->name, sizeof (m->name));
272 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
273 assert (m->proc.create != NULL);
277 status = (*m->proc.create) (ci, &m->user_data);
280 WARNING ("Filter subsystem: Failed to create a %s match.",
286 if (*matches_head != NULL)
289 while (ptr->next != NULL)
300 } /* }}} int fc_config_add_match */
302 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
309 if ((ci->values_num != 1)
310 || (ci->values[0].type != OCONFIG_TYPE_STRING))
312 WARNING ("Filter subsystem: `Target' blocks require "
313 "exactly one string argument.");
317 ptr = target_list_head;
320 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
327 WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
328 "Did you load the appropriate plugin?",
329 ci->values[0].value.string);
333 t = (fc_target_t *) malloc (sizeof (*t));
336 ERROR ("fc_config_add_match: malloc failed.");
339 memset (t, 0, sizeof (*t));
341 sstrncpy (t->name, ptr->name, sizeof (t->name));
342 memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
346 if (t->proc.create != NULL)
348 status = (*t->proc.create) (ci, &t->user_data);
351 WARNING ("Filter subsystem: Failed to create a %s match.",
362 if (*targets_head != NULL)
365 while (ptr->next != NULL)
376 } /* }}} int fc_config_add_target */
378 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
382 char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
386 if (ci->values_num > 1)
388 WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
391 else if ((ci->values_num == 1)
392 && (ci->values[0].type != OCONFIG_TYPE_STRING))
394 WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
395 "or no argument at all.");
399 rule = (fc_rule_t *) malloc (sizeof (*rule));
402 ERROR ("fc_config_add_rule: malloc failed.");
405 memset (rule, 0, sizeof (*rule));
408 if (ci->values_num == 1)
410 sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
411 ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
412 ci->values[0].value.string);
415 for (i = 0; i < ci->children_num; i++)
417 oconfig_item_t *option = ci->children + i;
420 if (strcasecmp ("Match", option->key) == 0)
421 status = fc_config_add_match (&rule->matches, option);
422 else if (strcasecmp ("Target", option->key) == 0)
423 status = fc_config_add_target (&rule->targets, option);
426 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
427 "inside a <Rule> block.", rule_name, option->key);
433 } /* for (ci->children) */
435 /* Additional sanity checking. */
438 if (rule->targets == NULL)
440 WARNING ("Filter subsystem: %s: No target has been specified.",
447 } /* while (status == 0) */
451 fc_free_rules (rule);
455 if (chain->rules != NULL)
460 while (ptr->next != NULL)
471 } /* }}} int fc_config_add_rule */
473 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
479 if ((ci->values_num != 1)
480 || (ci->values[0].type != OCONFIG_TYPE_STRING))
482 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
487 chain = (fc_chain_t *) malloc (sizeof (*chain));
490 ERROR ("fc_config_add_chain: malloc failed.");
493 memset (chain, 0, sizeof (*chain));
494 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
496 chain->targets = NULL;
499 for (i = 0; i < ci->children_num; i++)
501 oconfig_item_t *option = ci->children + i;
504 if (strcasecmp ("Rule", option->key) == 0)
505 status = fc_config_add_rule (chain, option);
506 else if (strcasecmp ("Target", option->key) == 0)
507 status = fc_config_add_target (&chain->targets, option);
510 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
511 "inside a <Chain> block.", chain->name, option->key);
517 } /* for (ci->children) */
519 /* Additional sanity checking. */
522 if (chain->targets == NULL)
524 WARNING ("Filter subsystem: Chain %s: No default target has been "
525 "specified. Please make sure that there is a <Target> block within "
526 "the <Chain> block!", chain->name);
532 } /* while (status == 0) */
536 fc_free_chains (chain);
540 if (chain_list_head != NULL)
544 ptr = chain_list_head;
545 while (ptr->next != NULL)
552 chain_list_head = chain;
556 } /* }}} int fc_config_add_chain */
558 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
568 status = FC_ACTION_CONTINUE;
570 for (rule = chain->rules; rule != NULL; rule = rule->next)
574 /* N. B.: rule->matches may be NULL. */
575 for (match = rule->matches; match != NULL; match = match->next)
577 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
581 WARNING ("fc_process: A match failed.");
584 else if (status != FC_MATCH_MATCHES)
588 /* for-loop has been aborted: Either error or no match. */
592 for (target = rule->targets; target != NULL; target = target->next)
594 /* If we get here, all matches have matched the value. Execute the target. */
595 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
599 WARNING ("fc_process: A target failed.");
602 else if (status == FC_ACTION_CONTINUE)
604 else if (status == FC_ACTION_STOP)
608 WARNING ("fc_process: Unknown target return value: %i", status);
612 if (status == FC_ACTION_STOP)
616 /* for-loop has been aborted: A target returned `FC_ACTION_STOP' */
620 for (target = chain->targets; target != NULL; target = target->next)
622 /* If we get here, all matches have matched the value. Execute the target. */
623 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
627 WARNING ("fc_process: The default target failed.");
632 } /* }}} int fc_process_chain */
635 * Built-in target "jump"
637 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
639 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
642 oconfig_item_t *ci_chain;
644 if (ci->children_num != 1)
646 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
647 "one `Chain' argument!");
651 ci_chain = ci->children;
652 if (strcasecmp ("Chain", ci_chain->key) != 0)
654 ERROR ("Filter subsystem: The built-in target `jump' does not "
655 "support the configuration option `%s'.",
660 if ((ci_chain->values_num != 1)
661 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
663 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
664 "needs exactly one string argument.");
668 *user_data = fc_strdup (ci_chain->values[0].value.string);
669 if (*user_data == NULL)
671 ERROR ("fc_bit_jump_create: fc_strdup failed.");
676 } /* }}} int fc_bit_jump_create */
678 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
680 if (user_data != NULL)
687 } /* }}} int fc_bit_jump_destroy */
689 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
690 value_list_t *vl, notification_meta_t **meta, void **user_data)
696 chain_name = *user_data;
698 for (chain = chain_list_head; chain != NULL; chain = chain->next)
699 if (strcasecmp (chain_name, chain->name) == 0)
704 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
705 "named `%s'.", chain_name);
709 status = fc_process_chain (ds, vl, chain);
713 return (FC_ACTION_CONTINUE);
714 } /* }}} int fc_bit_jump_invoke */
716 static int fc_bit_stop_invoke (const data_set_t *ds, /* {{{ */
717 value_list_t *vl, notification_meta_t **meta, void **user_data)
719 return (FC_ACTION_STOP);
720 } /* }}} int fc_bit_stop_invoke */
722 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
728 size_t plugin_list_len;
733 for (i = 0; i < ci->children_num; i++)
735 oconfig_item_t *child = ci->children + i;
739 if (strcasecmp ("Plugin", child->key) != 0)
741 ERROR ("Filter subsystem: The built-in target `write' does not "
742 "support the configuration option `%s'.",
747 for (j = 0; j < child->values_num; j++)
749 if (child->values[j].type != OCONFIG_TYPE_STRING)
751 ERROR ("Filter subsystem: Built-in target `write': "
752 "The `Plugin' option accepts only string arguments.");
756 temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
757 * (sizeof (*plugin_list)));
760 ERROR ("fc_bit_write_create: realloc failed.");
765 plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
766 if (plugin_list[plugin_list_len] == NULL)
768 ERROR ("fc_bit_write_create: fc_strdup failed.");
772 plugin_list[plugin_list_len] = NULL;
773 } /* for (j = 0; j < child->values_num; j++) */
774 } /* for (i = 0; i < ci->children_num; i++) */
776 *user_data = plugin_list;
779 } /* }}} int fc_bit_write_create */
781 static int fc_bit_write_destroy (void **user_data) /* {{{ */
786 if ((user_data == NULL) || (*user_data == NULL))
789 plugin_list = *user_data;
791 for (i = 0; plugin_list[i] != NULL; i++)
792 free (plugin_list[i]);
796 } /* }}} int fc_bit_write_destroy */
798 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
799 value_list_t *vl, notification_meta_t **meta, void **user_data)
805 if (user_data != NULL)
806 plugin_list = *user_data;
808 if ((plugin_list == NULL) || (plugin_list[0] == NULL))
810 status = plugin_write (/* plugin = */ NULL, ds, vl);
813 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
814 "all write plugins failed with status %i.", status);
821 for (i = 0; plugin_list[i] != NULL; i++)
823 status = plugin_write (plugin_list[i], ds, vl);
826 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
827 "the `%s' plugin failed with status %i.", plugin_list[i], status);
829 } /* for (i = 0; plugin_list[i] != NULL; i++) */
832 return (FC_ACTION_CONTINUE);
833 } /* }}} int fc_bit_write_invoke */
835 static int fc_init_once (void) /* {{{ */
843 memset (&tproc, 0, sizeof (tproc));
844 tproc.create = fc_bit_jump_create;
845 tproc.destroy = fc_bit_jump_destroy;
846 tproc.invoke = fc_bit_jump_invoke;
847 fc_register_target ("jump", tproc);
849 memset (&tproc, 0, sizeof (tproc));
851 tproc.destroy = NULL;
852 tproc.invoke = fc_bit_stop_invoke;
853 fc_register_target ("stop", tproc);
855 memset (&tproc, 0, sizeof (tproc));
856 tproc.create = fc_bit_write_create;
857 tproc.destroy = fc_bit_write_destroy;
858 tproc.invoke = fc_bit_write_invoke;
859 fc_register_target ("write", tproc);
863 } /* }}} int fc_init_once */
868 /* Add a match to list of available matches. */
869 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
873 m = (fc_match_t *) malloc (sizeof (*m));
876 memset (m, 0, sizeof (*m));
878 sstrncpy (m->name, name, sizeof (m->name));
879 memcpy (&m->proc, &proc, sizeof (m->proc));
882 if (match_list_head == NULL)
890 ptr = match_list_head;
891 while (ptr->next != NULL)
898 } /* }}} int fc_register_match */
900 /* Add a target to list of available targets. */
901 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
905 t = (fc_target_t *) malloc (sizeof (*t));
908 memset (t, 0, sizeof (*t));
910 sstrncpy (t->name, name, sizeof (t->name));
911 memcpy (&t->proc, &proc, sizeof (t->proc));
914 if (target_list_head == NULL)
916 target_list_head = t;
922 ptr = target_list_head;
923 while (ptr->next != NULL)
930 } /* }}} int fc_register_target */
932 /* Iterate over all rules in the chain and execute all targets for which all
934 int fc_process (const data_set_t *ds, value_list_t *vl) /* {{{ */
938 for (chain = chain_list_head; chain != NULL; chain = chain->next)
939 if (strcasecmp ("Main", chain->name) == 0)
943 return (fc_process_chain (ds, vl, chain));
945 ERROR ("fc_process: TODO: Implement default behavior!");
948 } /* }}} int fc_process */
950 int fc_configure (const oconfig_item_t *ci) /* {{{ */
957 if (strcasecmp ("Chain", ci->key) == 0)
958 return (fc_config_add_chain (ci));
960 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
964 } /* }}} int fc_configure */
966 /* vim: set sw=2 sts=2 et fdm=marker : */