Merge pull request #3329 from efuss/fix-3311
[collectd.git] / src / snmp_agent.c
index fa73e92..bb4a7e4 100644 (file)
@@ -1,7 +1,7 @@
 /**
  * collectd - src/snmp_agent.c
  *
- * Copyright(c) 2017 Intel Corporation. All rights reserved.
+ * Copyright(c) 2017-2018 Intel Corporation. All rights reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -29,8 +29,8 @@
 
 #include "collectd.h"
 
-#include "common.h"
-#include "utils_avltree.h"
+#include "utils/avltree/avltree.h"
+#include "utils/common/common.h"
 #include "utils_cache.h"
 #include "utils_llist.h"
 #include <regex.h>
@@ -44,6 +44,7 @@
 #define PLUGIN_NAME "snmp_agent"
 #define TYPE_STRING -1
 #define GROUP_UNUSED -1
+#define OID_EXISTS 1
 #define MAX_KEY_SOURCES 5
 #define MAX_INDEX_KEYS 5
 #define MAX_MATCHES 5
@@ -88,6 +89,8 @@ struct table_definition_s {
   llist_t *columns;
   c_avl_tree_t *instance_index;
   c_avl_tree_t *index_instance;
+  c_avl_tree_t *instance_oids; /* Tells us how many OIDs registered for every
+                                  instance; */
   index_key_t index_keys[MAX_INDEX_KEYS]; /* Stores information about what each
                                              index key represents */
   int index_keys_len;
@@ -98,7 +101,7 @@ struct table_definition_s {
                                              will be split into sepearate
                                              tokens */
 
-  _Bool tokens_done; /* Set to 1 when all tokens are generated */
+  bool tokens_done; /* Set to true when all tokens are generated */
 };
 typedef struct table_definition_s table_definition_t;
 
@@ -126,11 +129,12 @@ struct snmp_agent_ctx_s {
 
   llist_t *tables;
   llist_t *scalars;
+  c_avl_tree_t *registered_oids; /* AVL tree containing all registered OIDs */
 };
 typedef struct snmp_agent_ctx_s snmp_agent_ctx_t;
 
 static snmp_agent_ctx_t *g_agent;
