1 /**
2 * collectd - src/utils_vl_lookup.c
3 * Copyright (C) 2012 Florian Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
29 #include <pthread.h>
30 #include <regex.h>
32 #include "common.h"
33 #include "utils_vl_lookup.h"
34 #include "utils_avltree.h"
36 #if BUILD_TEST
37 # define sstrncpy strncpy
38 # define plugin_log(s, ...) do { \
39 printf ("[severity %i] ", s); \
40 printf (__VA_ARGS__); \
41 printf ("\n"); \
42 } while (0)
43 #endif
45 /*
46 * Types
47 */
48 struct part_match_s
49 {
50 char str[DATA_MAX_NAME_LEN];
51 regex_t regex;
52 _Bool is_regex;
53 };
54 typedef struct part_match_s part_match_t;
56 struct identifier_match_s
57 {
58 part_match_t host;
59 part_match_t plugin;
60 part_match_t plugin_instance;
61 part_match_t type;
62 part_match_t type_instance;
64 unsigned int group_by;
65 };
66 typedef struct identifier_match_s identifier_match_t;
68 struct lookup_s
69 {
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;
76 };
78 struct user_obj_s;
79 typedef struct user_obj_s user_obj_t;
80 struct user_obj_s
81 {
82 void *user_obj;
83 identifier_t ident;
85 user_obj_t *next;
86 };
88 struct user_class_s
89 {
90 pthread_mutex_t lock;
91 void *user_class;
92 identifier_match_t match;
93 user_obj_t *user_obj_list; /* list of user_obj */
94 };
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
100 {
101 user_class_t entry;
102 user_class_list_t *next;
103 };
105 struct by_type_entry_s
106 {
107 c_avl_tree_t *by_plugin_tree; /* plugin -> user_class_list_t */
108 user_class_list_t *wildcard_plugin_list;
109 };
110 typedef struct by_type_entry_s by_type_entry_t;
112 /*
113 * Private functions
114 */
115 static _Bool lu_part_matches (part_match_t const *match, /* {{{ */
116 char const *str)
117 {
118 if (match->is_regex)
119 {
120 /* Short cut popular catch-all regex. */
121 if (strcmp (".*", match->str) == 0)
122 return (1);
124 int status = regexec (&match->regex, str,
125 /* nmatch = */ 0, /* pmatch = */ NULL,
126 /* flags = */ 0);
127 if (status == 0)
128 return (1);
129 else
130 return (0);
131 }
132 else if (strcmp (match->str, str) == 0)
133 return (1);
134 else
135 return (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)
140 {
141 size_t len = strlen (ident_part);
142 int status;
144 if ((len < 3) || (ident_part[0] != '/') || (ident_part[len - 1] != '/'))
145 {
146 sstrncpy (match_part->str, ident_part, sizeof (match_part->str));
147 match_part->is_regex = 0;
148 return (0);
149 }
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);
159 if (status != 0)
160 {
161 char errbuf[1024];
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);
165 return (EINVAL);
166 }
167 match_part->is_regex = 1;
169 return (0);
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)
174 {
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); \
181 if (status != 0) \
182 return (status); \
183 } while (0)
185 COPY_FIELD (host);
186 COPY_FIELD (plugin);
187 COPY_FIELD (plugin_instance);
188 COPY_FIELD (type);
189 COPY_FIELD (type_instance);
191 #undef COPY_FIELD
193 return (0);
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)
200 {
201 user_obj_t *user_obj;
203 user_obj = malloc (sizeof (*user_obj));
204 if (user_obj == NULL)
205 {
206 ERROR ("utils_vl_lookup: malloc failed.");
207 return (NULL);
208 }
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)
214 {
215 sfree (user_obj);
216 WARNING("utils_vl_lookup: User-provided constructor failed.");
217 return (NULL);
218 }
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)); \
224 else \
225 sstrncpy (user_obj->ident.field, vl->field, sizeof (user_obj->ident.field)); \
226 } while (0)
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);
234 #undef COPY_FIELD
236 if (user_class->user_obj_list == NULL)
237 {
238 user_class->user_obj_list = user_obj;
239 }
240 else
241 {
242 user_obj_t *last = user_class->user_obj_list;
243 while (last->next != NULL)
244 last = last->next;
245 last->next = user_obj;
246 }
248 return (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)
254 {
255 user_obj_t *ptr;
257 for (ptr = user_class->user_obj_list;
258 ptr != NULL;
259 ptr = ptr->next)
260 {
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))
264 continue;
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))
268 continue;
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))
272 continue;
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))
276 continue;
278 return (ptr);
279 }
281 return (NULL);
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)
287 {
288 user_obj_t *user_obj;
289 int status;
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))
299 return (1);
301 pthread_mutex_lock (&user_class->lock);
302 user_obj = lu_find_user_obj (user_class, vl);
303 if (user_obj == NULL)
304 {
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)
309 return (-1);
310 }
311 pthread_mutex_unlock (&user_class->lock);
313 status = obj->cb_user_obj (ds, vl,
314 user_class->user_class, user_obj->user_obj);
315 if (status != 0)
316 {
317 ERROR ("utils_vl_lookup: The user object callback failed with status %i.",
318 status);
319 /* Returning a negative value means: abort! */
320 if (status < 0)
321 return (status);
322 else
323 return (1);
324 }
326 return (0);
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)
332 {
333 user_class_list_t *ptr;
334 int retval = 0;
336 for (ptr = user_class_list; ptr != NULL; ptr = ptr->next)
337 {
338 int status;
340 status = lu_handle_user_class (obj, ds, vl, &ptr->entry);
341 if (status < 0)
342 return (status);
343 else if (status == 0)
344 retval++;
345 }
347 return (retval);
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)
352 {
353 by_type_entry_t *by_type;
354 char *type_copy;
355 int status;
357 status = c_avl_get (obj->by_type_tree, type, (void *) &by_type);
358 if (status == 0)
359 return (by_type);
361 if (!allocate_if_missing)
362 return (NULL);
364 type_copy = strdup (type);
365 if (type_copy == NULL)
366 {
367 ERROR ("utils_vl_lookup: strdup failed.");
368 return (NULL);
369 }
371 by_type = malloc (sizeof (*by_type));
372 if (by_type == NULL)
373 {
374 ERROR ("utils_vl_lookup: malloc failed.");
375 sfree (type_copy);
376 return (NULL);
377 }
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)
383 {
384 ERROR ("utils_vl_lookup: c_avl_create failed.");
385 sfree (by_type);
386 sfree (type_copy);
387 return (NULL);
388 }
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. */
393 if (status != 0)
394 {
395 ERROR ("utils_vl_lookup: c_avl_insert failed.");
396 c_avl_destroy (by_type->by_plugin_tree);
397 sfree (by_type);
398 sfree (type_copy);
399 return (NULL);
400 }
402 return (by_type);
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)
407 {
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)
415 {
416 if (by_type->wildcard_plugin_list == NULL)
417 {
418 by_type->wildcard_plugin_list = user_class_list;
419 return (0);
420 }
422 ptr = by_type->wildcard_plugin_list;
423 } /* if (plugin is wildcard) */
424 else /* (plugin is not wildcard) */
425 {
426 int status;
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 */
432 {
433 char *plugin_copy = strdup (match->plugin.str);
435 if (plugin_copy == NULL)
436 {
437 ERROR ("utils_vl_lookup: strdup failed.");
438 sfree (user_class_list);
439 return (ENOMEM);
440 }
442 status = c_avl_insert (by_type->by_plugin_tree,
443 plugin_copy, user_class_list);
444 if (status != 0)
445 {
446 ERROR ("utils_vl_lookup: c_avl_insert(\"%s\") failed with status %i.",
447 plugin_copy, status);
448 sfree (plugin_copy);
449 sfree (user_class_list);
450 return (status);
451 }
452 else
453 {
454 return (0);
455 }
456 } /* if (plugin not yet in tree) */
457 } /* if (plugin is not wildcard) */
459 assert (ptr != NULL);
461 while (ptr->next != NULL)
462 ptr = ptr->next;
463 ptr->next = user_class_list;
465 return (0);
466 } /* }}} int lu_add_by_plugin */
468 static void lu_destroy_user_obj (lookup_t *obj, /* {{{ */
469 user_obj_t *user_obj)
470 {
471 while (user_obj != NULL)
472 {
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;
479 sfree (user_obj);
480 user_obj = next;
481 }
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)
486 {
487 while (user_class_list != NULL)
488 {
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;
501 }
502 } /* }}} void lu_destroy_user_class_list */
504 static void lu_destroy_by_type (lookup_t *obj, /* {{{ */
505 by_type_entry_t *by_type)
506 {
508 while (42)
509 {
510 char *plugin = NULL;
511 user_class_list_t *user_class_list = NULL;
512 int status;
514 status = c_avl_pick (by_type->by_plugin_tree,
515 (void *) &plugin, (void *) &user_class_list);
516 if (status != 0)
517 break;
519 DEBUG ("utils_vl_lookup: lu_destroy_by_type: Destroying plugin \"%s\".",
520 plugin);
521 sfree (plugin);
522 lu_destroy_user_class_list (obj, user_class_list);
523 }
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;
531 sfree (by_type);
532 } /* }}} int lu_destroy_by_type */
534 /*
535 * Public functions
536 */
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)
541 {
542 lookup_t *obj = malloc (sizeof (*obj));
543 if (obj == NULL)
544 {
545 ERROR ("utils_vl_lookup: malloc failed.");
546 return (NULL);
547 }
548 memset (obj, 0, sizeof (*obj));
550 obj->by_type_tree = c_avl_create ((void *) strcmp);
551 if (obj->by_type_tree == NULL)
552 {
553 ERROR ("utils_vl_lookup: c_avl_create failed.");
554 sfree (obj);
555 return (NULL);
556 }
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;
563 return (obj);
564 } /* }}} lookup_t *lookup_create */
566 void lookup_destroy (lookup_t *obj) /* {{{ */
567 {
568 int status;
570 if (obj == NULL)
571 return;
573 while (42)
574 {
575 char *type = NULL;
576 by_type_entry_t *by_type = NULL;
578 status = c_avl_pick (obj->by_type_tree, (void *) &type, (void *) &by_type);
579 if (status != 0)
580 break;
582 DEBUG ("utils_vl_lookup: lookup_destroy: Destroying type \"%s\".", type);
583 sfree (type);
584 lu_destroy_by_type (obj, by_type);
585 }
587 c_avl_destroy (obj->by_type_tree);
588 obj->by_type_tree = NULL;
590 sfree (obj);
591 } /* }}} void lookup_destroy */
593 int lookup_add (lookup_t *obj, /* {{{ */
594 identifier_t const *ident, unsigned int group_by, void *user_class)
595 {
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);
600 if (by_type == NULL)
601 return (-1);
603 user_class_obj = malloc (sizeof (*user_class_obj));
604 if (user_class_obj == NULL)
605 {
606 ERROR ("utils_vl_lookup: malloc failed.");
607 return (ENOMEM);
608 }
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)
622 {
623 by_type_entry_t *by_type = NULL;
624 user_class_list_t *user_class_list = NULL;
625 int retval = 0;
626 int status;
628 if ((obj == NULL) || (ds == NULL) || (vl == NULL))
629 return (-EINVAL);
631 by_type = lu_search_by_type (obj, vl->type, /* allocate = */ 0);
632 if (by_type == NULL)
633 return (0);
635 status = c_avl_get (by_type->by_plugin_tree,
636 vl->plugin, (void *) &user_class_list);
637 if (status == 0)
638 {
639 status = lu_handle_user_class_list (obj, ds, vl, user_class_list);
640 if (status < 0)
641 return (status);
642 retval += status;
643 }
645 if (by_type->wildcard_plugin_list != NULL)
646 {
647 status = lu_handle_user_class_list (obj, ds, vl,
648 by_type->wildcard_plugin_list);
649 if (status < 0)
650 return (status);
651 retval += status;
652 }
654 return (retval);
655 } /* }}} lookup_search */