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",
137 DEBUG("regex compiled: %s - %i", entry, rcompile);
139 /* create new entry */
140 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL)
142 ERROR ("cannot allocate new config entry");
147 memset (new, '\0', sizeof(ignorelist_item_t));
148 new->rmatch = regtemp;
150 /* append new entry */
151 ignorelist_append (il, new);
154 } /* int ignorelist_append_regex(ignorelist_t *il, const char *entry) */
157 static int ignorelist_append_string(ignorelist_t *il, const char *entry)
159 ignorelist_item_t *new;
161 /* create new entry */
162 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
164 ERROR ("cannot allocate new entry");
167 memset (new, '\0', sizeof(ignorelist_item_t));
168 new->smatch = sstrdup(entry);
170 /* append new entry */
171 ignorelist_append (il, new);
174 } /* int ignorelist_append_string(ignorelist_t *il, const char *entry) */
178 * check list for entry regex match
181 static int ignorelist_match_regex (ignorelist_item_t *item, const char *entry)
183 assert ((item != NULL) && (item->rmatch != NULL)
184 && (entry != NULL) && (strlen (entry) > 0));
187 if (regexec (item->rmatch, entry, 0, NULL, 0) == 0)
191 } /* int ignorelist_match_regex (ignorelist_item_t *item, const char *entry) */
195 * check list for entry string match
198 static int ignorelist_match_string (ignorelist_item_t *item, const char *entry)
200 assert ((item != NULL) && (item->smatch != NULL)
201 && (entry != NULL) && (strlen (entry) > 0));
203 if (strcmp (entry, item->smatch) == 0)
207 } /* int ignorelist_match_string (ignorelist_item_t *item, const char *entry) */
210 /* *** *** *** ******************************************** *** *** *** */
211 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
212 /* *** *** *** ******************************************** *** *** *** */
215 * create the ignorelist_t with known ignore state
216 * return pointer to ignorelist_t
218 ignorelist_t *ignorelist_create (int invert)
222 /* smalloc exits if it failes */
223 il = (ignorelist_t *) smalloc (sizeof (ignorelist_t));
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);
253 sfree (this->rmatch);
257 if (this->smatch != NULL)
259 sfree (this->smatch);
267 } /* void ignorelist_destroy (ignorelist_t *il) */
270 * set ignore state of the ignorelist_t
272 void ignorelist_set_invert (ignorelist_t *il, int invert)
276 DEBUG("ignore call with ignorelist_t == NULL");
280 il->ignore = invert ? 0 : 1;
281 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
284 * append entry into ignorelist_t
285 * return 1 for success
287 int ignorelist_add (ignorelist_t *il, const char *entry)
294 DEBUG ("add called with ignorelist_t == NULL");
298 entry_len = strlen (entry);
303 DEBUG("not appending: empty entry");
308 /* regex string is enclosed in "/.../" */
309 if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
312 size_t entry_copy_size;
314 /* We need to copy `entry' since it's const */
315 entry_copy_size = entry_len - 1;
316 entry_copy = smalloc (entry_copy_size);
317 sstrncpy (entry_copy, entry + 1, entry_copy_size);
319 DEBUG("I'm about to add regex entry: %s", entry_copy);
320 ret = ignorelist_append_regex(il, entry_copy);
326 DEBUG("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;
341 /* if no entries, collect all */
342 if ((il == NULL) || (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) */