Merge pull request #1460 from rubenk/mark-lvm2app-linux-only
[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 *re_str)
93 {
94         regex_t *re;
95         ignorelist_item_t *entry;
96         int status;
97
98         re = malloc (sizeof (*re));
99         if (re == NULL)
100         {
101                 ERROR ("utils_ignorelist: malloc failed");
102                 return (ENOMEM);
103         }
104         memset (re, 0, sizeof (*re));
105
106         status = regcomp (re, re_str, REG_EXTENDED);
107         if (status != 0)
108         {
109                 char errbuf[1024] = "";
110                 regerror (status, re, errbuf, sizeof (errbuf));
111                 ERROR ("utils_ignorelist: regcomp failed: %s", errbuf);
112                 regfree (re);
113                 sfree (re);
114                 return (status);
115         }
116
117         entry = malloc (sizeof (*entry));
118         if (entry == NULL)
119         {
120                 ERROR ("utils_ignorelist: malloc failed");
121                 regfree (re);
122                 sfree (re);
123                 return (ENOMEM);
124         }
125         memset (entry, 0, sizeof (*entry));
126         entry->rmatch = re;
127
128         ignorelist_append (il, entry);
129         return (0);
130 } /* int ignorelist_append_regex */
131 #endif
132
133 static int ignorelist_append_string(ignorelist_t *il, const char *entry)
134 {
135         ignorelist_item_t *new;
136
137         /* create new entry */
138         if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
139         {
140                 ERROR ("cannot allocate new entry");
141                 return (1);
142         }
143         memset (new, '\0', sizeof(ignorelist_item_t));
144         new->smatch = sstrdup(entry);
145
146         /* append new entry */
147         ignorelist_append (il, new);
148
149         return (0);
150 } /* int ignorelist_append_string(ignorelist_t *il, const char *entry) */
151
152 #if HAVE_REGEX_H
153 /*
154  * check list for entry regex match
155  * return 1 if found
156  */
157 static int ignorelist_match_regex (ignorelist_item_t *item, const char *entry)
158 {
159         assert ((item != NULL) && (item->rmatch != NULL)
160                         && (entry != NULL) && (strlen (entry) > 0));
161
162         /* match regex */
163         if (regexec (item->rmatch, entry, 0, NULL, 0) == 0)
164                 return (1);
165
166         return (0);
167 } /* int ignorelist_match_regex (ignorelist_item_t *item, const char *entry) */
168 #endif
169
170 /*
171  * check list for entry string match
172  * return 1 if found
173  */
174 static int ignorelist_match_string (ignorelist_item_t *item, const char *entry)
175 {
176         assert ((item != NULL) && (item->smatch != NULL)
177                         && (entry != NULL) && (strlen (entry) > 0));
178
179         if (strcmp (entry, item->smatch) == 0)
180                 return (1);
181
182         return (0);
183 } /* int ignorelist_match_string (ignorelist_item_t *item, const char *entry) */
184
185
186 /* *** *** *** ******************************************** *** *** *** */
187 /* *** *** *** *** *** ***   public functions   *** *** *** *** *** *** */
188 /* *** *** *** ******************************************** *** *** *** */
189
190 /*
191  * create the ignorelist_t with known ignore state
192  * return pointer to ignorelist_t
193  */
194 ignorelist_t *ignorelist_create (int invert)
195 {
196         ignorelist_t *il;
197
198         /* smalloc exits if it failes */
199         il = (ignorelist_t *) smalloc (sizeof (ignorelist_t));
200         memset (il, '\0', sizeof (ignorelist_t));
201
202         /*
203          * ->ignore == 0  =>  collect
204          * ->ignore == 1  =>  ignore
205          */
206         il->ignore = invert ? 0 : 1;
207
208         return (il);
209 } /* ignorelist_t *ignorelist_create (int ignore) */
210
211 /*
212  * free memory used by ignorelist_t
213  */
214 void ignorelist_free (ignorelist_t *il)
215 {
216         ignorelist_item_t *this;
217         ignorelist_item_t *next;
218
219         if (il == NULL)
220                 return;
221
222         for (this = il->head; this != NULL; this = next)
223         {
224                 next = this->next;
225 #if HAVE_REGEX_H
226                 if (this->rmatch != NULL)
227                 {
228                         regfree (this->rmatch);
229                         sfree (this->rmatch);
230                         this->rmatch = NULL;
231                 }
232 #endif
233                 if (this->smatch != NULL)
234                 {
235                         sfree (this->smatch);
236                         this->smatch = NULL;
237                 }
238                 sfree (this);
239         }
240
241         sfree (il);
242         il = NULL;
243 } /* void ignorelist_destroy (ignorelist_t *il) */
244
245 /*
246  * set ignore state of the ignorelist_t
247  */
248 void ignorelist_set_invert (ignorelist_t *il, int invert)
249 {
250         if (il == NULL)
251         {
252                 DEBUG("ignore call with ignorelist_t == NULL");
253                 return;
254         }
255
256         il->ignore = invert ? 0 : 1;
257 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
258
259 /*
260  * append entry into ignorelist_t
261  * return 0 for success
262  */
263 int ignorelist_add (ignorelist_t *il, const char *entry)
264 {
265         size_t entry_len;
266
267         if (il == NULL)
268         {
269                 DEBUG ("add called with ignorelist_t == NULL");
270                 return (1);
271         }
272
273         entry_len = strlen (entry);
274
275         /* append nothing */
276         if (entry_len == 0)
277         {
278                 DEBUG("not appending: empty entry");
279                 return (1);
280         }
281
282 #if HAVE_REGEX_H
283         /* regex string is enclosed in "/.../" */
284         if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
285         {
286                 char *entry_copy;
287                 size_t entry_copy_size;
288                 int status;
289
290                 /* We need to copy `entry' since it's const */
291                 entry_copy_size = entry_len - 1;
292                 entry_copy = smalloc (entry_copy_size);
293                 sstrncpy (entry_copy, entry + 1, entry_copy_size);
294
295                 status = ignorelist_append_regex(il, entry_copy);
296                 sfree (entry_copy);
297                 return status;
298         }
299 #endif
300
301         return ignorelist_append_string(il, entry);
302 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
303
304 /*
305  * check list for entry
306  * return 1 for ignored entry
307  */
308 int ignorelist_match (ignorelist_t *il, const char *entry)
309 {
310         ignorelist_item_t *traverse;
311
312         /* if no entries, collect all */
313         if ((il == NULL) || (il->head == NULL))
314                 return (0);
315
316         if ((entry == NULL) || (strlen (entry) == 0))
317                 return (0);
318
319         /* traverse list and check entries */
320         for (traverse = il->head; traverse != NULL; traverse = traverse->next)
321         {
322 #if HAVE_REGEX_H
323                 if (traverse->rmatch != NULL)
324                 {
325                         if (ignorelist_match_regex (traverse, entry))
326                                 return (il->ignore);
327                 }
328                 else
329 #endif
330                 {
331                         if (ignorelist_match_string (traverse, entry))
332                                 return (il->ignore);
333                 }
334         } /* for traverse */
335
336         return (1 - il->ignore);
337 } /* int ignorelist_match (ignorelist_t *il, const char *entry) */
338