2 * collectd - src/filter_chain.c
3 * Copyright (C) 2008-2010 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
29 #include "configfile.h"
31 #include "utils_complain.h"
33 #include "filter_chain.h"
38 /* List of matches, used in fc_rule_t and for the global `match_list_head'
41 typedef struct fc_match_s fc_match_t; /* {{{ */
44 char name[DATA_MAX_NAME_LEN];
50 /* List of targets, used in fc_rule_t and for the global `target_list_head'
53 typedef struct fc_target_s fc_target_t; /* {{{ */
56 char name[DATA_MAX_NAME_LEN];
62 /* List of rules, used in fc_chain_t */
64 typedef struct fc_rule_s fc_rule_t; /* {{{ */
67 char name[DATA_MAX_NAME_LEN];
73 /* List of chains, used for `chain_list_head' */
74 struct fc_chain_s /* {{{ */
76 char name[DATA_MAX_NAME_LEN];
82 /* Writer configuration. */
84 typedef struct fc_writer_s fc_writer_t; /* {{{ */
88 c_complain_t complaint;
94 static fc_match_t *match_list_head;
95 static fc_target_t *target_list_head;
96 static fc_chain_t *chain_list_head;
101 static void fc_free_matches (fc_match_t *m) /* {{{ */
106 if (m->proc.destroy != NULL)
107 (*m->proc.destroy) (&m->user_data);
108 else if (m->user_data != NULL)
110 ERROR ("Filter subsystem: fc_free_matches: There is user data, but no "
111 "destroy functions has been specified. "
112 "Memory will probably be lost!");
116 fc_free_matches (m->next);
119 } /* }}} void fc_free_matches */
121 static void fc_free_targets (fc_target_t *t) /* {{{ */
126 if (t->proc.destroy != NULL)
127 (*t->proc.destroy) (&t->user_data);
128 else if (t->user_data != NULL)
130 ERROR ("Filter subsystem: fc_free_targets: There is user data, but no "
131 "destroy functions has been specified. "
132 "Memory will probably be lost!");
136 fc_free_targets (t->next);
139 } /* }}} void fc_free_targets */
141 static void fc_free_rules (fc_rule_t *r) /* {{{ */
146 fc_free_matches (r->matches);
147 fc_free_targets (r->targets);
150 fc_free_rules (r->next);
153 } /* }}} void fc_free_rules */
155 static void fc_free_chains (fc_chain_t *c) /* {{{ */
160 fc_free_rules (c->rules);
161 fc_free_targets (c->targets);
164 fc_free_chains (c->next);
167 } /* }}} void fc_free_chains */
169 static char *fc_strdup (const char *orig) /* {{{ */
177 sz = strlen (orig) + 1;
182 memcpy (dest, orig, sz);
185 } /* }}} char *fc_strdup */
190 * The configuration looks somewhat like this:
196 * Type "^mysql_command$"
197 * TypeInstance "^show_"
208 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
215 if ((ci->values_num != 1)
216 || (ci->values[0].type != OCONFIG_TYPE_STRING))
218 WARNING ("Filter subsystem: `Match' blocks require "
219 "exactly one string argument.");
223 ptr = match_list_head;
226 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
233 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
234 "Did you load the appropriate plugin?",
235 ci->values[0].value.string);
239 m = calloc (1, sizeof (*m));
242 ERROR ("fc_config_add_match: calloc failed.");
246 sstrncpy (m->name, ptr->name, sizeof (m->name));
247 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
251 if (m->proc.create != NULL)
253 status = (*m->proc.create) (ci, &m->user_data);
256 WARNING ("Filter subsystem: Failed to create a %s match.",
263 if (*matches_head != NULL)
266 while (ptr->next != NULL)
277 } /* }}} int fc_config_add_match */
279 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
286 if ((ci->values_num != 1)
287 || (ci->values[0].type != OCONFIG_TYPE_STRING))
289 WARNING ("Filter subsystem: `Target' blocks require "
290 "exactly one string argument.");
294 ptr = target_list_head;
297 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
304 WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
305 "Did you load the appropriate plugin?",
306 ci->values[0].value.string);
310 t = calloc (1, sizeof (*t));
313 ERROR ("fc_config_add_target: calloc failed.");
317 sstrncpy (t->name, ptr->name, sizeof (t->name));
318 memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
322 if (t->proc.create != NULL)
324 status = (*t->proc.create) (ci, &t->user_data);
327 WARNING ("Filter subsystem: Failed to create a %s target.",
338 if (*targets_head != NULL)
341 while (ptr->next != NULL)
352 } /* }}} int fc_config_add_target */
354 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
358 char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
362 if (ci->values_num > 1)
364 WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
367 else if ((ci->values_num == 1)
368 && (ci->values[0].type != OCONFIG_TYPE_STRING))
370 WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
371 "or no argument at all.");
375 rule = calloc (1, sizeof (*rule));
378 ERROR ("fc_config_add_rule: calloc failed.");
382 if (ci->values_num == 1)
384 sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
385 ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
386 ci->values[0].value.string);
389 for (i = 0; i < ci->children_num; i++)
391 oconfig_item_t *option = ci->children + i;
393 if (strcasecmp ("Match", option->key) == 0)
394 status = fc_config_add_match (&rule->matches, option);
395 else if (strcasecmp ("Target", option->key) == 0)
396 status = fc_config_add_target (&rule->targets, option);
399 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
400 "inside a <Rule> block.", rule_name, option->key);
406 } /* for (ci->children) */
408 /* Additional sanity checking. */
411 if (rule->targets == NULL)
413 WARNING ("Filter subsystem: %s: No target has been specified.",
420 } /* while (status == 0) */
424 fc_free_rules (rule);
428 if (chain->rules != NULL)
433 while (ptr->next != NULL)
444 } /* }}} int fc_config_add_rule */
446 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
448 fc_chain_t *chain = NULL;
453 if ((ci->values_num != 1)
454 || (ci->values[0].type != OCONFIG_TYPE_STRING))
456 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
461 if (chain_list_head != NULL)
463 if ((chain = fc_chain_get_by_name (ci->values[0].value.string)) != NULL)
469 chain = calloc (1, sizeof (*chain));
472 ERROR ("fc_config_add_chain: calloc failed.");
475 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
478 for (i = 0; i < ci->children_num; i++)
480 oconfig_item_t *option = ci->children + i;
482 if (strcasecmp ("Rule", option->key) == 0)
483 status = fc_config_add_rule (chain, option);
484 else if (strcasecmp ("Target", option->key) == 0)
485 status = fc_config_add_target (&chain->targets, option);
488 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
489 "inside a <Chain> block.", chain->name, option->key);
495 } /* for (ci->children) */
499 fc_free_chains (chain);
503 if (chain_list_head != NULL)
510 ptr = chain_list_head;
511 while (ptr->next != NULL)
518 chain_list_head = chain;
522 } /* }}} int fc_config_add_chain */
525 * Built-in target "jump"
527 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
529 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
532 oconfig_item_t *ci_chain;
534 if (ci->children_num != 1)
536 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
537 "one `Chain' argument!");
541 ci_chain = ci->children;
542 if (strcasecmp ("Chain", ci_chain->key) != 0)
544 ERROR ("Filter subsystem: The built-in target `jump' does not "
545 "support the configuration option `%s'.",
550 if ((ci_chain->values_num != 1)
551 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
553 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
554 "needs exactly one string argument.");
558 *user_data = fc_strdup (ci_chain->values[0].value.string);
559 if (*user_data == NULL)
561 ERROR ("fc_bit_jump_create: fc_strdup failed.");
566 } /* }}} int fc_bit_jump_create */
568 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
570 if (user_data != NULL)
577 } /* }}} int fc_bit_jump_destroy */
579 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
580 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
587 chain_name = *user_data;
589 for (chain = chain_list_head; chain != NULL; chain = chain->next)
590 if (strcasecmp (chain_name, chain->name) == 0)
595 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
596 "named `%s'.", chain_name);
600 status = fc_process_chain (ds, vl, chain);
603 else if (status == FC_TARGET_STOP)
604 return (FC_TARGET_STOP);
606 return (FC_TARGET_CONTINUE);
607 } /* }}} int fc_bit_jump_invoke */
609 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
610 value_list_t __attribute__((unused)) *vl,
611 notification_meta_t __attribute__((unused)) **meta,
612 void __attribute__((unused)) **user_data)
614 return (FC_TARGET_STOP);
615 } /* }}} int fc_bit_stop_invoke */
617 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
618 value_list_t __attribute__((unused)) *vl,
619 notification_meta_t __attribute__((unused)) **meta,
620 void __attribute__((unused)) **user_data)
622 return (FC_TARGET_RETURN);
623 } /* }}} int fc_bit_return_invoke */
625 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
630 fc_writer_t *plugin_list = NULL;
631 size_t plugin_list_len = 0;
633 for (i = 0; i < ci->children_num; i++)
635 oconfig_item_t *child = ci->children + i;
639 if (strcasecmp ("Plugin", child->key) != 0)
641 ERROR ("Filter subsystem: The built-in target `write' does not "
642 "support the configuration option `%s'.",
647 for (j = 0; j < child->values_num; j++)
651 if (child->values[j].type != OCONFIG_TYPE_STRING)
653 ERROR ("Filter subsystem: Built-in target `write': "
654 "The `Plugin' option accepts only string arguments.");
657 plugin = child->values[j].value.string;
659 temp = realloc (plugin_list, (plugin_list_len + 2)
660 * (sizeof (*plugin_list)));
663 ERROR ("fc_bit_write_create: realloc failed.");
668 plugin_list[plugin_list_len].plugin = fc_strdup (plugin);
669 if (plugin_list[plugin_list_len].plugin == NULL)
671 ERROR ("fc_bit_write_create: fc_strdup failed.");
674 C_COMPLAIN_INIT (&plugin_list[plugin_list_len].complaint);
676 plugin_list[plugin_list_len].plugin = NULL;
677 } /* for (j = 0; j < child->values_num; j++) */
678 } /* for (i = 0; i < ci->children_num; i++) */
680 *user_data = plugin_list;
683 } /* }}} int fc_bit_write_create */
685 static int fc_bit_write_destroy (void **user_data) /* {{{ */
687 fc_writer_t *plugin_list;
690 if ((user_data == NULL) || (*user_data == NULL))
693 plugin_list = *user_data;
695 for (i = 0; plugin_list[i].plugin != NULL; i++)
696 free (plugin_list[i].plugin);
700 } /* }}} int fc_bit_write_destroy */
702 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
703 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
706 fc_writer_t *plugin_list;
710 if (user_data != NULL)
711 plugin_list = *user_data;
713 if ((plugin_list == NULL) || (plugin_list[0].plugin == NULL))
715 static c_complain_t write_complaint = C_COMPLAIN_INIT_STATIC;
717 status = plugin_write (/* plugin = */ NULL, ds, vl);
718 if (status == ENOENT)
720 /* in most cases this is a permanent error, so use the complain
721 * mechanism rather than spamming the logs */
722 c_complain (LOG_INFO, &write_complaint,
723 "Filter subsystem: Built-in target `write': Dispatching value to "
724 "all write plugins failed with status %i (ENOENT). "
725 "Most likely this means you didn't load any write plugins.",
728 plugin_log_available_writers ();
730 else if (status != 0)
732 /* often, this is a permanent error (e.g. target system unavailable),
733 * so use the complain mechanism rather than spamming the logs */
734 c_complain (LOG_INFO, &write_complaint,
735 "Filter subsystem: Built-in target `write': Dispatching value to "
736 "all write plugins failed with status %i.", status);
740 assert (status == 0);
741 c_release (LOG_INFO, &write_complaint, "Filter subsystem: "
742 "Built-in target `write': Some write plugin is back to normal "
743 "operation. `write' succeeded.");
750 for (i = 0; plugin_list[i].plugin != NULL; i++)
752 status = plugin_write (plugin_list[i].plugin, ds, vl);
755 c_complain (LOG_INFO, &plugin_list[i].complaint,
756 "Filter subsystem: Built-in target `write': Dispatching value to "
757 "the `%s' plugin failed with status %i.",
758 plugin_list[i].plugin, status);
760 plugin_log_available_writers ();
764 c_release (LOG_INFO, &plugin_list[i].complaint,
765 "Filter subsystem: Built-in target `write': Plugin `%s' is back "
766 "to normal operation. `write' succeeded.", plugin_list[i].plugin);
768 } /* for (i = 0; plugin_list[i] != NULL; i++) */
771 return (FC_TARGET_CONTINUE);
772 } /* }}} int fc_bit_write_invoke */
774 static int fc_init_once (void) /* {{{ */
777 target_proc_t tproc = { 0 };
782 tproc.create = fc_bit_jump_create;
783 tproc.destroy = fc_bit_jump_destroy;
784 tproc.invoke = fc_bit_jump_invoke;
785 fc_register_target ("jump", tproc);
787 memset (&tproc, 0, sizeof (tproc));
789 tproc.destroy = NULL;
790 tproc.invoke = fc_bit_stop_invoke;
791 fc_register_target ("stop", tproc);
793 memset (&tproc, 0, sizeof (tproc));
795 tproc.destroy = NULL;
796 tproc.invoke = fc_bit_return_invoke;
797 fc_register_target ("return", tproc);
799 memset (&tproc, 0, sizeof (tproc));
800 tproc.create = fc_bit_write_create;
801 tproc.destroy = fc_bit_write_destroy;
802 tproc.invoke = fc_bit_write_invoke;
803 fc_register_target ("write", tproc);
807 } /* }}} int fc_init_once */
812 /* Add a match to list of available matches. */
813 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
817 DEBUG ("fc_register_match (%s);", name);
819 m = calloc (1, sizeof (*m));
823 sstrncpy (m->name, name, sizeof (m->name));
824 memcpy (&m->proc, &proc, sizeof (m->proc));
826 if (match_list_head == NULL)
834 ptr = match_list_head;
835 while (ptr->next != NULL)
842 } /* }}} int fc_register_match */
844 /* Add a target to list of available targets. */
845 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
849 DEBUG ("fc_register_target (%s);", name);
851 t = calloc (1, sizeof (*t));
855 sstrncpy (t->name, name, sizeof (t->name));
856 memcpy (&t->proc, &proc, sizeof (t->proc));
858 if (target_list_head == NULL)
860 target_list_head = t;
866 ptr = target_list_head;
867 while (ptr->next != NULL)
874 } /* }}} int fc_register_target */
876 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
880 if (chain_name == NULL)
883 for (chain = chain_list_head; chain != NULL; chain = chain->next)
884 if (strcasecmp (chain_name, chain->name) == 0)
888 } /* }}} int fc_chain_get_by_name */
890 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
895 int status = FC_TARGET_CONTINUE;
900 DEBUG ("fc_process_chain (chain = %s);", chain->name);
902 for (rule = chain->rules; rule != NULL; rule = rule->next)
905 status = FC_TARGET_CONTINUE;
907 if (rule->name[0] != 0)
909 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
910 chain->name, rule->name);
913 /* N. B.: rule->matches may be NULL. */
914 for (match = rule->matches; match != NULL; match = match->next)
916 /* FIXME: Pass the meta-data to match targets here (when implemented). */
917 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
921 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
924 else if (status != FC_MATCH_MATCHES)
928 /* for-loop has been aborted: Either error or no match. */
931 status = FC_TARGET_CONTINUE;
935 if (rule->name[0] != 0)
937 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
938 chain->name, rule->name);
941 for (target = rule->targets; target != NULL; target = target->next)
943 /* If we get here, all matches have matched the value. Execute the
945 /* FIXME: Pass the meta-data to match targets here (when implemented). */
946 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
950 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
953 else if (status == FC_TARGET_CONTINUE)
955 else if (status == FC_TARGET_STOP)
957 else if (status == FC_TARGET_RETURN)
961 WARNING ("fc_process_chain (%s): Unknown return value "
962 "from target `%s': %i",
963 chain->name, target->name, status);
967 if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN))
969 if (rule->name[0] != 0)
971 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
973 chain->name, rule->name,
974 (status == FC_TARGET_STOP) ? "stop" : "return");
980 if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN))
983 DEBUG ("fc_process_chain (%s): Executing the default targets.",
986 status = FC_TARGET_CONTINUE;
987 for (target = chain->targets; target != NULL; target = target->next)
989 /* If we get here, all matches have matched the value. Execute the
991 /* FIXME: Pass the meta-data to match targets here (when implemented). */
992 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
996 WARNING ("fc_process_chain (%s): The default target failed.",
999 else if (status == FC_TARGET_CONTINUE)
1001 else if (status == FC_TARGET_STOP)
1003 else if (status == FC_TARGET_RETURN)
1007 WARNING ("fc_process_chain (%s): Unknown return value "
1008 "from target `%s': %i",
1009 chain->name, target->name, status);
1013 if ((status == FC_TARGET_STOP)
1014 || (status == FC_TARGET_RETURN))
1016 assert (target != NULL);
1017 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1018 "the %s condition.",
1019 chain->name, target->name,
1020 (status == FC_TARGET_STOP) ? "stop" : "return");
1021 if (status == FC_TARGET_STOP)
1022 return (FC_TARGET_STOP);
1024 return (FC_TARGET_CONTINUE);
1027 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1030 return (FC_TARGET_CONTINUE);
1031 } /* }}} int fc_process_chain */
1033 /* Iterate over all rules in the chain and execute all targets for which all
1035 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1037 /* FIXME: Pass the meta-data to match targets here (when implemented). */
1038 return (fc_bit_write_invoke (ds, vl,
1039 /* meta = */ NULL, /* user_data = */ NULL));
1040 } /* }}} int fc_default_action */
1042 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1049 if (strcasecmp ("Chain", ci->key) == 0)
1050 return (fc_config_add_chain (ci));
1052 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1056 } /* }}} int fc_configure */
1058 /* vim: set sw=2 sts=2 et fdm=marker : */