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];
76 /* Writer configuration. */
78 typedef struct fc_writer_s fc_writer_t; /* {{{ */
82 c_complain_t complaint;
88 static fc_match_t *match_list_head;
89 static fc_target_t *target_list_head;
90 static fc_chain_t *chain_list_head;
95 static void fc_free_matches (fc_match_t *m) /* {{{ */
100 if (m->proc.destroy != NULL)
101 (*m->proc.destroy) (&m->user_data);
102 else if (m->user_data != NULL)
104 ERROR ("Filter subsystem: fc_free_matches: There is user data, but no "
105 "destroy functions has been specified. "
106 "Memory will probably be lost!");
110 fc_free_matches (m->next);
113 } /* }}} void fc_free_matches */
115 static void fc_free_targets (fc_target_t *t) /* {{{ */
120 if (t->proc.destroy != NULL)
121 (*t->proc.destroy) (&t->user_data);
122 else if (t->user_data != NULL)
124 ERROR ("Filter subsystem: fc_free_targets: There is user data, but no "
125 "destroy functions has been specified. "
126 "Memory will probably be lost!");
130 fc_free_targets (t->next);
133 } /* }}} void fc_free_targets */
135 static void fc_free_rules (fc_rule_t *r) /* {{{ */
140 fc_free_matches (r->matches);
141 fc_free_targets (r->targets);
144 fc_free_rules (r->next);
147 } /* }}} void fc_free_rules */
149 static void fc_free_chains (fc_chain_t *c) /* {{{ */
154 fc_free_rules (c->rules);
155 fc_free_targets (c->targets);
158 fc_free_chains (c->next);
161 } /* }}} void fc_free_chains */
163 static char *fc_strdup (const char *orig) /* {{{ */
171 sz = strlen (orig) + 1;
172 dest = (char *) malloc (sz);
176 memcpy (dest, orig, sz);
179 } /* }}} char *fc_strdup */
184 * The configuration looks somewhat like this:
190 * Type "^mysql_command$"
191 * TypeInstance "^show_"
202 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
209 if ((ci->values_num != 1)
210 || (ci->values[0].type != OCONFIG_TYPE_STRING))
212 WARNING ("Filter subsystem: `Match' blocks require "
213 "exactly one string argument.");
217 ptr = match_list_head;
220 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
227 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
228 "Did you load the appropriate plugin?",
229 ci->values[0].value.string);
233 m = (fc_match_t *) malloc (sizeof (*m));
236 ERROR ("fc_config_add_match: malloc failed.");
239 memset (m, 0, sizeof (*m));
241 sstrncpy (m->name, ptr->name, sizeof (m->name));
242 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
246 if (m->proc.create != NULL)
248 status = (*m->proc.create) (ci, &m->user_data);
251 WARNING ("Filter subsystem: Failed to create a %s match.",
258 if (*matches_head != NULL)
261 while (ptr->next != NULL)
272 } /* }}} int fc_config_add_match */
274 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
281 if ((ci->values_num != 1)
282 || (ci->values[0].type != OCONFIG_TYPE_STRING))
284 WARNING ("Filter subsystem: `Target' blocks require "
285 "exactly one string argument.");
289 ptr = target_list_head;
292 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
299 WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
300 "Did you load the appropriate plugin?",
301 ci->values[0].value.string);
305 t = (fc_target_t *) malloc (sizeof (*t));
308 ERROR ("fc_config_add_target: malloc failed.");
311 memset (t, 0, sizeof (*t));
313 sstrncpy (t->name, ptr->name, sizeof (t->name));
314 memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
318 if (t->proc.create != NULL)
320 status = (*t->proc.create) (ci, &t->user_data);
323 WARNING ("Filter subsystem: Failed to create a %s target.",
334 if (*targets_head != NULL)
337 while (ptr->next != NULL)
348 } /* }}} int fc_config_add_target */
350 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
354 char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
358 if (ci->values_num > 1)
360 WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
363 else if ((ci->values_num == 1)
364 && (ci->values[0].type != OCONFIG_TYPE_STRING))
366 WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
367 "or no argument at all.");
371 rule = (fc_rule_t *) malloc (sizeof (*rule));
374 ERROR ("fc_config_add_rule: malloc failed.");
377 memset (rule, 0, sizeof (*rule));
380 if (ci->values_num == 1)
382 sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
383 ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
384 ci->values[0].value.string);
387 for (i = 0; i < ci->children_num; i++)
389 oconfig_item_t *option = ci->children + i;
391 if (strcasecmp ("Match", option->key) == 0)
392 status = fc_config_add_match (&rule->matches, option);
393 else if (strcasecmp ("Target", option->key) == 0)
394 status = fc_config_add_target (&rule->targets, option);
397 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
398 "inside a <Rule> block.", rule_name, option->key);
404 } /* for (ci->children) */
406 /* Additional sanity checking. */
409 if (rule->targets == NULL)
411 WARNING ("Filter subsystem: %s: No target has been specified.",
418 } /* while (status == 0) */
422 fc_free_rules (rule);
426 if (chain->rules != NULL)
431 while (ptr->next != NULL)
442 } /* }}} int fc_config_add_rule */
444 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
450 if ((ci->values_num != 1)
451 || (ci->values[0].type != OCONFIG_TYPE_STRING))
453 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
458 chain = (fc_chain_t *) malloc (sizeof (*chain));
461 ERROR ("fc_config_add_chain: malloc failed.");
464 memset (chain, 0, sizeof (*chain));
465 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
467 chain->targets = NULL;
470 for (i = 0; i < ci->children_num; i++)
472 oconfig_item_t *option = ci->children + i;
474 if (strcasecmp ("Rule", option->key) == 0)
475 status = fc_config_add_rule (chain, option);
476 else if (strcasecmp ("Target", option->key) == 0)
477 status = fc_config_add_target (&chain->targets, option);
480 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
481 "inside a <Chain> block.", chain->name, option->key);
487 } /* for (ci->children) */
491 fc_free_chains (chain);
495 if (chain_list_head != NULL)
499 ptr = chain_list_head;
500 while (ptr->next != NULL)
507 chain_list_head = chain;
511 } /* }}} int fc_config_add_chain */
514 * Built-in target "jump"
516 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
518 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
521 oconfig_item_t *ci_chain;
523 if (ci->children_num != 1)
525 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
526 "one `Chain' argument!");
530 ci_chain = ci->children;
531 if (strcasecmp ("Chain", ci_chain->key) != 0)
533 ERROR ("Filter subsystem: The built-in target `jump' does not "
534 "support the configuration option `%s'.",
539 if ((ci_chain->values_num != 1)
540 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
542 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
543 "needs exactly one string argument.");
547 *user_data = fc_strdup (ci_chain->values[0].value.string);
548 if (*user_data == NULL)
550 ERROR ("fc_bit_jump_create: fc_strdup failed.");
555 } /* }}} int fc_bit_jump_create */
557 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
559 if (user_data != NULL)
566 } /* }}} int fc_bit_jump_destroy */
568 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
569 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
576 chain_name = *user_data;
578 for (chain = chain_list_head; chain != NULL; chain = chain->next)
579 if (strcasecmp (chain_name, chain->name) == 0)
584 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
585 "named `%s'.", chain_name);
589 status = fc_process_chain (ds, vl, chain);
592 else if (status == FC_TARGET_STOP)
593 return (FC_TARGET_STOP);
595 return (FC_TARGET_CONTINUE);
596 } /* }}} int fc_bit_jump_invoke */
598 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
599 value_list_t __attribute__((unused)) *vl,
600 notification_meta_t __attribute__((unused)) **meta,
601 void __attribute__((unused)) **user_data)
603 return (FC_TARGET_STOP);
604 } /* }}} int fc_bit_stop_invoke */
606 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
607 value_list_t __attribute__((unused)) *vl,
608 notification_meta_t __attribute__((unused)) **meta,
609 void __attribute__((unused)) **user_data)
611 return (FC_TARGET_RETURN);
612 } /* }}} int fc_bit_return_invoke */
614 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
619 fc_writer_t *plugin_list = NULL;
620 size_t plugin_list_len = 0;
622 for (i = 0; i < ci->children_num; i++)
624 oconfig_item_t *child = ci->children + i;
628 if (strcasecmp ("Plugin", child->key) != 0)
630 ERROR ("Filter subsystem: The built-in target `write' does not "
631 "support the configuration option `%s'.",
636 for (j = 0; j < child->values_num; j++)
640 if (child->values[j].type != OCONFIG_TYPE_STRING)
642 ERROR ("Filter subsystem: Built-in target `write': "
643 "The `Plugin' option accepts only string arguments.");
646 plugin = child->values[j].value.string;
648 temp = (fc_writer_t *) realloc (plugin_list, (plugin_list_len + 2)
649 * (sizeof (*plugin_list)));
652 ERROR ("fc_bit_write_create: realloc failed.");
657 plugin_list[plugin_list_len].plugin = fc_strdup (plugin);
658 if (plugin_list[plugin_list_len].plugin == NULL)
660 ERROR ("fc_bit_write_create: fc_strdup failed.");
663 C_COMPLAIN_INIT (&plugin_list[plugin_list_len].complaint);
665 plugin_list[plugin_list_len].plugin = NULL;
666 } /* for (j = 0; j < child->values_num; j++) */
667 } /* for (i = 0; i < ci->children_num; i++) */
669 *user_data = plugin_list;
672 } /* }}} int fc_bit_write_create */
674 static int fc_bit_write_destroy (void **user_data) /* {{{ */
676 fc_writer_t *plugin_list;
679 if ((user_data == NULL) || (*user_data == NULL))
682 plugin_list = *user_data;
684 for (i = 0; plugin_list[i].plugin != NULL; i++)
685 free (plugin_list[i].plugin);
689 } /* }}} int fc_bit_write_destroy */
691 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
692 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
695 fc_writer_t *plugin_list;
699 if (user_data != NULL)
700 plugin_list = *user_data;
702 if ((plugin_list == NULL) || (plugin_list[0].plugin == NULL))
704 static c_complain_t write_complaint = C_COMPLAIN_INIT_STATIC;
706 status = plugin_write (/* plugin = */ NULL, ds, vl);
707 if (status == ENOENT)
709 /* in most cases this is a permanent error, so use the complain
710 * mechanism rather than spamming the logs */
711 c_complain (LOG_INFO, &write_complaint,
712 "Filter subsystem: Built-in target `write': Dispatching value to "
713 "all write plugins failed with status %i (ENOENT). "
714 "Most likely this means you didn't load any write plugins.",
717 else if (status != 0)
719 /* often, this is a permanent error (e.g. target system unavailable),
720 * so use the complain mechanism rather than spamming the logs */
721 c_complain (LOG_INFO, &write_complaint,
722 "Filter subsystem: Built-in target `write': Dispatching value to "
723 "all write plugins failed with status %i.", status);
727 assert (status == 0);
728 c_release (LOG_INFO, &write_complaint, "Filter subsystem: "
729 "Built-in target `write': Some write plugin is back to normal "
730 "operation. `write' succeeded.");
737 for (i = 0; plugin_list[i].plugin != NULL; i++)
739 status = plugin_write (plugin_list[i].plugin, ds, vl);
742 c_complain (LOG_INFO, &plugin_list[i].complaint,
743 "Filter subsystem: Built-in target `write': Dispatching value to "
744 "the `%s' plugin failed with status %i.",
745 plugin_list[i].plugin, status);
749 c_release (LOG_INFO, &plugin_list[i].complaint,
750 "Filter subsystem: Built-in target `write': Plugin `%s' is back "
751 "to normal operation. `write' succeeded.", plugin_list[i].plugin);
753 } /* for (i = 0; plugin_list[i] != NULL; i++) */
756 return (FC_TARGET_CONTINUE);
757 } /* }}} int fc_bit_write_invoke */
759 static int fc_init_once (void) /* {{{ */
767 memset (&tproc, 0, sizeof (tproc));
768 tproc.create = fc_bit_jump_create;
769 tproc.destroy = fc_bit_jump_destroy;
770 tproc.invoke = fc_bit_jump_invoke;
771 fc_register_target ("jump", tproc);
773 memset (&tproc, 0, sizeof (tproc));
775 tproc.destroy = NULL;
776 tproc.invoke = fc_bit_stop_invoke;
777 fc_register_target ("stop", tproc);
779 memset (&tproc, 0, sizeof (tproc));
781 tproc.destroy = NULL;
782 tproc.invoke = fc_bit_return_invoke;
783 fc_register_target ("return", tproc);
785 memset (&tproc, 0, sizeof (tproc));
786 tproc.create = fc_bit_write_create;
787 tproc.destroy = fc_bit_write_destroy;
788 tproc.invoke = fc_bit_write_invoke;
789 fc_register_target ("write", tproc);
793 } /* }}} int fc_init_once */
798 /* Add a match to list of available matches. */
799 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
803 DEBUG ("fc_register_match (%s);", name);
805 m = (fc_match_t *) malloc (sizeof (*m));
808 memset (m, 0, sizeof (*m));
810 sstrncpy (m->name, name, sizeof (m->name));
811 memcpy (&m->proc, &proc, sizeof (m->proc));
814 if (match_list_head == NULL)
822 ptr = match_list_head;
823 while (ptr->next != NULL)
830 } /* }}} int fc_register_match */
832 /* Add a target to list of available targets. */
833 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
837 DEBUG ("fc_register_target (%s);", name);
839 t = (fc_target_t *) malloc (sizeof (*t));
842 memset (t, 0, sizeof (*t));
844 sstrncpy (t->name, name, sizeof (t->name));
845 memcpy (&t->proc, &proc, sizeof (t->proc));
848 if (target_list_head == NULL)
850 target_list_head = t;
856 ptr = target_list_head;
857 while (ptr->next != NULL)
864 } /* }}} int fc_register_target */
866 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
870 if (chain_name == NULL)
873 for (chain = chain_list_head; chain != NULL; chain = chain->next)
874 if (strcasecmp (chain_name, chain->name) == 0)
878 } /* }}} int fc_chain_get_by_name */
880 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
885 int status = FC_TARGET_CONTINUE;
890 DEBUG ("fc_process_chain (chain = %s);", chain->name);
892 for (rule = chain->rules; rule != NULL; rule = rule->next)
895 status = FC_TARGET_CONTINUE;
897 if (rule->name[0] != 0)
899 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
900 chain->name, rule->name);
903 /* N. B.: rule->matches may be NULL. */
904 for (match = rule->matches; match != NULL; match = match->next)
906 /* FIXME: Pass the meta-data to match targets here (when implemented). */
907 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
911 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
914 else if (status != FC_MATCH_MATCHES)
918 /* for-loop has been aborted: Either error or no match. */
921 status = FC_TARGET_CONTINUE;
925 if (rule->name[0] != 0)
927 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
928 chain->name, rule->name);
931 for (target = rule->targets; target != NULL; target = target->next)
933 /* If we get here, all matches have matched the value. Execute the
935 /* FIXME: Pass the meta-data to match targets here (when implemented). */
936 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
940 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
943 else if (status == FC_TARGET_CONTINUE)
945 else if (status == FC_TARGET_STOP)
947 else if (status == FC_TARGET_RETURN)
951 WARNING ("fc_process_chain (%s): Unknown return value "
952 "from target `%s': %i",
953 chain->name, target->name, status);
957 if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN))
959 if (rule->name[0] != 0)
961 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
963 chain->name, rule->name,
964 (status == FC_TARGET_STOP) ? "stop" : "return");
970 if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN))
973 DEBUG ("fc_process_chain (%s): Executing the default targets.",
976 status = FC_TARGET_CONTINUE;
977 for (target = chain->targets; target != NULL; target = target->next)
979 /* If we get here, all matches have matched the value. Execute the
981 /* FIXME: Pass the meta-data to match targets here (when implemented). */
982 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
986 WARNING ("fc_process_chain (%s): The default target failed.",
989 else if (status == FC_TARGET_CONTINUE)
991 else if (status == FC_TARGET_STOP)
993 else if (status == FC_TARGET_RETURN)
997 WARNING ("fc_process_chain (%s): Unknown return value "
998 "from target `%s': %i",
999 chain->name, target->name, status);
1003 if ((status == FC_TARGET_STOP)
1004 || (status == FC_TARGET_RETURN))
1006 assert (target != NULL);
1007 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1008 "the %s condition.",
1009 chain->name, target->name,
1010 (status == FC_TARGET_STOP) ? "stop" : "return");
1011 if (status == FC_TARGET_STOP)
1012 return (FC_TARGET_STOP);
1014 return (FC_TARGET_CONTINUE);
1017 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1020 return (FC_TARGET_CONTINUE);
1021 } /* }}} int fc_process_chain */
1023 /* Iterate over all rules in the chain and execute all targets for which all
1025 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1027 /* FIXME: Pass the meta-data to match targets here (when implemented). */
1028 return (fc_bit_write_invoke (ds, vl,
1029 /* meta = */ NULL, /* user_data = */ NULL));
1030 } /* }}} int fc_default_action */
1032 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1039 if (strcasecmp ("Chain", ci->key) == 0)
1040 return (fc_config_add_chain (ci));
1042 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1046 } /* }}} int fc_configure */
1048 /* vim: set sw=2 sts=2 et fdm=marker : */