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))
55 #include "utils_debug.h"
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 syslog (LOG_ERR, "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 syslog (LOG_ERR, "Cannot compile regex %s: %i/%s",
119 entry, rcompile, regerr);
123 fprintf (stderr, "Cannot compile regex %s: %i",
125 syslog (LOG_ERR, "Cannot compile regex %s: %i",
134 DBG("regex compiled: %s - %i", entry, rcompile);
136 /* create new entry */
137 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL)
139 syslog (LOG_ERR, "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 syslog (LOG_ERR, "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 DBG("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;
243 DBG ("(il = 0x%p)", (void *) il);
248 for (this = il->head; this != NULL; this = next)
252 if (this->rmatch != NULL)
254 regfree (this->rmatch);
258 if (this->smatch != NULL)
260 sfree (this->smatch);
268 } /* void ignorelist_destroy (ignorelist_t *il) */
271 * set ignore state of the ignorelist_t
273 void ignorelist_set_invert (ignorelist_t *il, int invert)
277 DBG("ignore call with ignorelist_t == NULL");
281 il->ignore = invert ? 0 : 1;
282 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
285 * append entry into ignorelist_t
286 * return 1 for success
288 int ignorelist_add (ignorelist_t *il, const char *entry)
295 DBG ("add called with ignorelist_t == NULL");
299 entry_len = strlen (entry);
304 DBG("not appending: empty entry");
309 /* regex string is enclosed in "/.../" */
310 if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
314 /* We need to copy `entry' since it's const */
315 entry_copy = smalloc (entry_len);
316 memset (entry_copy, '\0', entry_len);
317 strncpy (entry_copy, entry + 1, entry_len - 2);
319 DBG("I'm about to add regex entry: %s", entry_copy);
320 ret = ignorelist_append_regex(il, entry_copy);
326 DBG("to add entry: %s", entry);
327 ret = ignorelist_append_string(il, entry);
331 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
334 * check list for entry
335 * return 1 for ignored entry
337 int ignorelist_match (ignorelist_t *il, const char *entry)
339 ignorelist_item_t *traverse;
343 /* if no entries, collect all */
344 if (il->head == NULL)
347 if ((entry == NULL) || (strlen (entry) == 0))
350 /* traverse list and check entries */
351 for (traverse = il->head; traverse != NULL; traverse = traverse->next)
354 if (traverse->rmatch != NULL)
356 if (ignorelist_match_regex (traverse, entry))
362 if (ignorelist_match_string (traverse, entry))
367 return (1 - il->ignore);
368 } /* int ignorelist_match (ignorelist_t *il, const char *entry) */