Code

df plugin: Remove trailing white space.
[collectd.git] / src / match_regex.c
index e0ab7bd70917295300abd59bb87204a7945ab369..1defc18026241e6580e777b6ec8324c84c1411b7 100644 (file)
@@ -1,6 +1,7 @@
 /**
  * collectd - src/match_regex.c
  * Copyright (C) 2008  Sebastian Harl
+ * Copyright (C) 2008  Florian 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
@@ -57,6 +58,7 @@ struct mr_match_s
        mr_regex_t *plugin_instance;
        mr_regex_t *type;
        mr_regex_t *type_instance;
+       _Bool invert;
 };
 
 /*
@@ -105,10 +107,20 @@ static int mr_match_regexen (mr_regex_t *re_head, /* {{{ */
                                /* nmatch = */ 0, /* pmatch = */ NULL,
                                /* eflags = */ 0);
                if (status == 0)
-                       return (FC_MATCH_MATCHES);
+               {
+                       DEBUG ("regex match: Regular expression `%s' matches `%s'.",
+                                       re->re_str, string);
+               }
+               else
+               {
+                       DEBUG ("regex match: Regular expression `%s' does not match `%s'.",
+                                       re->re_str, string);
+                       return (FC_MATCH_NO_MATCH);
+               }
+
        }
 
-       return (FC_MATCH_NO_MATCH);
+       return (FC_MATCH_MATCHES);
 } /* }}} int mr_match_regexen */
 
 static int mr_config_add_regex (mr_regex_t **re_head, /* {{{ */
@@ -184,6 +196,8 @@ static int mr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
                return (-ENOMEM);
        }
        memset (m, 0, sizeof (*m));
+       
+       m->invert = 0;
 
        status = 0;
        for (i = 0; i < ci->children_num; i++)
@@ -201,6 +215,8 @@ static int mr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
                        status = mr_config_add_regex (&m->type, child);
                else if (strcasecmp ("TypeInstance", child->key) == 0)
                        status = mr_config_add_regex (&m->type_instance, child);
+               else if (strcasecmp ("Invert", child->key) == 0)
+                       status = cf_util_get_boolean(child, &m->invert);
                else
                {
                        log_err ("The `%s' configuration option is not understood and "
@@ -246,30 +262,40 @@ static int mr_destroy (void **user_data) /* {{{ */
        return (0);
 } /* }}} int mr_destroy */
 
-static int mr_match (const data_set_t *ds, const value_list_t *vl, /* {{{ */
-               notification_meta_t **meta, void **user_data)
+static int mr_match (const data_set_t __attribute__((unused)) *ds, /* {{{ */
+               const value_list_t *vl,
+               notification_meta_t __attribute__((unused)) **meta,
+               void **user_data)
 {
        mr_match_t *m;
+       int match_value = FC_MATCH_MATCHES;
+       int nomatch_value = FC_MATCH_NO_MATCH;
 
        if ((user_data == NULL) || (*user_data == NULL))
                return (-1);
 
        m = *user_data;
 
+       if (m->invert)
+       {
+               match_value = FC_MATCH_NO_MATCH;
+               nomatch_value = FC_MATCH_MATCHES;
+       }
+
        if (mr_match_regexen (m->host, vl->host) == FC_MATCH_NO_MATCH)
-               return (FC_MATCH_NO_MATCH);
+               return (nomatch_value);
        if (mr_match_regexen (m->plugin, vl->plugin) == FC_MATCH_NO_MATCH)
-               return (FC_MATCH_NO_MATCH);
+               return (nomatch_value);
        if (mr_match_regexen (m->plugin_instance,
                                vl->plugin_instance) == FC_MATCH_NO_MATCH)
-               return (FC_MATCH_NO_MATCH);
+               return (nomatch_value);
        if (mr_match_regexen (m->type, vl->type) == FC_MATCH_NO_MATCH)
-               return (FC_MATCH_NO_MATCH);
+               return (nomatch_value);
        if (mr_match_regexen (m->type_instance,
                                vl->type_instance) == FC_MATCH_NO_MATCH)
-               return (FC_MATCH_NO_MATCH);
+               return (nomatch_value);
 
-       return (FC_MATCH_MATCHES);
+       return (match_value);
 } /* }}} int mr_match */
 
 void module_register (void)