Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / utils_vl_lookup.c
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  **/
26
27 #include "collectd.h"
28
29 #include <pthread.h>
30 #include <regex.h>
31
32 #include "common.h"
33 #include "utils_vl_lookup.h"
34 #include "utils_avltree.h"
35
36 #if HAVE_LIBKSTAT
37 kstat_ctl_t *kc;
38 #endif /* HAVE_LIBKSTAT */
39
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
48
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;
59
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;
67
68   unsigned int group_by;
69 };
70 typedef struct identifier_match_s identifier_match_t;
71
72 struct lookup_s
73 {
74   c_avl_tree_t *by_type_tree;
75
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 };
81
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;
88
89   user_obj_t *next;
90 };
91
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;
100
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 };
108
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;
115
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);
127
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 */
141
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;
147
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   }
154
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;
160   
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;
172   
173   return (0);
174 } /* }}} int lu_copy_ident_to_match_part */
175
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));
180
181   match->group_by = group_by;
182
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)
188
189   COPY_FIELD (host);
190   COPY_FIELD (plugin);
191   COPY_FIELD (plugin_instance);
192   COPY_FIELD (type);
193   COPY_FIELD (type_instance);
194
195 #undef COPY_FIELD
196
197   return (0);
198 } /* }}} int lu_copy_ident_to_match */
199
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;
206
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;
215
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   }
223
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)
231
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);
237
238 #undef COPY_FIELD
239
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   }
251
252   return (user_obj);
253 } /* }}} void *lu_create_user_obj */
254
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;
260
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;
281
282     return (ptr);
283   }
284
285   return (NULL);
286 } /* }}} user_obj_t *lu_find_user_obj */
287
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;
294
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);
298
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);
304
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     if (user_obj == NULL) {
312       pthread_mutex_unlock (&user_class->lock);
313       return (-1);
314     }
315   }
316   pthread_mutex_unlock (&user_class->lock);
317
318   status = obj->cb_user_obj (ds, vl,
319       user_class->user_class, user_obj->user_obj);
320   if (status != 0)
321   {
322     ERROR ("utils_vl_lookup: The user object callback failed with status %i.",
323         status);
324     /* Returning a negative value means: abort! */
325     if (status < 0)
326       return (status);
327     else
328       return (1);
329   }
330
331   return (0);
332 } /* }}} int lu_handle_user_class */
333
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)
337 {
338   user_class_list_t *ptr;
339   int retval = 0;
340   
341   for (ptr = user_class_list; ptr != NULL; ptr = ptr->next)
342   {
343     int status;
344
345     status = lu_handle_user_class (obj, ds, vl, &ptr->entry);
346     if (status < 0)
347       return (status);
348     else if (status == 0)
349       retval++;
350   }
351
352   return (retval);
353 } /* }}} int lu_handle_user_class_list */
354
355 static by_type_entry_t *lu_search_by_type (lookup_t *obj, /* {{{ */
356     char const *type, _Bool allocate_if_missing)
357 {
358   by_type_entry_t *by_type;
359   char *type_copy;
360   int status;
361
362   status = c_avl_get (obj->by_type_tree, type, (void *) &by_type);
363   if (status == 0)
364     return (by_type);
365
366   if (!allocate_if_missing)
367     return (NULL);
368
369   type_copy = strdup (type);
370   if (type_copy == NULL)
371   {
372     ERROR ("utils_vl_lookup: strdup failed.");
373     return (NULL);
374   }
375
376   by_type = malloc (sizeof (*by_type));
377   if (by_type == NULL)
378   {
379     ERROR ("utils_vl_lookup: malloc failed.");
380     sfree (type_copy);
381     return (NULL);
382   }
383   memset (by_type, 0, sizeof (*by_type));
384   by_type->wildcard_plugin_list = NULL;
385   
386   by_type->by_plugin_tree = c_avl_create ((void *) strcmp);
387   if (by_type->by_plugin_tree == NULL)
388   {
389     ERROR ("utils_vl_lookup: c_avl_create failed.");
390     sfree (by_type);
391     sfree (type_copy);
392     return (NULL);
393   }
394
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. */
398   if (status != 0)
399   {
400     ERROR ("utils_vl_lookup: c_avl_insert failed.");
401     c_avl_destroy (by_type->by_plugin_tree);
402     sfree (by_type);
403     sfree (type_copy);
404     return (NULL);
405   }
406   
407   return (by_type);
408 } /* }}} by_type_entry_t *lu_search_by_type */
409
410 static int lu_add_by_plugin (by_type_entry_t *by_type, /* {{{ */
411     user_class_list_t *user_class_list)
412 {
413   user_class_list_t *ptr = NULL;
414   identifier_match_t const *match = &user_class_list->entry.match;
415
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)
420   {
421     if (by_type->wildcard_plugin_list == NULL)
422     {
423       by_type->wildcard_plugin_list = user_class_list;
424       return (0);
425     }
426
427     ptr = by_type->wildcard_plugin_list;
428   } /* if (plugin is wildcard) */
429   else /* (plugin is not wildcard) */
430   {
431     int status;
432
433     status = c_avl_get (by_type->by_plugin_tree,
434         match->plugin.str, (void *) &ptr);
435
436     if (status != 0) /* plugin not yet in tree */
437     {
438       char *plugin_copy = strdup (match->plugin.str);
439
440       if (plugin_copy == NULL)
441       {
442         ERROR ("utils_vl_lookup: strdup failed.");
443         sfree (user_class_list);
444         return (ENOMEM);
445       }
446
447       status = c_avl_insert (by_type->by_plugin_tree,
448           plugin_copy, user_class_list);
449       if (status != 0)
450       {
451         ERROR ("utils_vl_lookup: c_avl_insert(\"%s\") failed with status %i.",
452             plugin_copy, status);
453         sfree (plugin_copy);
454         sfree (user_class_list);
455         return (status);
456       }
457       else
458       {
459         return (0);
460       }
461     } /* if (plugin not yet in tree) */
462   } /* if (plugin is not wildcard) */
463
464   assert (ptr != NULL);
465
466   while (ptr->next != NULL)
467     ptr = ptr->next;
468   ptr->next = user_class_list;
469
470   return (0);
471 } /* }}} int lu_add_by_plugin */
472
473 static void lu_destroy_user_obj (lookup_t *obj, /* {{{ */
474     user_obj_t *user_obj)
475 {
476   while (user_obj != NULL)
477   {
478     user_obj_t *next = user_obj->next;
479
480     if (obj->cb_free_obj != NULL)
481       obj->cb_free_obj (user_obj->user_obj);
482     user_obj->user_obj = NULL;
483
484     sfree (user_obj);
485     user_obj = next;
486   }
487 } /* }}} void lu_destroy_user_obj */
488
489 static void lu_destroy_user_class_list (lookup_t *obj, /* {{{ */
490     user_class_list_t *user_class_list)
491 {
492   while (user_class_list != NULL)
493   {
494     user_class_list_t *next = user_class_list->next;
495
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;
499
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);
503
504     sfree (user_class_list);
505     user_class_list = next;
506   }
507 } /* }}} void lu_destroy_user_class_list */
508
509 static void lu_destroy_by_type (lookup_t *obj, /* {{{ */
510     by_type_entry_t *by_type)
511 {
512   
513   while (42)
514   {
515     char *plugin = NULL;
516     user_class_list_t *user_class_list = NULL;
517     int status;
518
519     status = c_avl_pick (by_type->by_plugin_tree,
520         (void *) &plugin, (void *) &user_class_list);
521     if (status != 0)
522       break;
523
524     DEBUG ("utils_vl_lookup: lu_destroy_by_type: Destroying plugin \"%s\".",
525         plugin);
526     sfree (plugin);
527     lu_destroy_user_class_list (obj, user_class_list);
528   }
529
530   c_avl_destroy (by_type->by_plugin_tree);
531   by_type->by_plugin_tree = NULL;
532
533   lu_destroy_user_class_list (obj, by_type->wildcard_plugin_list);
534   by_type->wildcard_plugin_list = NULL;
535
536   sfree (by_type);
537 } /* }}} int lu_destroy_by_type */
538
539 /*
540  * Public functions
541  */
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)
546 {
547   lookup_t *obj = malloc (sizeof (*obj));
548   if (obj == NULL)
549   {
550     ERROR ("utils_vl_lookup: malloc failed.");
551     return (NULL);
552   }
553   memset (obj, 0, sizeof (*obj));
554
555   obj->by_type_tree = c_avl_create ((void *) strcmp);
556   if (obj->by_type_tree == NULL)
557   {
558     ERROR ("utils_vl_lookup: c_avl_create failed.");
559     sfree (obj);
560     return (NULL);
561   }
562
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;
567
568   return (obj);
569 } /* }}} lookup_t *lookup_create */
570
571 void lookup_destroy (lookup_t *obj) /* {{{ */
572 {
573   int status;
574
575   if (obj == NULL)
576     return;
577
578   while (42)
579   {
580     char *type = NULL;
581     by_type_entry_t *by_type = NULL;
582
583     status = c_avl_pick (obj->by_type_tree, (void *) &type, (void *) &by_type);
584     if (status != 0)
585       break;
586
587     DEBUG ("utils_vl_lookup: lookup_destroy: Destroying type \"%s\".", type);
588     sfree (type);
589     lu_destroy_by_type (obj, by_type);
590   }
591
592   c_avl_destroy (obj->by_type_tree);
593   obj->by_type_tree = NULL;
594
595   sfree (obj);
596 } /* }}} void lookup_destroy */
597
598 int lookup_add (lookup_t *obj, /* {{{ */
599     identifier_t const *ident, unsigned int group_by, void *user_class)
600 {
601   by_type_entry_t *by_type = NULL;
602   user_class_list_t *user_class_obj;
603
604   by_type = lu_search_by_type (obj, ident->type, /* allocate = */ 1);
605   if (by_type == NULL)
606     return (-1);
607
608   user_class_obj = malloc (sizeof (*user_class_obj));
609   if (user_class_obj == NULL)
610   {
611     ERROR ("utils_vl_lookup: malloc failed.");
612     return (ENOMEM);
613   }
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;
620
621   return (lu_add_by_plugin (by_type, user_class_obj));
622 } /* }}} int lookup_add */
623
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)
627 {
628   by_type_entry_t *by_type = NULL;
629   user_class_list_t *user_class_list = NULL;
630   int retval = 0;
631   int status;
632
633   if ((obj == NULL) || (ds == NULL) || (vl == NULL))
634     return (-EINVAL);
635
636   by_type = lu_search_by_type (obj, vl->type, /* allocate = */ 0);
637   if (by_type == NULL)
638     return (0);
639
640   status = c_avl_get (by_type->by_plugin_tree,
641       vl->plugin, (void *) &user_class_list);
642   if (status == 0)
643   {
644     status = lu_handle_user_class_list (obj, ds, vl, user_class_list);
645     if (status < 0)
646       return (status);
647     retval += status;
648   }
649
650   if (by_type->wildcard_plugin_list != NULL)
651   {
652     status = lu_handle_user_class_list (obj, ds, vl,
653         by_type->wildcard_plugin_list);
654     if (status < 0)
655       return (status);
656     retval += status;
657   }
658     
659   return (retval);
660 } /* }}} lookup_search */