Fix compile time issues
[collectd.git] / src / snmp.c
index d629259..aeb04fd 100644 (file)
 
 #include "collectd.h"
 
-#include "common.h"
 #include "plugin.h"
+#include "utils/common/common.h"
+#include "utils/ignorelist/ignorelist.h"
 #include "utils_complain.h"
-#include "utils_ignorelist.h"
 
 #include <net-snmp/net-snmp-config.h>
 #include <net-snmp/net-snmp-includes.h>
@@ -97,7 +97,6 @@ struct host_definition_s {
 
   void *sess_handle;
   c_complain_t complaint;
-  cdtime_t interval;
   data_definition_t **data_list;
   int data_list_len;
 };
@@ -172,7 +171,7 @@ static int csnmp_oid_to_string(char *buffer, size_t buffer_size,
   char *oid_str_ptr[MAX_OID_LEN];
 
   for (size_t i = 0; i < o->oid_len; i++) {
-    snprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
+    ssnprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
     oid_str_ptr[i] = oid_str[i];
   }
 
@@ -434,8 +433,7 @@ static int csnmp_config_add_data(oconfig_item_t *ci) {
       }
     } else if (strcasecmp("InstancePrefix", option->key) == 0) {
       WARNING("snmp plugin: data %s: Option `InstancePrefix' is deprecated, "
-              "please use "
-              "option `TypeInstancePrefix'.",
+              "please use option `TypeInstancePrefix'.",
               dd->name);
       status = cf_util_get_string(option, &dd->type_instance.prefix);
     } else if (strcasecmp("PluginInstance", option->key) == 0)
