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
17 * Licence along with this program; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
22 * Lubos Stanek <lubek at users.sourceforge.net>
23 * Florian Forster <octo at collectd.org>
26 * ignorelist handles plugin's list of configured collectable
27 * entries with global ignore action
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))
58 #include "utils_ignorelist.h"
63 struct ignorelist_item_s
66 regex_t *rmatch; /* regular expression entry identification */
68 char *smatch; /* string entry identification */
69 struct ignorelist_item_s *next;
71 typedef struct ignorelist_item_s ignorelist_item_t;
75 int ignore; /* ignore entries */
76 ignorelist_item_t *head; /* pointer to the first entry */
79 /* *** *** *** ********************************************* *** *** *** */
80 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
81 /* *** *** *** ********************************************* *** *** *** */
83 static inline void ignorelist_append (ignorelist_t *il, ignorelist_item_t *item)
85 assert ((il != NULL) && (item != NULL));
87 item->next = il->head;
92 static int ignorelist_append_regex(ignorelist_t *il, const char *entry)
98 ignorelist_item_t *new;
101 if ((regtemp = malloc(sizeof(regex_t))) == NULL)
103 ERROR ("cannot allocate new config entry");
106 memset (regtemp, '\0', sizeof(regex_t));
109 if ((rcompile = regcomp (regtemp, entry, REG_EXTENDED)) != 0)
111 /* prepare message buffer */
112 errsize = regerror(rcompile, regtemp, NULL, 0);
114 regerr = smalloc(errsize);
115 /* get error message */
116 if (regerror (rcompile, regtemp, regerr, errsize))
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);
125 fprintf (stderr, "Cannot compile regex %s: %i",
127 ERROR ("Cannot compile regex %s: %i",
136 DEBUG("regex compiled: %s - %i", entry, rcompile);
138 /* create new entry */
139 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL)
141 ERROR ("cannot allocate new config entry");
145 memset (new, '\0', sizeof(ignorelist_item_t));
146 new->rmatch = regtemp;
148 /* append new entry */
149 ignorelist_append (il, new);
152 } /* int ignorelist_append_regex(ignorelist_t *il, const char *entry) */
155 static int ignorelist_append_string(ignorelist_t *il, const char *entry)
157 ignorelist_item_t *new;
159 /* create new entry */
160 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
162 ERROR ("cannot allocate new entry");
165 memset (new, '\0', sizeof(ignorelist_item_t));
166 new->smatch = sstrdup(entry);
168 /* append new entry */
169 ignorelist_append (il, new);
172 } /* int ignorelist_append_string(ignorelist_t *il, const char *entry) */
176 * check list for entry regex match
179 static int ignorelist_match_regex (ignorelist_item_t *item, const char *entry)
181 assert ((item != NULL) && (item->rmatch != NULL)
182 && (entry != NULL) && (strlen (entry) > 0));
185 if (regexec (item->rmatch, entry, 0, NULL, 0) == 0)
189 } /* int ignorelist_match_regex (ignorelist_item_t *item, const char *entry) */
193 * check list for entry string match
196 static int ignorelist_match_string (ignorelist_item_t *item, const char *entry)
198 assert ((item != NULL) && (item->smatch != NULL)
199 && (entry != NULL) && (strlen (entry) > 0));
201 if (strcmp (entry, item->smatch) == 0)
205 } /* int ignorelist_match_string (ignorelist_item_t *item, const char *entry) */
208 /* *** *** *** ******************************************** *** *** *** */
209 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
210 /* *** *** *** ******************************************** *** *** *** */
213 * create the ignorelist_t with known ignore state
214 * return pointer to ignorelist_t
216 ignorelist_t *ignorelist_create (int invert)
220 /* smalloc exits if it failes */
221 il = (ignorelist_t *) smalloc (sizeof (ignorelist_t));
222 memset (il, '\0', sizeof (ignorelist_t));
225 * ->ignore == 0 => collect
226 * ->ignore == 1 => ignore
228 il->ignore = invert ? 0 : 1;
231 } /* ignorelist_t *ignorelist_create (int ignore) */
234 * free memory used by ignorelist_t
236 void ignorelist_free (ignorelist_t *il)
238 ignorelist_item_t *this;
239 ignorelist_item_t *next;
244 for (this = il->head; this != NULL; this = next)
248 if (this->rmatch != NULL)
250 regfree (this->rmatch);
254 if (this->smatch != NULL)
256 sfree (this->smatch);
264 } /* void ignorelist_destroy (ignorelist_t *il) */
267 * set ignore state of the ignorelist_t
269 void ignorelist_set_invert (ignorelist_t *il, int invert)
273 DEBUG("ignore call with ignorelist_t == NULL");
277 il->ignore = invert ? 0 : 1;
278 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
281 * append entry into ignorelist_t
282 * return 1 for success
284 int ignorelist_add (ignorelist_t *il, const char *entry)
291 DEBUG ("add called with ignorelist_t == NULL");
295 entry_len = strlen (entry);
300 DEBUG("not appending: empty entry");
305 /* regex string is enclosed in "/.../" */
306 if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
309 size_t entry_copy_size;
311 /* We need to copy `entry' since it's const */
312 entry_copy_size = entry_len - 1;
313 entry_copy = smalloc (entry_copy_size);
314 sstrncpy (entry_copy, entry + 1, entry_copy_size);
316 DEBUG("I'm about to add regex entry: %s", entry_copy);
317 ret = ignorelist_append_regex(il, entry_copy);
323 DEBUG("to add entry: %s", entry);
324 ret = ignorelist_append_string(il, entry);
328 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
331 * check list for entry
332 * return 1 for ignored entry
334 int ignorelist_match (ignorelist_t *il, const char *entry)
336 ignorelist_item_t *traverse;
338 /* if no entries, collect all */
339 if ((il == NULL) || (il->head == NULL))
342 if ((entry == NULL) || (strlen (entry) == 0))
345 /* traverse list and check entries */
346 for (traverse = il->head; traverse != NULL; traverse = traverse->next)
349 if (traverse->rmatch != NULL)
351 if (ignorelist_match_regex (traverse, entry))
357 if (ignorelist_match_string (traverse, entry))
362 return (1 - il->ignore);
363 } /* int ignorelist_match (ignorelist_t *il, const char *entry) */