1 /**
2 * collectd - src/utils_ignorelist.c
3 * Copyright (C) 2006 Lubos Stanek <lubek at users.sourceforge.net>
4 *
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.
9 *
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.
14 *
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,
18 * USA.
19 *
20 * Authors:
21 * Lubos Stanek <lubek at users.sourceforge.net>
22 **/
23 /**
24 * ignorelist handles plugin's list of configured collectable
25 * entries with global ignore action
26 **/
27 /**
28 * Usage:
29 *
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))
47 * return;
48 **/
50 #if HAVE_CONFIG_H
51 # include "config.h"
52 #endif
54 #include "common.h"
55 #include "plugin.h"
56 #include "utils_ignorelist.h"
58 /*
59 * private prototypes
60 */
61 struct ignorelist_item_s
62 {
63 #if HAVE_REGEX_H
64 regex_t *rmatch; /* regular expression entry identification */
65 #endif
66 char *smatch; /* string entry identification */
67 struct ignorelist_item_s *next;
68 };
69 typedef struct ignorelist_item_s ignorelist_item_t;
71 struct ignorelist_s
72 {
73 int ignore; /* ignore entries */
74 ignorelist_item_t *head; /* pointer to the first entry */
75 };
77 /* *** *** *** ********************************************* *** *** *** */
78 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
79 /* *** *** *** ********************************************* *** *** *** */
81 static inline void ignorelist_append (ignorelist_t *il, ignorelist_item_t *item)
82 {
83 assert ((il != NULL) && (item != NULL));
85 item->next = il->head;
86 il->head = item;
87 }
89 #if HAVE_REGEX_H
90 static int ignorelist_append_regex(ignorelist_t *il, const char *entry)
91 {
92 int rcompile;
93 regex_t *regtemp;
94 int errsize;
95 char *regerr = NULL;
96 ignorelist_item_t *new;
98 /* create buffer */
99 if ((regtemp = malloc(sizeof(regex_t))) == NULL)
100 {
101 ERROR ("cannot allocate new config entry");
102 return (1);
103 }
104 memset (regtemp, '\0', sizeof(regex_t));
106 /* compile regex */
107 if ((rcompile = regcomp (regtemp, entry, REG_EXTENDED)) != 0)
108 {
109 /* prepare message buffer */
110 errsize = regerror(rcompile, regtemp, NULL, 0);
111 if (errsize)
112 regerr = smalloc(errsize);
113 /* get error message */
114 if (regerror (rcompile, regtemp, regerr, errsize))
115 {
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);
120 }
121 else
122 {
123 fprintf (stderr, "Cannot compile regex %s: %i",
124 entry, rcompile);
125 ERROR ("Cannot compile regex %s: %i",
126 entry, rcompile);
127 }
129 if (errsize)
130 sfree (regerr);
131 regfree (regtemp);
132 return (1);
133 }
134 DEBUG("regex compiled: %s - %i", entry, rcompile);
136 /* create new entry */
137 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL)
138 {
139 ERROR ("cannot allocate new config entry");
140 regfree (regtemp);
141 return (1);
142 }
143 memset (new, '\0', sizeof(ignorelist_item_t));
144 new->rmatch = regtemp;
146 /* append new entry */
147 ignorelist_append (il, new);
149 return (0);
150 } /* int ignorelist_append_regex(ignorelist_t *il, const char *entry) */
151 #endif
153 static int ignorelist_append_string(ignorelist_t *il, const char *entry)
154 {
155 ignorelist_item_t *new;
157 /* create new entry */
158 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
159 {
160 ERROR ("cannot allocate new entry");
161 return (1);
162 }
163 memset (new, '\0', sizeof(ignorelist_item_t));
164 new->smatch = sstrdup(entry);
166 /* append new entry */
167 ignorelist_append (il, new);
169 return (0);
170 } /* int ignorelist_append_string(ignorelist_t *il, const char *entry) */
172 #if HAVE_REGEX_H
173 /*
174 * check list for entry regex match
175 * return 1 if found
176 */
177 static int ignorelist_match_regex (ignorelist_item_t *item, const char *entry)
178 {
179 assert ((item != NULL) && (item->rmatch != NULL)
180 && (entry != NULL) && (strlen (entry) > 0));
182 /* match regex */
183 if (regexec (item->rmatch, entry, 0, NULL, 0) == 0)
184 return (1);
186 return (0);
187 } /* int ignorelist_match_regex (ignorelist_item_t *item, const char *entry) */
188 #endif
190 /*
191 * check list for entry string match
192 * return 1 if found
193 */
194 static int ignorelist_match_string (ignorelist_item_t *item, const char *entry)
195 {
196 assert ((item != NULL) && (item->smatch != NULL)
197 && (entry != NULL) && (strlen (entry) > 0));
199 if (strcmp (entry, item->smatch) == 0)
200 return (1);
202 return (0);
203 } /* int ignorelist_match_string (ignorelist_item_t *item, const char *entry) */
206 /* *** *** *** ******************************************** *** *** *** */
207 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
208 /* *** *** *** ******************************************** *** *** *** */
210 /*
211 * create the ignorelist_t with known ignore state
212 * return pointer to ignorelist_t
213 */
214 ignorelist_t *ignorelist_create (int invert)
215 {
216 ignorelist_t *il;
218 /* smalloc exits if it failes */
219 il = (ignorelist_t *) smalloc (sizeof (ignorelist_t));
220 DEBUG("Ignorelist created 0x%p, default is %s",
221 (void *) il,
222 invert ? "collect" : "ignore");
224 memset (il, '\0', sizeof (ignorelist_t));
226 /*
227 * ->ignore == 0 => collect
228 * ->ignore == 1 => ignore
229 */
230 il->ignore = invert ? 0 : 1;
232 return (il);
233 } /* ignorelist_t *ignorelist_create (int ignore) */
235 /*
236 * free memory used by ignorelist_t
237 */
238 void ignorelist_free (ignorelist_t *il)
239 {
240 ignorelist_item_t *this;
241 ignorelist_item_t *next;
243 if (il == NULL)
244 return;
246 for (this = il->head; this != NULL; this = next)
247 {
248 next = this->next;
249 #if HAVE_REGEX_H
250 if (this->rmatch != NULL)
251 {
252 regfree (this->rmatch);
253 this->rmatch = NULL;
254 }
255 #endif
256 if (this->smatch != NULL)
257 {
258 sfree (this->smatch);
259 this->smatch = NULL;
260 }
261 sfree (this);
262 }
264 sfree (il);
265 il = NULL;
266 } /* void ignorelist_destroy (ignorelist_t *il) */
268 /*
269 * set ignore state of the ignorelist_t
270 */
271 void ignorelist_set_invert (ignorelist_t *il, int invert)
272 {
273 if (il == NULL)
274 {
275 DEBUG("ignore call with ignorelist_t == NULL");
276 return;
277 }
279 il->ignore = invert ? 0 : 1;
280 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
282 /*
283 * append entry into ignorelist_t
284 * return 1 for success
285 */
286 int ignorelist_add (ignorelist_t *il, const char *entry)
287 {
288 int ret;
289 size_t entry_len;
291 if (il == NULL)
292 {
293 DEBUG ("add called with ignorelist_t == NULL");
294 return (1);
295 }
297 entry_len = strlen (entry);
299 /* append nothing */
300 if (entry_len == 0)
301 {
302 DEBUG("not appending: empty entry");
303 return (1);
304 }
306 #if HAVE_REGEX_H
307 /* regex string is enclosed in "/.../" */
308 if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
309 {
310 char *entry_copy;
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);
319 sfree (entry_copy);
320 }
321 else
322 #endif
323 {
324 DEBUG("to add entry: %s", entry);
325 ret = ignorelist_append_string(il, entry);
326 }
328 return (ret);
329 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
331 /*
332 * check list for entry
333 * return 1 for ignored entry
334 */
335 int ignorelist_match (ignorelist_t *il, const char *entry)
336 {
337 ignorelist_item_t *traverse;
339 assert (il != NULL);
341 /* if no entries, collect all */
342 if (il->head == NULL)
343 return (0);
345 if ((entry == NULL) || (strlen (entry) == 0))
346 return (0);
348 /* traverse list and check entries */
349 for (traverse = il->head; traverse != NULL; traverse = traverse->next)
350 {
351 #if HAVE_REGEX_H
352 if (traverse->rmatch != NULL)
353 {
354 if (ignorelist_match_regex (traverse, entry))
355 return (il->ignore);
356 }
357 else
358 #endif
359 {
360 if (ignorelist_match_string (traverse, entry))
361 return (il->ignore);
362 }
363 } /* for traverse */
365 return (1 - il->ignore);
366 } /* int ignorelist_match (ignorelist_t *il, const char *entry) */