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>
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.
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.
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
21 * Lubos Stanek <lubek at users.sourceforge.net>
22 * Florian Forster <octo at collectd.org>
25 * ignorelist handles plugin's list of configured collectable
26 * entries with global ignore action
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))
57 #include "utils_ignorelist.h"
62 struct ignorelist_item_s
65 regex_t *rmatch; /* regular expression entry identification */
67 char *smatch; /* string entry identification */
68 struct ignorelist_item_s *next;
70 typedef struct ignorelist_item_s ignorelist_item_t;
74 int ignore; /* ignore entries */
75 ignorelist_item_t *head; /* pointer to the first entry */
78 /* *** *** *** ********************************************* *** *** *** */
79 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
80 /* *** *** *** ********************************************* *** *** *** */
82 static inline void ignorelist_append (ignorelist_t *il, ignorelist_item_t *item)
84 assert ((il != NULL) && (item != NULL));
86 item->next = il->head;
91 static int ignorelist_append_regex(ignorelist_t *il, const char *re_str)
94 ignorelist_item_t *entry;
97 re = malloc (sizeof (*re));
100 ERROR ("ignorelist_append_regex: malloc failed.");
103 memset (re, 0, sizeof (*re));
105 status = regcomp (re, re_str, REG_EXTENDED);
109 (void) regerror (status, re, errbuf, sizeof (errbuf));
110 ERROR ("utils_ignorelist: regcomp failed: %s", errbuf);
111 ERROR ("ignorelist_append_regex: Compiling regular expression \"%s\" failed: %s", re_str, errbuf);
116 entry = malloc (sizeof (*entry));
119 ERROR ("ignorelist_append_regex: malloc failed.");
124 memset (entry, 0, sizeof (*entry));
127 ignorelist_append (il, entry);
129 } /* int ignorelist_append_regex */
132 static int ignorelist_append_string(ignorelist_t *il, const char *entry)
134 ignorelist_item_t *new;
136 /* create new entry */
137 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
139 ERROR ("cannot allocate new entry");
142 memset (new, '\0', sizeof(ignorelist_item_t));
143 new->smatch = sstrdup(entry);
145 /* append new entry */
146 ignorelist_append (il, new);
149 } /* int ignorelist_append_string(ignorelist_t *il, const char *entry) */
153 * check list for entry regex match
156 static int ignorelist_match_regex (ignorelist_item_t *item, const char *entry)
158 assert ((item != NULL) && (item->rmatch != NULL)
159 && (entry != NULL) && (strlen (entry) > 0));
162 if (regexec (item->rmatch, entry, 0, NULL, 0) == 0)
166 } /* int ignorelist_match_regex (ignorelist_item_t *item, const char *entry) */
170 * check list for entry string match
173 static int ignorelist_match_string (ignorelist_item_t *item, const char *entry)
175 assert ((item != NULL) && (item->smatch != NULL)
176 && (entry != NULL) && (strlen (entry) > 0));
178 if (strcmp (entry, item->smatch) == 0)
182 } /* int ignorelist_match_string (ignorelist_item_t *item, const char *entry) */
185 /* *** *** *** ******************************************** *** *** *** */
186 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
187 /* *** *** *** ******************************************** *** *** *** */
190 * create the ignorelist_t with known ignore state
191 * return pointer to ignorelist_t
193 ignorelist_t *ignorelist_create (int invert)
197 il = malloc (sizeof (*il));
200 memset (il, 0, sizeof (*il));
203 * ->ignore == 0 => collect
204 * ->ignore == 1 => ignore
206 il->ignore = invert ? 0 : 1;
209 } /* ignorelist_t *ignorelist_create (int ignore) */
212 * free memory used by ignorelist_t
214 void ignorelist_free (ignorelist_t *il)
216 ignorelist_item_t *this;
217 ignorelist_item_t *next;
222 for (this = il->head; this != NULL; this = next)
226 if (this->rmatch != NULL)
228 regfree (this->rmatch);
229 sfree (this->rmatch);
233 if (this->smatch != NULL)
235 sfree (this->smatch);
243 } /* void ignorelist_destroy (ignorelist_t *il) */
246 * set ignore state of the ignorelist_t
248 void ignorelist_set_invert (ignorelist_t *il, int invert)
252 DEBUG("ignore call with ignorelist_t == NULL");
256 il->ignore = invert ? 0 : 1;
257 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
260 * append entry into ignorelist_t
261 * return 0 for success
263 int ignorelist_add (ignorelist_t *il, const char *entry)
269 DEBUG ("add called with ignorelist_t == NULL");
273 len = strlen (entry);
278 DEBUG("not appending: empty entry");
283 /* regex string is enclosed in "/.../" */
284 if ((len > 2) && (entry[0] == '/') && entry[len - 1] == '/')
289 /* skip leading slash */
290 copy = strdup (entry + 1);
294 /* trim trailing slash */
295 copy[strlen (copy) - 1] = 0;
297 status = ignorelist_append_regex (il, copy);
303 return ignorelist_append_string(il, entry);
304 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
307 * check list for entry
308 * return 1 for ignored entry
310 int ignorelist_match (ignorelist_t *il, const char *entry)
312 ignorelist_item_t *traverse;
314 /* if no entries, collect all */
315 if ((il == NULL) || (il->head == NULL))
318 if ((entry == NULL) || (strlen (entry) == 0))
321 /* traverse list and check entries */
322 for (traverse = il->head; traverse != NULL; traverse = traverse->next)
325 if (traverse->rmatch != NULL)
327 if (ignorelist_match_regex (traverse, entry))
333 if (ignorelist_match_string (traverse, entry))
338 return (1 - il->ignore);
339 } /* int ignorelist_match (ignorelist_t *il, const char *entry) */