X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fsnmp_agent.c;h=fa73e92648ef23498781fa5fa39f12c219972301;hb=3bf0437e2a57919ac2ae1fa944f30ade393c7a05;hp=6cbe881a040c230137958637692881c463d430c5;hpb=1634cdf408671dc430d2860bc98acd1d8a3f84e6;p=collectd.git diff --git a/src/snmp_agent.c b/src/snmp_agent.c index 6cbe881a..fa73e926 100644 --- a/src/snmp_agent.c +++ b/src/snmp_agent.c @@ -24,6 +24,7 @@ * Authors: * Roman Korynkevych * Serhiy Pshyk + * Marcin Mozejko **/ #include "collectd.h" @@ -32,6 +33,7 @@ #include "utils_avltree.h" #include "utils_cache.h" #include "utils_llist.h" +#include #include @@ -40,12 +42,31 @@ #include #define PLUGIN_NAME "snmp_agent" -#define ERR_BUF_SIZE 1024 #define TYPE_STRING -1 +#define GROUP_UNUSED -1 +#define MAX_KEY_SOURCES 5 +#define MAX_INDEX_KEYS 5 +#define MAX_MATCHES 5 + +/* Identifies index key source */ +enum index_key_src_e { + INDEX_HOST = 0, + INDEX_PLUGIN, + INDEX_PLUGIN_INSTANCE, + INDEX_TYPE, + INDEX_TYPE_INSTANCE +}; +typedef enum index_key_src_e index_key_src_t; -#ifndef MIN -#define MIN(x, y) ((x) < (y) ? (x) : (y)) -#endif +struct index_key_s { + index_key_src_t source; + u_char type; + char *regex; /* Pattern used to parse index key source string */ + int group; /* If pattern gives more than one group we can specify which one + we want to take */ + regex_t regex_info; +}; +typedef struct index_key_s index_key_t; struct oid_s { oid oid[MAX_OID_LEN]; @@ -54,6 +75,12 @@ struct oid_s { }; typedef struct oid_s oid_t; +struct token_s { + char *str; + netsnmp_variable_list *key; /* Points to succeeding key */ +}; +typedef struct token_s token_t; + struct table_definition_s { char *name; oid_t index_oid; @@ -61,6 +88,17 @@ struct table_definition_s { llist_t *columns; c_avl_tree_t *instance_index; c_avl_tree_t *index_instance; + index_key_t index_keys[MAX_INDEX_KEYS]; /* Stores information about what each + index key represents */ + int index_keys_len; + netsnmp_variable_list *index_list_cont; /* Index key container used for + generating as well as parsing + OIDs, not thread-safe */ + c_avl_tree_t *tokens[MAX_KEY_SOURCES]; /* Input string after regex execution + will be split into sepearate + tokens */ + + _Bool tokens_done; /* Set to 1 when all tokens are generated */ }; typedef struct table_definition_s table_definition_t; @@ -71,7 +109,8 @@ struct data_definition_s { char *type; char *type_instance; const table_definition_t *table; - _Bool is_instance; + bool is_index_key; /* indicates if table column is an index key */ + int index_key_pos; /* position in indexes list */ oid_t *oids; size_t oids_len; double scale; @@ -90,7 +129,9 @@ struct snmp_agent_ctx_s { }; typedef struct snmp_agent_ctx_s snmp_agent_ctx_t; -static snmp_agent_ctx_t *g_agent = NULL; +static snmp_agent_ctx_t *g_agent; +const char *const index_opts[MAX_KEY_SOURCES] = { + "Hostname", "Plugin", "PluginInstance", "Type", "TypeInstance"}; #define CHECK_DD_TYPE(_dd, _p, _pi, _t, _ti) \ (_dd->plugin ? !strcmp(_dd->plugin, _p) : 0) && \ @@ -98,12 +139,14 @@ static snmp_agent_ctx_t *g_agent = NULL; (_dd->type ? !strcmp(_dd->type, _t) : 0) && \ (_dd->type_instance ? !strcmp(_dd->type_instance, _ti) : 1) +static int snmp_agent_shutdown(void); static void *snmp_agent_thread_run(void *arg); static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler); 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 num_compare(const int *a, const int *b); static u_char snmp_agent_get_asn_type(oid *oid, size_t oid_len) { struct tree *node = get_tree(oid, oid_len, g_agent->tp); @@ -123,17 +166,62 @@ 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++) { - ssnprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]); + snprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]); oid_str_ptr[i] = oid_str[i]; } return strjoin(buf, buf_size, oid_str_ptr, o->oid_len, "."); } -static void snmp_agent_dump_data(void) { +/* Prints a configuration storing list. It handles both table columns list + and scalars list */ #if COLLECT_DEBUG +static void snmp_agent_dump_data(llist_t *list) { char oid_str[DATA_MAX_NAME_LEN]; + for (llentry_t *de = llist_head(list); de != NULL; de = de->next) { + data_definition_t *dd = de->value; + table_definition_t const *td = dd->table; + if (dd->table != NULL) + DEBUG(PLUGIN_NAME ": Column:"); + else + DEBUG(PLUGIN_NAME ": Scalar:"); + + DEBUG(PLUGIN_NAME ": Name: %s", dd->name); + if (dd->plugin) + DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin); + if (dd->plugin_instance) + DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance); + if (dd->is_index_key) { + index_key_t const *index_key = &td->index_keys[dd->index_key_pos]; + + DEBUG(PLUGIN_NAME ": IndexKey:"); + DEBUG(PLUGIN_NAME ": Source: %s", index_opts[index_key->source]); + DEBUG(PLUGIN_NAME ": Type: %s", + (index_key->type == ASN_INTEGER) ? "Integer" : "String"); + if (index_key->regex) + DEBUG(PLUGIN_NAME ": Regex: %s", index_key->regex); + if (index_key->group != GROUP_UNUSED) + DEBUG(PLUGIN_NAME ": Group: %d", index_key->group); + } + if (dd->type) + DEBUG(PLUGIN_NAME ": Type: %s", dd->type); + if (dd->type_instance) + DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance); + for (size_t i = 0; i < dd->oids_len; i++) { + snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &dd->oids[i]); + DEBUG(PLUGIN_NAME ": OID[%" PRIsz "]: %s", i, oid_str); + } + DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale); + DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift); + } +} + +/* Prints parsed configuration */ +static void snmp_agent_dump_config(void) { + char oid_str[DATA_MAX_NAME_LEN]; + + /* Printing tables */ for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) { table_definition_t *td = te->value; @@ -148,62 +236,28 @@ static void snmp_agent_dump_data(void) { DEBUG(PLUGIN_NAME ": SizeOID: %s", oid_str); } - for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) { - data_definition_t *dd = de->value; - - DEBUG(PLUGIN_NAME ": Column:"); - DEBUG(PLUGIN_NAME ": Name: %s", dd->name); - if (dd->plugin) - DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin); - if (dd->plugin_instance) - DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance); - if (dd->is_instance) - DEBUG(PLUGIN_NAME ": Instance: true"); - if (dd->type) - DEBUG(PLUGIN_NAME ": Type: %s", dd->type); - if (dd->type_instance) - DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance); - for (int i = 0; i < dd->oids_len; i++) { - snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &dd->oids[i]); - DEBUG(PLUGIN_NAME ": OID[%d]: %s", i, oid_str); - } - DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale); - DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift); - } + snmp_agent_dump_data(td->columns); } - for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) { - data_definition_t *dd = e->value; - - DEBUG(PLUGIN_NAME ": Scalar:"); - DEBUG(PLUGIN_NAME ": Name: %s", dd->name); - if (dd->plugin) - DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin); - if (dd->plugin_instance) - DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance); - if (dd->is_instance) - DEBUG(PLUGIN_NAME ": Instance: true"); - if (dd->type) - DEBUG(PLUGIN_NAME ": Type: %s", dd->type); - if (dd->type_instance) - DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance); - for (int i = 0; i < dd->oids_len; i++) { - snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &dd->oids[i]); - DEBUG(PLUGIN_NAME ": OID[%d]: %s", i, oid_str); - } - DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale); - DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift); - } -#endif /* COLLECT_DEBUG */ + /* Printing scalars */ + snmp_agent_dump_data(g_agent->scalars); } +#endif /* COLLECT_DEBUG */ -static int snmp_agent_validate_data(void) { +static int snmp_agent_validate_config(void) { - snmp_agent_dump_data(); +#if COLLECT_DEBUG + snmp_agent_dump_config(); +#endif for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) { table_definition_t *td = te->value; + if (!td->index_keys_len) { + ERROR(PLUGIN_NAME ": Index keys not defined for '%s'", td->name); + return -EINVAL; + } + for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) { data_definition_t *dd = de->value; @@ -226,11 +280,10 @@ static int snmp_agent_validate_data(void) { return -EINVAL; } - if (dd->is_instance) { - + if (dd->is_index_key) { if (dd->type || dd->type_instance) { ERROR(PLUGIN_NAME ": Type and TypeInstance are not valid for " - "instance data '%s'.'%s'", + "index data '%s'.'%s'", td->name, dd->name); return -EINVAL; } @@ -266,9 +319,8 @@ static int snmp_agent_validate_data(void) { return -EINVAL; } - if (dd->is_instance) { - ERROR(PLUGIN_NAME - ": Instance flag can't be specified for scalar data '%s'", + if (dd->is_index_key) { + ERROR(PLUGIN_NAME ": Index field can't be specified for scalar data '%s'", dd->name); return -EINVAL; } @@ -282,65 +334,314 @@ static int snmp_agent_validate_data(void) { return 0; } -static void snmp_agent_generate_oid2string(oid_t *oid, int offset, char *key) { - int key_len = oid->oid[offset]; - int i; +static int snmp_agent_parse_index_key(const char *input, char *regex, + regex_t *regex_info, int gi, + regmatch_t *m) { + regmatch_t matches[MAX_MATCHES]; + + int ret = regexec(regex_info, input, MAX_MATCHES, matches, 0); + if (!ret) { + if (gi > regex_info->re_nsub) { + ERROR(PLUGIN_NAME ": Group index %d not found. Check regex config", gi); + return -1; + } + *m = matches[gi]; + } else if (ret == REG_NOMATCH) { + ERROR(PLUGIN_NAME ": No match found"); + return -1; + } else { + char msgbuf[100]; + + regerror(ret, regex_info, msgbuf, sizeof(msgbuf)); + ERROR(PLUGIN_NAME ": Regex match failed: %s", msgbuf); + return -1; + } + + return 0; +} + +static int snmp_agent_create_token(char const *input, int t_off, int n, + c_avl_tree_t *tree, + netsnmp_variable_list *index_key) { + token_t *token = malloc(sizeof(*token)); + int *offset = malloc(sizeof(*offset)); + int ret = 0; + + assert(tree != NULL); + + if (token == NULL || offset == NULL) + goto error; + + token->key = index_key; + token->str = strndup(input + t_off, n); + + if (token->str == NULL) + goto error; + + *offset = t_off; + ret = c_avl_insert(tree, (void *)offset, (void *)token); + + if (ret != 0) + goto error; + + return 0; + +error: + + ERROR(PLUGIN_NAME ": Could not allocate memory to create token"); + sfree(token->str); + sfree(token); + sfree(offset); + return -1; +} + +static int snmp_agent_delete_token(int t_off, c_avl_tree_t *tree) { + token_t *token = NULL; + int *offset = NULL; + + int ret = c_avl_remove(tree, &t_off, (void **)&offset, (void **)&token); + + if (ret != 0) { + ERROR(PLUGIN_NAME ": Could not delete token"); + return -1; + } + + sfree(token->str); + sfree(token); + sfree(offset); + return 0; +} + +static int snmp_agent_get_token(c_avl_tree_t *tree, int mpos) { + + int *pos; + char *token; + int prev_pos = 0; + + c_avl_iterator_t *it = c_avl_get_iterator(tree); + while (c_avl_iterator_next(it, (void **)&pos, (void **)&token) == 0) { + if (*pos >= mpos) + break; + else + prev_pos = *pos; + } + + c_avl_iterator_destroy(it); + return prev_pos; +} + +static int snmp_agent_tokenize(const char *input, c_avl_tree_t *tokens, + const regmatch_t *m, + netsnmp_variable_list *key) { + assert(tokens != NULL); + + int ret = 0; + int len = strlen(input); + + /* Creating first token that is going to be split later */ + if (c_avl_size(tokens) == 0) { + ret = snmp_agent_create_token(input, 0, len, tokens, NULL); + if (ret != 0) + return ret; + } + + /* Divide token that contains current match into two */ + int t_pos = snmp_agent_get_token(tokens, m->rm_so); + ret = snmp_agent_delete_token(t_pos, tokens); - for (i = 0; i < key_len && offset < oid->oid_len; i++) - key[i] = oid->oid[++offset]; + if (ret != 0) + return -1; - key[i] = '\0'; + ret = snmp_agent_create_token(input, t_pos, m->rm_so - t_pos, tokens, key); + + if (ret != 0) + return -1; + + if (len - m->rm_eo > 1) { + ret = snmp_agent_create_token(input, m->rm_eo, len - m->rm_eo + 1, tokens, + NULL); + if (ret != 0) { + snmp_agent_delete_token(t_pos, tokens); + return -1; + } + } + + return 0; } -static int snmp_agent_generate_string2oid(oid_t *oid, const char *key) { - int key_len = strlen(key); +static int snmp_agent_fill_index_list(table_definition_t *td, + value_list_t const *vl) { + int ret; + int i; + netsnmp_variable_list *key = td->index_list_cont; + char const *ptr; + + for (i = 0; i < td->index_keys_len; i++) { + /* 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 */ + switch (source) { + case INDEX_HOST: + ptr = vl->host; + break; + case INDEX_PLUGIN: + ptr = vl->plugin; + break; + case INDEX_PLUGIN_INSTANCE: + ptr = vl->plugin_instance; + break; + case INDEX_TYPE: + ptr = vl->type; + break; + case INDEX_TYPE_INSTANCE: + ptr = vl->type_instance; + break; + default: + 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}; - oid->oid[oid->oid_len++] = key_len; - for (int i = 0; i < key_len; i++) { - oid->oid[oid->oid_len++] = key[i]; - if (oid->oid_len >= MAX_OID_LEN) { - ERROR(PLUGIN_NAME ": Conversion key string %s to OID failed", key); + /* Parsing input string */ + ret = snmp_agent_parse_index_key(ptr, td->index_keys[i].regex, + &td->index_keys[i].regex_info, + td->index_keys[i].group, &m); + if (ret != 0) { + ERROR(PLUGIN_NAME ": Error executing regex"); + return ret; + } + + /* Tokenizing input string if not done yet */ + if (td->tokens_done == 0) + ret = snmp_agent_tokenize(ptr, tokens, &m, key); + + if (ret != 0) + return -1; + + if (td->index_keys[i].type == ASN_INTEGER) { + int val = strtol(ptr + m.rm_so, NULL, 0); + ret = snmp_set_var_value(key, &val, sizeof(val)); + } else + ret = snmp_set_var_value(key, ptr + m.rm_so, m.rm_eo - m.rm_so); + } else + ret = snmp_set_var_value(key, ptr, strlen(ptr)); + key = key->next_variable; + } + + /* Tokens for all source strings are generated */ + for (i = 0; i < MAX_KEY_SOURCES; i++) + td->tokens_done = 1; + + return 0; +} + +static int snmp_agent_prep_index_list(table_definition_t const *td, + netsnmp_variable_list **index_list) { + /* Generating list having only the structure (with no values) letting us + * know how to parse an OID*/ + for (int i = 0; i < td->index_keys_len; i++) { + switch (td->index_keys[i].source) { + case INDEX_HOST: + case INDEX_PLUGIN: + case INDEX_PLUGIN_INSTANCE: + case INDEX_TYPE: + case INDEX_TYPE_INSTANCE: + snmp_varlist_add_variable(index_list, NULL, 0, td->index_keys[i].type, + NULL, 0); + break; + default: + ERROR(PLUGIN_NAME ": Unknown index key source provided"); return -EINVAL; } } + return 0; +} + +static int snmp_agent_generate_index(table_definition_t *td, + value_list_t const *vl, oid_t *index_oid) { + + /* According to given information by index_keys list + * index OID is going to be built + */ + int ret = snmp_agent_fill_index_list(td, vl); + if (ret != 0) + return -EINVAL; + + /* Building only index part OID (without table prefix OID) */ + ret = build_oid_noalloc(index_oid->oid, sizeof(index_oid->oid), + &index_oid->oid_len, NULL, 0, td->index_list_cont); + if (ret != SNMPERR_SUCCESS) { + ERROR(PLUGIN_NAME ": Error building index OID"); + return -EINVAL; + } + + return 0; +} + +/* It appends one OID to the end of another */ +static int snmp_agent_append_oid(oid_t *out, const oid_t *in) { + + if (out->oid_len + in->oid_len > MAX_OID_LEN) { + ERROR(PLUGIN_NAME ": Cannot create OID. Output length is too long!"); + return -EINVAL; + } + memcpy(&out->oid[out->oid_len], in->oid, in->oid_len * sizeof(oid)); + out->oid_len += in->oid_len; return 0; } -static int snmp_agent_register_oid_string(oid_t *oid, const char *key, +static int snmp_agent_register_oid_string(const oid_t *oid, + const oid_t *index_oid, Netsnmp_Node_Handler *handler) { oid_t new_oid; memcpy(&new_oid, oid, sizeof(*oid)); - int ret = snmp_agent_generate_string2oid(&new_oid, key); + /* Concatenating two string oids */ + int ret = snmp_agent_append_oid(&new_oid, index_oid); if (ret != 0) return ret; return snmp_agent_register_oid(&new_oid, handler); } -static int snmp_agent_unregister_oid_string(oid_t *oid, const char *key) { +static int snmp_agent_unregister_oid_string(oid_t *oid, + const oid_t *index_oid) { oid_t new_oid; + char oid_str[DATA_MAX_NAME_LEN]; memcpy(&new_oid, oid, sizeof(*oid)); - int ret = snmp_agent_generate_string2oid(&new_oid, key); + /* Concatenating two string oids */ + int ret = snmp_agent_append_oid(&new_oid, index_oid); if (ret != 0) return ret; + 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); } static int snmp_agent_table_row_remove(table_definition_t *td, - const char *instance) { + oid_t *index_oid) { int *index = NULL; - char *ins = NULL; + oid_t *ind_oid = NULL; if (td->index_oid.oid_len) { - if ((c_avl_get(td->instance_index, instance, (void **)&index) != 0) || - (c_avl_get(td->index_instance, index, (void **)&ins) != 0)) + if ((c_avl_get(td->instance_index, index_oid, (void **)&index) != 0) || + (c_avl_get(td->index_instance, index, NULL) != 0)) return 0; } else { - if (c_avl_get(td->instance_index, instance, (void **)&ins) != 0) + if (c_avl_get(td->instance_index, index_oid, NULL) != 0) return 0; } @@ -352,35 +653,38 @@ static int snmp_agent_table_row_remove(table_definition_t *td, for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) { data_definition_t *dd = de->value; - for (int i = 0; i < dd->oids_len; i++) + 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], ins); + snmp_agent_unregister_oid_string(&dd->oids[i], index_oid); } pthread_mutex_unlock(&g_agent->agentx_lock); - DEBUG(PLUGIN_NAME ": Removed row for '%s' table [%d, %s]", td->name, - (index != NULL) ? *index : -1, ins); + 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); notification_t n = { .severity = NOTIF_WARNING, .time = cdtime(), .plugin = PLUGIN_NAME}; sstrncpy(n.host, hostname_g, sizeof(n.host)); - sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance)); - ssnprintf(n.message, sizeof(n.message), - "Removed data row from table %s instance %s index %d", td->name, - ins, (index != NULL) ? *index : -1); + snprintf(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) { - c_avl_remove(td->index_instance, index, NULL, (void **)&ins); - c_avl_remove(td->instance_index, instance, NULL, (void **)&index); + 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(ins); + sfree(ind_oid); } else { - c_avl_remove(td->instance_index, instance, NULL, (void **)&ins); - sfree(ins); + c_avl_remove(td->instance_index, index_oid, NULL, NULL); + sfree(index_oid); } return 0; @@ -396,12 +700,17 @@ 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_instance) { + if (!dd->is_index_key) { if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type, vl->type_instance)) { - snmp_agent_table_row_remove(td, vl->plugin_instance); - return 0; + ret = snmp_agent_generate_index(td, vl, index_oid); + if (ret == 0) + ret = snmp_agent_table_row_remove(td, index_oid); + + return ret; } } } @@ -417,31 +726,9 @@ static void snmp_agent_free_data(data_definition_t **dd) { /* unregister scalar type OID */ if ((*dd)->table == NULL) { - for (int i = 0; i < (*dd)->oids_len; i++) + for (size_t i = 0; i < (*dd)->oids_len; i++) unregister_mib((*dd)->oids[i].oid, (*dd)->oids[i].oid_len); } - if (!(*dd)->table->index_oid.oid_len) { - char *instance; - - c_avl_iterator_t *iter = c_avl_get_iterator((*dd)->table->instance_index); - while (c_avl_iterator_next(iter, (void *)&instance, (void *)&instance) == - 0) { - for (int i = 0; i < (*dd)->oids_len; i++) - snmp_agent_unregister_oid_string(&(*dd)->oids[i], instance); - } - c_avl_iterator_destroy(iter); - } else { - /* unregister all table OIDs */ - int *index; - char *value; - - c_avl_iterator_t *iter = c_avl_get_iterator((*dd)->table->index_instance); - while (c_avl_iterator_next(iter, (void *)&index, (void *)&value) == 0) { - for (int i = 0; i < (*dd)->oids_len; i++) - snmp_agent_unregister_oid_index(&(*dd)->oids[i], *index); - } - c_avl_iterator_destroy(iter); - } sfree((*dd)->name); sfree((*dd)->plugin); @@ -455,6 +742,42 @@ static void snmp_agent_free_data(data_definition_t **dd) { return; } +static void snmp_agent_free_table_columns(table_definition_t *td) { + if (td->columns == NULL) + return; + + for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) { + data_definition_t *dd = de->value; + + 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) { + for (size_t i = 0; i < dd->oids_len; i++) + snmp_agent_unregister_oid_index(&dd->oids[i], *index); + } + c_avl_iterator_destroy(iter); + } else { + oid_t *index_oid; + + c_avl_iterator_t *iter = c_avl_get_iterator(dd->table->instance_index); + while (c_avl_iterator_next(iter, (void *)&index_oid, NULL) == 0) { + for (size_t i = 0; i < dd->oids_len; i++) + snmp_agent_unregister_oid_string(&dd->oids[i], index_oid); + } + c_avl_iterator_destroy(iter); + } + + snmp_agent_free_data(&dd); + } + + llist_destroy(td->columns); + td->columns = NULL; +} /* void snmp_agent_free_table_columns */ + static void snmp_agent_free_table(table_definition_t **td) { if (td == NULL || *td == NULL) @@ -463,23 +786,20 @@ 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); + /* Unregister Index OIDs */ if ((*td)->index_oid.oid_len) { int *index; - char *value; + 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 *)&value) == 0) + while (c_avl_iterator_next(iter, (void **)&index, (void **)&index_oid) == 0) snmp_agent_unregister_oid_index(&(*td)->index_oid, *index); c_avl_iterator_destroy(iter); } - for (llentry_t *de = llist_head((*td)->columns); de != NULL; de = de->next) { - data_definition_t *dd = de->value; - snmp_agent_free_data(&dd); - } - - llist_destroy((*td)->columns); + /* Unregister all table columns and their registered OIDs */ + snmp_agent_free_table_columns(*td); void *key = NULL; void *value = NULL; @@ -497,20 +817,177 @@ static void snmp_agent_free_table(table_definition_t **td) { c_avl_destroy((*td)->instance_index); (*td)->instance_index = NULL; } + snmp_free_varbind((*td)->index_list_cont); + int i; + token_t *tok = NULL; + + for (i = 0; i < (*td)->index_keys_len; i++) { + sfree((*td)->index_keys[i].regex); + regfree(&(*td)->index_keys[i].regex_info); + } + for (i = 0; i < MAX_KEY_SOURCES; i++) + if ((*td)->tokens[i] != NULL) { + while (c_avl_pick((*td)->tokens[i], &key, (void **)&tok) == 0) { + sfree(key); + sfree(tok->str); + sfree(tok); + } + c_avl_destroy((*td)->tokens[i]); + (*td)->tokens[i] = NULL; + } sfree((*td)->name); sfree(*td); return; } +static int snmp_agent_parse_oid_index_keys(const table_definition_t *td, + oid_t *index_oid) { + 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, strlen(tok->str)); + 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 */ + strncat(out, (char *)tok->key->val.string, + strlen((char *)tok->key->val.string)); + } + } + } + *name = strdup(out); + c_avl_iterator_destroy(it); + + 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; +} + static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests, - data_definition_t *dd, char *instance, + data_definition_t *dd, oid_t *index_oid, int oid_index) { + int ret; + + if (dd->is_index_key) { + const table_definition_t *td = dd->table; + int ret = snmp_agent_parse_oid_index_keys(td, index_oid); + + if (ret != 0) + return ret; + + netsnmp_variable_list *key = td->index_list_cont; + /* Searching index key */ + for (int pos = 0; pos < dd->index_key_pos; pos++) + key = key->next_variable; + + requests->requestvb->type = td->index_keys[dd->index_key_pos].type; + + if (requests->requestvb->type == ASN_INTEGER) + snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type, + key->val.integer, sizeof(*key->val.integer)); + else /* OCTET_STR */ + snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type, + (const u_char *)key->val.string, + strlen((const char *)key->val.string)); + + pthread_mutex_unlock(&g_agent->lock); + + return SNMP_ERR_NOERROR; + } + char name[DATA_MAX_NAME_LEN]; - format_name(name, sizeof(name), hostname_g, dd->plugin, - instance ? instance : dd->plugin_instance, dd->type, - dd->type_instance); + + ret = snmp_agent_format_name(name, sizeof(name), dd, index_oid); + if (ret != 0) + return ret; + DEBUG(PLUGIN_NAME ": Identifier '%s'", name); value_t *values; @@ -521,7 +998,7 @@ static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests, return SNMP_NOSUCHINSTANCE; } - int ret = uc_get_value_by_name(name, &values, &values_num); + ret = uc_get_value_by_name(name, &values, &values_num); if (ret != 0) { ERROR(PLUGIN_NAME ": Failed to get value for '%s'", name); @@ -529,7 +1006,7 @@ static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests, } assert(ds->ds_num == values_num); - assert(oid_index < values_num); + assert(oid_index < (int)values_num); char data[DATA_MAX_NAME_LEN]; size_t data_len = sizeof(data); @@ -545,8 +1022,8 @@ static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests, } requests->requestvb->type = dd->oids[oid_index].type; - snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type, data, - data_len); + snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type, + (const u_char *)data, data_len); return SNMP_ERR_NOERROR; } @@ -557,14 +1034,14 @@ snmp_agent_table_oid_handler(struct netsnmp_mib_handler_s *handler, struct netsnmp_agent_request_info_s *reqinfo, struct netsnmp_request_info_s *requests) { - if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) { + if (reqinfo->mode != MODE_GET) { DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode); return SNMP_ERR_NOERROR; } pthread_mutex_lock(&g_agent->lock); - oid_t oid; + oid_t oid; /* Requested OID */ memcpy(oid.oid, requests->requestvb->name, sizeof(oid.oid[0]) * requests->requestvb->name_length); oid.oid_len = requests->requestvb->name_length; @@ -574,6 +1051,7 @@ snmp_agent_table_oid_handler(struct netsnmp_mib_handler_s *handler, snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid); DEBUG(PLUGIN_NAME ": Get request received for table OID '%s'", oid_str); #endif + oid_t index_oid; /* Index part of requested OID */ for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) { table_definition_t *td = te->value; @@ -581,52 +1059,40 @@ snmp_agent_table_oid_handler(struct netsnmp_mib_handler_s *handler, for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) { data_definition_t *dd = de->value; - for (int i = 0; i < dd->oids_len; i++) { + for (size_t i = 0; i < dd->oids_len; i++) { int ret = snmp_oid_ncompare(oid.oid, oid.oid_len, dd->oids[i].oid, dd->oids[i].oid_len, - MIN(oid.oid_len, dd->oids[i].oid_len)); + SNMP_MIN(oid.oid_len, dd->oids[i].oid_len)); if (ret != 0) continue; - char *instance; + /* Calculating OID length for index part */ + index_oid.oid_len = oid.oid_len - dd->oids[i].oid_len; + /* Fetching index part of the OID */ + memcpy(index_oid.oid, &oid.oid[dd->oids[i].oid_len], + index_oid.oid_len * sizeof(*oid.oid)); + + char index_str[DATA_MAX_NAME_LEN]; + snmp_agent_oid_to_string(index_str, sizeof(index_str), &index_oid); if (!td->index_oid.oid_len) { - char key[MAX_OID_LEN]; - - memset(key, 0, sizeof(key)); - snmp_agent_generate_oid2string( - &oid, MIN(oid.oid_len, dd->oids[i].oid_len), key); - - ret = c_avl_get(td->instance_index, key, (void **)&instance); - if (ret != 0) { - DEBUG(PLUGIN_NAME ": Nonexisting index string '%s' requested", key); - pthread_mutex_unlock(&g_agent->lock); - return SNMP_NOSUCHINSTANCE; - } + ret = c_avl_get(td->instance_index, &index_oid, NULL); } else { - int index = oid.oid[oid.oid_len - 1]; - - ret = c_avl_get(td->index_instance, &index, (void **)&instance); - if (ret != 0) { - DEBUG(PLUGIN_NAME ": Nonexisting index '%d' requested", index); - pthread_mutex_unlock(&g_agent->lock); - return SNMP_NOSUCHINSTANCE; - } - } + oid_t *temp_oid; - if (dd->is_instance) { - requests->requestvb->type = ASN_OCTET_STR; - snmp_set_var_typed_value(requests->requestvb, - requests->requestvb->type, instance, - strlen((instance))); + assert(index_oid.oid_len == 1); + ret = c_avl_get(td->index_instance, (int *)&index_oid.oid[0], + (void **)&temp_oid); + memcpy(&index_oid, temp_oid, sizeof(index_oid)); + } + if (ret != 0) { + INFO(PLUGIN_NAME ": Non-existing index (%s) requested", index_str); pthread_mutex_unlock(&g_agent->lock); - - return SNMP_ERR_NOERROR; + return SNMP_NOSUCHINSTANCE; } - ret = snmp_agent_form_reply(requests, dd, instance, i); - + ret = snmp_agent_form_reply(requests, dd, &index_oid, i); pthread_mutex_unlock(&g_agent->lock); return ret; @@ -645,7 +1111,7 @@ static int snmp_agent_table_index_oid_handler( struct netsnmp_agent_request_info_s *reqinfo, struct netsnmp_request_info_s *requests) { - if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) { + if (reqinfo->mode != MODE_GET) { DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode); return SNMP_ERR_NOERROR; } @@ -661,15 +1127,15 @@ static int snmp_agent_table_index_oid_handler( table_definition_t *td = te->value; if (td->index_oid.oid_len && - (snmp_oid_ncompare(oid.oid, oid.oid_len, td->index_oid.oid, - td->index_oid.oid_len, - MIN(oid.oid_len, td->index_oid.oid_len)) == 0)) { + (snmp_oid_ncompare( + oid.oid, oid.oid_len, td->index_oid.oid, td->index_oid.oid_len, + SNMP_MIN(oid.oid_len, td->index_oid.oid_len)) == 0)) { DEBUG(PLUGIN_NAME ": Handle '%s' table index OID", td->name); int index = oid.oid[oid.oid_len - 1]; - int ret = c_avl_get(td->index_instance, &index, &(void *){NULL}); + int ret = c_avl_get(td->index_instance, &index, NULL); if (ret != 0) { /* nonexisting index requested */ pthread_mutex_unlock(&g_agent->lock); @@ -678,7 +1144,7 @@ static int snmp_agent_table_index_oid_handler( requests->requestvb->type = ASN_INTEGER; snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type, - &index, sizeof(index)); + (const u_char *)&index, sizeof(index)); pthread_mutex_unlock(&g_agent->lock); @@ -697,7 +1163,7 @@ static int snmp_agent_table_size_oid_handler( struct netsnmp_agent_request_info_s *reqinfo, struct netsnmp_request_info_s *requests) { - if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) { + if (reqinfo->mode != MODE_GET) { DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode); return SNMP_ERR_NOERROR; } @@ -717,14 +1183,18 @@ static int snmp_agent_table_size_oid_handler( if (td->size_oid.oid_len && (snmp_oid_ncompare(oid.oid, oid.oid_len, td->size_oid.oid, td->size_oid.oid_len, - MIN(oid.oid_len, td->size_oid.oid_len)) == 0)) { + SNMP_MIN(oid.oid_len, td->size_oid.oid_len)) == 0)) { DEBUG(PLUGIN_NAME ": Handle '%s' table size OID", td->name); - long size = c_avl_size(td->index_instance); + long size; + if (td->index_oid.oid_len) + size = c_avl_size(td->index_instance); + else + size = c_avl_size(td->instance_index); - requests->requestvb->type = td->size_oid.type; + requests->requestvb->type = ASN_INTEGER; snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type, - &size, sizeof(size)); + (const u_char *)&size, sizeof(size)); pthread_mutex_unlock(&g_agent->lock); @@ -743,7 +1213,7 @@ snmp_agent_scalar_oid_handler(struct netsnmp_mib_handler_s *handler, struct netsnmp_agent_request_info_s *reqinfo, struct netsnmp_request_info_s *requests) { - if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) { + if (reqinfo->mode != MODE_GET) { DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode); return SNMP_ERR_NOERROR; } @@ -765,7 +1235,7 @@ snmp_agent_scalar_oid_handler(struct netsnmp_mib_handler_s *handler, de = de->next) { data_definition_t *dd = de->value; - for (int i = 0; i < dd->oids_len; i++) { + for (size_t i = 0; i < dd->oids_len; i++) { int ret = snmp_oid_compare(oid.oid, oid.oid_len, dd->oids[i].oid, dd->oids[i].oid_len); @@ -803,7 +1273,7 @@ static int snmp_agent_register_table_oids(void) { for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) { data_definition_t *dd = de->value; - for (int i = 0; i < dd->oids_len; i++) { + for (size_t i = 0; i < dd->oids_len; i++) { dd->oids[i].type = snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len); } @@ -818,7 +1288,7 @@ static int snmp_agent_register_scalar_oids(void) { for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) { data_definition_t *dd = e->value; - for (int i = 0; i < dd->oids_len; i++) { + for (size_t i = 0; i < dd->oids_len; i++) { dd->oids[i].type = snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len); @@ -921,98 +1391,125 @@ static int snmp_agent_config_table_index_oid(table_definition_t *td, return 0; } -static int snmp_agent_config_table_data(table_definition_t *td, - oconfig_item_t *ci) { - data_definition_t *dd; - int ret = 0; +/* Getting index key source that will represent table row */ +static int snmp_agent_config_index_key_source(table_definition_t *td, + data_definition_t *dd, + oconfig_item_t *ci) { + char *val = NULL; - assert(ci != NULL); + int ret = cf_util_get_string(ci, &val); + if (ret != 0) + return -1; - dd = calloc(1, sizeof(*dd)); - if (dd == NULL) { - ERROR(PLUGIN_NAME ": Failed to allocate memory for table data definition"); - return -ENOMEM; + _Bool match = 0; + + for (int i = 0; i < MAX_KEY_SOURCES; i++) { + if (strcasecmp(index_opts[i], (const char *)val) == 0) { + td->index_keys[td->index_keys_len].source = i; + td->index_keys[td->index_keys_len].group = GROUP_UNUSED; + td->index_keys[td->index_keys_len].regex = NULL; + match = 1; + break; + } } - ret = cf_util_get_string(ci, &dd->name); - if (ret != 0) { - sfree(dd); - return -1; + if (!match) { + ERROR(PLUGIN_NAME ": Failed to parse index key source: '%s'", val); + sfree(val); + return -EINVAL; } - dd->scale = 1.0; - dd->shift = 0.0; + sfree(val); + dd->index_key_pos = td->index_keys_len++; + dd->is_index_key = 1; - dd->table = td; + return 0; +} - for (int i = 0; i < ci->children_num; i++) { - oconfig_item_t *option = ci->children + i; +/* Getting format string used to parse values from index key source */ +static int snmp_agent_config_index_key_regex(table_definition_t *td, + data_definition_t *dd, + oconfig_item_t *ci) { + index_key_t *index_key = &td->index_keys[dd->index_key_pos]; - if (strcasecmp("Instance", option->key) == 0) - ret = cf_util_get_boolean(option, &dd->is_instance); - else if (strcasecmp("Plugin", option->key) == 0) - ret = cf_util_get_string(option, &dd->plugin); - else if (strcasecmp("PluginInstance", option->key) == 0) - ret = cf_util_get_string(option, &dd->plugin_instance); - else if (strcasecmp("Type", option->key) == 0) - ret = cf_util_get_string(option, &dd->type); - else if (strcasecmp("TypeInstance", option->key) == 0) - ret = cf_util_get_string(option, &dd->type_instance); - else if (strcasecmp("Shift", option->key) == 0) - ret = cf_util_get_double(option, &dd->shift); - else if (strcasecmp("Scale", option->key) == 0) - ret = cf_util_get_double(option, &dd->scale); - else if (strcasecmp("OIDs", option->key) == 0) - ret = snmp_agent_config_data_oids(dd, option); - else { - WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key); - ret = -1; - } + int ret = cf_util_get_string(ci, &index_key->regex); + if (ret != 0) + return -1; - if (ret != 0) { - snmp_agent_free_data(&dd); - return -1; - } + ret = regcomp(&index_key->regex_info, index_key->regex, REG_EXTENDED); + if (ret) { + ERROR(PLUGIN_NAME ": Could not compile regex for %s", dd->name); + return -1; } - llentry_t *entry = llentry_create(dd->name, dd); - if (entry == NULL) { - snmp_agent_free_data(&dd); - return -ENOMEM; + index_key_src_t source = index_key->source; + if (td->tokens[source] == NULL && index_key->regex != NULL) { + td->tokens[source] = + c_avl_create((int (*)(const void *, const void *))num_compare); + if (td->tokens[source] == NULL) { + ERROR(PLUGIN_NAME ": Could not allocate memory for AVL tree"); + return -ENOMEM; + } } - llist_append(td->columns, entry); - return 0; } -static int snmp_agent_config_data(oconfig_item_t *ci) { +static int snmp_agent_config_index_key(table_definition_t *td, + data_definition_t *dd, + oconfig_item_t *ci) { + int ret = 0; + + for (int i = 0; (i < ci->children_num && ret == 0); i++) { + oconfig_item_t *option = ci->children + i; + + if (strcasecmp("Source", option->key) == 0) + ret = snmp_agent_config_index_key_source(td, dd, option); + else if (strcasecmp("Regex", option->key) == 0) + ret = snmp_agent_config_index_key_regex(td, dd, option); + else if (strcasecmp("Group", option->key) == 0) + ret = cf_util_get_int(option, &td->index_keys[dd->index_key_pos].group); + } + + return ret; +} + +/* This function parses configuration of both scalar and table column + * because they have nearly the same structure */ +static int snmp_agent_config_table_column(table_definition_t *td, + oconfig_item_t *ci) { data_definition_t *dd; int ret = 0; + oconfig_item_t *option_tmp = NULL; assert(ci != NULL); dd = calloc(1, sizeof(*dd)); if (dd == NULL) { - ERROR(PLUGIN_NAME ": Failed to allocate memory for data definition"); + ERROR(PLUGIN_NAME ": Failed to allocate memory for table data definition"); return -ENOMEM; } ret = cf_util_get_string(ci, &dd->name); if (ret != 0) { - free(dd); + sfree(dd); return -1; } dd->scale = 1.0; dd->shift = 0.0; + /* NULL if it's a scalar */ + dd->table = td; + dd->is_index_key = 0; for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *option = ci->children + i; - if (strcasecmp("Instance", option->key) == 0) - ret = cf_util_get_boolean(option, &dd->is_instance); - else if (strcasecmp("Plugin", option->key) == 0) + /* First 3 options are reserved for table entry only */ + if (td != NULL && strcasecmp("IndexKey", option->key) == 0) { + dd->is_index_key = 1; + option_tmp = option; + } else if (strcasecmp("Plugin", option->key) == 0) ret = cf_util_get_string(option, &dd->plugin); else if (strcasecmp("PluginInstance", option->key) == 0) ret = cf_util_get_string(option, &dd->plugin_instance); @@ -1037,17 +1534,35 @@ static int snmp_agent_config_data(oconfig_item_t *ci) { } } + if (dd->is_index_key) { + ret = snmp_agent_config_index_key(td, dd, option_tmp); + td->index_keys[dd->index_key_pos].type = + snmp_agent_get_asn_type(dd->oids[0].oid, dd->oids[0].oid_len); + + if (ret != 0) { + snmp_agent_free_data(&dd); + return -1; + } + } + llentry_t *entry = llentry_create(dd->name, dd); if (entry == NULL) { snmp_agent_free_data(&dd); return -ENOMEM; } - llist_append(g_agent->scalars, entry); + /* Append to column list in parent table */ + if (td != NULL) + llist_append(td->columns, entry); return 0; } +/* Parses scalar configuration entry */ +static int snmp_agent_config_scalar(oconfig_item_t *ci) { + return snmp_agent_config_table_column(NULL, ci); +} + static int num_compare(const int *a, const int *b) { assert((a != NULL) && (b != NULL)); if (*a < *b) @@ -1058,6 +1573,10 @@ static int num_compare(const int *a, const int *b) { return 0; } +static int oid_compare(const oid_t *a, const oid_t *b) { + return snmp_oid_compare(a->oid, a->oid_len, b->oid, b->oid_len); +} + static int snmp_agent_config_table(oconfig_item_t *ci) { table_definition_t *td; int ret = 0; @@ -1083,6 +1602,10 @@ static int snmp_agent_config_table(oconfig_item_t *ci) { return -ENOMEM; } + for (int i = 0; i < MAX_KEY_SOURCES; i++) + td->tokens[i] = NULL; + td->tokens_done = 0; + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *option = ci->children + i; @@ -1091,7 +1614,7 @@ static int snmp_agent_config_table(oconfig_item_t *ci) { else if (strcasecmp("SizeOID", option->key) == 0) ret = snmp_agent_config_table_size_oid(td, option); else if (strcasecmp("Data", option->key) == 0) - ret = snmp_agent_config_table_data(td, option); + ret = snmp_agent_config_table_column(td, option); else { WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key); ret = -1; @@ -1103,14 +1626,13 @@ static int snmp_agent_config_table(oconfig_item_t *ci) { } } - llentry_t *entry = llentry_create(td->name, td); - if (entry == NULL) { - snmp_agent_free_table(&td); - return -ENOMEM; - } + /* Preparing index list container */ + ret = snmp_agent_prep_index_list(td, &td->index_list_cont); + if (ret != 0) + return -EINVAL; td->instance_index = - c_avl_create((int (*)(const void *, const void *))strcmp); + c_avl_create((int (*)(const void *, const void *))oid_compare); if (td->instance_index == NULL) { snmp_agent_free_table(&td); return -ENOMEM; @@ -1123,6 +1645,11 @@ static int snmp_agent_config_table(oconfig_item_t *ci) { 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; @@ -1226,43 +1753,37 @@ static int snmp_agent_unregister_oid_index(oid_t *oid, int index) { return unregister_mib(new_oid.oid, new_oid.oid_len); } -static int snmp_agent_update_index(table_definition_t *td, - const char *instance) { +static int snmp_agent_update_index(table_definition_t *td, oid_t *index_oid) { - if (c_avl_get(td->instance_index, instance, NULL) == 0) + if (c_avl_get(td->instance_index, index_oid, NULL) == 0) return 0; int ret; int *index = NULL; - char *ins; - - ins = strdup(instance); - if (ins == NULL) - return -ENOMEM; /* need to generate index for the table */ if (td->index_oid.oid_len) { index = calloc(1, sizeof(*index)); if (index == NULL) { - sfree(ins); + sfree(index_oid); return -ENOMEM; } *index = c_avl_size(td->instance_index) + 1; - ret = c_avl_insert(td->instance_index, ins, index); + ret = c_avl_insert(td->instance_index, index_oid, index); if (ret != 0) { - sfree(ins); + sfree(index_oid); sfree(index); return ret; } - ret = c_avl_insert(td->index_instance, index, ins); + 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); - c_avl_remove(td->instance_index, ins, NULL, (void **)&index); - sfree(ins); + c_avl_remove(td->instance_index, index_oid, NULL, (void **)&index); + sfree(index_oid); sfree(index); return ret; } @@ -1273,9 +1794,9 @@ static int snmp_agent_update_index(table_definition_t *td, return ret; } else { /* instance as a key is required for any table */ - ret = c_avl_insert(td->instance_index, ins, ins); + ret = c_avl_insert(td->instance_index, index_oid, NULL); if (ret != 0) { - sfree(ins); + sfree(index_oid); return ret; } } @@ -1284,37 +1805,39 @@ static int snmp_agent_update_index(table_definition_t *td, for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) { data_definition_t *dd = de->value; - for (int i = 0; i < dd->oids_len; i++) { - if (td->index_oid.oid_len) { + 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], ins, + else + ret = snmp_agent_register_oid_string(&dd->oids[i], index_oid, snmp_agent_table_oid_handler); - } if (ret != 0) return ret; } } - DEBUG(PLUGIN_NAME ": Updated index for '%s' table [%d, %s]", td->name, - (index != NULL) ? *index : -1, ins); + 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); notification_t n = { .severity = NOTIF_OKAY, .time = cdtime(), .plugin = PLUGIN_NAME}; sstrncpy(n.host, hostname_g, sizeof(n.host)); - sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance)); - ssnprintf(n.message, sizeof(n.message), - "Data row added to table %s instance %s index %d", td->name, ins, - (index != NULL) ? *index : -1); + 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); + plugin_dispatch_notification(&n); return 0; } static int snmp_agent_write(value_list_t const *vl) { - if (vl == NULL) return -EINVAL; @@ -1323,12 +1846,20 @@ 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_instance) { + if (!dd->is_index_key) { if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type, vl->type_instance)) { - snmp_agent_update_index(td, vl->plugin_instance); - return 0; + ret = snmp_agent_generate_index(td, vl, index_oid); + if (ret == 0) + ret = snmp_agent_update_index(td, index_oid); + + return ret; } } } @@ -1350,10 +1881,6 @@ static int snmp_agent_collect(const data_set_t *ds, const value_list_t *vl, } static int snmp_agent_preinit(void) { - if (g_agent != NULL) { - /* already initialized if config callback was called before init callback */ - return 0; - } g_agent = calloc(1, sizeof(*g_agent)); if (g_agent == NULL) { @@ -1364,12 +1891,21 @@ static int snmp_agent_preinit(void) { g_agent->tables = llist_create(); g_agent->scalars = llist_create(); + 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); + return -ENOMEM; + } + int err; - /* make us a agentx client. */ + /* make us an agentx client. */ err = netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1); if (err != 0) { ERROR(PLUGIN_NAME ": Failed to set agent role (%d)", err); + llist_destroy(g_agent->scalars); + llist_destroy(g_agent->tables); return -1; } @@ -1381,6 +1917,8 @@ static int snmp_agent_preinit(void) { err = init_agent(PLUGIN_NAME); if (err != 0) { ERROR(PLUGIN_NAME ": Failed to initialize the agent library (%d)", err); + llist_destroy(g_agent->scalars); + llist_destroy(g_agent->tables); return -1; } @@ -1394,9 +1932,13 @@ static int snmp_agent_preinit(void) { static int snmp_agent_init(void) { int ret; - ret = snmp_agent_preinit(); - if (ret != 0) - return ret; + if (g_agent == NULL || ((llist_head(g_agent->scalars) == NULL) && + (llist_head(g_agent->tables) == NULL))) { + ERROR(PLUGIN_NAME ": snmp_agent_init: plugin not configured"); + return -EINVAL; + } + + plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown); ret = snmp_agent_register_scalar_oids(); if (ret != 0) @@ -1406,13 +1948,6 @@ static int snmp_agent_init(void) { if (ret != 0) return ret; - /* create a second thread to listen for requests from AgentX*/ - ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL); - if (ret != 0) { - ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret); - return ret; - } - ret = pthread_mutex_init(&g_agent->lock, NULL); if (ret != 0) { ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret); @@ -1425,6 +1960,18 @@ static int snmp_agent_init(void) { return ret; } + /* create a second thread to listen for requests from AgentX*/ + ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL); + if (ret != 0) { + ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret); + return ret; + } + + if (llist_head(g_agent->tables) != NULL) { + plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL); + plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL); + } + return 0; } @@ -1527,7 +2074,6 @@ static int snmp_agent_shutdown(void) { } static int snmp_agent_config(oconfig_item_t *ci) { - int ret = snmp_agent_preinit(); if (ret != 0) { @@ -1538,7 +2084,7 @@ static int snmp_agent_config(oconfig_item_t *ci) { for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp("Data", child->key) == 0) { - ret = snmp_agent_config_data(child); + ret = snmp_agent_config_scalar(child); } else if (strcasecmp("Table", child->key) == 0) { ret = snmp_agent_config_table(child); } else { @@ -1555,7 +2101,7 @@ static int snmp_agent_config(oconfig_item_t *ci) { } } - ret = snmp_agent_validate_data(); + ret = snmp_agent_validate_config(); if (ret != 0) { ERROR(PLUGIN_NAME ": Invalid configuration provided"); snmp_agent_free_config(); @@ -1570,7 +2116,4 @@ static int snmp_agent_config(oconfig_item_t *ci) { void module_register(void) { plugin_register_init(PLUGIN_NAME, snmp_agent_init); plugin_register_complex_config(PLUGIN_NAME, snmp_agent_config); - plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL); - plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL); - plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown); }