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))
51 #include "utils_debug.h"
52 #include "utils_ignorelist.h"
57 struct ignorelist_item_s
60 regex_t *rmatch; /* regular expression entry identification */
62 char *smatch; /* string entry identification */
63 struct ignorelist_item_s *next;
65 typedef struct ignorelist_item_s ignorelist_item_t;
69 int ignore; /* ignore entries */
70 ignorelist_item_t *head; /* pointer to the first entry */
73 /* *** *** *** ********************************************* *** *** *** */
74 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
75 /* *** *** *** ********************************************* *** *** *** */
77 static inline void ignorelist_append (ignorelist_t *il, ignorelist_item_t *item)
79 assert ((il != NULL) && (item != NULL));
81 item->next = il->head;
86 static int ignorelist_append_regex(ignorelist_t *il, const char *entry)
92 ignorelist_item_t *new;
95 if ((regtemp = malloc(sizeof(regex_t))) == NULL)
97 syslog (LOG_ERR, "cannot allocate new config entry");
100 memset (regtemp, '\0', sizeof(regex_t));
103 if ((rcompile = regcomp (regtemp, entry, REG_EXTENDED)) != 0)
105 /* prepare message buffer */
106 errsize = regerror(rcompile, regtemp, NULL, 0);
108 regerr = smalloc(errsize);
109 /* get error message */
110 if (regerror (rcompile, regtemp, regerr, errsize))
112 fprintf (stderr, "Cannot compile regex %s: %i/%s",
113 entry, rcompile, regerr);
114 syslog (LOG_ERR, "Cannot compile regex %s: %i/%s",
115 entry, rcompile, regerr);
119 fprintf (stderr, "Cannot compile regex %s: %i",
121 syslog (LOG_ERR, "Cannot compile regex %s: %i",
130 DBG("regex compiled: %s - %i", entry, rcompile);
132 /* create new entry */
133 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL)
135 syslog (LOG_ERR, "cannot allocate new config entry");
139 memset (new, '\0', sizeof(ignorelist_item_t));
140 new->rmatch = regtemp;
142 /* append new entry */
143 ignorelist_append (il, new);
146 } /* int ignorelist_append_regex(ignorelist_t *il, const char *entry) */
149 static int ignorelist_append_string(ignorelist_t *il, const char *entry)
151 ignorelist_item_t *new;
153 /* create new entry */
154 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
156 syslog (LOG_ERR, "cannot allocate new entry");
159 memset (new, '\0', sizeof(ignorelist_item_t));
160 new->smatch = sstrdup(entry);
162 /* append new entry */
163 ignorelist_append (il, new);
166 } /* int ignorelist_append_string(ignorelist_t *il, const char *entry) */
170 * check list for entry regex match
173 static int ignorelist_match_regex (ignorelist_item_t *item, const char *entry)
175 assert ((item != NULL) && (item->rmatch != NULL)
176 && (entry != NULL) && (strlen (entry) > 0));
179 if (regexec (item->rmatch, entry, 0, NULL, 0) == 0)
183 } /* int ignorelist_match_regex (ignorelist_item_t *item, const char *entry) */
187 * check list for entry string match
190 static int ignorelist_match_string (ignorelist_item_t *item, const char *entry)
192 assert ((item != NULL) && (item->smatch != NULL)
193 && (entry != NULL) && (strlen (entry) > 0));
195 if (strcmp (entry, item->smatch) == 0)
199 } /* int ignorelist_match_string (ignorelist_item_t *item, const char *entry) */
202 /* *** *** *** ******************************************** *** *** *** */
203 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
204 /* *** *** *** ******************************************** *** *** *** */
207 * create the ignorelist_t with known ignore state
208 * return pointer to ignorelist_t
210 ignorelist_t *ignorelist_create (int invert)
214 /* smalloc exits if it failes */
215 il = (ignorelist_t *) smalloc (sizeof (ignorelist_t));
216 DBG("Ignorelist created 0x%p, default is %s",
218 invert ? "collect" : "ignore");
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;
239 DBG ("(il = 0x%p)", (void *) il);
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 DBG("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)
292 DBG ("add called with ignorelist_t == NULL");
296 entry_len = strlen (entry);
301 DBG("not appending: empty entry");
306 /* regex string is enclosed in "/.../" */
307 if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
309 /* We need to copy `entry' since it's const */
310 entry_copy = smalloc (entry_len);
311 memset (entry_copy, '\0', entry_len);
312 strncpy (entry_copy, entry + 1, entry_len - 2);
314 DBG("I'm about to add regex entry: %s", entry_copy);
315 ret = ignorelist_append_regex(il, entry_copy);
321 DBG("to add entry: %s", entry);
322 ret = ignorelist_append_string(il, entry);
326 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
329 * check list for entry
330 * return 1 for ignored entry
332 int ignorelist_match (ignorelist_t *il, const char *entry)
334 ignorelist_item_t *traverse;
338 /* if no entries, collect all */
339 if (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) */