2 * collectd - src/config_list.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 int num; /* number of entries */
71 ignorelist_item_t *head; /* pointer to the first entry */
74 /* *** *** *** ********************************************* *** *** *** */
75 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
76 /* *** *** *** ********************************************* *** *** *** */
78 static inline void ignorelist_append (ignorelist_t *il, ignorelist_item_t *item)
80 assert ((il != NULL) && (item != NULL));
82 item->next = il->head;
89 static int ignorelist_append_regex(ignorelist_t *il, const char *entry)
95 ignorelist_item_t *new;
98 if ((regtemp = malloc(sizeof(regex_t))) == NULL)
100 syslog (LOG_ERR, "cannot allocate new config entry");
103 memset (regtemp, '\0', sizeof(regex_t));
106 if ((rcompile = regcomp (regtemp, entry, REG_EXTENDED)) != 0)
108 /* prepare message buffer */
109 errsize = regerror(rcompile, regtemp, NULL, 0);
111 regerr = smalloc(errsize);
112 /* get error message */
113 if (regerror (rcompile, regtemp, regerr, errsize))
115 fprintf (stderr, "Cannot compile regex %s: %i/%s",
116 entry, rcompile, regerr);
117 syslog (LOG_ERR, "Cannot compile regex %s: %i/%s",
118 entry, rcompile, regerr);
122 fprintf (stderr, "Cannot compile regex %s: %i",
124 syslog (LOG_ERR, "Cannot compile regex %s: %i",
133 DBG("regex compiled: %s - %i", entry, rcompile);
135 /* create new entry */
136 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL)
138 syslog (LOG_ERR, "cannot allocate new config entry");
142 memset (new, '\0', sizeof(ignorelist_item_t));
143 new->rmatch = regtemp;
145 /* append new entry */
146 ignorelist_append (il, new);
149 } /* int ignorelist_append_regex(ignorelist_t *il, const char *entry) */
152 static int ignorelist_append_string(ignorelist_t *il, const char *entry)
154 ignorelist_item_t *new;
156 /* create new entry */
157 if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
159 syslog (LOG_ERR, "cannot allocate new entry");
162 memset (new, '\0', sizeof(ignorelist_item_t));
163 new->smatch = sstrdup(entry);
165 /* append new entry */
166 ignorelist_append (il, new);
169 } /* int ignorelist_append_string(ignorelist_t *il, const char *entry) */
173 * check list for entry regex match
176 static int ignorelist_match_regex (ignorelist_item_t *item, const char *entry)
178 assert ((item != NULL) && (item->rmatch != NULL)
179 && (entry != NULL) && (strlen (entry) > 0));
182 if (regexec (item->rmatch, entry, 0, NULL, 0) == 0)
186 } /* int ignorelist_match_regex (ignorelist_item_t *item, const char *entry) */
190 * check list for entry string match
193 static int ignorelist_match_string (ignorelist_item_t *item, const char *entry)
195 assert ((item != NULL) && (item->smatch != NULL)
196 && (entry != NULL) && (strlen (entry) > 0));
198 if (strcmp (entry, item->smatch) == 0)
202 } /* int ignorelist_match_string (ignorelist_item_t *item, const char *entry) */
205 /* *** *** *** ******************************************** *** *** *** */
206 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
207 /* *** *** *** ******************************************** *** *** *** */
210 * create the ignorelist_t with known ignore state
211 * return pointer to ignorelist_t
213 ignorelist_t *ignorelist_create (int invert)
217 /* smalloc exits if it failes */
218 il = (ignorelist_t *) smalloc (sizeof (ignorelist_t));
219 DBG("Ignorelist created 0x%p, default is %s",
221 invert ? "collect" : "ignore");
223 memset (il, '\0', sizeof (ignorelist_t));
226 * ->ignore == 0 => collect
227 * ->ignore == 1 => ignore
229 il->ignore = invert ? 0 : 1;
232 } /* ignorelist_t *ignorelist_create (int ignore) */
235 * free memory used by ignorelist_t
237 void ignorelist_free (ignorelist_t *il)
239 ignorelist_item_t *this;
240 ignorelist_item_t *next;
242 DBG ("(il = 0x%p)", (void *) il);
247 for (this = il->head; this != NULL; this = next)
249 DBG ("free - item = 0x%p, numlist %i", (void *) this, il->num);
253 if (this->rmatch != NULL)
255 regfree (this->rmatch);
259 if (this->smatch != NULL)
261 sfree (this->smatch);
268 DBG ("after free numlist: %i", il->num);
273 } /* void ignorelist_destroy (ignorelist_t *il) */
276 * set ignore state of the ignorelist_t
278 void ignorelist_set_invert (ignorelist_t *il, int invert)
282 DBG("ignore call with ignorelist_t == NULL");
286 il->ignore = invert ? 0 : 1;
287 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
290 * get number of entries in the ignorelist_t
293 int ignorelist_num (ignorelist_t *il)
297 DBG("get num called with ignorelist_t == NULL");
302 } /* int ignorelist_num (ignorelist_t *il) */
305 * append entry into ignorelist_t
306 * return 1 for success
308 int ignorelist_add (ignorelist_t *il, const char *entry)
316 DBG ("add called with ignorelist_t == NULL");
320 entry_len = strlen (entry);
325 DBG("not appending: empty entry");
330 /* regex string is enclosed in "/.../" */
331 if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
333 /* We need to copy `entry' since it's const */
334 entry_copy = smalloc (entry_len);
335 memset (entry_copy, '\0', entry_len);
336 strncpy (entry_copy, entry + 1, entry_len - 2);
338 DBG("I'm about to add regex entry: %s", entry_copy);
339 ret = ignorelist_append_regex(il, entry_copy);
345 DBG("to add entry: %s", entry);
346 ret = ignorelist_append_string(il, entry);
350 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
353 * check list for entry
354 * return 1 for ignored entry
356 int ignorelist_match (ignorelist_t *il, const char *entry)
358 ignorelist_item_t *traverse;
360 /* if no entries, collect all */
361 if (ignorelist_num(il) == 0)
364 /* traverse list and check entries */
365 for (traverse = il->head; traverse != NULL; traverse = traverse->next)
368 if (traverse->rmatch != NULL)
370 if (ignorelist_match_regex (traverse, entry))
376 if (ignorelist_match_string (traverse, entry))
381 return (1 - il->ignore);
382 } /* int ignorelist_match (ignorelist_t *il, const char *entry) */