@@ -487,7 +485,7 @@ static int csnmp_config_add_data(oconfig_item_t *ci) {
   while (status == 0) {
     if (dd->is_table) {
       /* Set type_instance to SUBID by default */
-      if (!dd->type_instance.configured && !dd->host.configured)
+      if (!dd->plugin_instance.configured && !dd->host.configured)
         dd->type_instance.configured = true;
 
       if (dd->plugin_instance.value && dd->plugin_instance.configured) {
@@ -611,7 +609,7 @@ static int csnmp_config_add_host_version(host_definition_t *hd,
   hd->version = version;
 
   return 0;
-} /* int csnmp_config_add_host_address */
+} /* int csnmp_config_add_host_version */
 
 static int csnmp_config_add_host_collect(host_definition_t *host,
                                          oconfig_item_t *ci) {
@@ -744,6 +742,7 @@ static int csnmp_config_add_host(oconfig_item_t *ci) {
   int status = 0;
 
   /* Registration stuff. */
+  cdtime_t interval = 0;
   char cb_name[DATA_MAX_NAME_LEN];
 
   hd = calloc(1, sizeof(*hd));
@@ -759,7 +758,6 @@ static int csnmp_config_add_host(oconfig_item_t *ci) {
   }
 
   hd->sess_handle = NULL;
-  hd->interval = 0;
 
   /* These mean that we have not set a timeout or retry value */
   hd->timeout = 0;
@@ -781,7 +779,7 @@ static int csnmp_config_add_host(oconfig_item_t *ci) {
     else if (strcasecmp("Collect", option->key) == 0)
       status = csnmp_config_add_host_collect(hd, option);
     else if (strcasecmp("Interval", option->key) == 0)
-      status = cf_util_get_cdtime(option, &hd->interval);
+      status = cf_util_get_cdtime(option, &interval);
     else if (strcasecmp("Username", option->key) == 0)
       status = cf_util_get_string(option, &hd->username);
     else if (strcasecmp("AuthProtocol", option->key) == 0)
@@ -873,12 +871,13 @@ static int csnmp_config_add_host(oconfig_item_t *ci) {
         "= %i }",
         hd->name, hd->address, hd->community, hd->version);
 
-  snprintf(cb_name, sizeof(cb_name), "snmp-%s", hd->name);
+  ssnprintf(cb_name, sizeof(cb_name), "snmp-%s", hd->name);
 
   status = plugin_register_complex_read(
-      /* group = */ NULL, cb_name, csnmp_read_host, hd->interval,
+      /* group = */ NULL, cb_name, csnmp_read_host, interval,
       &(user_data_t){
-          .data = hd, .free_func = csnmp_host_definition_destroy,
+          .data = hd,
+          .free_func = csnmp_host_definition_destroy,
       });
   if (status != 0) {
     ERROR("snmp plugin: Registering complex read function failed.");
@@ -1142,13 +1141,13 @@ static int csnmp_strvbcopy_hexstring(char *dst, /* {{{ */
   for (size_t i = 0; i < vb->val_len; i++) {
     int status;
 
-    status = snprintf(buffer_ptr, buffer_free, (i == 0) ? "%02x" : ":%02x",
-                      (unsigned int)vb->val.bitstring[i]);
+    status = ssnprintf(buffer_ptr, buffer_free, (i == 0) ? "%02x" : ":%02x",
+                       (unsigned int)vb->val.bitstring[i]);
     assert(status >= 0);
 
     if (((size_t)status) >= buffer_free) /* truncated */
     {
-      dst[dst_size - 1] = 0;
+      dst[dst_size - 1] = '\0';
       return ENOMEM;
     } else /* if (status < buffer_free) */
     {
@@ -1175,10 +1174,10 @@ static int csnmp_strvbcopy(char *dst, /* {{{ */
   else if (vb->type == ASN_BIT_STR)
     src = (char *)vb->val.bitstring;
   else if (vb->type == ASN_IPADDRESS) {
-    return snprintf(dst, dst_size,
-                    "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 "",
-                    (uint8_t)vb->val.string[0], (uint8_t)vb->val.string[1],
-                    (uint8_t)vb->val.string[2], (uint8_t)vb->val.string[3]);
+    return ssnprintf(dst, dst_size,
+                     "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 "",
+                     (uint8_t)vb->val.string[0], (uint8_t)vb->val.string[1],
+                     (uint8_t)vb->val.string[2], (uint8_t)vb->val.string[3]);
   } else {
     dst[0] = 0;
     return EINVAL;
@@ -1195,7 +1194,7 @@ static int csnmp_strvbcopy(char *dst, /* {{{ */
     dst[i] = src[i];
   }
   dst[num_chars] = 0;
-  dst[dst_size - 1] = 0;
+  dst[dst_size - 1] = '\0';
 
   if (dst_size <= vb->val_len)
     return ENOMEM;
@@ -1236,7 +1235,7 @@ static csnmp_cell_char_t *csnmp_get_char_cell(const struct variable_list *vb,
     value_t val = csnmp_value_list_to_value(
         vb, DS_TYPE_COUNTER,
         /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
-    snprintf(il->value, sizeof(il->value), "%" PRIu64, (uint64_t)val.counter);
+    ssnprintf(il->value, sizeof(il->value), "%" PRIu64, (uint64_t)val.counter);
   }
 
   return il;
@@ -1279,7 +1278,7 @@ static void csnmp_cell_replace_reserved_chars(csnmp_cell_char_t *cell) {
     else if (*ptr == '/')
       *ptr = '_';
   }
-}
+} /* void csnmp_cell_replace_reserved_chars */
 
 static int csnmp_dispatch_table(host_definition_t *host,
                                 data_definition_t *data,
@@ -1313,8 +1312,7 @@ static int csnmp_dispatch_table(host_definition_t *host,
     value_cell_ptr[i] = value_cells[i];
 
   sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
-
-  vl.interval = host->interval;
+  sstrncpy(vl.type, data->type, sizeof(vl.type));
 
   have_more = 1;
   while (have_more) {
@@ -1364,8 +1362,6 @@ static int csnmp_dispatch_table(host_definition_t *host,
         /* This suffix is missing in the subtree. Indicate this with the
          * "suffix_skipped" flag and try the next instance / suffix. */
         suffix_skipped = 1;
-        NOTICE("snmp plugin: suffix skipped, variable not found for "
-               "TypeInstanceOID.");
       }
     }
 
@@ -1385,7 +1381,6 @@ static int csnmp_dispatch_table(host_definition_t *host,
         /* This suffix is missing in the subtree. Indicate this with the
          * "suffix_skipped" flag and try the next instance / suffix. */
         suffix_skipped = 1;
-        NOTICE("snmp plugin: suffix skipped, variable not found for HostOID.");
       }
     }
 
@@ -1404,7 +1399,6 @@ static int csnmp_dispatch_table(host_definition_t *host,
         /* This suffix is missing in the subtree. Indicate this with the
          * "suffix_skipped" flag and try the next instance / suffix. */
         suffix_skipped = 1;
-        NOTICE("snmp plugin: suffix skipped, variable not found for HostOID.");
       }
     }
 
@@ -1424,8 +1418,6 @@ static int csnmp_dispatch_table(host_definition_t *host,
         /* This suffix is missing in the subtree. Indicate this with the
          * "suffix_skipped" flag and try the next instance / suffix. */
         suffix_skipped = 1;
-        NOTICE(
-            "snmp plugin: suffix skipped, variable not found for Value OID.");
         break;
       }
     } /* for (i = 0; i < columns; i++) */
@@ -1466,6 +1458,7 @@ static int csnmp_dispatch_table(host_definition_t *host,
                               &value_cell_ptr[0]->suffix) == 0));
 #endif
 
+    /* Check the value in filter column */
     if (filter_cell_ptr &&
         ignorelist_match(data->ignorelist, filter_cell_ptr->value) != 0) {
       if (type_instance_cells != NULL)
@@ -1476,57 +1469,56 @@ static int csnmp_dispatch_table(host_definition_t *host,
       continue;
     }
 
-    sstrncpy(vl.type, data->type, sizeof(vl.type));
-
-    {
-      if (data->host.configured) {
-        char temp[DATA_MAX_NAME_LEN];
-        if (hostname_cell_ptr == NULL)
-          csnmp_oid_to_string(temp, sizeof(temp), &current_suffix);
-        else
-          sstrncpy(temp, hostname_cell_ptr->value, sizeof(temp));
+    /* set vl.host */
+    if (data->host.configured) {
+      char temp[DATA_MAX_NAME_LEN];
+      if (hostname_cell_ptr == NULL)
+        csnmp_oid_to_string(temp, sizeof(temp), &current_suffix);
+      else
+        sstrncpy(temp, hostname_cell_ptr->value, sizeof(temp));
 
-        if (data->host.prefix == NULL)
-          sstrncpy(vl.host, temp, sizeof(vl.host));
-        else
-          snprintf(vl.host, sizeof(vl.host), "%s%s", data->host.prefix, temp);
-      } else {
-        sstrncpy(vl.host, host->name, sizeof(vl.host));
-      }
+      if (data->host.prefix == NULL)
+        sstrncpy(vl.host, temp, sizeof(vl.host));
+      else
+        ssnprintf(vl.host, sizeof(vl.host), "%s%s", data->host.prefix, temp);
+    } else {
+      sstrncpy(vl.host, host->name, sizeof(vl.host));
+    }
 
-      if (data->type_instance.configured) {
-        char temp[DATA_MAX_NAME_LEN];
-        if (type_instance_cell_ptr == NULL)
-          csnmp_oid_to_string(temp, sizeof(temp), &current_suffix);
-        else
-          sstrncpy(temp, type_instance_cell_ptr->value, sizeof(temp));
+    /* set vl.type_instance */
+    if (data->type_instance.configured) {
+      char temp[DATA_MAX_NAME_LEN];
+      if (type_instance_cell_ptr == NULL)
+        csnmp_oid_to_string(temp, sizeof(temp), &current_suffix);
+      else
+        sstrncpy(temp, type_instance_cell_ptr->value, sizeof(temp));
 
-        if (data->type_instance.prefix == NULL)
-          sstrncpy(vl.type_instance, temp, sizeof(vl.type_instance));
-        else
-          snprintf(vl.type_instance, sizeof(vl.type_instance), "%s%s",
-                   data->type_instance.prefix, temp);
-      } else if (data->type_instance.value) {
-        sstrncpy(vl.type_instance, data->type_instance.value,
-                 sizeof(vl.type_instance));
-      }
+      if (data->type_instance.prefix == NULL)
+        sstrncpy(vl.type_instance, temp, sizeof(vl.type_instance));
+      else
+        ssnprintf(vl.type_instance, sizeof(vl.type_instance), "%s%s",
+                  data->type_instance.prefix, temp);
+    } else if (data->type_instance.value) {
+      sstrncpy(vl.type_instance, data->type_instance.value,
+               sizeof(vl.type_instance));
+    }
 
-      if (data->plugin_instance.configured) {
-        char temp[DATA_MAX_NAME_LEN];
-        if (plugin_instance_cell_ptr == NULL)
-          csnmp_oid_to_string(temp, sizeof(temp), &current_suffix);
-        else
-          sstrncpy(temp, plugin_instance_cell_ptr->value, sizeof(temp));
+    /* set vl.plugin_instance */
+    if (data->plugin_instance.configured) {
+      char temp[DATA_MAX_NAME_LEN];
+      if (plugin_instance_cell_ptr == NULL)
+        csnmp_oid_to_string(temp, sizeof(temp), &current_suffix);
+      else
+        sstrncpy(temp, plugin_instance_cell_ptr->value, sizeof(temp));
 
-        if (data->plugin_instance.prefix == NULL)
-          sstrncpy(vl.plugin_instance, temp, sizeof(vl.plugin_instance));
-        else
-          snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s%s",
-                   data->plugin_instance.prefix, temp);
-      } else if (data->plugin_instance.value) {
-        sstrncpy(vl.plugin_instance, data->plugin_instance.value,
-                 sizeof(vl.plugin_instance));
-      }
+      if (data->plugin_instance.prefix == NULL)
+        sstrncpy(vl.plugin_instance, temp, sizeof(vl.plugin_instance));
+      else
+        ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s%s",
+                  data->plugin_instance.prefix, temp);
+    } else if (data->plugin_instance.value) {
+      sstrncpy(vl.plugin_instance, data->plugin_instance.value,
+               sizeof(vl.plugin_instance));
     }
 
     vl.values_len = data->values_len;
@@ -1548,7 +1540,7 @@ static int csnmp_dispatch_table(host_definition_t *host,
       value_cell_ptr[0] = value_cell_ptr[0]->next;
   } /* while (have_more) */
 
-  return (0);
+  return 0;
 } /* int csnmp_dispatch_table */
 
 static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
@@ -1899,11 +1891,6 @@ static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
 
         csnmp_oid_init(&vb_name, vb->name, vb->name_length);
 
-        DEBUG("snmp plugin: src.oid_len = %" PRIsz " root.oid_len = %" PRIsz
-              " is_endofmib = %s",
-              vb_name.oid_len, (data->values + i)->oid_len,
-              (vb->type == SNMP_ENDOFMIBVIEW) ? "true" : "false");
-
         /* Calculate the current suffix. This is later used to check that the
          * suffix is increasing. This also checks if we left the subtree */
         ret = csnmp_oid_suffix(&suffix, &vb_name, data->values + i);
@@ -2059,8 +2046,6 @@ static int csnmp_read_value(host_definition_t *host, data_definition_t *data) {
     sstrncpy(vl.plugin_instance, data->plugin_instance.value,
              sizeof(vl.plugin_instance));
 
-  vl.interval = host->interval;
-
   req = snmp_pdu_create(SNMP_MSG_GET);
   if (req == NULL) {
     ERROR("snmp plugin: snmp_pdu_create failed.");
@@ -2122,9 +2107,6 @@ static int csnmp_read_host(user_data_t *ud) {
 
   host = ud->data;
 
-  if (host->interval == 0)
-    host->interval = plugin_get_interval();
-
   if (host->sess_handle == NULL)
     csnmp_host_open_session(host);