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;
392 if (strcasecmp ("Match", option->key) == 0)
393 status = fc_config_add_match (&rule->matches, option);
394 else if (strcasecmp ("Target", option->key) == 0)
395 status = fc_config_add_target (&rule->targets, option);
398 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
399 "inside a <Rule> block.", rule_name, option->key);
405 } /* for (ci->children) */
407 /* Additional sanity checking. */
410 if (rule->targets == NULL)
412 WARNING ("Filter subsystem: %s: No target has been specified.",
419 } /* while (status == 0) */
423 fc_free_rules (rule);
427 if (chain->rules != NULL)
432 while (ptr->next != NULL)
443 } /* }}} int fc_config_add_rule */
445 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
451 if ((ci->values_num != 1)
452 || (ci->values[0].type != OCONFIG_TYPE_STRING))
454 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
459 chain = (fc_chain_t *) malloc (sizeof (*chain));
462 ERROR ("fc_config_add_chain: malloc failed.");
465 memset (chain, 0, sizeof (*chain));
466 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
468 chain->targets = NULL;
471 for (i = 0; i < ci->children_num; i++)
473 oconfig_item_t *option = ci->children + i;
476 if (strcasecmp ("Rule", option->key) == 0)
477 status = fc_config_add_rule (chain, option);
478 else if (strcasecmp ("Target", option->key) == 0)
479 status = fc_config_add_target (&chain->targets, option);
482 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
483 "inside a <Chain> block.", chain->name, option->key);
489 } /* for (ci->children) */
493 fc_free_chains (chain);
497 if (chain_list_head != NULL)
501 ptr = chain_list_head;
502 while (ptr->next != NULL)
509 chain_list_head = chain;
513 } /* }}} int fc_config_add_chain */
516 * Built-in target "jump"
518 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
520 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
523 oconfig_item_t *ci_chain;
525 if (ci->children_num != 1)
527 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
528 "one `Chain' argument!");
532 ci_chain = ci->children;
533 if (strcasecmp ("Chain", ci_chain->key) != 0)
535 ERROR ("Filter subsystem: The built-in target `jump' does not "
536 "support the configuration option `%s'.",
541 if ((ci_chain->values_num != 1)
542 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
544 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
545 "needs exactly one string argument.");
549 *user_data = fc_strdup (ci_chain->values[0].value.string);
550 if (*user_data == NULL)
552 ERROR ("fc_bit_jump_create: fc_strdup failed.");
557 } /* }}} int fc_bit_jump_create */
559 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
561 if (user_data != NULL)
568 } /* }}} int fc_bit_jump_destroy */
570 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
571 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
578 chain_name = *user_data;
580 for (chain = chain_list_head; chain != NULL; chain = chain->next)
581 if (strcasecmp (chain_name, chain->name) == 0)
586 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
587 "named `%s'.", chain_name);
591 status = fc_process_chain (ds, vl, chain);
594 else if (status == FC_TARGET_STOP)
595 return (FC_TARGET_STOP);
597 return (FC_TARGET_CONTINUE);
598 } /* }}} int fc_bit_jump_invoke */
600 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
601 value_list_t __attribute__((unused)) *vl,
602 notification_meta_t __attribute__((unused)) **meta,
603 void __attribute__((unused)) **user_data)
605 return (FC_TARGET_STOP);
606 } /* }}} int fc_bit_stop_invoke */
608 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
609 value_list_t __attribute__((unused)) *vl,
610 notification_meta_t __attribute__((unused)) **meta,
611 void __attribute__((unused)) **user_data)
613 return (FC_TARGET_RETURN);
614 } /* }}} int fc_bit_return_invoke */
616 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
621 fc_writer_t *plugin_list = NULL;
622 size_t plugin_list_len = 0;
624 for (i = 0; i < ci->children_num; i++)
626 oconfig_item_t *child = ci->children + i;
630 if (strcasecmp ("Plugin", child->key) != 0)
632 ERROR ("Filter subsystem: The built-in target `write' does not "
633 "support the configuration option `%s'.",
638 for (j = 0; j < child->values_num; j++)
642 if (child->values[j].type != OCONFIG_TYPE_STRING)
644 ERROR ("Filter subsystem: Built-in target `write': "
645 "The `Plugin' option accepts only string arguments.");
648 plugin = child->values[j].value.string;
650 temp = (fc_writer_t *) realloc (plugin_list, (plugin_list_len + 2)
651 * (sizeof (*plugin_list)));
654 ERROR ("fc_bit_write_create: realloc failed.");
659 plugin_list[plugin_list_len].plugin = fc_strdup (plugin);
660 if (plugin_list[plugin_list_len].plugin == NULL)
662 ERROR ("fc_bit_write_create: fc_strdup failed.");
665 C_COMPLAIN_INIT (&plugin_list[plugin_list_len].complaint);
667 plugin_list[plugin_list_len].plugin = NULL;
668 } /* for (j = 0; j < child->values_num; j++) */
669 } /* for (i = 0; i < ci->children_num; i++) */
671 *user_data = plugin_list;
674 } /* }}} int fc_bit_write_create */
676 static int fc_bit_write_destroy (void **user_data) /* {{{ */
678 fc_writer_t *plugin_list;
681 if ((user_data == NULL) || (*user_data == NULL))
684 plugin_list = *user_data;
686 for (i = 0; plugin_list[i].plugin != NULL; i++)
687 free (plugin_list[i].plugin);
691 } /* }}} int fc_bit_write_destroy */
693 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
694 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
697 fc_writer_t *plugin_list;
701 if (user_data != NULL)
702 plugin_list = *user_data;
704 if ((plugin_list == NULL) || (plugin_list[0].plugin == NULL))
706 static c_complain_t write_complaint = C_COMPLAIN_INIT_STATIC;
708 status = plugin_write (/* plugin = */ NULL, ds, vl);
709 if (status == ENOENT)
711 /* in most cases this is a permanent error, so use the complain
712 * mechanism rather than spamming the logs */
713 c_complain (LOG_INFO, &write_complaint,
714 "Filter subsystem: Built-in target `write': Dispatching value to "
715 "all write plugins failed with status %i (ENOENT). "
716 "Most likely this means you didn't load any write plugins.",
719 else if (status != 0)
721 /* often, this is a permanent error (e.g. target system unavailable),
722 * so use the complain mechanism rather than spamming the logs */
723 c_complain (LOG_INFO, &write_complaint,
724 "Filter subsystem: Built-in target `write': Dispatching value to "
725 "all write plugins failed with status %i.", status);
729 assert (status == 0);
730 c_release (LOG_INFO, &write_complaint, "Filter subsystem: "
731 "Built-in target `write': Some write plugin is back to normal "
732 "operation. `write' succeeded.");
739 for (i = 0; plugin_list[i].plugin != NULL; i++)
741 status = plugin_write (plugin_list[i].plugin, ds, vl);
744 c_complain (LOG_INFO, &plugin_list[i].complaint,
745 "Filter subsystem: Built-in target `write': Dispatching value to "
746 "the `%s' plugin failed with status %i.",
747 plugin_list[i].plugin, status);
751 c_release (LOG_INFO, &plugin_list[i].complaint,
752 "Filter subsystem: Built-in target `write': Plugin `%s' is back "
753 "to normal operation. `write' succeeded.", plugin_list[i].plugin);
755 } /* for (i = 0; plugin_list[i] != NULL; i++) */
758 return (FC_TARGET_CONTINUE);
759 } /* }}} int fc_bit_write_invoke */
761 static int fc_init_once (void) /* {{{ */
769 memset (&tproc, 0, sizeof (tproc));
770 tproc.create = fc_bit_jump_create;
771 tproc.destroy = fc_bit_jump_destroy;
772 tproc.invoke = fc_bit_jump_invoke;
773 fc_register_target ("jump", tproc);
775 memset (&tproc, 0, sizeof (tproc));
777 tproc.destroy = NULL;
778 tproc.invoke = fc_bit_stop_invoke;
779 fc_register_target ("stop", tproc);
781 memset (&tproc, 0, sizeof (tproc));
783 tproc.destroy = NULL;
784 tproc.invoke = fc_bit_return_invoke;
785 fc_register_target ("return", tproc);
787 memset (&tproc, 0, sizeof (tproc));
788 tproc.create = fc_bit_write_create;
789 tproc.destroy = fc_bit_write_destroy;
790 tproc.invoke = fc_bit_write_invoke;
791 fc_register_target ("write", tproc);
795 } /* }}} int fc_init_once */
800 /* Add a match to list of available matches. */
801 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
805 DEBUG ("fc_register_match (%s);", name);
807 m = (fc_match_t *) malloc (sizeof (*m));
810 memset (m, 0, sizeof (*m));
812 sstrncpy (m->name, name, sizeof (m->name));
813 memcpy (&m->proc, &proc, sizeof (m->proc));
816 if (match_list_head == NULL)
824 ptr = match_list_head;
825 while (ptr->next != NULL)
832 } /* }}} int fc_register_match */
834 /* Add a target to list of available targets. */
835 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
839 DEBUG ("fc_register_target (%s);", name);
841 t = (fc_target_t *) malloc (sizeof (*t));
844 memset (t, 0, sizeof (*t));
846 sstrncpy (t->name, name, sizeof (t->name));
847 memcpy (&t->proc, &proc, sizeof (t->proc));
850 if (target_list_head == NULL)
852 target_list_head = t;
858 ptr = target_list_head;
859 while (ptr->next != NULL)
866 } /* }}} int fc_register_target */
868 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
872 if (chain_name == NULL)
875 for (chain = chain_list_head; chain != NULL; chain = chain->next)
876 if (strcasecmp (chain_name, chain->name) == 0)
880 } /* }}} int fc_chain_get_by_name */
882 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
892 DEBUG ("fc_process_chain (chain = %s);", chain->name);
894 status = FC_TARGET_CONTINUE;
895 for (rule = chain->rules; rule != NULL; rule = rule->next)
899 if (rule->name[0] != 0)
901 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
902 chain->name, rule->name);
905 /* N. B.: rule->matches may be NULL. */
906 for (match = rule->matches; match != NULL; match = match->next)
908 /* FIXME: Pass the meta-data to match targets here (when implemented). */
909 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
913 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
916 else if (status != FC_MATCH_MATCHES)
920 /* for-loop has been aborted: Either error or no match. */
923 status = FC_TARGET_CONTINUE;
927 if (rule->name[0] != 0)
929 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
930 chain->name, rule->name);
933 for (target = rule->targets; target != NULL; target = target->next)
935 /* If we get here, all matches have matched the value. Execute the
937 /* FIXME: Pass the meta-data to match targets here (when implemented). */
938 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
942 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
945 else if (status == FC_TARGET_CONTINUE)
947 else if (status == FC_TARGET_STOP)
949 else if (status == FC_TARGET_RETURN)
953 WARNING ("fc_process_chain (%s): Unknown return value "
954 "from target `%s': %i",
955 chain->name, target->name, status);
959 if ((status == FC_TARGET_STOP)
960 || (status == FC_TARGET_RETURN))
962 if (rule->name[0] != 0)
964 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
966 chain->name, rule->name,
967 (status == FC_TARGET_STOP) ? "stop" : "return");
973 status = FC_TARGET_CONTINUE;
977 if (status == FC_TARGET_STOP)
978 return (FC_TARGET_STOP);
979 else if (status == FC_TARGET_RETURN)
980 return (FC_TARGET_CONTINUE);
982 /* for-loop has been aborted: A target returned `FC_TARGET_STOP' */
984 return (FC_TARGET_CONTINUE);
986 DEBUG ("fc_process_chain (%s): Executing the default targets.",
989 status = FC_TARGET_CONTINUE;
990 for (target = chain->targets; target != NULL; target = target->next)
992 /* If we get here, all matches have matched the value. Execute the
994 /* FIXME: Pass the meta-data to match targets here (when implemented). */
995 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
999 WARNING ("fc_process_chain (%s): The default target failed.",
1002 else if (status == FC_TARGET_CONTINUE)
1004 else if (status == FC_TARGET_STOP)
1006 else if (status == FC_TARGET_RETURN)
1010 WARNING ("fc_process_chain (%s): Unknown return value "
1011 "from target `%s': %i",
1012 chain->name, target->name, status);
1016 if ((status == FC_TARGET_STOP)
1017 || (status == FC_TARGET_RETURN))
1019 assert (target != NULL);
1020 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1021 "the %s condition.",
1022 chain->name, target->name,
1023 (status == FC_TARGET_STOP) ? "stop" : "return");
1024 if (status == FC_TARGET_STOP)
1025 return (FC_TARGET_STOP);
1027 return (FC_TARGET_CONTINUE);
1030 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1033 return (FC_TARGET_CONTINUE);
1034 } /* }}} int fc_process_chain */
1036 /* Iterate over all rules in the chain and execute all targets for which all
1038 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1040 /* FIXME: Pass the meta-data to match targets here (when implemented). */
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 : */