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