diff --git a/src/snmp.c b/src/snmp.c
index 491d45a2fd877291a6826a3e410aa3b4b0c77b61..7d6e0a10fc2484683a97417a2f0fe154e8b73eea 100644 (file)
--- a/src/snmp.c
+++ b/src/snmp.c
* collectd - src/snmp.c
* Copyright (C) 2007-2012 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
- * 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:
* Florian octo Forster <octo at collectd.org>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
+#include <fnmatch.h>
+
/*
* Private data structes
*/
double scale;
double shift;
struct data_definition_s *next;
+ char **ignores;
+ size_t ignores_len;
+ int invert_match;
};
typedef struct data_definition_s data_definition_t;
@@ -324,6 +334,50 @@ static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *
return (0);
} /* int csnmp_config_add_data_instance */
+static int csnmp_config_add_data_blacklist(data_definition_t *dd, oconfig_item_t *ci)
+{
+ int i;
+
+ if (ci->values_num < 1)
+ return (0);
+
+ for (i = 0; i < ci->values_num; i++)
+ {
+ if (ci->values[i].type != OCONFIG_TYPE_STRING)
+ {
+ WARNING ("snmp plugin: `Ignore' needs only string argument.");
+ return (-1);
+ }
+ }
+
+ dd->ignores_len = 0;
+ dd->ignores = NULL;
+
+ for (i = 0; i < ci->values_num; ++i)
+ {
+ if (strarray_add(&(dd->ignores), &(dd->ignores_len), ci->values[i].value.string) != 0)
+ {
+ ERROR("snmp plugin: Can't allocate memory");
+ strarray_free(dd->ignores, dd->ignores_len);
+ return (ENOMEM);
+ }
+ }
+ return 0;
+} /* int csnmp_config_add_data_blacklist */
+
+static int csnmp_config_add_data_blacklist_match_inverted(data_definition_t *dd, oconfig_item_t *ci)
+{
+ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
+ {
+ WARNING ("snmp plugin: `InvertMatch' needs exactly one boolean argument.");
+ return (-1);
+ }
+
+ dd->invert_match = ci->values[0].value.boolean ? 1 : 0;
+
+ return (0);
+} /* int csnmp_config_add_data_blacklist_match_inverted */
+
static int csnmp_config_add_data (oconfig_item_t *ci)
{
data_definition_t *dd;
status = cf_util_get_double(option, &dd->shift);
else if (strcasecmp ("Scale", option->key) == 0)
status = cf_util_get_double(option, &dd->scale);
+ else if (strcasecmp ("Ignore", option->key) == 0)
+ status = csnmp_config_add_data_blacklist(dd, option);
+ else if (strcasecmp ("InvertMatch", option->key) == 0)
+ status = csnmp_config_add_data_blacklist_match_inverted(dd, option);
else
{
WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
sfree (dd->name);
sfree (dd->instance_prefix);
sfree (dd->values);
+ sfree (dd->ignores);
sfree (dd);
return (-1);
}
struct variable_list *vb;
oid_t vb_name;
int status;
+ uint32_t i;
+ uint32_t is_matched;
/* Set vb on the last variable */
for (vb = res->variables;
char *ptr;
csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
-
+ is_matched = 0;
+ for (i = 0; i < dd->ignores_len; i++)
+ {
+ status = fnmatch(dd->ignores[i], il->instance, 0);
+ if (status == 0)
+ {
+ if (dd->invert_match == 0)
+ {
+ sfree(il);
+ return 0;
+ }
+ else
+ {
+ is_matched = 1;
+ break;
+ }
+ }
+ }
+ if (dd->invert_match != 0 && is_matched == 0)
+ {
+ sfree(il);
+ return 0;
+ }
for (ptr = il->instance; *ptr != '\0'; ptr++)
{
if ((*ptr > 0) && (*ptr < 32))
@@ -1471,13 +1554,14 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
csnmp_table_values_t *vt;
oid_t vb_name;
oid_t suffix;
+ int ret;
csnmp_oid_init (&vb_name, vb->name, vb->name_length);
/* Calculate the current suffix. This is later used to check that the
* suffix is increasing. This also checks if we left the subtree */
- status = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
- if (status != 0)
+ ret = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
+ if (ret != 0)
{
DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
"Value probably left its subtree.",
@@ -1534,6 +1618,10 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
snmp_free_pdu (res);
res = NULL;
+ if (req != NULL)
+ snmp_free_pdu (req);
+ req = NULL;
+
if (status == 0)
csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
sfree (data_this->name);
sfree (data_this->type);
sfree (data_this->values);
+ sfree (data_this->ignores);
sfree (data_this);
data_this = data_next;