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>
33 #include "utils_vl_lookup.h"
34 #include "utils_avltree.h"
38 #endif /* HAVE_LIBKSTAT */
41 # define sstrncpy strncpy
42 # define plugin_log(s, ...) do { \
43 printf ("[severity %i] ", s); \
44 printf (__VA_ARGS__); \
54 char str[DATA_MAX_NAME_LEN];
58 typedef struct part_match_s part_match_t;
60 struct identifier_match_s
64 part_match_t plugin_instance;
66 part_match_t type_instance;
68 unsigned int group_by;
70 typedef struct identifier_match_s identifier_match_t;
74 c_avl_tree_t *by_type_tree;
76 lookup_class_callback_t cb_user_class;
77 lookup_obj_callback_t cb_user_obj;
78 lookup_free_class_callback_t cb_free_class;
79 lookup_free_obj_callback_t cb_free_obj;
83 typedef struct user_obj_s user_obj_t;
96 identifier_match_t match;
97 user_obj_t *user_obj_list; /* list of user_obj */
99 typedef struct user_class_s user_class_t;
101 struct user_class_list_s;
102 typedef struct user_class_list_s user_class_list_t;
103 struct user_class_list_s
106 user_class_list_t *next;
109 struct by_type_entry_s
111 c_avl_tree_t *by_plugin_tree; /* plugin -> user_class_list_t */
112 user_class_list_t *wildcard_plugin_list;
114 typedef struct by_type_entry_s by_type_entry_t;
119 static _Bool lu_part_matches (part_match_t const *match, /* {{{ */
124 /* Short cut popular catch-all regex. */
125 if (strcmp (".*", match->str) == 0)
128 int status = regexec (&match->regex, str,
129 /* nmatch = */ 0, /* pmatch = */ NULL,
136 else if (strcmp (match->str, str) == 0)
140 } /* }}} _Bool lu_part_matches */
142 static int lu_copy_ident_to_match_part (part_match_t *match_part, /* {{{ */
143 char const *ident_part)
145 size_t len = strlen (ident_part);
148 if ((len < 3) || (ident_part[0] != '/') || (ident_part[len - 1] != '/'))
150 sstrncpy (match_part->str, ident_part, sizeof (match_part->str));
151 match_part->is_regex = 0;
155 /* Copy string without the leading slash. */
156 sstrncpy (match_part->str, ident_part + 1, sizeof (match_part->str));
157 assert (sizeof (match_part->str) > len);
158 /* strip trailing slash */
159 match_part->str[len - 2] = 0;
161 status = regcomp (&match_part->regex, match_part->str,
162 /* flags = */ REG_EXTENDED);
166 regerror (status, &match_part->regex, errbuf, sizeof (errbuf));
167 ERROR ("utils_vl_lookup: Compiling regular expression \"%s\" failed: %s",
168 match_part->str, errbuf);
171 match_part->is_regex = 1;
174 } /* }}} int lu_copy_ident_to_match_part */
176 static int lu_copy_ident_to_match (identifier_match_t *match, /* {{{ */
177 identifier_t const *ident, unsigned int group_by)
179 memset (match, 0, sizeof (*match));
181 match->group_by = group_by;
183 #define COPY_FIELD(field) do { \
184 int status = lu_copy_ident_to_match_part (&match->field, ident->field); \
191 COPY_FIELD (plugin_instance);
193 COPY_FIELD (type_instance);
198 } /* }}} int lu_copy_ident_to_match */
200 /* user_class->lock must be held when calling this function */
201 static void *lu_create_user_obj (lookup_t *obj, /* {{{ */
202 data_set_t const *ds, value_list_t const *vl,
203 user_class_t *user_class)
205 user_obj_t *user_obj;
207 user_obj = malloc (sizeof (*user_obj));
208 if (user_obj == NULL)
210 ERROR ("utils_vl_lookup: malloc failed.");
213 memset (user_obj, 0, sizeof (*user_obj));
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 = malloc (sizeof (*by_type));
379 ERROR ("utils_vl_lookup: malloc failed.");
383 memset (by_type, 0, sizeof (*by_type));
384 by_type->wildcard_plugin_list = NULL;
386 by_type->by_plugin_tree = c_avl_create ((void *) strcmp);
387 if (by_type->by_plugin_tree == NULL)
389 ERROR ("utils_vl_lookup: c_avl_create failed.");
395 status = c_avl_insert (obj->by_type_tree,
396 /* key = */ type_copy, /* value = */ by_type);
397 assert (status <= 0); /* >0 => entry exists => race condition. */
400 ERROR ("utils_vl_lookup: c_avl_insert failed.");
401 c_avl_destroy (by_type->by_plugin_tree);
408 } /* }}} by_type_entry_t *lu_search_by_type */
410 static int lu_add_by_plugin (by_type_entry_t *by_type, /* {{{ */
411 user_class_list_t *user_class_list)
413 user_class_list_t *ptr = NULL;
414 identifier_match_t const *match = &user_class_list->entry.match;
416 /* Lookup user_class_list from the per-plugin structure. If this is the first
417 * user_class to be added, the block returns immediately. Otherwise they will
418 * set "ptr" to non-NULL. */
419 if (match->plugin.is_regex)
421 if (by_type->wildcard_plugin_list == NULL)
423 by_type->wildcard_plugin_list = user_class_list;
427 ptr = by_type->wildcard_plugin_list;
428 } /* if (plugin is wildcard) */
429 else /* (plugin is not wildcard) */
433 status = c_avl_get (by_type->by_plugin_tree,
434 match->plugin.str, (void *) &ptr);
436 if (status != 0) /* plugin not yet in tree */
438 char *plugin_copy = strdup (match->plugin.str);
440 if (plugin_copy == NULL)
442 ERROR ("utils_vl_lookup: strdup failed.");
443 sfree (user_class_list);
447 status = c_avl_insert (by_type->by_plugin_tree,
448 plugin_copy, user_class_list);
451 ERROR ("utils_vl_lookup: c_avl_insert(\"%s\") failed with status %i.",
452 plugin_copy, status);
454 sfree (user_class_list);
461 } /* if (plugin not yet in tree) */
462 } /* if (plugin is not wildcard) */
464 assert (ptr != NULL);
466 while (ptr->next != NULL)
468 ptr->next = user_class_list;
471 } /* }}} int lu_add_by_plugin */
473 static void lu_destroy_user_obj (lookup_t *obj, /* {{{ */
474 user_obj_t *user_obj)
476 while (user_obj != NULL)
478 user_obj_t *next = user_obj->next;
480 if (obj->cb_free_obj != NULL)
481 obj->cb_free_obj (user_obj->user_obj);
482 user_obj->user_obj = NULL;
487 } /* }}} void lu_destroy_user_obj */
489 static void lu_destroy_user_class_list (lookup_t *obj, /* {{{ */
490 user_class_list_t *user_class_list)
492 while (user_class_list != NULL)
494 user_class_list_t *next = user_class_list->next;
496 if (obj->cb_free_class != NULL)
497 obj->cb_free_class (user_class_list->entry.user_class);
498 user_class_list->entry.user_class = NULL;
500 lu_destroy_user_obj (obj, user_class_list->entry.user_obj_list);
501 user_class_list->entry.user_obj_list = NULL;
502 pthread_mutex_destroy (&user_class_list->entry.lock);
504 sfree (user_class_list);
505 user_class_list = next;
507 } /* }}} void lu_destroy_user_class_list */
509 static void lu_destroy_by_type (lookup_t *obj, /* {{{ */
510 by_type_entry_t *by_type)
516 user_class_list_t *user_class_list = NULL;
519 status = c_avl_pick (by_type->by_plugin_tree,
520 (void *) &plugin, (void *) &user_class_list);
524 DEBUG ("utils_vl_lookup: lu_destroy_by_type: Destroying plugin \"%s\".",
527 lu_destroy_user_class_list (obj, user_class_list);
530 c_avl_destroy (by_type->by_plugin_tree);
531 by_type->by_plugin_tree = NULL;
533 lu_destroy_user_class_list (obj, by_type->wildcard_plugin_list);
534 by_type->wildcard_plugin_list = NULL;
537 } /* }}} int lu_destroy_by_type */
542 lookup_t *lookup_create (lookup_class_callback_t cb_user_class, /* {{{ */
543 lookup_obj_callback_t cb_user_obj,
544 lookup_free_class_callback_t cb_free_class,
545 lookup_free_obj_callback_t cb_free_obj)
547 lookup_t *obj = malloc (sizeof (*obj));
550 ERROR ("utils_vl_lookup: malloc failed.");
553 memset (obj, 0, sizeof (*obj));
555 obj->by_type_tree = c_avl_create ((void *) strcmp);
556 if (obj->by_type_tree == NULL)
558 ERROR ("utils_vl_lookup: c_avl_create failed.");
563 obj->cb_user_class = cb_user_class;
564 obj->cb_user_obj = cb_user_obj;
565 obj->cb_free_class = cb_free_class;
566 obj->cb_free_obj = cb_free_obj;
569 } /* }}} lookup_t *lookup_create */
571 void lookup_destroy (lookup_t *obj) /* {{{ */
581 by_type_entry_t *by_type = NULL;
583 status = c_avl_pick (obj->by_type_tree, (void *) &type, (void *) &by_type);
587 DEBUG ("utils_vl_lookup: lookup_destroy: Destroying type \"%s\".", type);
589 lu_destroy_by_type (obj, by_type);
592 c_avl_destroy (obj->by_type_tree);
593 obj->by_type_tree = NULL;
596 } /* }}} void lookup_destroy */
598 int lookup_add (lookup_t *obj, /* {{{ */
599 identifier_t const *ident, unsigned int group_by, void *user_class)
601 by_type_entry_t *by_type = NULL;
602 user_class_list_t *user_class_obj;
604 by_type = lu_search_by_type (obj, ident->type, /* allocate = */ 1);
608 user_class_obj = malloc (sizeof (*user_class_obj));
609 if (user_class_obj == NULL)
611 ERROR ("utils_vl_lookup: malloc failed.");
614 memset (user_class_obj, 0, sizeof (*user_class_obj));
615 pthread_mutex_init (&user_class_obj->entry.lock, /* attr = */ NULL);
616 user_class_obj->entry.user_class = user_class;
617 lu_copy_ident_to_match (&user_class_obj->entry.match, ident, group_by);
618 user_class_obj->entry.user_obj_list = NULL;
619 user_class_obj->next = NULL;
621 return (lu_add_by_plugin (by_type, user_class_obj));
622 } /* }}} int lookup_add */
624 /* returns the number of successful calls to the callback function */
625 int lookup_search (lookup_t *obj, /* {{{ */
626 data_set_t const *ds, value_list_t const *vl)
628 by_type_entry_t *by_type = NULL;
629 user_class_list_t *user_class_list = NULL;
633 if ((obj == NULL) || (ds == NULL) || (vl == NULL))
636 by_type = lu_search_by_type (obj, vl->type, /* allocate = */ 0);
640 status = c_avl_get (by_type->by_plugin_tree,
641 vl->plugin, (void *) &user_class_list);
644 status = lu_handle_user_class_list (obj, ds, vl, user_class_list);
650 if (by_type->wildcard_plugin_list != NULL)
652 status = lu_handle_user_class_list (obj, ds, vl,
653 by_type->wildcard_plugin_list);
660 } /* }}} lookup_search */