X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fmatch_regex.c;h=2dee9a8fc39b4afa59c7a5dfadaed0c7ecf36632;hb=71bbf854d3e6f8c6d6c3582527263bb01a3a7e04;hp=9233d803ede5c4113333e38bef74732b6fbb5131;hpb=97fe230249ff8bf6aa71acfa206443c4a7238ab7;p=collectd.git diff --git a/src/match_regex.c b/src/match_regex.c index 9233d803..2dee9a8f 100644 --- a/src/match_regex.c +++ b/src/match_regex.c @@ -1,24 +1,29 @@ /** * collectd - src/match_regex.c - * Copyright (C) 2008 Sebastian Harl - * Copyright (C) 2008 Florian Forster + * 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 - * Free Software Foundation; only version 2 of the License is applicable. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * * Authors: * Sebastian Harl - * Florian Forster + * Florian Forster **/ /* @@ -58,6 +63,7 @@ struct mr_match_s mr_regex_t *plugin_instance; mr_regex_t *type; mr_regex_t *type_instance; + _Bool invert; }; /* @@ -134,13 +140,12 @@ static int mr_config_add_regex (mr_regex_t **re_head, /* {{{ */ return (-1); } - re = (mr_regex_t *) malloc (sizeof (*re)); + re = calloc (1, sizeof (*re)); if (re == NULL) { - log_err ("mr_config_add_regex: malloc failed."); + log_err ("mr_config_add_regex: calloc failed."); return (-1); } - memset (re, 0, sizeof (*re)); re->next = NULL; re->re_str = strdup (ci->values[0].value.string); @@ -157,7 +162,7 @@ static int mr_config_add_regex (mr_regex_t **re_head, /* {{{ */ char errmsg[1024]; regerror (status, &re->re, errmsg, sizeof (errmsg)); errmsg[sizeof (errmsg) - 1] = 0; - log_err ("Compiling regex `%s' for `%s' failed: %s.", + log_err ("Compiling regex `%s' for `%s' failed: %s.", re->re_str, ci->key, errmsg); free (re->re_str); free (re); @@ -188,13 +193,14 @@ static int mr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ int status; int i; - m = (mr_match_t *) malloc (sizeof (*m)); + m = calloc (1, sizeof (*m)); if (m == NULL) { - log_err ("mr_create: malloc failed."); + log_err ("mr_create: calloc failed."); return (-ENOMEM); } - memset (m, 0, sizeof (*m)); + + m->invert = 0; status = 0; for (i = 0; i < ci->children_num; i++) @@ -212,6 +218,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 " @@ -257,37 +265,46 @@ 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) { - match_proc_t mproc; + match_proc_t mproc = { 0 }; - memset (&mproc, 0, sizeof (mproc)); mproc.create = mr_create; mproc.destroy = mr_destroy; mproc.match = mr_match;