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"
37 # define sstrncpy strncpy
38 # define plugin_log(s, ...) do { \
39 printf ("[severity %i] ", s); \
40 printf (__VA_ARGS__); \
50 char str[DATA_MAX_NAME_LEN];
54 typedef struct part_match_s part_match_t;
56 struct identifier_match_s
60 part_match_t plugin_instance;
62 part_match_t type_instance;
64 unsigned int group_by;
66 typedef struct identifier_match_s identifier_match_t;
70 c_avl_tree_t *by_type_tree;
72 lookup_class_callback_t cb_user_class;
73 lookup_obj_callback_t cb_user_obj;
74 lookup_free_class_callback_t cb_free_class;
75 lookup_free_obj_callback_t cb_free_obj;
79 typedef struct user_obj_s user_obj_t;
92 identifier_match_t match;
93 user_obj_t *user_obj_list; /* list of user_obj */
95 typedef struct user_class_s user_class_t;
97 struct user_class_list_s;
98 typedef struct user_class_list_s user_class_list_t;
99 struct user_class_list_s
102 user_class_list_t *next;
105 struct by_type_entry_s
107 c_avl_tree_t *by_plugin_tree; /* plugin -> user_class_list_t */
108 user_class_list_t *wildcard_plugin_list;
110 typedef struct by_type_entry_s by_type_entry_t;
115 static _Bool lu_part_matches (part_match_t const *match, /* {{{ */
120 /* Short cut popular catch-all regex. */
121 if (strcmp (".*", match->str) == 0)
124 int status = regexec (&match->regex, str,
125 /* nmatch = */ 0, /* pmatch = */ NULL,
132 else if (strcmp (match->str, str) == 0)
136 } /* }}} _Bool lu_part_matches */
138 static int lu_copy_ident_to_match_part (part_match_t *match_part, /* {{{ */
139 char const *ident_part)
141 size_t len = strlen (ident_part);
144 if ((len < 3) || (ident_part[0] != '/') || (ident_part[len - 1] != '/'))
146 sstrncpy (match_part->str, ident_part, sizeof (match_part->str));
147 match_part->is_regex = 0;
151 /* Copy string without the leading slash. */
152 sstrncpy (match_part->str, ident_part + 1, sizeof (match_part->str));
153 assert (sizeof (match_part->str) > len);
154 /* strip trailing slash */
155 match_part->str[len - 2] = 0;
157 status = regcomp (&match_part->regex, match_part->str,
158 /* flags = */ REG_EXTENDED);
162 regerror (status, &match_part->regex, errbuf, sizeof (errbuf));
163 ERROR ("utils_vl_lookup: Compiling regular expression \"%s\" failed: %s",
164 match_part->str, errbuf);
167 match_part->is_regex = 1;
170 } /* }}} int lu_copy_ident_to_match_part */
172 static int lu_copy_ident_to_match (identifier_match_t *match, /* {{{ */
173 identifier_t const *ident, unsigned int group_by)
175 memset (match, 0, sizeof (*match));
177 match->group_by = group_by;
179 #define COPY_FIELD(field) do { \
180 int status = lu_copy_ident_to_match_part (&match->field, ident->field); \
187 COPY_FIELD (plugin_instance);
189 COPY_FIELD (type_instance);
194 } /* }}} int lu_copy_ident_to_match */
196 /* user_class->lock must be held when calling this function */
197 static void *lu_create_user_obj (lookup_t *obj, /* {{{ */
198 data_set_t const *ds, value_list_t const *vl,
199 user_class_t *user_class)
201 user_obj_t *user_obj;
203 user_obj = malloc (sizeof (*user_obj));
204 if (user_obj == NULL)
206 ERROR ("utils_vl_lookup: malloc failed.");
209 memset (user_obj, 0, sizeof (*user_obj));
210 user_obj->next = NULL;
212 user_obj->user_obj = obj->cb_user_class (ds, vl, user_class->user_class);
213 if (user_obj->user_obj == NULL)
216 WARNING("utils_vl_lookup: User-provided constructor failed.");
220 #define COPY_FIELD(field, group_mask) do { \
221 if (user_class->match.field.is_regex \
222 && ((user_class->match.group_by & group_mask) == 0)) \
223 sstrncpy (user_obj->ident.field, "/.*/", sizeof (user_obj->ident.field)); \
225 sstrncpy (user_obj->ident.field, vl->field, sizeof (user_obj->ident.field)); \
228 COPY_FIELD (host, LU_GROUP_BY_HOST);
229 COPY_FIELD (plugin, LU_GROUP_BY_PLUGIN);
230 COPY_FIELD (plugin_instance, LU_GROUP_BY_PLUGIN_INSTANCE);
231 COPY_FIELD (type, 0);
232 COPY_FIELD (type_instance, LU_GROUP_BY_TYPE_INSTANCE);
236 if (user_class->user_obj_list == NULL)
238 user_class->user_obj_list = user_obj;
242 user_obj_t *last = user_class->user_obj_list;
243 while (last->next != NULL)
245 last->next = user_obj;
249 } /* }}} void *lu_create_user_obj */
251 /* user_class->lock must be held when calling this function */
252 static user_obj_t *lu_find_user_obj (user_class_t *user_class, /* {{{ */
253 value_list_t const *vl)
257 for (ptr = user_class->user_obj_list;
261 if (user_class->match.host.is_regex
262 && (user_class->match.group_by & LU_GROUP_BY_HOST)
263 && (strcmp (vl->host, ptr->ident.host) != 0))
265 if (user_class->match.plugin.is_regex
266 && (user_class->match.group_by & LU_GROUP_BY_PLUGIN)
267 && (strcmp (vl->plugin, ptr->ident.plugin) != 0))
269 if (user_class->match.plugin_instance.is_regex
270 && (user_class->match.group_by & LU_GROUP_BY_PLUGIN_INSTANCE)
271 && (strcmp (vl->plugin_instance, ptr->ident.plugin_instance) != 0))
273 if (user_class->match.type_instance.is_regex
274 && (user_class->match.group_by & LU_GROUP_BY_TYPE_INSTANCE)
275 && (strcmp (vl->type_instance, ptr->ident.type_instance) != 0))
282 } /* }}} user_obj_t *lu_find_user_obj */
284 static int lu_handle_user_class (lookup_t *obj, /* {{{ */
285 data_set_t const *ds, value_list_t const *vl,
286 user_class_t *user_class)
288 user_obj_t *user_obj;
291 assert (strcmp (vl->type, user_class->match.type.str) == 0);
292 assert (user_class->match.plugin.is_regex
293 || (strcmp (vl->plugin, user_class->match.plugin.str)) == 0);
295 if (!lu_part_matches (&user_class->match.type_instance, vl->type_instance)
296 || !lu_part_matches (&user_class->match.plugin_instance, vl->plugin_instance)
297 || !lu_part_matches (&user_class->match.plugin, vl->plugin)
298 || !lu_part_matches (&user_class->match.host, vl->host))
301 pthread_mutex_lock (&user_class->lock);
302 user_obj = lu_find_user_obj (user_class, vl);
303 if (user_obj == NULL)
305 /* call lookup_class_callback_t() and insert into the list of user objects. */
306 user_obj = lu_create_user_obj (obj, ds, vl, user_class);
307 pthread_mutex_unlock (&user_class->lock);
308 if (user_obj == NULL)
311 pthread_mutex_unlock (&user_class->lock);
313 status = obj->cb_user_obj (ds, vl,
314 user_class->user_class, user_obj->user_obj);
317 ERROR ("utils_vl_lookup: The user object callback failed with status %i.",
319 /* Returning a negative value means: abort! */
327 } /* }}} int lu_handle_user_class */
329 static int lu_handle_user_class_list (lookup_t *obj, /* {{{ */
330 data_set_t const *ds, value_list_t const *vl,
331 user_class_list_t *user_class_list)
333 user_class_list_t *ptr;
336 for (ptr = user_class_list; ptr != NULL; ptr = ptr->next)
340 status = lu_handle_user_class (obj, ds, vl, &ptr->entry);
343 else if (status == 0)
348 } /* }}} int lu_handle_user_class_list */
350 static by_type_entry_t *lu_search_by_type (lookup_t *obj, /* {{{ */
351 char const *type, _Bool allocate_if_missing)
353 by_type_entry_t *by_type;
357 status = c_avl_get (obj->by_type_tree, type, (void *) &by_type);
361 if (!allocate_if_missing)
364 type_copy = strdup (type);
365 if (type_copy == NULL)
367 ERROR ("utils_vl_lookup: strdup failed.");
371 by_type = malloc (sizeof (*by_type));
374 ERROR ("utils_vl_lookup: malloc failed.");
378 memset (by_type, 0, sizeof (*by_type));
379 by_type->wildcard_plugin_list = NULL;
381 by_type->by_plugin_tree = c_avl_create ((void *) strcmp);
382 if (by_type->by_plugin_tree == NULL)
384 ERROR ("utils_vl_lookup: c_avl_create failed.");
390 status = c_avl_insert (obj->by_type_tree,
391 /* key = */ type_copy, /* value = */ by_type);
392 assert (status <= 0); /* >0 => entry exists => race condition. */
395 ERROR ("utils_vl_lookup: c_avl_insert failed.");
396 c_avl_destroy (by_type->by_plugin_tree);
403 } /* }}} by_type_entry_t *lu_search_by_type */
405 static int lu_add_by_plugin (by_type_entry_t *by_type, /* {{{ */
406 user_class_list_t *user_class_list)
408 user_class_list_t *ptr = NULL;
409 identifier_match_t const *match = &user_class_list->entry.match;
411 /* Lookup user_class_list from the per-plugin structure. If this is the first
412 * user_class to be added, the block returns immediately. Otherwise they will
413 * set "ptr" to non-NULL. */
414 if (match->plugin.is_regex)
416 if (by_type->wildcard_plugin_list == NULL)
418 by_type->wildcard_plugin_list = user_class_list;
422 ptr = by_type->wildcard_plugin_list;
423 } /* if (plugin is wildcard) */
424 else /* (plugin is not wildcard) */
428 status = c_avl_get (by_type->by_plugin_tree,
429 match->plugin.str, (void *) &ptr);
431 if (status != 0) /* plugin not yet in tree */
433 char *plugin_copy = strdup (match->plugin.str);
435 if (plugin_copy == NULL)
437 ERROR ("utils_vl_lookup: strdup failed.");
438 sfree (user_class_list);
442 status = c_avl_insert (by_type->by_plugin_tree,
443 plugin_copy, user_class_list);
446 ERROR ("utils_vl_lookup: c_avl_insert(\"%s\") failed with status %i.",
447 plugin_copy, status);
449 sfree (user_class_list);
456 } /* if (plugin not yet in tree) */
457 } /* if (plugin is not wildcard) */
459 assert (ptr != NULL);
461 while (ptr->next != NULL)
463 ptr->next = user_class_list;
466 } /* }}} int lu_add_by_plugin */
468 static void lu_destroy_user_obj (lookup_t *obj, /* {{{ */
469 user_obj_t *user_obj)
471 while (user_obj != NULL)
473 user_obj_t *next = user_obj->next;
475 if (obj->cb_free_obj != NULL)
476 obj->cb_free_obj (user_obj->user_obj);
477 user_obj->user_obj = NULL;
482 } /* }}} void lu_destroy_user_obj */
484 static void lu_destroy_user_class_list (lookup_t *obj, /* {{{ */
485 user_class_list_t *user_class_list)
487 while (user_class_list != NULL)
489 user_class_list_t *next = user_class_list->next;
491 if (obj->cb_free_class != NULL)
492 obj->cb_free_class (user_class_list->entry.user_class);
493 user_class_list->entry.user_class = NULL;
495 lu_destroy_user_obj (obj, user_class_list->entry.user_obj_list);
496 user_class_list->entry.user_obj_list = NULL;
497 pthread_mutex_destroy (&user_class_list->entry.lock);
499 sfree (user_class_list);
500 user_class_list = next;
502 } /* }}} void lu_destroy_user_class_list */
504 static void lu_destroy_by_type (lookup_t *obj, /* {{{ */
505 by_type_entry_t *by_type)
511 user_class_list_t *user_class_list = NULL;
514 status = c_avl_pick (by_type->by_plugin_tree,
515 (void *) &plugin, (void *) &user_class_list);
519 DEBUG ("utils_vl_lookup: lu_destroy_by_type: Destroying plugin \"%s\".",
522 lu_destroy_user_class_list (obj, user_class_list);
525 c_avl_destroy (by_type->by_plugin_tree);
526 by_type->by_plugin_tree = NULL;
528 lu_destroy_user_class_list (obj, by_type->wildcard_plugin_list);
529 by_type->wildcard_plugin_list = NULL;
532 } /* }}} int lu_destroy_by_type */
537 lookup_t *lookup_create (lookup_class_callback_t cb_user_class, /* {{{ */
538 lookup_obj_callback_t cb_user_obj,
539 lookup_free_class_callback_t cb_free_class,
540 lookup_free_obj_callback_t cb_free_obj)
542 lookup_t *obj = malloc (sizeof (*obj));
545 ERROR ("utils_vl_lookup: malloc failed.");
548 memset (obj, 0, sizeof (*obj));
550 obj->by_type_tree = c_avl_create ((void *) strcmp);
551 if (obj->by_type_tree == NULL)
553 ERROR ("utils_vl_lookup: c_avl_create failed.");
558 obj->cb_user_class = cb_user_class;
559 obj->cb_user_obj = cb_user_obj;
560 obj->cb_free_class = cb_free_class;
561 obj->cb_free_obj = cb_free_obj;
564 } /* }}} lookup_t *lookup_create */
566 void lookup_destroy (lookup_t *obj) /* {{{ */
576 by_type_entry_t *by_type = NULL;
578 status = c_avl_pick (obj->by_type_tree, (void *) &type, (void *) &by_type);
582 DEBUG ("utils_vl_lookup: lookup_destroy: Destroying type \"%s\".", type);
584 lu_destroy_by_type (obj, by_type);
587 c_avl_destroy (obj->by_type_tree);
588 obj->by_type_tree = NULL;
591 } /* }}} void lookup_destroy */
593 int lookup_add (lookup_t *obj, /* {{{ */
594 identifier_t const *ident, unsigned int group_by, void *user_class)
596 by_type_entry_t *by_type = NULL;
597 user_class_list_t *user_class_obj;
599 by_type = lu_search_by_type (obj, ident->type, /* allocate = */ 1);
603 user_class_obj = malloc (sizeof (*user_class_obj));
604 if (user_class_obj == NULL)
606 ERROR ("utils_vl_lookup: malloc failed.");
609 memset (user_class_obj, 0, sizeof (*user_class_obj));
610 pthread_mutex_init (&user_class_obj->entry.lock, /* attr = */ NULL);
611 user_class_obj->entry.user_class = user_class;
612 lu_copy_ident_to_match (&user_class_obj->entry.match, ident, group_by);
613 user_class_obj->entry.user_obj_list = NULL;
614 user_class_obj->next = NULL;
616 return (lu_add_by_plugin (by_type, user_class_obj));
617 } /* }}} int lookup_add */
619 /* returns the number of successful calls to the callback function */
620 int lookup_search (lookup_t *obj, /* {{{ */
621 data_set_t const *ds, value_list_t const *vl)
623 by_type_entry_t *by_type = NULL;
624 user_class_list_t *user_class_list = NULL;
628 if ((obj == NULL) || (ds == NULL) || (vl == NULL))
631 by_type = lu_search_by_type (obj, vl->type, /* allocate = */ 0);
635 status = c_avl_get (by_type->by_plugin_tree,
636 vl->plugin, (void *) &user_class_list);
639 status = lu_handle_user_class_list (obj, ds, vl, user_class_list);
645 if (by_type->wildcard_plugin_list != NULL)
647 status = lu_handle_user_class_list (obj, ds, vl,
648 by_type->wildcard_plugin_list);
655 } /* }}} lookup_search */