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