diff --git a/src/utils_match.c b/src/utils_match.c
index 2aaeb963bc5451cb3f7c403e0edd75353be53701..bb53a9a317bd79b718e26f7262c49ea2268ccd93 100644 (file)
--- a/src/utils_match.c
+++ b/src/utils_match.c
/**
* collectd - src/utils_match.c
- * Copyright (C) 2008 Florian octo Forster
+ * Copyright (C) 2008-2014 Florian octo Forster
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
#include <regex.h>
#define UTILS_MATCH_FLAGS_FREE_USER_DATA 0x01
+#define UTILS_MATCH_FLAGS_EXCLUDE_REGEX 0x02
struct cu_match_s
{
regex_t regex;
+ regex_t excluderegex;
int flags;
int (*callback) (const char *str, char * const *matches, size_t matches_num,
/*
* Public functions
*/
-cu_match_t *match_create_callback (const char *regex,
+cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
int (*callback) (const char *str,
char * const *matches, size_t matches_num, void *user_data),
void *user_data)
cu_match_t *obj;
int status;
- DEBUG ("utils_match: match_create_callback: regex = %s", regex);
+ DEBUG ("utils_match: match_create_callback: regex = %s, excluderegex = %s",
+ regex, excluderegex);
obj = (cu_match_t *) malloc (sizeof (cu_match_t));
if (obj == NULL)
return (NULL);
}
+ if (excluderegex && strcmp(excluderegex, "") != 0) {
+ status = regcomp (&obj->excluderegex, excluderegex, REG_EXTENDED);
+ if (status != 0)
+ {
+ ERROR ("Compiling the excluding regular expression \"%s\" failed.",
+ excluderegex);
+ sfree (obj);
+ return (NULL);
+ }
+ obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
+ }
+
obj->callback = callback;
obj->user_data = user_data;
return (obj);
} /* cu_match_t *match_create_callback */
-cu_match_t *match_create_simple (const char *regex, int match_ds_type)
+cu_match_t *match_create_simple (const char *regex,
+ const char *excluderegex, int match_ds_type)
{
cu_match_value_t *user_data;
cu_match_t *obj;
memset (user_data, '\0', sizeof (cu_match_value_t));
user_data->ds_type = match_ds_type;
- obj = match_create_callback (regex, default_callback, user_data);
+ obj = match_create_callback (regex, excluderegex,
+ default_callback, user_data);
if (obj == NULL)
{
sfree (user_data);
return (obj);
} /* cu_match_t *match_create_simple */
+void match_value_reset (cu_match_value_t *mv)
+{
+ if (mv == NULL)
+ return;
+
+ if (mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
+ {
+ mv->value.gauge = NAN;
+ mv->values_num = 0;
+ }
+} /* }}} void match_value_reset */
+
void match_destroy (cu_match_t *obj)
{
if (obj == NULL)
if ((obj == NULL) || (str == NULL))
return (-1);
+ if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX) {
+ status = regexec (&obj->excluderegex, str,
+ STATIC_ARRAY_SIZE (re_match), re_match,
+ /* eflags = */ 0);
+ /* Regex did match, so exclude this line */
+ if (status == 0) {
+ DEBUG("ExludeRegex matched, don't count that line\n");
+ return (0);
+ }
+ }
+
status = regexec (&obj->regex, str,
STATIC_ARRAY_SIZE (re_match), re_match,
/* eflags = */ 0);