src/plugin.c: assert (ds->ds_num == vl->values_len); when debug is enabled.
[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 #include "common.h"
51 #include "plugin.h"
52 #include "utils_ignorelist.h"
53
54 /*
55  * private prototypes
56  */
57 struct ignorelist_item_s
58 {
59 #if HAVE_REGEX_H
60         regex_t *rmatch;        /* regular expression entry identification */
61 #endif
62         char *smatch;           /* string entry identification */
63         struct ignorelist_item_s *next;
64 };
65 typedef struct ignorelist_item_s ignorelist_item_t;
66
67 struct ignorelist_s
68 {
69         int ignore;             /* ignore entries */
70         ignorelist_item_t *head;        /* pointer to the first entry */
71 };
72
73 /* *** *** *** ********************************************* *** *** *** */
74 /* *** *** *** *** *** ***   private functions   *** *** *** *** *** *** */
75 /* *** *** *** ********************************************* *** *** *** */
76
77 static inline void ignorelist_append (ignorelist_t *il, ignorelist_item_t *item)
78 {
79         assert ((il != NULL) && (item != NULL));
80
81         item->next = il->head;
82         il->head = item;
83 }
84
85 #if HAVE_REGEX_H
86 static int ignorelist_append_regex(ignorelist_t *il, const char *entry)
87 {
88         int rcompile;
89         regex_t *regtemp;
90         int errsize;
91         char *regerr = NULL;
92         ignorelist_item_t *new;
93
94         /* create buffer */
95         if ((regtemp = malloc(sizeof(regex_t))) == NULL)
96         {
97                 ERROR ("cannot allocate new config entry");
98                 return (1);
99         }
100         memset (regtemp, '\0', sizeof(regex_t));
101
102         /* compile regex */
103         if ((rcompile = regcomp (regtemp, entry, REG_EXTENDED)) != 0)
104         {
105                 /* prepare message buffer */
106                 errsize = regerror(rcompile, regtemp, NULL, 0);
107                 if (errsize)
108                         regerr = smalloc(errsize);
109                 /* get error message */
110                 if (regerror (rcompile, regtemp, regerr, errsize))
111                 {
112                         fprintf (stderr, "Cannot compile regex %s: %i/%s",
113                                         entry, rcompile, regerr);
114                         ERROR ("Cannot compile regex %s: %i/%s",
115                                         entry, rcompile, regerr);
116                 }
117                 else
118                 {
119                         fprintf (stderr, "Cannot compile regex %s: %i",
120                                         entry, rcompile);
121                         ERROR ("Cannot compile regex %s: %i",
122                                         entry, rcompile);
123                 }
124
125                 if (errsize)
126                         sfree (regerr);
127                 regfree (regtemp);
128                 return (1);
129         }
130         DEBUG("regex compiled: %s - %i", entry, rcompile);
131
132         /* create new entry */
133         if ((new = malloc(sizeof(ignorelist_item_t))) == NULL)
134         {
135                 ERROR ("cannot allocate new config entry");
136                 regfree (regtemp);
137                 return (1);
138         }
139         memset (new, '\0', sizeof(ignorelist_item_t));
140         new->rmatch = regtemp;
141
142         /* append new entry */
143         ignorelist_append (il, new);
144
145         return (0);
146 } /* int ignorelist_append_regex(ignorelist_t *il, const char *entry) */
147 #endif
148
149 static int ignorelist_append_string(ignorelist_t *il, const char *entry)
150 {
151         ignorelist_item_t *new;
152
153         /* create new entry */
154         if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
155         {
156                 ERROR ("cannot allocate new entry");
157                 return (1);
158         }
159         memset (new, '\0', sizeof(ignorelist_item_t));
160         new->smatch = sstrdup(entry);
161
162         /* append new entry */
163         ignorelist_append (il, new);
164
165         return (0);
166 } /* int ignorelist_append_string(ignorelist_t *il, const char *entry) */
167
168 #if HAVE_REGEX_H
169 /*
170  * check list for entry regex match
171  * return 1 if found
172  */
173 static int ignorelist_match_regex (ignorelist_item_t *item, const char *entry)
174 {
175         assert ((item != NULL) && (item->rmatch != NULL)
176                         && (entry != NULL) && (strlen (entry) > 0));
177
178         /* match regex */
179         if (regexec (item->rmatch, entry, 0, NULL, 0) == 0)
180                 return (1);
181
182         return (0);
183 } /* int ignorelist_match_regex (ignorelist_item_t *item, const char *entry) */
184 #endif
185
186 /*
187  * check list for entry string match
188  * return 1 if found
189  */
190 static int ignorelist_match_string (ignorelist_item_t *item, const char *entry)
191 {
192         assert ((item != NULL) && (item->smatch != NULL)
193                         && (entry != NULL) && (strlen (entry) > 0));
194
195         if (strcmp (entry, item->smatch) == 0)
196                 return (1);
197
198         return (0);
199 } /* int ignorelist_match_string (ignorelist_item_t *item, const char *entry) */
200
201
202 /* *** *** *** ******************************************** *** *** *** */
203 /* *** *** *** *** *** ***   public functions   *** *** *** *** *** *** */
204 /* *** *** *** ******************************************** *** *** *** */
205
206 /*
207  * create the ignorelist_t with known ignore state
208  * return pointer to ignorelist_t
209  */
210 ignorelist_t *ignorelist_create (int invert)
211 {
212         ignorelist_t *il;
213
214         /* smalloc exits if it failes */
215         il = (ignorelist_t *) smalloc (sizeof (ignorelist_t));
216         DEBUG("Ignorelist created 0x%p, default is %s",
217                         (void *) il,
218                         invert ? "collect" : "ignore");
219
220         memset (il, '\0', sizeof (ignorelist_t));
221
222         /*
223          * ->ignore == 0  =>  collect
224          * ->ignore == 1  =>  ignore
225          */
226         il->ignore = invert ? 0 : 1;
227
228         return (il);
229 } /* ignorelist_t *ignorelist_create (int ignore) */
230
231 /*
232  * free memory used by ignorelist_t
233  */
234 void ignorelist_free (ignorelist_t *il)
235 {
236         ignorelist_item_t *this;
237         ignorelist_item_t *next;
238
239         DEBUG ("(il = 0x%p)", (void *) il);
240
241         if (il == NULL)
242                 return;
243
244         for (this = il->head; this != NULL; this = next)
245         {
246                 next = this->next;
247 #if HAVE_REGEX_H
248                 if (this->rmatch != NULL)
249                 {
250                         regfree (this->rmatch);
251                         this->rmatch = NULL;
252                 }
253 #endif
254                 if (this->smatch != NULL)
255                 {
256                         sfree (this->smatch);
257                         this->smatch = NULL;
258                 }
259                 sfree (this);
260         }
261
262         sfree (il);
263         il = NULL;
264 } /* void ignorelist_destroy (ignorelist_t *il) */
265
266 /*
267  * set ignore state of the ignorelist_t
268  */
269 void ignorelist_set_invert (ignorelist_t *il, int invert)
270 {
271         if (il == NULL)
272         {
273                 DEBUG("ignore call with ignorelist_t == NULL");
274                 return;
275         }
276
277         il->ignore = invert ? 0 : 1;
278 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
279
280 /*
281  * append entry into ignorelist_t
282  * return 1 for success
283  */
284 int ignorelist_add (ignorelist_t *il, const char *entry)
285 {
286         int ret;
287         size_t entry_len;
288
289         if (il == NULL)
290         {
291                 DEBUG ("add called with ignorelist_t == NULL");
292                 return (1);
293         }
294
295         entry_len = strlen (entry);
296
297         /* append nothing */
298         if (entry_len == 0)
299         {
300                 DEBUG("not appending: empty entry");
301                 return (1);
302         }
303
304 #if HAVE_REGEX_H
305         /* regex string is enclosed in "/.../" */
306         if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
307         {
308                 char *entry_copy;
309
310                 /* We need to copy `entry' since it's const */
311                 entry_copy = smalloc (entry_len);
312                 memset (entry_copy, '\0', entry_len);
313                 strncpy (entry_copy, entry + 1, entry_len - 2);
314
315                 DEBUG("I'm about to add regex entry: %s", entry_copy);
316                 ret = ignorelist_append_regex(il, entry_copy);
317                 sfree (entry_copy);
318         }
319         else
320 #endif
321         {
322                 DEBUG("to add entry: %s", entry);
323                 ret = ignorelist_append_string(il, entry);
324         }
325
326         return (ret);
327 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
328
329 /*
330  * check list for entry
331  * return 1 for ignored entry
332  */
333 int ignorelist_match (ignorelist_t *il, const char *entry)
334 {
335         ignorelist_item_t *traverse;
336
337         assert (il != NULL);
338
339         /* if no entries, collect all */
340         if (il->head == NULL)
341                 return (0);
342
343         if ((entry == NULL) || (strlen (entry) == 0))
344                 return (0);
345
346         /* traverse list and check entries */
347         for (traverse = il->head; traverse != NULL; traverse = traverse->next)
348         {
349 #if HAVE_REGEX_H
350                 if (traverse->rmatch != NULL)
351                 {
352                         if (ignorelist_match_regex (traverse, entry))
353                                 return (il->ignore);
354                 }
355                 else
356 #endif
357                 {
358                         if (ignorelist_match_string (traverse, entry))
359                                 return (il->ignore);
360                 }
361         } /* for traverse */
362
363         return (1 - il->ignore);
364 } /* int ignorelist_match (ignorelist_t *il, const char *entry) */
365