2 * collectd - src/utils_ignorelist.c
3 * Copyright (C) 2006 Lubos Stanek <lubek at users.sourceforge.net>
5 * This program is free software; you can redistribute it and/
6 * or modify it under the terms of the GNU General Public Li-
7 * cence as published by the Free Software Foundation; either
8 * version 2 of the Licence, or any later version.
10 * This program is distributed in the hope that it will be use-
11 * ful, but WITHOUT ANY WARRANTY; without even the implied war-
12 * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public Licence for more details.
15 * You should have received a copy of the GNU General Public
16 * Licence along with this program; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
21 * Lubos Stanek <lubek at users.sourceforge.net>
24 * ignorelist handles plugin's list of configured collectable
25 * entries with global ignore action
30 * Define plugin's global pointer variable of type ignorelist_t:
31 * ignorelist_t *myconfig_ignore;
32 * If you know the state of the global ignore (IgnoreSelected),
33 * allocate the variable with:
34 * myconfig_ignore = ignorelist_create (YourKnownIgnore);
35 * If you do not know the state of the global ignore,
36 * initialize the global variable and set the ignore flag later:
37 * myconfig_ignore = ignorelist_init ();
38 * Append single entries in your cf_register'ed callback function:
39 * ignorelist_add (myconfig_ignore, newentry);
40 * When you hit the IgnoreSelected config option,
41 * offer it to the list:
42 * ignorelist_ignore (myconfig_ignore, instantly_got_value_of_ignore);
43 * That is all for the ignorelist initialization.
44 * Later during read and write (plugin's registered functions) get
45 * the information whether this entry would be collected or not:
46 * if (ignorelist_match (myconfig_ignore, thisentry))
56 #include "utils_ignorelist.h"
61 struct ignorelist_item_s
64 regex_t *rmatch; /* regular expression entry identification */
66 char *smatch; /* string entry identification */
67 struct ignorelist_item_s *next;
69 typedef struct ignorelist_item_s ignorelist_item_t;
73 int ignore; /* ignore entries */
74 ignorelist_item_t *head; /* pointer to the first entry */
77 /* *** *** *** ********************************************* *** *** *** */
78 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
79 /* *** *** *** ********************************************* *** *** *** */
81 static inline void ignorelist_append (ignorelist_t *il, ignorelist_item_t *item)
83 assert ((il != NULL) && (item != NULL));
85 item->next = il->head;
90 static int ignorelist_append_regex(ignorelist_t *il, const char *entry)
96 ignorelist_item_t *new;
99 if ((regtemp = malloc(sizeof(regex_t))) == NULL)
101 ERROR ("cannot allocate new config entry");
104 memset (regtemp, '\0', sizeof(regex_t));
107 if ((rcompile = regcomp (regtemp, entry, REG_EXTENDED)) != 0)
109 /* prepare message buffer */
110 errsize = regerror(rcompile, regtemp, NULL, 0);
112 regerr = smalloc(errsize);
113 /* get error message */
114 if (regerror (rcompile, regtemp, regerr, errsize))
116 fprintf (stderr, "Cannot compile regex %s: %i/%s",
117 entry, rcompile, regerr);
118 ERROR ("Cannot compile regex %s: %i/%s",
119 entry, rcompile, regerr);
123 fprintf (stderr, "Cannot compile regex %s: %i",
125 ERROR ("Cannot compile regex %s: %i",
134 DEBUG("regex compiled: %s - %i", entry, rcompile);
136 /* create new entry */
137 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL)
139 ERROR ("cannot allocate new config entry");
143 memset (new, '\0', sizeof(ignorelist_item_t));
144 new->rmatch = regtemp;
146 /* append new entry */
147 ignorelist_append (il, new);
150 } /* int ignorelist_append_regex(ignorelist_t *il, const char *entry) */
153 static int ignorelist_append_string(ignorelist_t *il, const char *entry)
155 ignorelist_item_t *new;
157 /* create new entry */
158 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
160 ERROR ("cannot allocate new entry");
163 memset (new, '\0', sizeof(ignorelist_item_t));
164 new->smatch = sstrdup(entry);
166 /* append new entry */
167 ignorelist_append (il, new);
170 } /* int ignorelist_append_string(ignorelist_t *il, const char *entry) */
174 * check list for entry regex match
177 static int ignorelist_match_regex (ignorelist_item_t *item, const char *entry)
179 assert ((item != NULL) && (item->rmatch != NULL)
180 && (entry != NULL) && (strlen (entry) > 0));
183 if (regexec (item->rmatch, entry, 0, NULL, 0) == 0)
187 } /* int ignorelist_match_regex (ignorelist_item_t *item, const char *entry) */
191 * check list for entry string match
194 static int ignorelist_match_string (ignorelist_item_t *item, const char *entry)
196 assert ((item != NULL) && (item->smatch != NULL)
197 && (entry != NULL) && (strlen (entry) > 0));
199 if (strcmp (entry, item->smatch) == 0)
203 } /* int ignorelist_match_string (ignorelist_item_t *item, const char *entry) */
206 /* *** *** *** ******************************************** *** *** *** */
207 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
208 /* *** *** *** ******************************************** *** *** *** */
211 * create the ignorelist_t with known ignore state
212 * return pointer to ignorelist_t
214 ignorelist_t *ignorelist_create (int invert)
218 /* smalloc exits if it failes */
219 il = (ignorelist_t *) smalloc (sizeof (ignorelist_t));
220 DEBUG("Ignorelist created 0x%p, default is %s",
222 invert ? "collect" : "ignore");
224 memset (il, '\0', sizeof (ignorelist_t));
227 * ->ignore == 0 => collect
228 * ->ignore == 1 => ignore
230 il->ignore = invert ? 0 : 1;
233 } /* ignorelist_t *ignorelist_create (int ignore) */
236 * free memory used by ignorelist_t
238 void ignorelist_free (ignorelist_t *il)
240 ignorelist_item_t *this;
241 ignorelist_item_t *next;
246 for (this = il->head; this != NULL; this = next)
250 if (this->rmatch != NULL)
252 regfree (this->rmatch);
256 if (this->smatch != NULL)
258 sfree (this->smatch);
266 } /* void ignorelist_destroy (ignorelist_t *il) */
269 * set ignore state of the ignorelist_t
271 void ignorelist_set_invert (ignorelist_t *il, int invert)
275 DEBUG("ignore call with ignorelist_t == NULL");
279 il->ignore = invert ? 0 : 1;
280 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
283 * append entry into ignorelist_t
284 * return 1 for success
286 int ignorelist_add (ignorelist_t *il, const char *entry)
293 DEBUG ("add called with ignorelist_t == NULL");
297 entry_len = strlen (entry);
302 DEBUG("not appending: empty entry");
307 /* regex string is enclosed in "/.../" */
308 if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
312 /* We need to copy `entry' since it's const */
313 entry_copy = smalloc (entry_len);
314 memset (entry_copy, '\0', entry_len);
315 strncpy (entry_copy, entry + 1, entry_len - 2);
317 DEBUG("I'm about to add regex entry: %s", entry_copy);
318 ret = ignorelist_append_regex(il, entry_copy);
324 DEBUG("to add entry: %s", entry);
325 ret = ignorelist_append_string(il, entry);
329 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
332 * check list for entry
333 * return 1 for ignored entry
335 int ignorelist_match (ignorelist_t *il, const char *entry)
337 ignorelist_item_t *traverse;
341 /* if no entries, collect all */
342 if (il->head == NULL)
345 if ((entry == NULL) || (strlen (entry) == 0))
348 /* traverse list and check entries */
349 for (traverse = il->head; traverse != NULL; traverse = traverse->next)
352 if (traverse->rmatch != NULL)
354 if (ignorelist_match_regex (traverse, entry))
360 if (ignorelist_match_string (traverse, entry))
365 return (1 - il->ignore);
366 } /* int ignorelist_match (ignorelist_t *il, const char *entry) */