+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)
+ ERROR(PLUGIN_NAME ": index OID parse error!");
+ return ret;
+}
+
+static int snmp_agent_build_name(char **name, c_avl_tree_t *tokens) {
+ int *pos;
+ token_t *tok;
+ char str[DATA_MAX_NAME_LEN];
+ char out[DATA_MAX_NAME_LEN] = {0};
+ c_avl_iterator_t *it = c_avl_get_iterator(tokens);
+
+ if (it == NULL) {
+ ERROR(PLUGIN_NAME ": Error getting tokens list iterator");
+ return -1;
+ }
+
+ while (c_avl_iterator_next(it, (void **)&pos, (void **)&tok) == 0) {
+ 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, DATA_MAX_NAME_LEN - strlen(out) - 1);
+ } else /* OCTET_STR */
+ strncat(out, (char *)tok->key->val.string,
+ DATA_MAX_NAME_LEN - strlen(out) - 1);
+ }
+ }
+
+ c_avl_iterator_destroy(it);
+ *name = strdup(out);
+
+ if (*name == NULL) {
+ ERROR(PLUGIN_NAME ": Could not allocate memory");
+ return -ENOMEM;
+ }
+
+ return 0;
+}
+
+static int snmp_agent_format_name(char *name, int name_len,
+ data_definition_t *dd, oid_t *index_oid) {
+
+ int ret = 0;
+
+ if (index_oid == NULL) {
+ /* It's a scalar */
+ format_name(name, name_len, hostname_g, dd->plugin, dd->plugin_instance,
+ dd->type, dd->type_instance);
+ } else {
+ /* Need to parse string index OID */
+ const table_definition_t *td = dd->table;
+ ret = snmp_agent_parse_oid_index_keys(td, index_oid);
+ if (ret != 0)
+ return ret;
+
+ int i = 0;
+ netsnmp_variable_list *key = td->index_list_cont;
+ char str[DATA_MAX_NAME_LEN];
+ char *fields[MAX_KEY_SOURCES] = {hostname_g, dd->plugin,
+ dd->plugin_instance, dd->type,
+ dd->type_instance};
+
+ /* Looking for simple keys only */
+ while (key != NULL) {
+ if (!td->index_keys[i].regex) {
+ index_key_src_t source = td->index_keys[i].source;
+
+ if (source < INDEX_HOST || source > INDEX_TYPE_INSTANCE) {
+ ERROR(PLUGIN_NAME ": Unkown index key source!");
+ return -EINVAL;
+ }
+
+ if (td->index_keys[i].type == ASN_INTEGER) {
+ snprintf(str, sizeof(str), "%ld", *key->val.integer);
+ fields[source] = str;
+ } else /* OCTET_STR */
+ fields[source] = (char *)key->val.string;
+ }
+ key = key->next_variable;
+ i++;
+ }
+
+ /* Keys with regexes */
+ for (i = 0; i < MAX_KEY_SOURCES; i++) {
+ if (td->tokens[i] == NULL)
+ continue;
+ ret = snmp_agent_build_name(&fields[i], td->tokens[i]);
+ if (ret != 0)
+ return ret;
+ }
+ format_name(name, name_len, fields[INDEX_HOST], fields[INDEX_PLUGIN],
+ fields[INDEX_PLUGIN_INSTANCE], fields[INDEX_TYPE],
+ fields[INDEX_TYPE_INSTANCE]);
+ for (i = 0; i < MAX_KEY_SOURCES; i++) {
+ if (td->tokens[i])
+ sfree(fields[i]);
+ }
+ }
+
+ return 0;
+}
+