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 HAVE_LIBKSTAT
37 kstat_ctl_t *kc;
38 #endif /* HAVE_LIBKSTAT */
40 #if BUILD_TEST
41 # define sstrncpy strncpy
42 # define plugin_log(s, ...) do { \
43 printf ("[severity %i] ", s); \
44 printf (__VA_ARGS__); \
45 printf ("\n"); \
46 } while (0)
47 #endif
49 /*
50 * Types
51 */
52 struct part_match_s
53 {
54 char str[DATA_MAX_NAME_LEN];
55 regex_t regex;
56 _Bool is_regex;
57 };
58 typedef struct part_match_s part_match_t;
60 struct identifier_match_s
61 {
62 part_match_t host;
63 part_match_t plugin;
64 part_match_t plugin_instance;
65 part_match_t type;
66 part_match_t type_instance;
68 unsigned int group_by;
69 };
70 typedef struct identifier_match_s identifier_match_t;
72 struct lookup_s
73 {
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;
80 };
82 struct user_obj_s;
83 typedef struct user_obj_s user_obj_t;
84 struct user_obj_s
85 {
86 void *user_obj;
87 identifier_t ident;
89 user_obj_t *next;
90 };
92 struct user_class_s
93 {
94 pthread_mutex_t lock;
95 void *user_class;
96 identifier_match_t match;
97 user_obj_t *user_obj_list; /* list of user_obj */
98 };
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
104 {
105 user_class_t entry;
106 user_class_list_t *next;
107 };
109 struct by_type_entry_s
110 {
111 c_avl_tree_t *by_plugin_tree; /* plugin -> user_class_list_t */
112 user_class_list_t *wildcard_plugin_list;
113 };
114 typedef struct by_type_entry_s by_type_entry_t;
116 /*
117 * Private functions
118 */
119 static _Bool lu_part_matches (part_match_t const *match, /* {{{ */
120 char const *str)
121 {
122 if (match->is_regex)
123 {
124 /* Short cut popular catch-all regex. */
125 if (strcmp (".*", match->str) == 0)
126 return (1);
128 int status = regexec (&match->regex, str,
129 /* nmatch = */ 0, /* pmatch = */ NULL,
130 /* flags = */ 0);
131 if (status == 0)
132 return (1);
133 else
134 return (0);
135 }
136 else if (strcmp (match->str, str) == 0)
137 return (1);
138 else
139 return (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)
144 {
145 size_t len = strlen (ident_part);
146 int status;
148 if ((len < 3) || (ident_part[0] != '/') || (ident_part[len - 1] != '/'))
149 {
150 sstrncpy (match_part->str, ident_part, sizeof (match_part->str));
151 match_part->is_regex = 0;
152 return (0);
153 }
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);
163 if (status != 0)
164 {
165 char errbuf[1024];
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);
169 return (EINVAL);
170 }
171 match_part->is_regex = 1;
173 return (0);
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)
178 {
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); \
185 if (status != 0) \
186 return (status); \
187 } while (0)
189 COPY_FIELD (host);
190 COPY_FIELD (plugin);
191 COPY_FIELD (plugin_instance);
192 COPY_FIELD (type);
193 COPY_FIELD (type_instance);
195 #undef COPY_FIELD
197 return (0);
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)
204 {
205 user_obj_t *user_obj;
207 user_obj = malloc (sizeof (*user_obj));
208 if (user_obj == NULL)
209 {
210 ERROR ("utils_vl_lookup: malloc failed.");
211 return (NULL);
212 }
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)
218 {
219 sfree (user_obj);
220 WARNING("utils_vl_lookup: User-provided constructor failed.");
221 return (NULL);
222 }
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)); \
228 else \
229 sstrncpy (user_obj->ident.field, vl->field, sizeof (user_obj->ident.field)); \
230 } while (0)
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);
238 #undef COPY_FIELD
240 if (user_class->user_obj_list == NULL)
241 {
242 user_class->user_obj_list = user_obj;
243 }
244 else
245 {
246 user_obj_t *last = user_class->user_obj_list;
247 while (last->next != NULL)
248 last = last->next;
249 last->next = user_obj;
250 }
252 return (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)
258 {
259 user_obj_t *ptr;
261 for (ptr = user_class->user_obj_list;
262 ptr != NULL;
263 ptr = ptr->next)
264 {
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))
268 continue;
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))
272 continue;
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))
276 continue;
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))
280 continue;
282 return (ptr);
283 }
285 return (NULL);
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)
291 {
292 user_obj_t *user_obj;
293 int status;
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))
303 return (1);
305 pthread_mutex_lock (&user_class->lock);
306 user_obj = lu_find_user_obj (user_class, vl);
307 if (user_obj == NULL)
308 {
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 pthread_mutex_unlock (&user_class->lock);
312 if (user_obj == NULL)
313 return (-1);
314 }
315 pthread_mutex_unlock (&user_class->lock);
317 status = obj->cb_user_obj (ds, vl,
318 user_class->user_class, user_obj->user_obj);
319 if (status != 0)
320 {
321 ERROR ("utils_vl_lookup: The user object callback failed with status %i.",
322 status);
323 /* Returning a negative value means: abort! */
324 if (status < 0)
325 return (status);
326 else
327 return (1);
328 }
330 return (0);
331 } /* }}} int lu_handle_user_class */
333 static int lu_handle_user_class_list (lookup_t *obj, /* {{{ */
334 data_set_t const *ds, value_list_t const *vl,
335 user_class_list_t *user_class_list)
336 {
337 user_class_list_t *ptr;
338 int retval = 0;
340 for (ptr = user_class_list; ptr != NULL; ptr = ptr->next)
341 {
342 int status;
344 status = lu_handle_user_class (obj, ds, vl, &ptr->entry);
345 if (status < 0)
346 return (status);
347 else if (status == 0)
348 retval++;
349 }
351 return (retval);
352 } /* }}} int lu_handle_user_class_list */
354 static by_type_entry_t *lu_search_by_type (lookup_t *obj, /* {{{ */
355 char const *type, _Bool allocate_if_missing)
356 {
357 by_type_entry_t *by_type;
358 char *type_copy;
359 int status;
361 status = c_avl_get (obj->by_type_tree, type, (void *) &by_type);
362 if (status == 0)
363 return (by_type);
365 if (!allocate_if_missing)
366 return (NULL);
368 type_copy = strdup (type);
369 if (type_copy == NULL)
370 {
371 ERROR ("utils_vl_lookup: strdup failed.");
372 return (NULL);
373 }
375 by_type = malloc (sizeof (*by_type));
376 if (by_type == NULL)
377 {
378 ERROR ("utils_vl_lookup: malloc failed.");
379 sfree (type_copy);
380 return (NULL);
381 }
382 memset (by_type, 0, sizeof (*by_type));
383 by_type->wildcard_plugin_list = NULL;
385 by_type->by_plugin_tree = c_avl_create ((void *) strcmp);
386 if (by_type->by_plugin_tree == NULL)
387 {
388 ERROR ("utils_vl_lookup: c_avl_create failed.");
389 sfree (by_type);
390 sfree (type_copy);
391 return (NULL);
392 }
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. */
397 if (status != 0)
398 {
399 ERROR ("utils_vl_lookup: c_avl_insert failed.");
400 c_avl_destroy (by_type->by_plugin_tree);
401 sfree (by_type);
402 sfree (type_copy);
403 return (NULL);
404 }
406 return (by_type);
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)
411 {
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)
419 {
420 if (by_type->wildcard_plugin_list == NULL)
421 {
422 by_type->wildcard_plugin_list = user_class_list;
423 return (0);
424 }
426 ptr = by_type->wildcard_plugin_list;
427 } /* if (plugin is wildcard) */
428 else /* (plugin is not wildcard) */
429 {
430 int status;
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 */
436 {
437 char *plugin_copy = strdup (match->plugin.str);
439 if (plugin_copy == NULL)
440 {
441 ERROR ("utils_vl_lookup: strdup failed.");
442 sfree (user_class_list);
443 return (ENOMEM);
444 }
446 status = c_avl_insert (by_type->by_plugin_tree,
447 plugin_copy, user_class_list);
448 if (status != 0)
449 {
450 ERROR ("utils_vl_lookup: c_avl_insert(\"%s\") failed with status %i.",
451 plugin_copy, status);
452 sfree (plugin_copy);
453 sfree (user_class_list);
454 return (status);
455 }
456 else
457 {
458 return (0);
459 }
460 } /* if (plugin not yet in tree) */
461 } /* if (plugin is not wildcard) */
463 assert (ptr != NULL);
465 while (ptr->next != NULL)
466 ptr = ptr->next;
467 ptr->next = user_class_list;
469 return (0);
470 } /* }}} int lu_add_by_plugin */
472 static void lu_destroy_user_obj (lookup_t *obj, /* {{{ */
473 user_obj_t *user_obj)
474 {
475 while (user_obj != NULL)
476 {
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;
483 sfree (user_obj);
484 user_obj = next;
485 }
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)
490 {
491 while (user_class_list != NULL)
492 {
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 lu_destroy_user_obj (obj, user_class_list->entry.user_obj_list);
500 user_class_list->entry.user_obj_list = NULL;
501 pthread_mutex_destroy (&user_class_list->entry.lock);
503 sfree (user_class_list);
504 user_class_list = next;
505 }
506 } /* }}} void lu_destroy_user_class_list */
508 static void lu_destroy_by_type (lookup_t *obj, /* {{{ */
509 by_type_entry_t *by_type)
510 {
512 while (42)
513 {
514 char *plugin = NULL;
515 user_class_list_t *user_class_list = NULL;
516 int status;
518 status = c_avl_pick (by_type->by_plugin_tree,
519 (void *) &plugin, (void *) &user_class_list);
520 if (status != 0)
521 break;
523 DEBUG ("utils_vl_lookup: lu_destroy_by_type: Destroying plugin \"%s\".",
524 plugin);
525 sfree (plugin);
526 lu_destroy_user_class_list (obj, user_class_list);
527 }
529 c_avl_destroy (by_type->by_plugin_tree);
530 by_type->by_plugin_tree = NULL;
532 lu_destroy_user_class_list (obj, by_type->wildcard_plugin_list);
533 by_type->wildcard_plugin_list = NULL;
535 sfree (by_type);
536 } /* }}} int lu_destroy_by_type */
538 /*
539 * Public functions
540 */
541 lookup_t *lookup_create (lookup_class_callback_t cb_user_class, /* {{{ */
542 lookup_obj_callback_t cb_user_obj,
543 lookup_free_class_callback_t cb_free_class,
544 lookup_free_obj_callback_t cb_free_obj)
545 {
546 lookup_t *obj = malloc (sizeof (*obj));
547 if (obj == NULL)
548 {
549 ERROR ("utils_vl_lookup: malloc failed.");
550 return (NULL);
551 }
552 memset (obj, 0, sizeof (*obj));
554 obj->by_type_tree = c_avl_create ((void *) strcmp);
555 if (obj->by_type_tree == NULL)
556 {
557 ERROR ("utils_vl_lookup: c_avl_create failed.");
558 sfree (obj);
559 return (NULL);
560 }
562 obj->cb_user_class = cb_user_class;
563 obj->cb_user_obj = cb_user_obj;
564 obj->cb_free_class = cb_free_class;
565 obj->cb_free_obj = cb_free_obj;
567 return (obj);
568 } /* }}} lookup_t *lookup_create */
570 void lookup_destroy (lookup_t *obj) /* {{{ */
571 {
572 int status;
574 if (obj == NULL)
575 return;
577 while (42)
578 {
579 char *type = NULL;
580 by_type_entry_t *by_type = NULL;
582 status = c_avl_pick (obj->by_type_tree, (void *) &type, (void *) &by_type);
583 if (status != 0)
584 break;
586 DEBUG ("utils_vl_lookup: lookup_destroy: Destroying type \"%s\".", type);
587 sfree (type);
588 lu_destroy_by_type (obj, by_type);
589 }
591 c_avl_destroy (obj->by_type_tree);
592 obj->by_type_tree = NULL;
594 sfree (obj);
595 } /* }}} void lookup_destroy */
597 int lookup_add (lookup_t *obj, /* {{{ */
598 identifier_t const *ident, unsigned int group_by, void *user_class)
599 {
600 by_type_entry_t *by_type = NULL;
601 user_class_list_t *user_class_obj;
603 by_type = lu_search_by_type (obj, ident->type, /* allocate = */ 1);
604 if (by_type == NULL)
605 return (-1);
607 user_class_obj = malloc (sizeof (*user_class_obj));
608 if (user_class_obj == NULL)
609 {
610 ERROR ("utils_vl_lookup: malloc failed.");
611 return (ENOMEM);
612 }
613 memset (user_class_obj, 0, sizeof (*user_class_obj));
614 pthread_mutex_init (&user_class_obj->entry.lock, /* attr = */ NULL);
615 user_class_obj->entry.user_class = user_class;
616 lu_copy_ident_to_match (&user_class_obj->entry.match, ident, group_by);
617 user_class_obj->entry.user_obj_list = NULL;
618 user_class_obj->next = NULL;
620 return (lu_add_by_plugin (by_type, user_class_obj));
621 } /* }}} int lookup_add */
623 /* returns the number of successful calls to the callback function */
624 int lookup_search (lookup_t *obj, /* {{{ */
625 data_set_t const *ds, value_list_t const *vl)
626 {
627 by_type_entry_t *by_type = NULL;
628 user_class_list_t *user_class_list = NULL;
629 int retval = 0;
630 int status;
632 if ((obj == NULL) || (ds == NULL) || (vl == NULL))
633 return (-EINVAL);
635 by_type = lu_search_by_type (obj, vl->type, /* allocate = */ 0);
636 if (by_type == NULL)
637 return (0);
639 status = c_avl_get (by_type->by_plugin_tree,
640 vl->plugin, (void *) &user_class_list);
641 if (status == 0)
642 {
643 status = lu_handle_user_class_list (obj, ds, vl, user_class_list);
644 if (status < 0)
645 return (status);
646 retval += status;
647 }
649 if (by_type->wildcard_plugin_list != NULL)
650 {
651 status = lu_handle_user_class_list (obj, ds, vl,
652 by_type->wildcard_plugin_list);
653 if (status < 0)
654 return (status);
655 retval += status;
656 }
658 return (retval);
659 } /* }}} lookup_search */