snmp plugin: Allow to set plugin value in reported metrics
authorPavel Rochnyack <pavel2000@ngs.ru>
Tue, 12 Jun 2018 16:07:45 +0000 (23:07 +0700)
committerPavel Rochnyack <pavel2000@ngs.ru>
Tue, 12 Jun 2018 16:26:29 +0000 (23:26 +0700)
src/collectd-snmp.pod
src/collectd.conf.in
src/snmp.c

index d615088..f83c60b 100644 (file)
@@ -141,6 +141,11 @@ Since the semantic of B<Instance> and B<Values> depends on this setting you
 need to set it before setting them. Doing vice verse will result in undefined
 behavior.
 
 need to set it before setting them. Doing vice verse will result in undefined
 behavior.
 
+=item B<Plugin> I<Plugin>
+
+Use I<Plugin> as the plugin name of the values that are dispatched.
+Defaults to C<snmp>.
+
 =item B<Instance> I<Instance>
 
 Sets the type-instance of the values that are dispatched. The meaning of this
 =item B<Instance> I<Instance>
 
 Sets the type-instance of the values that are dispatched. The meaning of this
index 6e22a7f..f23706d 100644 (file)
 #   <Data "std_traffic">
 #       Type "if_octets"
 #       Table true
 #   <Data "std_traffic">
 #       Type "if_octets"
 #       Table true
+#       #Plugin "interface"
 #       Instance "IF-MIB::ifDescr"
 #       Values "IF-MIB::ifInOctets" "IF-MIB::ifOutOctets"
 #   </Data>
 #       Instance "IF-MIB::ifDescr"
 #       Values "IF-MIB::ifInOctets" "IF-MIB::ifOutOctets"
 #   </Data>
index 7c3ebc8..db36a09 100644 (file)
@@ -55,6 +55,7 @@ struct data_definition_s {
   char *type; /* used to find the data_set */
   bool is_table;
   instance_t instance;
   char *type; /* used to find the data_set */
   bool is_table;
   instance_t instance;
+  char *plugin_name;
   char *instance_prefix;
   oid_t *values;
   size_t values_len;
   char *instance_prefix;
   oid_t *values;
   size_t values_len;
@@ -315,9 +316,6 @@ static int csnmp_config_add_data_blacklist(data_definition_t *dd,
     }
   }
 
     }
   }
 
-  dd->ignores_len = 0;
-  dd->ignores = NULL;
-
   for (int i = 0; i < ci->values_num; ++i) {
     if (strarray_add(&(dd->ignores), &(dd->ignores_len),
                      ci->values[i].value.string) != 0) {
   for (int i = 0; i < ci->values_num; ++i) {
     if (strarray_add(&(dd->ignores), &(dd->ignores_len),
                      ci->values[i].value.string) != 0) {
@@ -342,6 +340,14 @@ static int csnmp_config_add_data(oconfig_item_t *ci) {
 
   dd->scale = 1.0;
   dd->shift = 0.0;
 
   dd->scale = 1.0;
   dd->shift = 0.0;
+  dd->ignores_len = 0;
+  dd->ignores = NULL;
+
+  dd->plugin_name = strdup("snmp");
+  if (dd->plugin_name == NULL) {
+      ERROR("snmp plugin: Can't allocate memory");
+      return ENOMEM;
+  }
 
   for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *option = ci->children + i;
 
   for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *option = ci->children + i;
@@ -350,6 +356,8 @@ static int csnmp_config_add_data(oconfig_item_t *ci) {
       status = cf_util_get_string(option, &dd->type);
     else if (strcasecmp("Table", option->key) == 0)
       status = cf_util_get_boolean(option, &dd->is_table);
       status = cf_util_get_string(option, &dd->type);
     else if (strcasecmp("Table", option->key) == 0)
       status = cf_util_get_boolean(option, &dd->is_table);
+    else if (strcasecmp("Plugin", option->key) == 0)
+      status = cf_util_get_string(option, &dd->plugin_name);
     else if (strcasecmp("Instance", option->key) == 0)
       status = csnmp_config_add_data_instance(dd, option);
     else if (strcasecmp("InstancePrefix", option->key) == 0)
     else if (strcasecmp("Instance", option->key) == 0)
       status = csnmp_config_add_data_instance(dd, option);
     else if (strcasecmp("InstancePrefix", option->key) == 0)
@@ -390,6 +398,8 @@ static int csnmp_config_add_data(oconfig_item_t *ci) {
 
   if (status != 0) {
     sfree(dd->name);
 
   if (status != 0) {
     sfree(dd->name);
+    sfree(dd->type);
+    sfree(dd->plugin_name);
     sfree(dd->instance_prefix);
     sfree(dd->values);
     sfree(dd->ignores);
     sfree(dd->instance_prefix);
     sfree(dd->values);
     sfree(dd->ignores);
@@ -1134,7 +1144,7 @@ static int csnmp_dispatch_table(host_definition_t *host,
     value_table_ptr[i] = value_table[i];
 
   sstrncpy(vl.host, host->name, sizeof(vl.host));
     value_table_ptr[i] = value_table[i];
 
   sstrncpy(vl.host, host->name, sizeof(vl.host));
-  sstrncpy(vl.plugin, "snmp", sizeof(vl.plugin));
+  sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
 
   vl.interval = host->interval;
 
 
   vl.interval = host->interval;
 
@@ -1597,7 +1607,7 @@ static int csnmp_read_value(host_definition_t *host, data_definition_t *data) {
   }
 
   sstrncpy(vl.host, host->name, sizeof(vl.host));
   }
 
   sstrncpy(vl.host, host->name, sizeof(vl.host));
-  sstrncpy(vl.plugin, "snmp", sizeof(vl.plugin));
+  sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
   sstrncpy(vl.type, data->type, sizeof(vl.type));
   sstrncpy(vl.type_instance, data->instance.string, sizeof(vl.type_instance));
 
   sstrncpy(vl.type, data->type, sizeof(vl.type));
   sstrncpy(vl.type_instance, data->instance.string, sizeof(vl.type_instance));
 
@@ -1713,6 +1723,8 @@ static int csnmp_shutdown(void) {
 
     sfree(data_this->name);
     sfree(data_this->type);
 
     sfree(data_this->name);
     sfree(data_this->type);
+    sfree(data_this->plugin_name);
+    sfree(data_this->instance_prefix);
     sfree(data_this->values);
     sfree(data_this->ignores);
     sfree(data_this);
     sfree(data_this->values);
     sfree(data_this->ignores);
     sfree(data_this);