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"
26 #include "filter_chain.h"
31 /* List of matches, used in fc_rule_t and for the global `match_list_head'
34 typedef struct fc_match_s fc_match_t; /* {{{ */
37 char name[DATA_MAX_NAME_LEN];
43 /* List of targets, used in fc_rule_t and for the global `target_list_head'
46 typedef struct fc_target_s fc_target_t; /* {{{ */
49 char name[DATA_MAX_NAME_LEN];
55 /* List of rules, used in fc_chain_t */
57 typedef struct fc_rule_s fc_rule_t; /* {{{ */
60 char name[DATA_MAX_NAME_LEN];
66 /* List of chains, used for `chain_list_head' */
67 struct fc_chain_s /* {{{ */
69 char name[DATA_MAX_NAME_LEN];
78 static fc_match_t *match_list_head;
79 static fc_target_t *target_list_head;
80 static fc_chain_t *chain_list_head;
85 static void fc_free_matches (fc_match_t *m) /* {{{ */
90 if (m->proc.destroy != NULL)
91 (*m->proc.destroy) (&m->user_data);
92 else if (m->user_data != NULL)
94 ERROR ("Filter sybsystem: fc_free_matches: There is user data, but no "
95 "destroy functions has been specified. "
96 "Memory will probably be lost!");
100 fc_free_matches (m->next);
103 } /* }}} void fc_free_matches */
105 static void fc_free_targets (fc_target_t *t) /* {{{ */
110 if (t->proc.destroy != NULL)
111 (*t->proc.destroy) (&t->user_data);
112 else if (t->user_data != NULL)
114 ERROR ("Filter sybsystem: fc_free_targets: There is user data, but no "
115 "destroy functions has been specified. "
116 "Memory will probably be lost!");
120 fc_free_targets (t->next);
123 } /* }}} void fc_free_targets */
125 static void fc_free_rules (fc_rule_t *r) /* {{{ */
130 fc_free_matches (r->matches);
131 fc_free_targets (r->targets);
134 fc_free_rules (r->next);
137 } /* }}} void fc_free_rules */
139 static void fc_free_chains (fc_chain_t *c) /* {{{ */
144 fc_free_rules (c->rules);
145 fc_free_targets (c->targets);
148 fc_free_chains (c->next);
151 } /* }}} void fc_free_chains */
153 static char *fc_strdup (const char *orig) /* {{{ */
161 sz = strlen (orig) + 1;
162 dest = (char *) malloc (sz);
166 memcpy (dest, orig, sz);
169 } /* }}} char *fc_strdup */
174 * The configuration looks somewhat like this:
180 * Type "^mysql_command$"
181 * TypeInstance "^show_"
192 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
199 if ((ci->values_num != 1)
200 || (ci->values[0].type != OCONFIG_TYPE_STRING))
202 WARNING ("Filter subsystem: `Match' blocks require "
203 "exactly one string argument.");
207 ptr = match_list_head;
210 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
217 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
218 "Did you load the appropriate plugin?",
219 ci->values[0].value.string);
223 m = (fc_match_t *) malloc (sizeof (*m));
226 ERROR ("fc_config_add_match: malloc failed.");
229 memset (m, 0, sizeof (*m));
231 sstrncpy (m->name, ptr->name, sizeof (m->name));
232 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
236 if (m->proc.create != NULL)
238 status = (*m->proc.create) (ci, &m->user_data);
241 WARNING ("Filter subsystem: Failed to create a %s match.",
248 if (*matches_head != NULL)
251 while (ptr->next != NULL)
262 } /* }}} int fc_config_add_match */
264 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
271 if ((ci->values_num != 1)
272 || (ci->values[0].type != OCONFIG_TYPE_STRING))
274 WARNING ("Filter subsystem: `Target' blocks require "
275 "exactly one string argument.");
279 ptr = target_list_head;
282 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
289 WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
290 "Did you load the appropriate plugin?",
291 ci->values[0].value.string);
295 t = (fc_target_t *) malloc (sizeof (*t));
298 ERROR ("fc_config_add_target: malloc failed.");
301 memset (t, 0, sizeof (*t));
303 sstrncpy (t->name, ptr->name, sizeof (t->name));
304 memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
308 if (t->proc.create != NULL)
310 status = (*t->proc.create) (ci, &t->user_data);
313 WARNING ("Filter subsystem: Failed to create a %s target.",
324 if (*targets_head != NULL)
327 while (ptr->next != NULL)
338 } /* }}} int fc_config_add_target */
340 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
344 char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
348 if (ci->values_num > 1)
350 WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
353 else if ((ci->values_num == 1)
354 && (ci->values[0].type != OCONFIG_TYPE_STRING))
356 WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
357 "or no argument at all.");
361 rule = (fc_rule_t *) malloc (sizeof (*rule));
364 ERROR ("fc_config_add_rule: malloc failed.");
367 memset (rule, 0, sizeof (*rule));
370 if (ci->values_num == 1)
372 sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
373 ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
374 ci->values[0].value.string);
377 for (i = 0; i < ci->children_num; i++)
379 oconfig_item_t *option = ci->children + i;
382 if (strcasecmp ("Match", option->key) == 0)
383 status = fc_config_add_match (&rule->matches, option);
384 else if (strcasecmp ("Target", option->key) == 0)
385 status = fc_config_add_target (&rule->targets, option);
388 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
389 "inside a <Rule> block.", rule_name, option->key);
395 } /* for (ci->children) */
397 /* Additional sanity checking. */
400 if (rule->targets == NULL)
402 WARNING ("Filter subsystem: %s: No target has been specified.",
409 } /* while (status == 0) */
413 fc_free_rules (rule);
417 if (chain->rules != NULL)
422 while (ptr->next != NULL)
433 } /* }}} int fc_config_add_rule */
435 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
441 if ((ci->values_num != 1)
442 || (ci->values[0].type != OCONFIG_TYPE_STRING))
444 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
449 chain = (fc_chain_t *) malloc (sizeof (*chain));
452 ERROR ("fc_config_add_chain: malloc failed.");
455 memset (chain, 0, sizeof (*chain));
456 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
458 chain->targets = NULL;
461 for (i = 0; i < ci->children_num; i++)
463 oconfig_item_t *option = ci->children + i;
466 if (strcasecmp ("Rule", option->key) == 0)
467 status = fc_config_add_rule (chain, option);
468 else if (strcasecmp ("Target", option->key) == 0)
469 status = fc_config_add_target (&chain->targets, option);
472 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
473 "inside a <Chain> block.", chain->name, option->key);
479 } /* for (ci->children) */
483 fc_free_chains (chain);
487 if (chain_list_head != NULL)
491 ptr = chain_list_head;
492 while (ptr->next != NULL)
499 chain_list_head = chain;
503 } /* }}} int fc_config_add_chain */
506 * Built-in target "jump"
508 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
510 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
513 oconfig_item_t *ci_chain;
515 if (ci->children_num != 1)
517 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
518 "one `Chain' argument!");
522 ci_chain = ci->children;
523 if (strcasecmp ("Chain", ci_chain->key) != 0)
525 ERROR ("Filter subsystem: The built-in target `jump' does not "
526 "support the configuration option `%s'.",
531 if ((ci_chain->values_num != 1)
532 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
534 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
535 "needs exactly one string argument.");
539 *user_data = fc_strdup (ci_chain->values[0].value.string);
540 if (*user_data == NULL)
542 ERROR ("fc_bit_jump_create: fc_strdup failed.");
547 } /* }}} int fc_bit_jump_create */
549 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
551 if (user_data != NULL)
558 } /* }}} int fc_bit_jump_destroy */
560 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
561 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
568 chain_name = *user_data;
570 for (chain = chain_list_head; chain != NULL; chain = chain->next)
571 if (strcasecmp (chain_name, chain->name) == 0)
576 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
577 "named `%s'.", chain_name);
581 status = fc_process_chain (ds, vl, chain);
584 else if (status == FC_TARGET_STOP)
585 return (FC_TARGET_STOP);
587 return (FC_TARGET_CONTINUE);
588 } /* }}} int fc_bit_jump_invoke */
590 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
591 value_list_t __attribute__((unused)) *vl,
592 notification_meta_t __attribute__((unused)) **meta,
593 void __attribute__((unused)) **user_data)
595 return (FC_TARGET_STOP);
596 } /* }}} int fc_bit_stop_invoke */
598 static int fc_bit_return_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_RETURN);
604 } /* }}} int fc_bit_return_invoke */
606 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
612 size_t plugin_list_len;
617 for (i = 0; i < ci->children_num; i++)
619 oconfig_item_t *child = ci->children + i;
623 if (strcasecmp ("Plugin", child->key) != 0)
625 ERROR ("Filter subsystem: The built-in target `write' does not "
626 "support the configuration option `%s'.",
631 for (j = 0; j < child->values_num; j++)
633 if (child->values[j].type != OCONFIG_TYPE_STRING)
635 ERROR ("Filter subsystem: Built-in target `write': "
636 "The `Plugin' option accepts only string arguments.");
640 temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
641 * (sizeof (*plugin_list)));
644 ERROR ("fc_bit_write_create: realloc failed.");
649 plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
650 if (plugin_list[plugin_list_len] == NULL)
652 ERROR ("fc_bit_write_create: fc_strdup failed.");
656 plugin_list[plugin_list_len] = NULL;
657 } /* for (j = 0; j < child->values_num; j++) */
658 } /* for (i = 0; i < ci->children_num; i++) */
660 *user_data = plugin_list;
663 } /* }}} int fc_bit_write_create */
665 static int fc_bit_write_destroy (void **user_data) /* {{{ */
670 if ((user_data == NULL) || (*user_data == NULL))
673 plugin_list = *user_data;
675 for (i = 0; plugin_list[i] != NULL; i++)
676 free (plugin_list[i]);
680 } /* }}} int fc_bit_write_destroy */
682 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
683 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
690 if (user_data != NULL)
691 plugin_list = *user_data;
693 if ((plugin_list == NULL) || (plugin_list[0] == NULL))
695 status = plugin_write (/* plugin = */ NULL, ds, vl);
696 if (status == ENOENT)
698 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
699 "all write plugins failed with status %i (ENOENT). "
700 "Most likely this means you didn't load any write plugins.",
703 else if (status != 0)
705 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
706 "all write plugins failed with status %i.", status);
713 for (i = 0; plugin_list[i] != NULL; i++)
715 status = plugin_write (plugin_list[i], ds, vl);
718 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
719 "the `%s' plugin failed with status %i.", plugin_list[i], status);
721 } /* for (i = 0; plugin_list[i] != NULL; i++) */
724 return (FC_TARGET_CONTINUE);
725 } /* }}} int fc_bit_write_invoke */
727 static int fc_init_once (void) /* {{{ */
735 memset (&tproc, 0, sizeof (tproc));
736 tproc.create = fc_bit_jump_create;
737 tproc.destroy = fc_bit_jump_destroy;
738 tproc.invoke = fc_bit_jump_invoke;
739 fc_register_target ("jump", tproc);
741 memset (&tproc, 0, sizeof (tproc));
743 tproc.destroy = NULL;
744 tproc.invoke = fc_bit_stop_invoke;
745 fc_register_target ("stop", tproc);
747 memset (&tproc, 0, sizeof (tproc));
749 tproc.destroy = NULL;
750 tproc.invoke = fc_bit_return_invoke;
751 fc_register_target ("return", tproc);
753 memset (&tproc, 0, sizeof (tproc));
754 tproc.create = fc_bit_write_create;
755 tproc.destroy = fc_bit_write_destroy;
756 tproc.invoke = fc_bit_write_invoke;
757 fc_register_target ("write", tproc);
761 } /* }}} int fc_init_once */
766 /* Add a match to list of available matches. */
767 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
771 DEBUG ("fc_register_match (%s);", name);
773 m = (fc_match_t *) malloc (sizeof (*m));
776 memset (m, 0, sizeof (*m));
778 sstrncpy (m->name, name, sizeof (m->name));
779 memcpy (&m->proc, &proc, sizeof (m->proc));
782 if (match_list_head == NULL)
790 ptr = match_list_head;
791 while (ptr->next != NULL)
798 } /* }}} int fc_register_match */
800 /* Add a target to list of available targets. */
801 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
805 DEBUG ("fc_register_target (%s);", name);
807 t = (fc_target_t *) malloc (sizeof (*t));
810 memset (t, 0, sizeof (*t));
812 sstrncpy (t->name, name, sizeof (t->name));
813 memcpy (&t->proc, &proc, sizeof (t->proc));
816 if (target_list_head == NULL)
818 target_list_head = t;
824 ptr = target_list_head;
825 while (ptr->next != NULL)
832 } /* }}} int fc_register_target */
834 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
838 if (chain_name == NULL)
841 for (chain = chain_list_head; chain != NULL; chain = chain->next)
842 if (strcasecmp (chain_name, chain->name) == 0)
846 } /* }}} int fc_chain_get_by_name */
848 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
858 DEBUG ("fc_process_chain (chain = %s);", chain->name);
860 status = FC_TARGET_CONTINUE;
861 for (rule = chain->rules; rule != NULL; rule = rule->next)
865 if (rule->name[0] != 0)
867 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
868 chain->name, rule->name);
871 /* N. B.: rule->matches may be NULL. */
872 for (match = rule->matches; match != NULL; match = match->next)
874 /* FIXME: Pass the meta-data to match targets here (when implemented). */
875 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
879 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
882 else if (status != FC_MATCH_MATCHES)
886 /* for-loop has been aborted: Either error or no match. */
889 status = FC_TARGET_CONTINUE;
893 if (rule->name[0] != 0)
895 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
896 chain->name, rule->name);
899 for (target = rule->targets; target != NULL; target = target->next)
901 /* If we get here, all matches have matched the value. Execute the
903 /* FIXME: Pass the meta-data to match targets here (when implemented). */
904 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
908 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
911 else if (status == FC_TARGET_CONTINUE)
913 else if (status == FC_TARGET_STOP)
915 else if (status == FC_TARGET_RETURN)
919 WARNING ("fc_process_chain (%s): Unknown return value "
920 "from target `%s': %i",
921 chain->name, target->name, status);
925 if ((status == FC_TARGET_STOP)
926 || (status == FC_TARGET_RETURN))
928 if (rule->name[0] != 0)
930 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
932 chain->name, rule->name,
933 (status == FC_TARGET_STOP) ? "stop" : "return");
939 status = FC_TARGET_CONTINUE;
943 if (status == FC_TARGET_STOP)
944 return (FC_TARGET_STOP);
945 else if (status == FC_TARGET_RETURN)
946 return (FC_TARGET_CONTINUE);
948 /* for-loop has been aborted: A target returned `FC_TARGET_STOP' */
950 return (FC_TARGET_CONTINUE);
952 DEBUG ("fc_process_chain (%s): Executing the default targets.",
955 status = FC_TARGET_CONTINUE;
956 for (target = chain->targets; target != NULL; target = target->next)
958 /* If we get here, all matches have matched the value. Execute the
960 /* FIXME: Pass the meta-data to match targets here (when implemented). */
961 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
965 WARNING ("fc_process_chain (%s): The default target failed.",
968 else if (status == FC_TARGET_CONTINUE)
970 else if (status == FC_TARGET_STOP)
972 else if (status == FC_TARGET_RETURN)
976 WARNING ("fc_process_chain (%s): Unknown return value "
977 "from target `%s': %i",
978 chain->name, target->name, status);
982 if ((status == FC_TARGET_STOP)
983 || (status == FC_TARGET_RETURN))
985 assert (target != NULL);
986 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
988 chain->name, target->name,
989 (status == FC_TARGET_STOP) ? "stop" : "return");
990 if (status == FC_TARGET_STOP)
991 return (FC_TARGET_STOP);
993 return (FC_TARGET_CONTINUE);
996 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
999 return (FC_TARGET_CONTINUE);
1000 } /* }}} int fc_process_chain */
1002 /* Iterate over all rules in the chain and execute all targets for which all
1004 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1006 /* FIXME: Pass the meta-data to match targets here (when implemented). */
1007 return (fc_bit_write_invoke (ds, vl,
1008 /* meta = */ NULL, /* user_data = */ NULL));
1009 } /* }}} int fc_default_action */
1011 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1018 if (strcasecmp ("Chain", ci->key) == 0)
1019 return (fc_config_add_chain (ci));
1021 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1025 } /* }}} int fc_configure */
1027 /* vim: set sw=2 sts=2 et fdm=marker : */