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 memset (il, '\0', sizeof (ignorelist_t));
223 * ->ignore == 0 => collect
224 * ->ignore == 1 => ignore
226 il->ignore = invert ? 0 : 1;
229 } /* ignorelist_t *ignorelist_create (int ignore) */
232 * free memory used by ignorelist_t
234 void ignorelist_free (ignorelist_t *il)
236 ignorelist_item_t *this;
237 ignorelist_item_t *next;
242 for (this = il->head; this != NULL; this = next)
246 if (this->rmatch != NULL)
248 regfree (this->rmatch);
252 if (this->smatch != NULL)
254 sfree (this->smatch);
262 } /* void ignorelist_destroy (ignorelist_t *il) */
265 * set ignore state of the ignorelist_t
267 void ignorelist_set_invert (ignorelist_t *il, int invert)
271 DEBUG("ignore call with ignorelist_t == NULL");
275 il->ignore = invert ? 0 : 1;
276 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
279 * append entry into ignorelist_t
280 * return 1 for success
282 int ignorelist_add (ignorelist_t *il, const char *entry)
289 DEBUG ("add called with ignorelist_t == NULL");
293 entry_len = strlen (entry);
298 DEBUG("not appending: empty entry");
303 /* regex string is enclosed in "/.../" */
304 if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
308 /* We need to copy `entry' since it's const */
309 entry_copy = smalloc (entry_len);
310 memset (entry_copy, '\0', entry_len);
311 strncpy (entry_copy, entry + 1, entry_len - 2);
313 DEBUG("I'm about to add regex entry: %s", entry_copy);
314 ret = ignorelist_append_regex(il, entry_copy);
320 DEBUG("to add entry: %s", entry);
321 ret = ignorelist_append_string(il, entry);
325 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
328 * check list for entry
329 * return 1 for ignored entry
331 int ignorelist_match (ignorelist_t *il, const char *entry)
333 ignorelist_item_t *traverse;
337 /* if no entries, collect all */
338 if (il->head == NULL)
341 if ((entry == NULL) || (strlen (entry) == 0))
344 /* traverse list and check entries */
345 for (traverse = il->head; traverse != NULL; traverse = traverse->next)
348 if (traverse->rmatch != NULL)
350 if (ignorelist_match_regex (traverse, entry))
356 if (ignorelist_match_string (traverse, entry))
361 return (1 - il->ignore);
362 } /* int ignorelist_match (ignorelist_t *il, const char *entry) */