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