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