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>
34 #include "utils_vl_lookup.h"
35 #include "utils_avltree.h"
39 #endif /* HAVE_LIBKSTAT */
42 # define sstrncpy strncpy
43 # define plugin_log(s, ...) do { \
44 printf ("[severity %i] ", s); \
45 printf (__VA_ARGS__); \
55 char str[DATA_MAX_NAME_LEN];
59 typedef struct part_match_s part_match_t;
61 struct identifier_match_s
65 part_match_t plugin_instance;
67 part_match_t type_instance;
69 unsigned int group_by;
71 typedef struct identifier_match_s identifier_match_t;
75 c_avl_tree_t *by_type_tree;
77 lookup_class_callback_t cb_user_class;
78 lookup_obj_callback_t cb_user_obj;
79 lookup_free_class_callback_t cb_free_class;
80 lookup_free_obj_callback_t cb_free_obj;
84 typedef struct user_obj_s user_obj_t;
97 identifier_match_t match;
98 user_obj_t *user_obj_list; /* list of user_obj */
100 typedef struct user_class_s user_class_t;
102 struct user_class_list_s;
103 typedef struct user_class_list_s user_class_list_t;
104 struct user_class_list_s
107 user_class_list_t *next;
110 struct by_type_entry_s
112 c_avl_tree_t *by_plugin_tree; /* plugin -> user_class_list_t */
113 user_class_list_t *wildcard_plugin_list;
115 typedef struct by_type_entry_s by_type_entry_t;
120 static _Bool lu_part_matches (part_match_t const *match, /* {{{ */
125 /* Short cut popular catch-all regex. */
126 if (strcmp (".*", match->str) == 0)
129 int status = regexec (&match->regex, str,
130 /* nmatch = */ 0, /* pmatch = */ NULL,
137 else if (strcmp (match->str, str) == 0)
141 } /* }}} _Bool lu_part_matches */
143 static int lu_copy_ident_to_match_part (part_match_t *match_part, /* {{{ */
144 char const *ident_part)
146 size_t len = strlen (ident_part);
149 if ((len < 3) || (ident_part[0] != '/') || (ident_part[len - 1] != '/'))
151 sstrncpy (match_part->str, ident_part, sizeof (match_part->str));
152 match_part->is_regex = 0;
156 /* Copy string without the leading slash. */
157 sstrncpy (match_part->str, ident_part + 1, sizeof (match_part->str));
158 assert (sizeof (match_part->str) > len);
159 /* strip trailing slash */
160 match_part->str[len - 2] = 0;
162 status = regcomp (&match_part->regex, match_part->str,
163 /* flags = */ REG_EXTENDED);
167 regerror (status, &match_part->regex, errbuf, sizeof (errbuf));
168 ERROR ("utils_vl_lookup: Compiling regular expression \"%s\" failed: %s",
169 match_part->str, errbuf);
172 match_part->is_regex = 1;
175 } /* }}} int lu_copy_ident_to_match_part */
177 static int lu_copy_ident_to_match (identifier_match_t *match, /* {{{ */
178 identifier_t const *ident, unsigned int group_by)
180 memset (match, 0, sizeof (*match));
182 match->group_by = group_by;
184 #define COPY_FIELD(field) do { \
185 int status = lu_copy_ident_to_match_part (&match->field, ident->field); \
192 COPY_FIELD (plugin_instance);
194 COPY_FIELD (type_instance);
199 } /* }}} int lu_copy_ident_to_match */
201 /* user_class->lock must be held when calling this function */
202 static void *lu_create_user_obj (lookup_t *obj, /* {{{ */
203 data_set_t const *ds, value_list_t const *vl,
204 user_class_t *user_class)
206 user_obj_t *user_obj;
208 user_obj = calloc (1, sizeof (*user_obj));
209 if (user_obj == NULL)
211 ERROR ("utils_vl_lookup: calloc failed.");
214 user_obj->next = NULL;
216 user_obj->user_obj = obj->cb_user_class (ds, vl, user_class->user_class);
217 if (user_obj->user_obj == NULL)
220 WARNING("utils_vl_lookup: User-provided constructor failed.");
224 #define COPY_FIELD(field, group_mask) do { \
225 if (user_class->match.field.is_regex \
226 && ((user_class->match.group_by & group_mask) == 0)) \
227 sstrncpy (user_obj->ident.field, "/.*/", sizeof (user_obj->ident.field)); \
229 sstrncpy (user_obj->ident.field, vl->field, sizeof (user_obj->ident.field)); \
232 COPY_FIELD (host, LU_GROUP_BY_HOST);
233 COPY_FIELD (plugin, LU_GROUP_BY_PLUGIN);
234 COPY_FIELD (plugin_instance, LU_GROUP_BY_PLUGIN_INSTANCE);
235 COPY_FIELD (type, 0);
236 COPY_FIELD (type_instance, LU_GROUP_BY_TYPE_INSTANCE);
240 if (user_class->user_obj_list == NULL)
242 user_class->user_obj_list = user_obj;
246 user_obj_t *last = user_class->user_obj_list;
247 while (last->next != NULL)
249 last->next = user_obj;
253 } /* }}} void *lu_create_user_obj */
255 /* user_class->lock must be held when calling this function */
256 static user_obj_t *lu_find_user_obj (user_class_t *user_class, /* {{{ */
257 value_list_t const *vl)
261 for (ptr = user_class->user_obj_list;
265 if (user_class->match.host.is_regex
266 && (user_class->match.group_by & LU_GROUP_BY_HOST)
267 && (strcmp (vl->host, ptr->ident.host) != 0))
269 if (user_class->match.plugin.is_regex
270 && (user_class->match.group_by & LU_GROUP_BY_PLUGIN)
271 && (strcmp (vl->plugin, ptr->ident.plugin) != 0))
273 if (user_class->match.plugin_instance.is_regex
274 && (user_class->match.group_by & LU_GROUP_BY_PLUGIN_INSTANCE)
275 && (strcmp (vl->plugin_instance, ptr->ident.plugin_instance) != 0))
277 if (user_class->match.type_instance.is_regex
278 && (user_class->match.group_by & LU_GROUP_BY_TYPE_INSTANCE)
279 && (strcmp (vl->type_instance, ptr->ident.type_instance) != 0))
286 } /* }}} user_obj_t *lu_find_user_obj */
288 static int lu_handle_user_class (lookup_t *obj, /* {{{ */
289 data_set_t const *ds, value_list_t const *vl,
290 user_class_t *user_class)
292 user_obj_t *user_obj;
295 assert (strcmp (vl->type, user_class->match.type.str) == 0);
296 assert (user_class->match.plugin.is_regex
297 || (strcmp (vl->plugin, user_class->match.plugin.str)) == 0);
299 if (!lu_part_matches (&user_class->match.type_instance, vl->type_instance)
300 || !lu_part_matches (&user_class->match.plugin_instance, vl->plugin_instance)
301 || !lu_part_matches (&user_class->match.plugin, vl->plugin)
302 || !lu_part_matches (&user_class->match.host, vl->host))
305 pthread_mutex_lock (&user_class->lock);
306 user_obj = lu_find_user_obj (user_class, vl);
307 if (user_obj == NULL)
309 /* call lookup_class_callback_t() and insert into the list of user objects. */
310 user_obj = lu_create_user_obj (obj, ds, vl, user_class);
311 if (user_obj == NULL) {
312 pthread_mutex_unlock (&user_class->lock);
316 pthread_mutex_unlock (&user_class->lock);
318 status = obj->cb_user_obj (ds, vl,
319 user_class->user_class, user_obj->user_obj);
322 ERROR ("utils_vl_lookup: The user object callback failed with status %i.",
324 /* Returning a negative value means: abort! */
332 } /* }}} int lu_handle_user_class */
334 static int lu_handle_user_class_list (lookup_t *obj, /* {{{ */
335 data_set_t const *ds, value_list_t const *vl,
336 user_class_list_t *user_class_list)
338 user_class_list_t *ptr;
341 for (ptr = user_class_list; ptr != NULL; ptr = ptr->next)
345 status = lu_handle_user_class (obj, ds, vl, &ptr->entry);
348 else if (status == 0)
353 } /* }}} int lu_handle_user_class_list */
355 static by_type_entry_t *lu_search_by_type (lookup_t *obj, /* {{{ */
356 char const *type, _Bool allocate_if_missing)
358 by_type_entry_t *by_type;
362 status = c_avl_get (obj->by_type_tree, type, (void *) &by_type);
366 if (!allocate_if_missing)
369 type_copy = strdup (type);
370 if (type_copy == NULL)
372 ERROR ("utils_vl_lookup: strdup failed.");
376 by_type = calloc (1, sizeof (*by_type));
379 ERROR ("utils_vl_lookup: calloc failed.");
383 by_type->wildcard_plugin_list = NULL;
385 by_type->by_plugin_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
386 if (by_type->by_plugin_tree == NULL)
388 ERROR ("utils_vl_lookup: c_avl_create failed.");
394 status = c_avl_insert (obj->by_type_tree,
395 /* key = */ type_copy, /* value = */ by_type);
396 assert (status <= 0); /* >0 => entry exists => race condition. */
399 ERROR ("utils_vl_lookup: c_avl_insert failed.");
400 c_avl_destroy (by_type->by_plugin_tree);
407 } /* }}} by_type_entry_t *lu_search_by_type */
409 static int lu_add_by_plugin (by_type_entry_t *by_type, /* {{{ */
410 user_class_list_t *user_class_list)
412 user_class_list_t *ptr = NULL;
413 identifier_match_t const *match = &user_class_list->entry.match;
415 /* Lookup user_class_list from the per-plugin structure. If this is the first
416 * user_class to be added, the block returns immediately. Otherwise they will
417 * set "ptr" to non-NULL. */
418 if (match->plugin.is_regex)
420 if (by_type->wildcard_plugin_list == NULL)
422 by_type->wildcard_plugin_list = user_class_list;
426 ptr = by_type->wildcard_plugin_list;
427 } /* if (plugin is wildcard) */
428 else /* (plugin is not wildcard) */
432 status = c_avl_get (by_type->by_plugin_tree,
433 match->plugin.str, (void *) &ptr);
435 if (status != 0) /* plugin not yet in tree */
437 char *plugin_copy = strdup (match->plugin.str);
439 if (plugin_copy == NULL)
441 ERROR ("utils_vl_lookup: strdup failed.");
442 sfree (user_class_list);
446 status = c_avl_insert (by_type->by_plugin_tree,
447 plugin_copy, user_class_list);
450 ERROR ("utils_vl_lookup: c_avl_insert(\"%s\") failed with status %i.",
451 plugin_copy, status);
453 sfree (user_class_list);
460 } /* if (plugin not yet in tree) */
461 } /* if (plugin is not wildcard) */
463 assert (ptr != NULL);
465 while (ptr->next != NULL)
467 ptr->next = user_class_list;
470 } /* }}} int lu_add_by_plugin */
472 static void lu_destroy_user_obj (lookup_t *obj, /* {{{ */
473 user_obj_t *user_obj)
475 while (user_obj != NULL)
477 user_obj_t *next = user_obj->next;
479 if (obj->cb_free_obj != NULL)
480 obj->cb_free_obj (user_obj->user_obj);
481 user_obj->user_obj = NULL;
486 } /* }}} void lu_destroy_user_obj */
488 static void lu_destroy_user_class_list (lookup_t *obj, /* {{{ */
489 user_class_list_t *user_class_list)
491 while (user_class_list != NULL)
493 user_class_list_t *next = user_class_list->next;
495 if (obj->cb_free_class != NULL)
496 obj->cb_free_class (user_class_list->entry.user_class);
497 user_class_list->entry.user_class = NULL;
499 #define CLEAR_FIELD(field) do { \
500 if (user_class_list->entry.match.field.is_regex) { \
501 regfree (&user_class_list->entry.match.field.regex); \
502 user_class_list->entry.match.field.is_regex = 0; \
507 CLEAR_FIELD (plugin);
508 CLEAR_FIELD (plugin_instance);
510 CLEAR_FIELD (type_instance);
514 lu_destroy_user_obj (obj, user_class_list->entry.user_obj_list);
515 user_class_list->entry.user_obj_list = NULL;
516 pthread_mutex_destroy (&user_class_list->entry.lock);
518 sfree (user_class_list);
519 user_class_list = next;
521 } /* }}} void lu_destroy_user_class_list */
523 static void lu_destroy_by_type (lookup_t *obj, /* {{{ */
524 by_type_entry_t *by_type)
530 user_class_list_t *user_class_list = NULL;
533 status = c_avl_pick (by_type->by_plugin_tree,
534 (void *) &plugin, (void *) &user_class_list);
538 DEBUG ("utils_vl_lookup: lu_destroy_by_type: Destroying plugin \"%s\".",
541 lu_destroy_user_class_list (obj, user_class_list);
544 c_avl_destroy (by_type->by_plugin_tree);
545 by_type->by_plugin_tree = NULL;
547 lu_destroy_user_class_list (obj, by_type->wildcard_plugin_list);
548 by_type->wildcard_plugin_list = NULL;
551 } /* }}} int lu_destroy_by_type */
556 lookup_t *lookup_create (lookup_class_callback_t cb_user_class, /* {{{ */
557 lookup_obj_callback_t cb_user_obj,
558 lookup_free_class_callback_t cb_free_class,
559 lookup_free_obj_callback_t cb_free_obj)
561 lookup_t *obj = calloc (1, sizeof (*obj));
564 ERROR ("utils_vl_lookup: calloc failed.");
568 obj->by_type_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
569 if (obj->by_type_tree == NULL)
571 ERROR ("utils_vl_lookup: c_avl_create failed.");
576 obj->cb_user_class = cb_user_class;
577 obj->cb_user_obj = cb_user_obj;
578 obj->cb_free_class = cb_free_class;
579 obj->cb_free_obj = cb_free_obj;
582 } /* }}} lookup_t *lookup_create */
584 void lookup_destroy (lookup_t *obj) /* {{{ */
594 by_type_entry_t *by_type = NULL;
596 status = c_avl_pick (obj->by_type_tree, (void *) &type, (void *) &by_type);
600 DEBUG ("utils_vl_lookup: lookup_destroy: Destroying type \"%s\".", type);
602 lu_destroy_by_type (obj, by_type);
605 c_avl_destroy (obj->by_type_tree);
606 obj->by_type_tree = NULL;
609 } /* }}} void lookup_destroy */
611 int lookup_add (lookup_t *obj, /* {{{ */
612 identifier_t const *ident, unsigned int group_by, void *user_class)
614 by_type_entry_t *by_type = NULL;
615 user_class_list_t *user_class_obj;
617 by_type = lu_search_by_type (obj, ident->type, /* allocate = */ 1);
621 user_class_obj = calloc (1, sizeof (*user_class_obj));
622 if (user_class_obj == NULL)
624 ERROR ("utils_vl_lookup: calloc failed.");
627 pthread_mutex_init (&user_class_obj->entry.lock, /* attr = */ NULL);
628 user_class_obj->entry.user_class = user_class;
629 lu_copy_ident_to_match (&user_class_obj->entry.match, ident, group_by);
630 user_class_obj->entry.user_obj_list = NULL;
631 user_class_obj->next = NULL;
633 return (lu_add_by_plugin (by_type, user_class_obj));
634 } /* }}} int lookup_add */
636 /* returns the number of successful calls to the callback function */
637 int lookup_search (lookup_t *obj, /* {{{ */
638 data_set_t const *ds, value_list_t const *vl)
640 by_type_entry_t *by_type = NULL;
641 user_class_list_t *user_class_list = NULL;
645 if ((obj == NULL) || (ds == NULL) || (vl == NULL))
648 by_type = lu_search_by_type (obj, vl->type, /* allocate = */ 0);
652 status = c_avl_get (by_type->by_plugin_tree,
653 vl->plugin, (void *) &user_class_list);
656 status = lu_handle_user_class_list (obj, ds, vl, user_class_list);
662 if (by_type->wildcard_plugin_list != NULL)
664 status = lu_handle_user_class_list (obj, ds, vl,
665 by_type->wildcard_plugin_list);
672 } /* }}} lookup_search */