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 sybsystem: 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 sybsystem: 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) /* {{{ */
442 if ((ci->values_num != 1)
443 || (ci->values[0].type != OCONFIG_TYPE_STRING))
445 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
450 chain = (fc_chain_t *) malloc (sizeof (*chain));
453 ERROR ("fc_config_add_chain: malloc failed.");
456 memset (chain, 0, sizeof (*chain));
457 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
459 chain->targets = NULL;
462 for (i = 0; i < ci->children_num; i++)
464 oconfig_item_t *option = ci->children + i;
467 if (strcasecmp ("Rule", option->key) == 0)
468 status = fc_config_add_rule (chain, option);
469 else if (strcasecmp ("Target", option->key) == 0)
470 status = fc_config_add_target (&chain->targets, option);
473 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
474 "inside a <Chain> block.", chain->name, option->key);
480 } /* for (ci->children) */
484 fc_free_chains (chain);
488 if (chain_list_head != NULL)
492 ptr = chain_list_head;
493 while (ptr->next != NULL)
500 chain_list_head = chain;
504 } /* }}} int fc_config_add_chain */
507 * Built-in target "jump"
509 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
511 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
514 oconfig_item_t *ci_chain;
516 if (ci->children_num != 1)
518 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
519 "one `Chain' argument!");
523 ci_chain = ci->children;
524 if (strcasecmp ("Chain", ci_chain->key) != 0)
526 ERROR ("Filter subsystem: The built-in target `jump' does not "
527 "support the configuration option `%s'.",
532 if ((ci_chain->values_num != 1)
533 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
535 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
536 "needs exactly one string argument.");
540 *user_data = fc_strdup (ci_chain->values[0].value.string);
541 if (*user_data == NULL)
543 ERROR ("fc_bit_jump_create: fc_strdup failed.");
548 } /* }}} int fc_bit_jump_create */
550 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
552 if (user_data != NULL)
559 } /* }}} int fc_bit_jump_destroy */
561 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
562 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
569 chain_name = *user_data;
571 for (chain = chain_list_head; chain != NULL; chain = chain->next)
572 if (strcasecmp (chain_name, chain->name) == 0)
577 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
578 "named `%s'.", chain_name);
582 status = fc_process_chain (ds, vl, chain);
585 else if (status == FC_TARGET_STOP)
586 return (FC_TARGET_STOP);
588 return (FC_TARGET_CONTINUE);
589 } /* }}} int fc_bit_jump_invoke */
591 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
592 value_list_t __attribute__((unused)) *vl,
593 notification_meta_t __attribute__((unused)) **meta,
594 void __attribute__((unused)) **user_data)
596 return (FC_TARGET_STOP);
597 } /* }}} int fc_bit_stop_invoke */
599 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
600 value_list_t __attribute__((unused)) *vl,
601 notification_meta_t __attribute__((unused)) **meta,
602 void __attribute__((unused)) **user_data)
604 return (FC_TARGET_RETURN);
605 } /* }}} int fc_bit_return_invoke */
607 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
613 size_t plugin_list_len;
618 for (i = 0; i < ci->children_num; i++)
620 oconfig_item_t *child = ci->children + i;
624 if (strcasecmp ("Plugin", child->key) != 0)
626 ERROR ("Filter subsystem: The built-in target `write' does not "
627 "support the configuration option `%s'.",
632 for (j = 0; j < child->values_num; j++)
634 if (child->values[j].type != OCONFIG_TYPE_STRING)
636 ERROR ("Filter subsystem: Built-in target `write': "
637 "The `Plugin' option accepts only string arguments.");
641 temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
642 * (sizeof (*plugin_list)));
645 ERROR ("fc_bit_write_create: realloc failed.");
650 plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
651 if (plugin_list[plugin_list_len] == NULL)
653 ERROR ("fc_bit_write_create: fc_strdup failed.");
657 plugin_list[plugin_list_len] = NULL;
658 } /* for (j = 0; j < child->values_num; j++) */
659 } /* for (i = 0; i < ci->children_num; i++) */
661 *user_data = plugin_list;
664 } /* }}} int fc_bit_write_create */
666 static int fc_bit_write_destroy (void **user_data) /* {{{ */
671 if ((user_data == NULL) || (*user_data == NULL))
674 plugin_list = *user_data;
676 for (i = 0; plugin_list[i] != NULL; i++)
677 free (plugin_list[i]);
681 } /* }}} int fc_bit_write_destroy */
683 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
684 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
691 if (user_data != NULL)
692 plugin_list = *user_data;
694 if ((plugin_list == NULL) || (plugin_list[0] == NULL))
696 static c_complain_t enoent_complaint = C_COMPLAIN_INIT_STATIC;
698 status = plugin_write (/* plugin = */ NULL, ds, vl);
699 if (status == ENOENT)
701 /* in most cases this is a permanent error, so use the complain
702 * mechanism rather than spamming the logs */
703 c_complain (LOG_INFO, &enoent_complaint,
704 "Filter subsystem: Built-in target `write': Dispatching value to "
705 "all write plugins failed with status %i (ENOENT). "
706 "Most likely this means you didn't load any write plugins.",
709 else if (status != 0)
711 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
712 "all write plugins failed with status %i.", status);
716 assert (status == 0);
717 c_release (LOG_INFO, &enoent_complaint, "Filter subsystem: "
718 "Built-in target `write': Some write plugin is back to normal "
719 "operation. `write' succeeded.");
726 for (i = 0; plugin_list[i] != NULL; i++)
728 status = plugin_write (plugin_list[i], ds, vl);
731 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
732 "the `%s' plugin failed with status %i.", plugin_list[i], status);
734 } /* for (i = 0; plugin_list[i] != NULL; i++) */
737 return (FC_TARGET_CONTINUE);
738 } /* }}} int fc_bit_write_invoke */
740 static int fc_init_once (void) /* {{{ */
748 memset (&tproc, 0, sizeof (tproc));
749 tproc.create = fc_bit_jump_create;
750 tproc.destroy = fc_bit_jump_destroy;
751 tproc.invoke = fc_bit_jump_invoke;
752 fc_register_target ("jump", tproc);
754 memset (&tproc, 0, sizeof (tproc));
756 tproc.destroy = NULL;
757 tproc.invoke = fc_bit_stop_invoke;
758 fc_register_target ("stop", tproc);
760 memset (&tproc, 0, sizeof (tproc));
762 tproc.destroy = NULL;
763 tproc.invoke = fc_bit_return_invoke;
764 fc_register_target ("return", tproc);
766 memset (&tproc, 0, sizeof (tproc));
767 tproc.create = fc_bit_write_create;
768 tproc.destroy = fc_bit_write_destroy;
769 tproc.invoke = fc_bit_write_invoke;
770 fc_register_target ("write", tproc);
774 } /* }}} int fc_init_once */
779 /* Add a match to list of available matches. */
780 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
784 DEBUG ("fc_register_match (%s);", name);
786 m = (fc_match_t *) malloc (sizeof (*m));
789 memset (m, 0, sizeof (*m));
791 sstrncpy (m->name, name, sizeof (m->name));
792 memcpy (&m->proc, &proc, sizeof (m->proc));
795 if (match_list_head == NULL)
803 ptr = match_list_head;
804 while (ptr->next != NULL)
811 } /* }}} int fc_register_match */
813 /* Add a target to list of available targets. */
814 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
818 DEBUG ("fc_register_target (%s);", name);
820 t = (fc_target_t *) malloc (sizeof (*t));
823 memset (t, 0, sizeof (*t));
825 sstrncpy (t->name, name, sizeof (t->name));
826 memcpy (&t->proc, &proc, sizeof (t->proc));
829 if (target_list_head == NULL)
831 target_list_head = t;
837 ptr = target_list_head;
838 while (ptr->next != NULL)
845 } /* }}} int fc_register_target */
847 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
851 if (chain_name == NULL)
854 for (chain = chain_list_head; chain != NULL; chain = chain->next)
855 if (strcasecmp (chain_name, chain->name) == 0)
859 } /* }}} int fc_chain_get_by_name */
861 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
871 DEBUG ("fc_process_chain (chain = %s);", chain->name);
873 status = FC_TARGET_CONTINUE;
874 for (rule = chain->rules; rule != NULL; rule = rule->next)
878 if (rule->name[0] != 0)
880 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
881 chain->name, rule->name);
884 /* N. B.: rule->matches may be NULL. */
885 for (match = rule->matches; match != NULL; match = match->next)
887 /* FIXME: Pass the meta-data to match targets here (when implemented). */
888 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
892 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
895 else if (status != FC_MATCH_MATCHES)
899 /* for-loop has been aborted: Either error or no match. */
902 status = FC_TARGET_CONTINUE;
906 if (rule->name[0] != 0)
908 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
909 chain->name, rule->name);
912 for (target = rule->targets; target != NULL; target = target->next)
914 /* If we get here, all matches have matched the value. Execute the
916 /* FIXME: Pass the meta-data to match targets here (when implemented). */
917 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
921 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
924 else if (status == FC_TARGET_CONTINUE)
926 else if (status == FC_TARGET_STOP)
928 else if (status == FC_TARGET_RETURN)
932 WARNING ("fc_process_chain (%s): Unknown return value "
933 "from target `%s': %i",
934 chain->name, target->name, status);
938 if ((status == FC_TARGET_STOP)
939 || (status == FC_TARGET_RETURN))
941 if (rule->name[0] != 0)
943 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
945 chain->name, rule->name,
946 (status == FC_TARGET_STOP) ? "stop" : "return");
952 status = FC_TARGET_CONTINUE;
956 if (status == FC_TARGET_STOP)
957 return (FC_TARGET_STOP);
958 else if (status == FC_TARGET_RETURN)
959 return (FC_TARGET_CONTINUE);
961 /* for-loop has been aborted: A target returned `FC_TARGET_STOP' */
963 return (FC_TARGET_CONTINUE);
965 DEBUG ("fc_process_chain (%s): Executing the default targets.",
968 status = FC_TARGET_CONTINUE;
969 for (target = chain->targets; target != NULL; target = target->next)
971 /* If we get here, all matches have matched the value. Execute the
973 /* FIXME: Pass the meta-data to match targets here (when implemented). */
974 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
978 WARNING ("fc_process_chain (%s): The default target failed.",
981 else if (status == FC_TARGET_CONTINUE)
983 else if (status == FC_TARGET_STOP)
985 else if (status == FC_TARGET_RETURN)
989 WARNING ("fc_process_chain (%s): Unknown return value "
990 "from target `%s': %i",
991 chain->name, target->name, status);
995 if ((status == FC_TARGET_STOP)
996 || (status == FC_TARGET_RETURN))
998 assert (target != NULL);
999 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1000 "the %s condition.",
1001 chain->name, target->name,
1002 (status == FC_TARGET_STOP) ? "stop" : "return");
1003 if (status == FC_TARGET_STOP)
1004 return (FC_TARGET_STOP);
1006 return (FC_TARGET_CONTINUE);
1009 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1012 return (FC_TARGET_CONTINUE);
1013 } /* }}} int fc_process_chain */
1015 /* Iterate over all rules in the chain and execute all targets for which all
1017 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1019 /* FIXME: Pass the meta-data to match targets here (when implemented). */
1020 return (fc_bit_write_invoke (ds, vl,
1021 /* meta = */ NULL, /* user_data = */ NULL));
1022 } /* }}} int fc_default_action */
1024 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1031 if (strcasecmp ("Chain", ci->key) == 0)
1032 return (fc_config_add_chain (ci));
1034 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1038 } /* }}} int fc_configure */
1040 /* vim: set sw=2 sts=2 et fdm=marker : */