d867c848fa51d26097c752316e5d803bac479a1e
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 **/
51 #if HAVE_CONFIG_H
52 # include "config.h"
53 #endif
55 #include "common.h"
56 #include "plugin.h"
57 #include "utils_ignorelist.h"
59 /*
60 * private prototypes
61 */
62 struct ignorelist_item_s
63 {
64 #if HAVE_REGEX_H
65 regex_t *rmatch; /* regular expression entry identification */
66 #endif
67 char *smatch; /* string entry identification */
68 struct ignorelist_item_s *next;
69 };
70 typedef struct ignorelist_item_s ignorelist_item_t;
72 struct ignorelist_s
73 {
74 int ignore; /* ignore entries */
75 ignorelist_item_t *head; /* pointer to the first entry */
76 };
78 /* *** *** *** ********************************************* *** *** *** */
79 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
80 /* *** *** *** ********************************************* *** *** *** */
82 static inline void ignorelist_append (ignorelist_t *il, ignorelist_item_t *item)
83 {
84 assert ((il != NULL) && (item != NULL));
86 item->next = il->head;
87 il->head = item;
88 }
90 #if HAVE_REGEX_H
91 static int ignorelist_append_regex(ignorelist_t *il, const char *re_str)
92 {
93 regex_t *re;
94 ignorelist_item_t *entry;
95 int status;
97 re = calloc (1, sizeof (*re));
98 if (re == NULL)
99 {
100 ERROR ("ignorelist_append_regex: calloc failed.");
101 return (ENOMEM);
102 }
104 status = regcomp (re, re_str, REG_EXTENDED);
105 if (status != 0)
106 {
107 char errbuf[1024];
108 (void) regerror (status, re, errbuf, sizeof (errbuf));
109 ERROR ("utils_ignorelist: regcomp failed: %s", errbuf);
110 ERROR ("ignorelist_append_regex: Compiling regular expression \"%s\" failed: %s", re_str, errbuf);
111 sfree (re);
112 return (status);
113 }
115 entry = calloc (1, sizeof (*entry));
116 if (entry == NULL)
117 {
118 ERROR ("ignorelist_append_regex: calloc failed.");
119 regfree (re);
120 sfree (re);
121 return (ENOMEM);
122 }
123 entry->rmatch = re;
125 ignorelist_append (il, entry);
126 return (0);
127 } /* int ignorelist_append_regex */
128 #endif
130 static int ignorelist_append_string(ignorelist_t *il, const char *entry)
131 {
132 ignorelist_item_t *new;
134 /* create new entry */
135 if ((new = calloc(1, sizeof (*new))) == NULL )
136 {
137 ERROR ("cannot allocate new entry");
138 return (1);
139 }
140 new->smatch = sstrdup(entry);
142 /* append new entry */
143 ignorelist_append (il, new);
145 return (0);
146 } /* int ignorelist_append_string(ignorelist_t *il, const char *entry) */
148 #if HAVE_REGEX_H
149 /*
150 * check list for entry regex match
151 * return 1 if found
152 */
153 static int ignorelist_match_regex (ignorelist_item_t *item, const char *entry)
154 {
155 assert ((item != NULL) && (item->rmatch != NULL)
156 && (entry != NULL) && (strlen (entry) > 0));
158 /* match regex */
159 if (regexec (item->rmatch, entry, 0, NULL, 0) == 0)
160 return (1);
162 return (0);
163 } /* int ignorelist_match_regex (ignorelist_item_t *item, const char *entry) */
164 #endif
166 /*
167 * check list for entry string match
168 * return 1 if found
169 */
170 static int ignorelist_match_string (ignorelist_item_t *item, const char *entry)
171 {
172 assert ((item != NULL) && (item->smatch != NULL)
173 && (entry != NULL) && (strlen (entry) > 0));
175 if (strcmp (entry, item->smatch) == 0)
176 return (1);
178 return (0);
179 } /* int ignorelist_match_string (ignorelist_item_t *item, const char *entry) */
182 /* *** *** *** ******************************************** *** *** *** */
183 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
184 /* *** *** *** ******************************************** *** *** *** */
186 /*
187 * create the ignorelist_t with known ignore state
188 * return pointer to ignorelist_t
189 */
190 ignorelist_t *ignorelist_create (int invert)
191 {
192 ignorelist_t *il;
194 il = calloc (1, sizeof (*il));
195 if (il == NULL)
196 return NULL;
198 /*
199 * ->ignore == 0 => collect
200 * ->ignore == 1 => ignore
201 */
202 il->ignore = invert ? 0 : 1;
204 return (il);
205 } /* ignorelist_t *ignorelist_create (int ignore) */
207 /*
208 * free memory used by ignorelist_t
209 */
210 void ignorelist_free (ignorelist_t *il)
211 {
212 ignorelist_item_t *this;
213 ignorelist_item_t *next;
215 if (il == NULL)
216 return;
218 for (this = il->head; this != NULL; this = next)
219 {
220 next = this->next;
221 #if HAVE_REGEX_H
222 if (this->rmatch != NULL)
223 {
224 regfree (this->rmatch);
225 sfree (this->rmatch);
226 this->rmatch = NULL;
227 }
228 #endif
229 if (this->smatch != NULL)
230 {
231 sfree (this->smatch);
232 this->smatch = NULL;
233 }
234 sfree (this);
235 }
237 sfree (il);
238 il = NULL;
239 } /* void ignorelist_destroy (ignorelist_t *il) */
241 /*
242 * set ignore state of the ignorelist_t
243 */
244 void ignorelist_set_invert (ignorelist_t *il, int invert)
245 {
246 if (il == NULL)
247 {
248 DEBUG("ignore call with ignorelist_t == NULL");
249 return;
250 }
252 il->ignore = invert ? 0 : 1;
253 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
255 /*
256 * append entry into ignorelist_t
257 * return 0 for success
258 */
259 int ignorelist_add (ignorelist_t *il, const char *entry)
260 {
261 size_t len;
263 if (il == NULL)
264 {
265 DEBUG ("add called with ignorelist_t == NULL");
266 return (1);
267 }
269 len = strlen (entry);
271 /* append nothing */
272 if (len == 0)
273 {
274 DEBUG("not appending: empty entry");
275 return (1);
276 }
278 #if HAVE_REGEX_H
279 /* regex string is enclosed in "/.../" */
280 if ((len > 2) && (entry[0] == '/') && entry[len - 1] == '/')
281 {
282 char *copy;
283 int status;
285 /* skip leading slash */
286 copy = strdup (entry + 1);
287 if (copy == NULL)
288 return ENOMEM;
290 /* trim trailing slash */
291 copy[strlen (copy) - 1] = 0;
293 status = ignorelist_append_regex (il, copy);
294 sfree (copy);
295 return status;
296 }
297 #endif
299 return ignorelist_append_string(il, entry);
300 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
302 /*
303 * check list for entry
304 * return 1 for ignored entry
305 */
306 int ignorelist_match (ignorelist_t *il, const char *entry)
307 {
308 ignorelist_item_t *traverse;
310 /* if no entries, collect all */
311 if ((il == NULL) || (il->head == NULL))
312 return (0);
314 if ((entry == NULL) || (strlen (entry) == 0))
315 return (0);
317 /* traverse list and check entries */
318 for (traverse = il->head; traverse != NULL; traverse = traverse->next)
319 {
320 #if HAVE_REGEX_H
321 if (traverse->rmatch != NULL)
322 {
323 if (ignorelist_match_regex (traverse, entry))
324 return (il->ignore);
325 }
326 else
327 #endif
328 {
329 if (ignorelist_match_string (traverse, entry))
330 return (il->ignore);
331 }
332 } /* for traverse */
334 return (1 - il->ignore);
335 } /* int ignorelist_match (ignorelist_t *il, const char *entry) */