Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / daemon / utils_ignorelist.c
1 /**
2  * collectd - src/utils_ignorelist.c
3  * Copyright (C) 2006 Lubos Stanek <lubek at users.sourceforge.net>
4  * Copyright (C) 2008 Florian Forster <octo at collectd.org>
5  *
6  * This program is free software; you can redistribute it and/
7  * or modify it under the terms of the GNU General Public Li-
8  * cence as published by the Free Software Foundation; either
9  * version 2 of the Licence, or any later version.
10  *
11  * This program is distributed in the hope that it will be use-
12  * ful, but WITHOUT ANY WARRANTY; without even the implied war-
13  * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public Licence for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Lubos Stanek <lubek at users.sourceforge.net>
22  *   Florian Forster <octo at collectd.org>
23  **/
24 /**
25  * ignorelist handles plugin's list of configured collectable
26  * entries with global ignore action
27  **/
28 /**
29  * Usage:
30  * 
31  * Define plugin's global pointer variable of type ignorelist_t:
32  *   ignorelist_t *myconfig_ignore;
33  * If you know the state of the global ignore (IgnoreSelected),
34  * allocate the variable with:
35  *   myconfig_ignore = ignorelist_create (YourKnownIgnore);
36  * If you do not know the state of the global ignore,
37  * initialize the global variable and set the ignore flag later:
38  *   myconfig_ignore = ignorelist_init ();
39  * Append single entries in your cf_register'ed callback function:
40  *   ignorelist_add (myconfig_ignore, newentry);
41  * When you hit the IgnoreSelected config option,
42  * offer it to the list:
43  *   ignorelist_ignore (myconfig_ignore, instantly_got_value_of_ignore);
44  * That is all for the ignorelist initialization.
45  * Later during read and write (plugin's registered functions) get
46  * the information whether this entry would be collected or not:
47  *   if (ignorelist_match (myconfig_ignore, thisentry))
48  *     return;
49  **/
50
51 #if HAVE_CONFIG_H
52 # include "config.h"
53 #endif
54
55 #include "common.h"
56 #include "plugin.h"
57 #include "utils_ignorelist.h"
58
59 /*
60  * private prototypes
61  */
62 struct ignorelist_item_s
63 {
64 #if HAVE_REGEX_H
65         regex_t *rmatch;        /* regular expression entry identification */
66 #endif
67         char *smatch;           /* string entry identification */
68         struct ignorelist_item_s *next;
69 };
70 typedef struct ignorelist_item_s ignorelist_item_t;
71
72 struct ignorelist_s
73 {
74         int ignore;             /* ignore entries */
75         ignorelist_item_t *head;        /* pointer to the first entry */
76 };
77
78 /* *** *** *** ********************************************* *** *** *** */
79 /* *** *** *** *** *** ***   private functions   *** *** *** *** *** *** */
80 /* *** *** *** ********************************************* *** *** *** */
81
82 static inline void ignorelist_append (ignorelist_t *il, ignorelist_item_t *item)
83 {
84         assert ((il != NULL) && (item != NULL));
85
86         item->next = il->head;
87         il->head = item;
88 }
89
90 #if HAVE_REGEX_H
91 static int ignorelist_append_regex(ignorelist_t *il, const char *entry)
92 {
93         regex_t *re;
94         ignorelist_item_t *item;
95         int status;
96
97         /* create buffer */
98         re = malloc (sizeof (*re));
99         if (re == NULL)
100         {
101                 ERROR ("ignorelist_append_regex: malloc failed.");
102                 return ENOMEM;
103         }
104         memset (re, 0, sizeof (*re));
105
106         /* compile regex */
107         status = regcomp (re, entry, REG_EXTENDED);
108         if (status != 0)
109         {
110                 char errbuf[1024];
111
112                 (void) regerror (status, re, errbuf, sizeof (errbuf));
113                 ERROR ("ignorelist_append_regex: Compiling regular expression \"%s\" failed: %s", entry, errbuf);
114                 sfree (re);
115                 return status;
116         }
117
118         /* create new entry */
119         item = malloc (sizeof (*item));
120         if (item == NULL)
121         {
122                 ERROR ("ignorelist_append_regex: malloc failed.");
123                 regfree (re);
124                 sfree (re);
125                 return ENOMEM;
126         }
127         memset (item, 0, sizeof (*item));
128         item->rmatch = re;
129
130         /* append new entry */
131         ignorelist_append (il, item);
132
133         return (0);
134 } /* int ignorelist_append_regex(ignorelist_t *il, const char *entry) */
135 #endif
136
137 static int ignorelist_append_string(ignorelist_t *il, const char *entry)
138 {
139         ignorelist_item_t *new;
140
141         /* create new entry */
142         if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
143         {
144                 ERROR ("cannot allocate new entry");
145                 return (1);
146         }
147         memset (new, '\0', sizeof(ignorelist_item_t));
148         new->smatch = sstrdup(entry);
149
150         /* append new entry */
151         ignorelist_append (il, new);
152
153         return (0);
154 } /* int ignorelist_append_string(ignorelist_t *il, const char *entry) */
155
156 #if HAVE_REGEX_H
157 /*
158  * check list for entry regex match
159  * return 1 if found
160  */
161 static int ignorelist_match_regex (ignorelist_item_t *item, const char *entry)
162 {
163         assert ((item != NULL) && (item->rmatch != NULL)
164                         && (entry != NULL) && (strlen (entry) > 0));
165
166         /* match regex */
167         if (regexec (item->rmatch, entry, 0, NULL, 0) == 0)
168                 return (1);
169
170         return (0);
171 } /* int ignorelist_match_regex (ignorelist_item_t *item, const char *entry) */
172 #endif
173
174 /*
175  * check list for entry string match
176  * return 1 if found
177  */
178 static int ignorelist_match_string (ignorelist_item_t *item, const char *entry)
179 {
180         assert ((item != NULL) && (item->smatch != NULL)
181                         && (entry != NULL) && (strlen (entry) > 0));
182
183         if (strcmp (entry, item->smatch) == 0)
184                 return (1);
185
186         return (0);
187 } /* int ignorelist_match_string (ignorelist_item_t *item, const char *entry) */
188
189
190 /* *** *** *** ******************************************** *** *** *** */
191 /* *** *** *** *** *** ***   public functions   *** *** *** *** *** *** */
192 /* *** *** *** ******************************************** *** *** *** */
193
194 /*
195  * create the ignorelist_t with known ignore state
196  * return pointer to ignorelist_t
197  */
198 ignorelist_t *ignorelist_create (int invert)
199 {
200         ignorelist_t *il;
201
202         il = malloc (sizeof (*il));
203         if (il == NULL)
204                 return NULL;
205         memset (il, 0, sizeof (*il));
206
207         /*
208          * ->ignore == 0  =>  collect
209          * ->ignore == 1  =>  ignore
210          */
211         il->ignore = invert ? 0 : 1;
212
213         return (il);
214 } /* ignorelist_t *ignorelist_create (int ignore) */
215
216 /*
217  * free memory used by ignorelist_t
218  */
219 void ignorelist_free (ignorelist_t *il)
220 {
221         ignorelist_item_t *this;
222         ignorelist_item_t *next;
223
224         if (il == NULL)
225                 return;
226
227         for (this = il->head; this != NULL; this = next)
228         {
229                 next = this->next;
230 #if HAVE_REGEX_H
231                 if (this->rmatch != NULL)
232                 {
233                         regfree (this->rmatch);
234                         sfree (this->rmatch);
235                         this->rmatch = NULL;
236                 }
237 #endif
238                 if (this->smatch != NULL)
239                 {
240                         sfree (this->smatch);
241                         this->smatch = NULL;
242                 }
243                 sfree (this);
244         }
245
246         sfree (il);
247         il = NULL;
248 } /* void ignorelist_destroy (ignorelist_t *il) */
249
250 /*
251  * set ignore state of the ignorelist_t
252  */
253 void ignorelist_set_invert (ignorelist_t *il, int invert)
254 {
255         if (il == NULL)
256         {
257                 DEBUG("ignore call with ignorelist_t == NULL");
258                 return;
259         }
260
261         il->ignore = invert ? 0 : 1;
262 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
263
264 /*
265  * append entry into ignorelist_t
266  * return 1 for success
267  */
268 int ignorelist_add (ignorelist_t *il, const char *entry)
269 {
270         size_t len;
271
272         if (il == NULL)
273         {
274                 DEBUG ("add called with ignorelist_t == NULL");
275                 return (1);
276         }
277
278         len = strlen (entry);
279
280         /* append nothing */
281         if (len == 0)
282         {
283                 DEBUG("not appending: empty entry");
284                 return (1);
285         }
286
287 #if HAVE_REGEX_H
288         /* regex string is enclosed in "/.../" */
289         if ((len > 2) && (entry[0] == '/') && entry[len - 1] == '/')
290         {
291                 char *copy;
292                 int status;
293
294                 /* skip leading slash */
295                 copy = strdup (entry + 1);
296                 if (copy == NULL)
297                         return ENOMEM;
298
299                 /* trim trailing slash */
300                 copy[strlen (copy) - 1] = 0;
301
302                 status = ignorelist_append_regex (il, copy);
303                 sfree (copy);
304                 return status;
305         }
306 #endif
307
308         return ignorelist_append_string(il, entry);
309 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
310
311 /*
312  * check list for entry
313  * return 1 for ignored entry
314  */
315 int ignorelist_match (ignorelist_t *il, const char *entry)
316 {
317         ignorelist_item_t *traverse;
318
319         /* if no entries, collect all */
320         if ((il == NULL) || (il->head == NULL))
321                 return (0);
322
323         if ((entry == NULL) || (strlen (entry) == 0))
324                 return (0);
325
326         /* traverse list and check entries */
327         for (traverse = il->head; traverse != NULL; traverse = traverse->next)
328         {
329 #if HAVE_REGEX_H
330                 if (traverse->rmatch != NULL)
331                 {
332                         if (ignorelist_match_regex (traverse, entry))
333                                 return (il->ignore);
334                 }
335                 else
336 #endif
337                 {
338                         if (ignorelist_match_string (traverse, entry))
339                                 return (il->ignore);
340                 }
341         } /* for traverse */
342
343         return (1 - il->ignore);
344 } /* int ignorelist_match (ignorelist_t *il, const char *entry) */
345