2 * collectd - src/utils_vl_lookup.c
3 * Copyright (C) 2012 Florian 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 Forster <octo at collectd.org>
32 #include "utils_vl_lookup.h"
33 #include "utils_avltree.h"
36 # define sstrncpy strncpy
37 # define plugin_log(s, ...) do { \
38 printf ("[severity %i] ", s); \
39 printf (__VA_ARGS__); \
49 char str[DATA_MAX_NAME_LEN];
53 typedef struct part_match_s part_match_t;
55 struct identifier_match_s
59 part_match_t plugin_instance;
61 part_match_t type_instance;
63 unsigned int group_by;
65 typedef struct identifier_match_s identifier_match_t;
69 c_avl_tree_t *by_type_tree;
71 lookup_class_callback_t cb_user_class;
72 lookup_obj_callback_t cb_user_obj;
73 lookup_free_class_callback_t cb_free_class;
74 lookup_free_obj_callback_t cb_free_obj;
78 typedef struct user_obj_s user_obj_t;
90 identifier_match_t match;
91 user_obj_t *user_obj_list; /* list of user_obj */
93 typedef struct user_class_s user_class_t;
95 struct user_class_list_s;
96 typedef struct user_class_list_s user_class_list_t;
97 struct user_class_list_s
100 user_class_list_t *next;
103 struct by_type_entry_s
105 c_avl_tree_t *by_plugin_tree; /* plugin -> user_class_list_t */
106 user_class_list_t *wildcard_plugin_list;
108 typedef struct by_type_entry_s by_type_entry_t;
113 static _Bool lu_part_matches (part_match_t const *match, /* {{{ */
118 /* Short cut popular catch-all regex. */
119 if (strcmp (".*", match->str) == 0)
122 int status = regexec (&match->regex, str,
123 /* nmatch = */ 0, /* pmatch = */ NULL,
130 else if (strcmp (match->str, str) == 0)
134 } /* }}} _Bool lu_part_matches */
136 static int lu_copy_ident_to_match_part (part_match_t *match_part, /* {{{ */
137 char const *ident_part)
139 size_t len = strlen (ident_part);
142 if ((len < 3) || (ident_part[0] != '/') || (ident_part[len - 1] != '/'))
144 sstrncpy (match_part->str, ident_part, sizeof (match_part->str));
145 match_part->is_regex = 0;
149 /* Copy string without the leading slash. */
150 sstrncpy (match_part->str, ident_part + 1, sizeof (match_part->str));
151 assert (sizeof (match_part->str) > len);
152 /* strip trailing slash */
153 match_part->str[len - 2] = 0;
155 status = regcomp (&match_part->regex, match_part->str,
156 /* flags = */ REG_EXTENDED);
160 regerror (status, &match_part->regex, errbuf, sizeof (errbuf));
161 ERROR ("utils_vl_lookup: Compiling regular expression \"%s\" failed: %s",
162 match_part->str, errbuf);
165 match_part->is_regex = 1;
168 } /* }}} int lu_copy_ident_to_match_part */
170 static int lu_copy_ident_to_match (identifier_match_t *match, /* {{{ */
171 identifier_t const *ident, unsigned int group_by)
173 memset (match, 0, sizeof (*match));
175 match->group_by = group_by;
177 #define COPY_FIELD(field) do { \
178 int status = lu_copy_ident_to_match_part (&match->field, ident->field); \
185 COPY_FIELD (plugin_instance);
187 COPY_FIELD (type_instance);
192 } /* }}} int lu_copy_ident_to_match */
194 static void *lu_create_user_obj (lookup_t *obj, /* {{{ */
195 data_set_t const *ds, value_list_t const *vl,
196 user_class_t *user_class)
198 user_obj_t *user_obj;
200 user_obj = malloc (sizeof (*user_obj));
201 if (user_obj == NULL)
203 ERROR ("utils_vl_lookup: malloc failed.");
206 memset (user_obj, 0, sizeof (*user_obj));
207 user_obj->next = NULL;
209 user_obj->user_obj = obj->cb_user_class (ds, vl, user_class->user_class);
210 if (user_obj->user_obj == NULL)
213 WARNING("utils_vl_lookup: User-provided constructor failed.");
217 #define COPY_FIELD(field, group_mask) do { \
218 if (user_class->match.field.is_regex \
219 && ((user_class->match.group_by & group_mask) == 0)) \
220 sstrncpy (user_obj->ident.field, "/.*/", sizeof (user_obj->ident.field)); \
222 sstrncpy (user_obj->ident.field, vl->field, sizeof (user_obj->ident.field)); \
225 COPY_FIELD (host, LU_GROUP_BY_HOST);
226 COPY_FIELD (plugin, LU_GROUP_BY_PLUGIN);
227 COPY_FIELD (plugin_instance, LU_GROUP_BY_PLUGIN_INSTANCE);
228 COPY_FIELD (type, 0);
229 COPY_FIELD (type_instance, LU_GROUP_BY_TYPE_INSTANCE);
233 if (user_class->user_obj_list == NULL)
235 user_class->user_obj_list = user_obj;
239 user_obj_t *last = user_class->user_obj_list;
240 while (last->next != NULL)
242 last->next = user_obj;
246 } /* }}} void *lu_create_user_obj */
248 static user_obj_t *lu_find_user_obj (user_class_t *user_class, /* {{{ */
249 value_list_t const *vl)
253 for (ptr = user_class->user_obj_list;
257 if (user_class->match.host.is_regex
258 && (user_class->match.group_by & LU_GROUP_BY_HOST)
259 && (strcmp (vl->host, ptr->ident.host) != 0))
261 if (user_class->match.plugin.is_regex
262 && (user_class->match.group_by & LU_GROUP_BY_PLUGIN)
263 && (strcmp (vl->plugin, ptr->ident.plugin) != 0))
265 if (user_class->match.plugin_instance.is_regex
266 && (user_class->match.group_by & LU_GROUP_BY_PLUGIN_INSTANCE)
267 && (strcmp (vl->plugin_instance, ptr->ident.plugin_instance) != 0))
269 if (user_class->match.type_instance.is_regex
270 && (user_class->match.group_by & LU_GROUP_BY_TYPE_INSTANCE)
271 && (strcmp (vl->type_instance, ptr->ident.type_instance) != 0))
278 } /* }}} user_obj_t *lu_find_user_obj */
280 static int lu_handle_user_class (lookup_t *obj, /* {{{ */
281 data_set_t const *ds, value_list_t const *vl,
282 user_class_t *user_class)
284 user_obj_t *user_obj;
287 assert (strcmp (vl->type, user_class->match.type.str) == 0);
288 assert (user_class->match.plugin.is_regex
289 || (strcmp (vl->plugin, user_class->match.plugin.str)) == 0);
291 if (!lu_part_matches (&user_class->match.type_instance, vl->type_instance)
292 || !lu_part_matches (&user_class->match.plugin_instance, vl->plugin_instance)
293 || !lu_part_matches (&user_class->match.plugin, vl->plugin)
294 || !lu_part_matches (&user_class->match.host, vl->host))
297 user_obj = lu_find_user_obj (user_class, vl);
298 if (user_obj == NULL)
300 /* call lookup_class_callback_t() and insert into the list of user objects. */
301 user_obj = lu_create_user_obj (obj, ds, vl, user_class);
302 if (user_obj == NULL)
306 status = obj->cb_user_obj (ds, vl,
307 user_class->user_class, user_obj->user_obj);
310 ERROR ("utils_vl_lookup: The user object callback failed with status %i.",
312 /* Returning a negative value means: abort! */
320 } /* }}} int lu_handle_user_class */
322 static int lu_handle_user_class_list (lookup_t *obj, /* {{{ */
323 data_set_t const *ds, value_list_t const *vl,
324 user_class_list_t *user_class_list)
326 user_class_list_t *ptr;
329 for (ptr = user_class_list; ptr != NULL; ptr = ptr->next)
333 status = lu_handle_user_class (obj, ds, vl, &ptr->entry);
336 else if (status == 0)
341 } /* }}} int lu_handle_user_class_list */
343 static by_type_entry_t *lu_search_by_type (lookup_t *obj, /* {{{ */
344 char const *type, _Bool allocate_if_missing)
346 by_type_entry_t *by_type;
350 status = c_avl_get (obj->by_type_tree, type, (void *) &by_type);
354 if (!allocate_if_missing)
357 type_copy = strdup (type);
358 if (type_copy == NULL)
360 ERROR ("utils_vl_lookup: strdup failed.");
364 by_type = malloc (sizeof (*by_type));
367 ERROR ("utils_vl_lookup: malloc failed.");
371 memset (by_type, 0, sizeof (*by_type));
372 by_type->wildcard_plugin_list = NULL;
374 by_type->by_plugin_tree = c_avl_create ((void *) strcmp);
375 if (by_type->by_plugin_tree == NULL)
377 ERROR ("utils_vl_lookup: c_avl_create failed.");
383 status = c_avl_insert (obj->by_type_tree,
384 /* key = */ type_copy, /* value = */ by_type);
385 assert (status <= 0); /* >0 => entry exists => race condition. */
388 ERROR ("utils_vl_lookup: c_avl_insert failed.");
389 c_avl_destroy (by_type->by_plugin_tree);
396 } /* }}} by_type_entry_t *lu_search_by_type */
398 static int lu_add_by_plugin (by_type_entry_t *by_type, /* {{{ */
399 user_class_list_t *user_class_list)
401 user_class_list_t *ptr = NULL;
402 identifier_match_t const *match = &user_class_list->entry.match;
404 /* Lookup user_class_list from the per-plugin structure. If this is the first
405 * user_class to be added, the blocks return immediately. Otherwise they will
406 * set "ptr" to non-NULL. */
407 if (match->plugin.is_regex)
409 if (by_type->wildcard_plugin_list == NULL)
411 by_type->wildcard_plugin_list = user_class_list;
415 ptr = by_type->wildcard_plugin_list;
416 } /* if (plugin is wildcard) */
417 else /* (plugin is not wildcard) */
421 status = c_avl_get (by_type->by_plugin_tree,
422 match->plugin.str, (void *) &ptr);
424 if (status != 0) /* plugin not yet in tree */
426 char *plugin_copy = strdup (match->plugin.str);
428 if (plugin_copy == NULL)
430 ERROR ("utils_vl_lookup: strdup failed.");
431 sfree (user_class_list);
435 status = c_avl_insert (by_type->by_plugin_tree,
436 plugin_copy, user_class_list);
439 ERROR ("utils_vl_lookup: c_avl_insert(\"%s\") failed with status %i.",
440 plugin_copy, status);
442 sfree (user_class_list);
449 } /* if (plugin not yet in tree) */
450 } /* if (plugin is not wildcard) */
452 assert (ptr != NULL);
454 while (ptr->next != NULL)
456 ptr->next = user_class_list;
459 } /* }}} int lu_add_by_plugin */
461 static void lu_destroy_user_obj (lookup_t *obj, /* {{{ */
462 user_obj_t *user_obj)
464 while (user_obj != NULL)
466 user_obj_t *next = user_obj->next;
468 if (obj->cb_free_obj != NULL)
469 obj->cb_free_obj (user_obj->user_obj);
470 user_obj->user_obj = NULL;
475 } /* }}} void lu_destroy_user_obj */
477 static void lu_destroy_user_class_list (lookup_t *obj, /* {{{ */
478 user_class_list_t *user_class_list)
480 while (user_class_list != NULL)
482 user_class_list_t *next = user_class_list->next;
484 if (obj->cb_free_class != NULL)
485 obj->cb_free_class (user_class_list->entry.user_class);
486 user_class_list->entry.user_class = NULL;
488 lu_destroy_user_obj (obj, user_class_list->entry.user_obj_list);
489 user_class_list->entry.user_obj_list = NULL;
491 sfree (user_class_list);
492 user_class_list = next;
494 } /* }}} void lu_destroy_user_class_list */
496 static void lu_destroy_by_type (lookup_t *obj, /* {{{ */
497 by_type_entry_t *by_type)
503 user_class_list_t *user_class_list = NULL;
506 status = c_avl_pick (by_type->by_plugin_tree,
507 (void *) &plugin, (void *) &user_class_list);
511 DEBUG ("utils_vl_lookup: lu_destroy_by_type: Destroying plugin \"%s\".",
514 lu_destroy_user_class_list (obj, user_class_list);
517 c_avl_destroy (by_type->by_plugin_tree);
518 by_type->by_plugin_tree = NULL;
520 lu_destroy_user_class_list (obj, by_type->wildcard_plugin_list);
521 by_type->wildcard_plugin_list = NULL;
524 } /* }}} int lu_destroy_by_type */
529 lookup_t *lookup_create (lookup_class_callback_t cb_user_class, /* {{{ */
530 lookup_obj_callback_t cb_user_obj,
531 lookup_free_class_callback_t cb_free_class,
532 lookup_free_obj_callback_t cb_free_obj)
534 lookup_t *obj = malloc (sizeof (*obj));
537 ERROR ("utils_vl_lookup: malloc failed.");
540 memset (obj, 0, sizeof (*obj));
542 obj->by_type_tree = c_avl_create ((void *) strcmp);
543 if (obj->by_type_tree == NULL)
545 ERROR ("utils_vl_lookup: c_avl_create failed.");
550 obj->cb_user_class = cb_user_class;
551 obj->cb_user_obj = cb_user_obj;
552 obj->cb_free_class = cb_free_class;
553 obj->cb_free_obj = cb_free_obj;
556 } /* }}} lookup_t *lookup_create */
558 void lookup_destroy (lookup_t *obj) /* {{{ */
568 by_type_entry_t *by_type = NULL;
570 status = c_avl_pick (obj->by_type_tree, (void *) &type, (void *) &by_type);
574 DEBUG ("utils_vl_lookup: lookup_destroy: Destroying type \"%s\".", type);
576 lu_destroy_by_type (obj, by_type);
579 c_avl_destroy (obj->by_type_tree);
580 obj->by_type_tree = NULL;
583 } /* }}} void lookup_destroy */
585 int lookup_add (lookup_t *obj, /* {{{ */
586 identifier_t const *ident, unsigned int group_by, void *user_class)
588 by_type_entry_t *by_type = NULL;
589 user_class_list_t *user_class_obj;
591 by_type = lu_search_by_type (obj, ident->type, /* allocate = */ 1);
595 user_class_obj = malloc (sizeof (*user_class_obj));
596 if (user_class_obj == NULL)
598 ERROR ("utils_vl_lookup: malloc failed.");
601 memset (user_class_obj, 0, sizeof (*user_class_obj));
602 user_class_obj->entry.user_class = user_class;
603 lu_copy_ident_to_match (&user_class_obj->entry.match, ident, group_by);
604 user_class_obj->entry.user_obj_list = NULL;
605 user_class_obj->next = NULL;
607 return (lu_add_by_plugin (by_type, user_class_obj));
608 } /* }}} int lookup_add */
610 /* returns the number of successful calls to the callback function */
611 int lookup_search (lookup_t *obj, /* {{{ */
612 data_set_t const *ds, value_list_t const *vl)
614 by_type_entry_t *by_type = NULL;
615 user_class_list_t *user_class_list = NULL;
619 if ((obj == NULL) || (ds == NULL) || (vl == NULL))
622 by_type = lu_search_by_type (obj, vl->type, /* allocate = */ 0);
626 status = c_avl_get (by_type->by_plugin_tree,
627 vl->plugin, (void *) &user_class_list);
630 status = lu_handle_user_class_list (obj, ds, vl, user_class_list);
636 if (by_type->wildcard_plugin_list != NULL)
638 status = lu_handle_user_class_list (obj, ds, vl,
639 by_type->wildcard_plugin_list);
646 } /* }}} lookup_search */