-const char *const index_opts[MAX_KEY_SOURCES] = {
+static const char *index_opts[MAX_KEY_SOURCES] = {
     "Hostname", "Plugin", "PluginInstance", "Type", "TypeInstance"};
 
 #define CHECK_DD_TYPE(_dd, _p, _pi, _t, _ti)                                   \
@@ -146,6 +150,8 @@ static int snmp_agent_set_vardata(void *dst_buf, size_t *dst_buf_len,
                                   u_char asn_type, double scale, double shift,
                                   const void *value, size_t len, int type);
 static int snmp_agent_unregister_oid_index(oid_t *oid, int index);
+static int snmp_agent_update_instance_oids(c_avl_tree_t *tree, oid_t *index_oid,
+                                           int value);
 static int num_compare(const int *a, const int *b);
 
 static u_char snmp_agent_get_asn_type(oid *oid, size_t oid_len) {
@@ -166,7 +172,7 @@ static int snmp_agent_oid_to_string(char *buf, size_t buf_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];
   }
 
@@ -334,9 +340,8 @@ static int snmp_agent_validate_config(void) {
   return 0;
 }
 
-static int snmp_agent_parse_index_key(const char *input, char *regex,
-                                      regex_t *regex_info, int gi,
-                                      regmatch_t *m) {
+static int snmp_agent_parse_index_key(const char *input, regex_t *regex_info,
+                                      int gi, regmatch_t *m) {
   regmatch_t matches[MAX_MATCHES];
 
   int ret = regexec(regex_info, input, MAX_MATCHES, matches, 0);
@@ -363,35 +368,51 @@ static int snmp_agent_parse_index_key(const char *input, char *regex,
 static int snmp_agent_create_token(char const *input, int t_off, int n,
                                    c_avl_tree_t *tree,
                                    netsnmp_variable_list *index_key) {
+  assert(tree != NULL);
+
   token_t *token = malloc(sizeof(*token));
+
+  if (token == NULL)
+    goto error;
+
   int *offset = malloc(sizeof(*offset));
-  int ret = 0;
 
-  assert(tree != NULL);
+  if (offset == NULL)
+    goto free_token_error;
 
-  if (token == NULL || offset == NULL)
-    goto error;
+  int ret = 0;
 
   token->key = index_key;
-  token->str = strndup(input + t_off, n);
+  input += t_off;
+  size_t len = strlen(input);
+
+  if (n < len)
+    len = n;
+
+  token->str = malloc(len + 1);
 
   if (token->str == NULL)
-    goto error;
+    goto free_offset_error;
 
+  /* copy at most n bytes from input with offset t_off into token->str */
+  sstrncpy(token->str, input, len + 1);
   *offset = t_off;
   ret = c_avl_insert(tree, (void *)offset, (void *)token);
 
-  if (ret != 0)
-    goto error;
+  if (ret == 0)
+    return 0;
 
-  return 0;
+  sfree(token->str);
 
-error:
+free_offset_error:
+  sfree(offset);
 
-  ERROR(PLUGIN_NAME ": Could not allocate memory to create token");
-  sfree(token->str);
+free_token_error:
   sfree(token);
-  sfree(offset);
+
+error:
+  ERROR(PLUGIN_NAME ": Could not allocate memory to create token");
+
   return -1;
 }
 
@@ -480,7 +501,6 @@ static int snmp_agent_fill_index_list(table_definition_t *td,
     /* var should never be NULL */
     assert(key != NULL);
     ptr = NULL;
-    ret = 0;
     const index_key_src_t source = td->index_keys[i].source;
     c_avl_tree_t *const tokens = td->tokens[source];
     /* Generating list filled with all data necessary to generate an OID */
@@ -504,16 +524,13 @@ static int snmp_agent_fill_index_list(table_definition_t *td,
       ERROR(PLUGIN_NAME ": Unknown index key source provided");
       return -EINVAL;
     }
-    if (ret != 0)
-      return -EINVAL;
 
     /* Parsing input string if necessary */
     if (td->index_keys[i].regex) {
-      regmatch_t m = {-1, -1};
+      regmatch_t m;
 
       /* Parsing input string */
-      ret = snmp_agent_parse_index_key(ptr, td->index_keys[i].regex,
-                                       &td->index_keys[i].regex_info,
+      ret = snmp_agent_parse_index_key(ptr, &td->index_keys[i].regex_info,
                                        td->index_keys[i].group, &m);
       if (ret != 0) {
         ERROR(PLUGIN_NAME ": Error executing regex");
@@ -521,7 +538,7 @@ static int snmp_agent_fill_index_list(table_definition_t *td,
       }
 
       /* Tokenizing input string if not done yet */
-      if (td->tokens_done == 0)
+      if (td->tokens_done == false)
         ret = snmp_agent_tokenize(ptr, tokens, &m, key);
 
       if (ret != 0)
@@ -529,17 +546,35 @@ static int snmp_agent_fill_index_list(table_definition_t *td,
 
       if (td->index_keys[i].type == ASN_INTEGER) {
         int val = strtol(ptr + m.rm_so, NULL, 0);
+
+#ifdef HAVE_NETSNMP_OLD_API
+        ret = snmp_set_var_value(key, (const u_char *)&val, sizeof(val));
+#else
         ret = snmp_set_var_value(key, &val, sizeof(val));
+#endif
       } else
+#ifdef HAVE_NETSNMP_OLD_API
+        ret = snmp_set_var_value(key, (const u_char *)(ptr + m.rm_so),
+                                 m.rm_eo - m.rm_so);
+#else
         ret = snmp_set_var_value(key, ptr + m.rm_so, m.rm_eo - m.rm_so);
+#endif
     } else
+#ifdef HAVE_NETSNMP_OLD_API
+      ret = snmp_set_var_value(key, (const u_char *)ptr, strlen(ptr));
+#else
       ret = snmp_set_var_value(key, ptr, strlen(ptr));
+#endif
+
+    if (ret != 0)
+      return -1;
+
     key = key->next_variable;
   }
 
   /* Tokens for all source strings are generated */
   for (i = 0; i < MAX_KEY_SOURCES; i++)
-    td->tokens_done = 1;
+    td->tokens_done = true;
 
   return 0;
 }
@@ -614,6 +649,15 @@ static int snmp_agent_register_oid_string(const oid_t *oid,
   return snmp_agent_register_oid(&new_oid, handler);
 }
 
+static int snmp_agent_unregister_oid(oid_t *oid) {
+  int ret = c_avl_remove(g_agent->registered_oids, (void *)oid, NULL, NULL);
+
+  if (ret != 0)
+    ERROR(PLUGIN_NAME ": Could not delete registration info");
+
+  return unregister_mib(oid->oid, oid->oid_len);
+}
+
 static int snmp_agent_unregister_oid_string(oid_t *oid,
                                             const oid_t *index_oid) {
   oid_t new_oid;
@@ -628,66 +672,98 @@ static int snmp_agent_unregister_oid_string(oid_t *oid,
   snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &new_oid);
   DEBUG(PLUGIN_NAME ": Unregistered handler for OID (%s)", oid_str);
 
-  return unregister_mib(new_oid.oid, new_oid.oid_len);
+  return snmp_agent_unregister_oid(&new_oid);
 }
 
-static int snmp_agent_table_row_remove(table_definition_t *td,
-                                       oid_t *index_oid) {
+static void snmp_agent_table_data_remove(data_definition_t *dd,
+                                         table_definition_t *td,
+                                         oid_t *index_oid) {
   int *index = NULL;
   oid_t *ind_oid = NULL;
 
   if (td->index_oid.oid_len) {
     if ((c_avl_get(td->instance_index, index_oid, (void **)&index) != 0) ||
         (c_avl_get(td->index_instance, index, NULL) != 0))
-      return 0;
+      return;
   } else {
     if (c_avl_get(td->instance_index, index_oid, NULL) != 0)
-      return 0;
+      return;
   }
 
   pthread_mutex_lock(&g_agent->agentx_lock);
 
-  if (td->index_oid.oid_len)
-    snmp_agent_unregister_oid_index(&td->index_oid, *index);
+  int reg_oids = -1; /* Number of registered oids for given instance */
+
+  for (size_t i = 0; i < dd->oids_len; i++) {
+    if (td->index_oid.oid_len)
+      snmp_agent_unregister_oid_index(&dd->oids[i], *index);
+    else
+      snmp_agent_unregister_oid_string(&dd->oids[i], index_oid);
+
+    reg_oids =
+        snmp_agent_update_instance_oids(td->instance_oids, index_oid, -1);
+  }
 
+  /* Checking if any metrics are left registered */
+  if (reg_oids != 0) {
+    pthread_mutex_unlock(&g_agent->agentx_lock);
+    return;
+  }
+
+  /* All metrics have been unregistered. Unregistering index key OIDs */
+  int keys_processed = 0;
   for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
-    data_definition_t *dd = de->value;
+    data_definition_t *idd = de->value;
+
+    if (!idd->is_index_key)
+      continue;
 
-    for (size_t i = 0; i < dd->oids_len; i++)
+    for (size_t i = 0; i < idd->oids_len; i++)
       if (td->index_oid.oid_len)
-        snmp_agent_unregister_oid_index(&dd->oids[i], *index);
+        snmp_agent_unregister_oid_index(&idd->oids[i], *index);
       else
-        snmp_agent_unregister_oid_string(&dd->oids[i], index_oid);
-  }
+        snmp_agent_unregister_oid_string(&idd->oids[i], index_oid);
 
+    if (++keys_processed >= td->index_keys_len)
+      break;
+  }
   pthread_mutex_unlock(&g_agent->agentx_lock);
 
+  /* All OIDs have been unregistered so we dont need this instance registered
+   * as well */
   char index_str[DATA_MAX_NAME_LEN];
 
   if (index == NULL)
     snmp_agent_oid_to_string(index_str, sizeof(index_str), index_oid);
   else
-    snprintf(index_str, sizeof(index_str), "%d", *index);
+    ssnprintf(index_str, sizeof(index_str), "%d", *index);
 
   notification_t n = {
       .severity = NOTIF_WARNING, .time = cdtime(), .plugin = PLUGIN_NAME};
   sstrncpy(n.host, hostname_g, sizeof(n.host));
-  snprintf(n.message, sizeof(n.message),
-           "Removed data row from table %s with index %s", td->name, index_str);
+  ssnprintf(n.message, sizeof(n.message),
+            "Removed data row from table %s with index %s", td->name,
+            index_str);
   DEBUG(PLUGIN_NAME ": %s", n.message);
   plugin_dispatch_notification(&n);
 
-  if (td->index_oid.oid_len) {
+  int *val = NULL;
+
+  c_avl_remove(td->instance_oids, index_oid, NULL, (void **)&val);
+  sfree(val);
+
+  if (index != NULL) {
+    pthread_mutex_lock(&g_agent->agentx_lock);
+    snmp_agent_unregister_oid_index(&td->index_oid, *index);
+    pthread_mutex_unlock(&g_agent->agentx_lock);
+
     c_avl_remove(td->index_instance, index, NULL, (void **)&ind_oid);
     c_avl_remove(td->instance_index, index_oid, NULL, (void **)&index);
     sfree(index);
     sfree(ind_oid);
   } else {
     c_avl_remove(td->instance_index, index_oid, NULL, NULL);
-    sfree(index_oid);
   }
-
-  return 0;
 }
 
 static int snmp_agent_clear_missing(const value_list_t *vl,
@@ -700,15 +776,22 @@ static int snmp_agent_clear_missing(const value_list_t *vl,
 
     for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
       data_definition_t *dd = de->value;
-      oid_t *index_oid = (oid_t *)calloc(1, sizeof(*index_oid));
-      int ret;
 
       if (!dd->is_index_key) {
         if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
                           vl->type_instance)) {
-          ret = snmp_agent_generate_index(td, vl, index_oid);
+          oid_t *index_oid = calloc(1, sizeof(*index_oid));
+
+          if (index_oid == NULL) {
+            ERROR(PLUGIN_NAME ": Could not allocate memory for index_oid");
+            return -ENOMEM;
+          }
+
+          int ret = snmp_agent_generate_index(td, vl, index_oid);
+
           if (ret == 0)
-            ret = snmp_agent_table_row_remove(td, index_oid);
+            snmp_agent_table_data_remove(dd, td, index_oid);
+          sfree(index_oid);
 
           return ret;
         }
@@ -786,10 +869,11 @@ static void snmp_agent_free_table(table_definition_t **td) {
   if ((*td)->size_oid.oid_len)
     unregister_mib((*td)->size_oid.oid, (*td)->size_oid.oid_len);
 
+  oid_t *index_oid;
+
   /* Unregister Index OIDs */
   if ((*td)->index_oid.oid_len) {
     int *index;
-    oid_t *index_oid;
 
     c_avl_iterator_t *iter = c_avl_get_iterator((*td)->index_instance);
     while (c_avl_iterator_next(iter, (void **)&index, (void **)&index_oid) == 0)
@@ -803,6 +887,15 @@ static void snmp_agent_free_table(table_definition_t **td) {
 
   void *key = NULL;
   void *value = NULL;
+  int *num = NULL;
+
+  /* Removing data from instance_oids, leaving key pointers since they are still
+   * used in other AVL trees */
+  c_avl_iterator_t *iter = c_avl_get_iterator((*td)->instance_oids);
+  while (c_avl_iterator_next(iter, (void **)&index_oid, (void **)&num) == 0)
+    sfree(num);
+  c_avl_iterator_destroy(iter);
+  c_avl_destroy((*td)->instance_oids);
 
   /* index_instance and instance_index contain the same pointers */
   c_avl_destroy((*td)->index_instance);
@@ -844,6 +937,7 @@ static void snmp_agent_free_table(table_definition_t **td) {
 
 static int snmp_agent_parse_oid_index_keys(const table_definition_t *td,
                                            oid_t *index_oid) {
+  assert(index_oid != NULL);
   int ret = parse_oid_indexes(index_oid->oid, index_oid->oid_len,
                               td->index_list_cont);
   if (ret != SNMPERR_SUCCESS)
@@ -852,7 +946,6 @@ static int snmp_agent_parse_oid_index_keys(const table_definition_t *td,
 }
 
 static int snmp_agent_build_name(char **name, c_avl_tree_t *tokens) {
-
   int *pos;
   token_t *tok;
   char str[DATA_MAX_NAME_LEN];
@@ -865,19 +958,19 @@ static int snmp_agent_build_name(char **name, c_avl_tree_t *tokens) {
   }
 
   while (c_avl_iterator_next(it, (void **)&pos, (void **)&tok) == 0) {
-    strncat(out, tok->str, strlen(tok->str));
+    strncat(out, tok->str, DATA_MAX_NAME_LEN - strlen(out) - 1);
     if (tok->key != NULL) {
       if (tok->key->type == ASN_INTEGER) {
-        snprintf(str, sizeof(str), "%ld", *tok->key->val.integer);
-        strncat(out, str, strlen(str));
-      } else /* OCTET_STR */
+        ssnprintf(str, sizeof(str), "%ld", *tok->key->val.integer);
+        strncat(out, str, DATA_MAX_NAME_LEN - strlen(out) - 1);
+      } else /* OCTET_STR */
         strncat(out, (char *)tok->key->val.string,
-                strlen((char *)tok->key->val.string));
-      }
+                DATA_MAX_NAME_LEN - strlen(out) - 1);
     }
   }
-  *name = strdup(out);
+
   c_avl_iterator_destroy(it);
+  *name = strdup(out);
 
   if (*name == NULL) {
     ERROR(PLUGIN_NAME ": Could not allocate memory");
@@ -921,7 +1014,7 @@ static int snmp_agent_format_name(char *name, int name_len,
         }
 
         if (td->index_keys[i].type == ASN_INTEGER) {
-          snprintf(str, sizeof(str), "%ld", *key->val.integer);
+          ssnprintf(str, sizeof(str), "%ld", *key->val.integer);
           fields[source] = str;
         } else /* OCTET_STR */
           fields[source] = (char *)key->val.string;
@@ -970,12 +1063,24 @@ static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests,
     requests->requestvb->type = td->index_keys[dd->index_key_pos].type;
 
     if (requests->requestvb->type == ASN_INTEGER)
+#ifdef HAVE_NETSNMP_OLD_API
+      snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
+                               (const u_char *)key->val.integer,
+                               sizeof(*key->val.integer));
+#else
       snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
                                key->val.integer, sizeof(*key->val.integer));
+#endif
     else /* OCTET_STR */
+#ifdef HAVE_NETSNMP_OLD_API
       snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
                                (const u_char *)key->val.string,
                                strlen((const char *)key->val.string));
+#else
+      snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
+                               key->val.string,
+                               strlen((const char *)key->val.string));
+#endif
 
     pthread_mutex_unlock(&g_agent->lock);
 
@@ -1316,10 +1421,14 @@ static int snmp_agent_config_data_oids(data_definition_t *dd,
       return -EINVAL;
     }
 
-  if (dd->oids != NULL)
-    sfree(dd->oids);
+  if (dd->oids != NULL) {
+    WARNING(PLUGIN_NAME ": OIDs can be configured only once for each data");
+    return -EINVAL;
+  }
+
   dd->oids_len = 0;
   dd->oids = calloc(ci->values_num, sizeof(*dd->oids));
+
   if (dd->oids == NULL)
     return -ENOMEM;
   dd->oids_len = (size_t)ci->values_num;
@@ -1401,7 +1510,7 @@ static int snmp_agent_config_index_key_source(table_definition_t *td,
   if (ret != 0)
     return -1;
 
-  _Bool match = 0;
+  bool match = false;
 
   for (int i = 0; i < MAX_KEY_SOURCES; i++) {
     if (strcasecmp(index_opts[i], (const char *)val) == 0) {
@@ -1421,7 +1530,7 @@ static int snmp_agent_config_index_key_source(table_definition_t *td,
 
   sfree(val);
   dd->index_key_pos = td->index_keys_len++;
-  dd->is_index_key = 1;
+  dd->is_index_key = true;
 
   return 0;
 }
@@ -1443,7 +1552,7 @@ static int snmp_agent_config_index_key_regex(table_definition_t *td,
   }
 
   index_key_src_t source = index_key->source;
-  if (td->tokens[source] == NULL && index_key->regex != NULL) {
+  if (td->tokens[source] == NULL) {
     td->tokens[source] =
         c_avl_create((int (*)(const void *, const void *))num_compare);
     if (td->tokens[source] == NULL) {
@@ -1500,14 +1609,14 @@ static int snmp_agent_config_table_column(table_definition_t *td,
   dd->shift = 0.0;
   /* NULL if it's a scalar */
   dd->table = td;
-  dd->is_index_key = 0;
+  dd->is_index_key = false;
 
   for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *option = ci->children + i;
 
     /* First 3 options are reserved for table entry only */
     if (td != NULL && strcasecmp("IndexKey", option->key) == 0) {
-      dd->is_index_key = 1;
+      dd->is_index_key = true;
       option_tmp = option;
     } else if (strcasecmp("Plugin", option->key) == 0)
       ret = cf_util_get_string(option, &dd->plugin);
@@ -1554,6 +1663,8 @@ static int snmp_agent_config_table_column(table_definition_t *td,
   /* Append to column list in parent table */
   if (td != NULL)
     llist_append(td->columns, entry);
+  else
+    llentry_destroy(entry);
 
   return 0;
 }
@@ -1604,7 +1715,7 @@ static int snmp_agent_config_table(oconfig_item_t *ci) {
 
   for (int i = 0; i < MAX_KEY_SOURCES; i++)
     td->tokens[i] = NULL;
-  td->tokens_done = 0;
+  td->tokens_done = false;
 
   for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *option = ci->children + i;
@@ -1645,11 +1756,19 @@ static int snmp_agent_config_table(oconfig_item_t *ci) {
     return -ENOMEM;
   }
 
+  td->instance_oids =
+      c_avl_create((int (*)(const void *, const void *))oid_compare);
+  if (td->instance_oids == NULL) {
+    snmp_agent_free_table(&td);
+    return -ENOMEM;
+  }
+
   llentry_t *entry = llentry_create(td->name, td);
   if (entry == NULL) {
     snmp_agent_free_table(&td);
     return -ENOMEM;
   }
+
   llist_append(g_agent->tables, entry);
 
   return 0;
@@ -1718,7 +1837,7 @@ static int snmp_agent_set_vardata(void *data, size_t *data_len, u_char asn_type,
   case ASN_OCTET_STR:
     if (type == DS_TYPE_GAUGE) {
       char buf[DATA_MAX_NAME_LEN];
-      snprintf(buf, sizeof(buf), "%.2f", val->gauge);
+      ssnprintf(buf, sizeof(buf), "%.2f", val->gauge);
       if (*data_len < strlen(buf))
         return -EINVAL;
       *data_len = strlen(buf);
@@ -1750,91 +1869,164 @@ static int snmp_agent_unregister_oid_index(oid_t *oid, int index) {
   oid_t new_oid;
   memcpy(&new_oid, oid, sizeof(*oid));
   new_oid.oid[new_oid.oid_len++] = index;
-  return unregister_mib(new_oid.oid, new_oid.oid_len);
+  return snmp_agent_unregister_oid(&new_oid);
 }
 
-static int snmp_agent_update_index(table_definition_t *td, oid_t *index_oid) {
+static int snmp_agent_update_instance_oids(c_avl_tree_t *tree, oid_t *index_oid,
+                                           int value) {
+  int *oids_num; /* number of oids registered for instance */
 
-  if (c_avl_get(td->instance_index, index_oid, NULL) == 0)
-    return 0;
+  if (c_avl_get(tree, index_oid, (void **)&oids_num) == 0) {
+    *oids_num += value;
+    return *oids_num;
+  } else {
+    ERROR(PLUGIN_NAME ": Error updating index data");
+    return -1;
+  }
+}
 
+static int snmp_agent_update_index(data_definition_t *dd,
+                                   table_definition_t *td, oid_t *index_oid,
+                                   bool *free_index_oid) {
   int ret;
   int *index = NULL;
+  int *value = NULL;
 
-  /* need to generate index for the table */
-  if (td->index_oid.oid_len) {
-    index = calloc(1, sizeof(*index));
-    if (index == NULL) {
-      sfree(index_oid);
-      return -ENOMEM;
+  if (c_avl_get(td->instance_index, (void *)index_oid, (void **)&index) != 0) {
+    /* We'll keep index_oid stored in AVL tree */
+    *free_index_oid = false;
+
+    /* need to generate index for the table */
+    if (td->index_oid.oid_len) {
+      index = calloc(1, sizeof(*index));
+      if (index == NULL) {
+        ret = -ENOMEM;
+        goto error;
+      }
+
+      *index = c_avl_size(td->instance_index) + 1;
+
+      ret = c_avl_insert(td->instance_index, index_oid, index);
+      if (ret != 0)
+        goto free_index;
+
+      ret = c_avl_insert(td->index_instance, index, index_oid);
+      if (ret < 0) {
+        DEBUG(PLUGIN_NAME ": Failed to update index_instance for '%s' table",
+              td->name);
+        goto remove_avl_index_oid;
+      }
+
+      ret = snmp_agent_register_oid_index(&td->index_oid, *index,
+                                          snmp_agent_table_index_oid_handler);
+      if (ret != 0)
+        goto remove_avl_index;
+    } else {
+      /* instance as a key is required for any table */
+      ret = c_avl_insert(td->instance_index, index_oid, NULL);
+      if (ret != 0)
+        goto error;
     }
 
-    *index = c_avl_size(td->instance_index) + 1;
+    value = calloc(1, sizeof(*value));
 
-    ret = c_avl_insert(td->instance_index, index_oid, index);
-    if (ret != 0) {
-      sfree(index_oid);
-      sfree(index);
-      return ret;
+    if (value == NULL) {
+      ERROR(PLUGIN_NAME ": Failed to allocate memory");
+      ret = -ENOMEM;
+      goto unregister_index;
     }
 
-    ret = c_avl_insert(td->index_instance, index, index_oid);
+    ret = c_avl_insert(td->instance_oids, index_oid, value);
+
     if (ret < 0) {
-      DEBUG(PLUGIN_NAME ": Failed to update index_instance for '%s' table",
+      DEBUG(PLUGIN_NAME ": Failed to update instance_oids for '%s' table",
             td->name);
-      c_avl_remove(td->instance_index, index_oid, NULL, (void **)&index);
-      sfree(index_oid);
-      sfree(index);
-      return ret;
+      goto free_value;
     }
 
-    ret = snmp_agent_register_oid_index(&td->index_oid, *index,
-                                        snmp_agent_table_index_oid_handler);
-    if (ret != 0)
-      return ret;
-  } else {
-    /* instance as a key is required for any table */
-    ret = c_avl_insert(td->instance_index, index_oid, NULL);
-    if (ret != 0) {
-      sfree(index_oid);
-      return ret;
+    int keys_processed = 0;
+
+    /* Registering index keys OIDs */
+    for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
+      data_definition_t *idd = de->value;
+      if (!idd->is_index_key)
+        continue;
+
+      for (size_t i = 0; i < idd->oids_len; i++) {
+        if (td->index_oid.oid_len)
+          ret = snmp_agent_register_oid_index(&idd->oids[i], *index,
+                                              snmp_agent_table_oid_handler);
+        else
+          ret = snmp_agent_register_oid_string(&idd->oids[i], index_oid,
+                                               snmp_agent_table_oid_handler);
+
+        if (ret != 0) {
+          ERROR(PLUGIN_NAME ": Could not register OID");
+          goto free_index;
+        }
+      }
+
+      if (++keys_processed >= td->index_keys_len)
+        break;
     }
   }
 
-  /* register new oids for all columns */
-  for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
-    data_definition_t *dd = de->value;
+  ret = 0;
 
-    for (size_t i = 0; i < dd->oids_len; i++) {
-      if (td->index_oid.oid_len)
-        ret = snmp_agent_register_oid_index(&dd->oids[i], *index,
-                                            snmp_agent_table_oid_handler);
-      else
-        ret = snmp_agent_register_oid_string(&dd->oids[i], index_oid,
-                                             snmp_agent_table_oid_handler);
+  for (size_t i = 0; i < dd->oids_len; i++) {
+    if (td->index_oid.oid_len)
+      ret = snmp_agent_register_oid_index(&dd->oids[i], *index,
+                                          snmp_agent_table_oid_handler);
+    else
+      ret = snmp_agent_register_oid_string(&dd->oids[i], index_oid,
+                                           snmp_agent_table_oid_handler);
 
-      if (ret != 0)
-        return ret;
-    }
+    if (ret < 0)
+      goto free_index;
+    else if (ret == OID_EXISTS)
+      break;
+    else if (snmp_agent_update_instance_oids(td->instance_oids, index_oid, 1) <
+             0)
+      goto free_index;
   }
 
-  char index_str[DATA_MAX_NAME_LEN];
+  if (ret != OID_EXISTS) {
+    char index_str[DATA_MAX_NAME_LEN];
 
-  if (index == NULL)
-    snmp_agent_oid_to_string(index_str, sizeof(index_str), index_oid);
-  else
-    snprintf(index_str, sizeof(index_str), "%d", *index);
+    if (index == NULL)
+      snmp_agent_oid_to_string(index_str, sizeof(index_str), index_oid);
+    else
+      ssnprintf(index_str, sizeof(index_str), "%d", *index);
 
-  notification_t n = {
-      .severity = NOTIF_OKAY, .time = cdtime(), .plugin = PLUGIN_NAME};
-  sstrncpy(n.host, hostname_g, sizeof(n.host));
-  snprintf(n.message, sizeof(n.message),
-           "Data row added to table %s with index %s", td->name, index_str);
-  DEBUG(PLUGIN_NAME ": %s", n.message);
+    notification_t n = {
+        .severity = NOTIF_OKAY, .time = cdtime(), .plugin = PLUGIN_NAME};
+    sstrncpy(n.host, hostname_g, sizeof(n.host));
+    ssnprintf(n.message, sizeof(n.message),
+              "Data added to table %s with index %s", td->name, index_str);
+    DEBUG(PLUGIN_NAME ": %s", n.message);
 
-  plugin_dispatch_notification(&n);
+    plugin_dispatch_notification(&n);
+  }
 
   return 0;
+
+free_value:
+  sfree(value);
+unregister_index:
+  if (td->index_oid.oid_len)
+    snmp_agent_unregister_oid_index(index_oid, *index);
+remove_avl_index:
+  if (td->index_oid.oid_len)
+    c_avl_remove(td->index_instance, index, NULL, NULL);
+remove_avl_index_oid:
+  c_avl_remove(td->instance_index, index_oid, NULL, NULL);
+free_index:
+  if (index != NULL)
+    sfree(index);
+error:
+  *free_index_oid = true;
+
+  return ret;
 }
 
 static int snmp_agent_write(value_list_t const *vl) {
@@ -1846,18 +2038,26 @@ static int snmp_agent_write(value_list_t const *vl) {
 
     for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
       data_definition_t *dd = de->value;
-      oid_t *index_oid = (oid_t *)calloc(1, sizeof(*index_oid));
-      int ret;
-
-      if (index_oid == NULL)
-        return -ENOMEM;
 
       if (!dd->is_index_key) {
         if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
                           vl->type_instance)) {
-          ret = snmp_agent_generate_index(td, vl, index_oid);
+          oid_t *index_oid = calloc(1, sizeof(*index_oid));
+          bool free_index_oid = true;
+
+          if (index_oid == NULL) {
+            ERROR(PLUGIN_NAME ": Could not allocate memory for index_oid");
+            return -ENOMEM;
+          }
+
+          int ret = snmp_agent_generate_index(td, vl, index_oid);
+
           if (ret == 0)
-            ret = snmp_agent_update_index(td, index_oid);
+            ret = snmp_agent_update_index(dd, td, index_oid, &free_index_oid);
+
+          /* Index exists or update failed */
+          if (free_index_oid)
+            sfree(index_oid);
 
           return ret;
         }
@@ -1890,11 +2090,14 @@ static int snmp_agent_preinit(void) {
 
   g_agent->tables = llist_create();
   g_agent->scalars = llist_create();
+  g_agent->registered_oids =
+      c_avl_create((int (*)(const void *, const void *))oid_compare);
 
   if (g_agent->tables == NULL || g_agent->scalars == NULL) {
     ERROR(PLUGIN_NAME ": llist_create() failed");
     llist_destroy(g_agent->scalars);
     llist_destroy(g_agent->tables);
+    c_avl_destroy(g_agent->registered_oids);
     return -ENOMEM;
   }
 
@@ -1906,6 +2109,7 @@ static int snmp_agent_preinit(void) {
     ERROR(PLUGIN_NAME ": Failed to set agent role (%d)", err);
     llist_destroy(g_agent->scalars);
     llist_destroy(g_agent->tables);
+    c_avl_destroy(g_agent->registered_oids);
     return -1;
   }
 
@@ -1919,6 +2123,7 @@ static int snmp_agent_preinit(void) {
     ERROR(PLUGIN_NAME ": Failed to initialize the agent library (%d)", err);
     llist_destroy(g_agent->scalars);
     llist_destroy(g_agent->tables);
+    c_avl_destroy(g_agent->registered_oids);
     return -1;
   }
 
@@ -1994,6 +2199,26 @@ static void *snmp_agent_thread_run(void __attribute__((unused)) * arg) {
 
 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler) {
   netsnmp_handler_registration *reg;
+
+  if (c_avl_get(g_agent->registered_oids, (void *)oid, NULL) == 0)
+    return OID_EXISTS;
+  else {
+    oid_t *new_oid = calloc(1, sizeof(*new_oid));
+    if (new_oid == NULL) {
+      ERROR(PLUGIN_NAME ": Could not allocate memory to register new OID");
+      return -ENOMEM;
+    }
+
+    memcpy(new_oid, oid, sizeof(*oid));
+
+    int ret = c_avl_insert(g_agent->registered_oids, (void *)new_oid, NULL);
+    if (ret != 0) {
+      ERROR(PLUGIN_NAME ": Could not allocate memory to register new OID");
+      sfree(new_oid);
+      return -ENOMEM;
+    }
+  }
+
   char *oid_name = snmp_agent_get_oid_name(oid->oid, oid->oid_len - 1);
   char oid_str[DATA_MAX_NAME_LEN];
 
@@ -2068,6 +2293,16 @@ static int snmp_agent_shutdown(void) {
   pthread_mutex_destroy(&g_agent->lock);
   pthread_mutex_destroy(&g_agent->agentx_lock);
 
+  /* Freeing registered OIDs list */
+  void *oid;
+
+  if (g_agent->registered_oids != NULL) {
+    while (c_avl_pick(g_agent->registered_oids, &oid, NULL) == 0) {
+      sfree(oid);
+    }
+    c_avl_destroy(g_agent->registered_oids);
+  }
+
   sfree(g_agent);
 
   return ret;