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);
regmatch_t m = {-1, -1};
/* 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");
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
key = key->next_variable;
}
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);
#include "snmp_agent.c"
#include "testing.h"
+#define TEST_HOSTNAME "test_hostname"
+#define TEST_PLUGIN "test_plugin"
+#define TEST_PLUGIN_INST "test_plugin_inst"
+#define TEST_TYPE "test_type"
+#define TEST_TYPE_INST "test_type_inst"
+
DEF_TEST(oid_to_string) {
oid_t o = {.oid = {1, 2, 3, 4, 5, 6, 7, 8, 9}, .oid_len = 9};
char oid_str[DATA_MAX_NAME_LEN];
DEF_TEST(format_name_scalar) {
data_definition_t *dd = calloc(1, sizeof(*dd));
- dd->plugin = strdup("test_plugin");
- dd->plugin_instance = strdup("test_plugin_inst");
- dd->type = strdup("test_type");
- dd->type_instance = strdup("test_type_inst");
+ dd->plugin = TEST_PLUGIN;
+ dd->plugin_instance = TEST_PLUGIN_INST;
+ dd->type = TEST_TYPE;
+ dd->type_instance = TEST_TYPE_INST;
char name[DATA_MAX_NAME_LEN];
int ret = snmp_agent_format_name(name, sizeof(name), dd, NULL);
"example.com/test_plugin-test_plugin_inst/test_type-test_type_inst",
name);
- sfree(dd->plugin);
- sfree(dd->plugin_instance);
- sfree(dd->type);
- sfree(dd->type_instance);
sfree(dd);
return 0;
td->index_keys[1].source = INDEX_TYPE_INSTANCE;
td->index_keys[1].type = ASN_OCTET_STR;
dd->table = td;
- dd->plugin = strdup("test_plugin");
- dd->type = strdup("test_type");
+ dd->plugin = TEST_PLUGIN;
+ dd->type = TEST_TYPE;
- char *plugin_inst = strdup("test_plugin_inst");
- char *type_inst = strdup("test_type_inst");
+ const char plugin_inst[] = TEST_PLUGIN_INST;
+ const char type_inst[] = TEST_TYPE_INST;
snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_OCTET_STR,
- plugin_inst, strlen(plugin_inst));
- snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_OCTET_STR, type_inst,
- strlen(type_inst));
+ (const u_char *)plugin_inst, strlen(plugin_inst));
+ snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_OCTET_STR,
+ (const u_char *)type_inst, strlen(type_inst));
build_oid_noalloc(index_oid.oid, sizeof(index_oid.oid), &index_oid.oid_len,
NULL, 0, index_list_tmp);
snmp_free_varbind(index_list_tmp);
snmp_free_varbind(td->index_list_cont);
- sfree(dd->plugin);
- sfree(dd->type);
sfree(dd);
sfree(td);
- sfree(plugin_inst);
- sfree(type_inst);
return 0;
}
td->index_keys[0].type = ASN_OCTET_STR;
td->index_keys[1].source = INDEX_TYPE_INSTANCE;
td->index_keys[1].type = ASN_INTEGER;
- td->index_keys[1].regex = strdup("^vcpu_([0-9]{1,3})-cpu_[0-9]{1,3}$");
+ td->index_keys[1].regex = "^vcpu_([0-9]{1,3})-cpu_[0-9]{1,3}$";
td->index_keys[1].group = 1;
td->index_keys[2].source = INDEX_TYPE_INSTANCE;
td->index_keys[2].type = ASN_INTEGER;
- td->index_keys[2].regex = strdup("^vcpu_[0-9]{1,3}-cpu_([0-9]{1,3})$");
+ td->index_keys[2].regex = "^vcpu_[0-9]{1,3}-cpu_([0-9]{1,3})$";
td->index_keys[2].group = 1;
dd->table = td;
- dd->plugin = strdup("test_plugin");
- dd->type = strdup("test_type");
+ dd->plugin = TEST_PLUGIN;
+ dd->type = TEST_TYPE;
- char *plugin_inst = strdup("test_plugin_inst");
- char *type_inst = strdup("vcpu_1-cpu_10");
+ const char plugin_inst[] = TEST_PLUGIN_INST;
int vcpu = 1;
int cpu = 10;
snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_OCTET_STR,
- plugin_inst, strlen(plugin_inst));
- snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_INTEGER, &vcpu, 1);
- snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_INTEGER, &cpu, 1);
+ (const u_char *)plugin_inst, strlen(plugin_inst));
+ snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_INTEGER,
+ (const u_char *)&vcpu, 1);
+ snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_INTEGER,
+ (const u_char *)&cpu, 1);
build_oid_noalloc(index_oid.oid, sizeof(index_oid.oid), &index_oid.oid_len,
NULL, 0, index_list_tmp);
c_avl_destroy(td->tokens[INDEX_TYPE_INSTANCE]);
snmp_free_varbind(index_list_tmp);
snmp_free_varbind(td->index_list_cont);
- for (int i = 0; i < td->index_keys_len; i++)
- sfree(td->index_keys[i].regex);
- sfree(dd->plugin);
- sfree(dd->type);
sfree(dd);
sfree(td);
- sfree(plugin_inst);
- sfree(type_inst);
return 0;
}
/* Preparing value list */
value_list_t *vl = calloc(1, sizeof(*vl));
assert(vl != NULL);
- strncpy(vl->host, "test_hostname", DATA_MAX_NAME_LEN);
- strncpy(vl->plugin, "test_plugin", DATA_MAX_NAME_LEN);
- strncpy(vl->plugin_instance, "test_plugin_inst", DATA_MAX_NAME_LEN);
- strncpy(vl->type, "test_type", DATA_MAX_NAME_LEN);
- strncpy(vl->type_instance, "test_type_inst", DATA_MAX_NAME_LEN);
+ strncpy(vl->host, TEST_HOSTNAME, DATA_MAX_NAME_LEN);
+ strncpy(vl->plugin, TEST_PLUGIN, DATA_MAX_NAME_LEN);
+ strncpy(vl->plugin_instance, TEST_PLUGIN_INST, DATA_MAX_NAME_LEN);
+ strncpy(vl->type, TEST_TYPE, DATA_MAX_NAME_LEN);
+ strncpy(vl->type_instance, TEST_TYPE_INST, DATA_MAX_NAME_LEN);
td->index_keys_len = 5;
td->index_keys[0].source = INDEX_HOST;
/* Preparing value list */
value_list_t *vl = calloc(1, sizeof(*vl));
- strncpy(vl->plugin_instance, "test_plugin_inst", DATA_MAX_NAME_LEN);
+ strncpy(vl->plugin_instance, TEST_PLUGIN_INST, DATA_MAX_NAME_LEN);
strncpy(vl->type_instance, "1test2test3", DATA_MAX_NAME_LEN);
td->index_keys_len = 4;
td->index_keys[0].type = ASN_OCTET_STR;
td->index_keys[1].source = INDEX_TYPE_INSTANCE;
td->index_keys[1].type = ASN_INTEGER;
- td->index_keys[1].regex = strdup("^([0-9])test[0-9]test[0-9]$");
+ td->index_keys[1].regex = "^([0-9])test[0-9]test[0-9]$";
td->index_keys[1].group = 1;
td->index_keys[2].source = INDEX_TYPE_INSTANCE;
td->index_keys[2].type = ASN_INTEGER;
- td->index_keys[2].regex = strdup("^[0-9]test([0-9])test[0-9]$");
+ td->index_keys[2].regex = "^[0-9]test([0-9])test[0-9]$";
td->index_keys[2].group = 1;
td->index_keys[3].source = INDEX_TYPE_INSTANCE;
td->index_keys[3].type = ASN_INTEGER;
- td->index_keys[3].regex = strdup("^[0-9]test[0-9]test([0-9])$");
+ td->index_keys[3].regex = "^[0-9]test[0-9]test([0-9])$";
td->index_keys[3].group = 1;
td->index_list_cont = NULL;
sfree(vl);
for (int i = 0; i < td->index_keys_len; i++) {
- sfree(td->index_keys[i].regex);
regfree(&td->index_keys[i].regex_info);
}
sfree(td);
ci->values = calloc(1, sizeof(*ci->values));
assert(ci->values != NULL);
ci->values_num = 1;
- ci->values->value.string = strdup("PluginInstance");
+ ci->values->value.string = "PluginInstance";
ci->values->type = OCONFIG_TYPE_STRING;
int ret = snmp_agent_config_index_key_source(td, dd, ci);
EXPECT_EQ_INT(GROUP_UNUSED, td->index_keys[0].group);
OK(td->index_keys[0].regex == NULL);
- sfree(ci->values->value.string);
sfree(ci->values);
sfree(ci);
sfree(td);
ci->values = calloc(1, sizeof(*ci->values));
assert(ci->values != NULL);
ci->values_num = 1;
- ci->values->value.string = strdup("^([0-9])test[0-9]test[0-9]$");
+ ci->values->value.string = "^([0-9])test[0-9]test[0-9]$";
ci->values->type = OCONFIG_TYPE_STRING;
int ret = snmp_agent_config_index_key_regex(td, dd, ci);
OK(td->tokens[INDEX_PLUGIN_INSTANCE] != NULL);
c_avl_destroy(td->tokens[INDEX_PLUGIN_INSTANCE]);
- sfree(ci->values->value.string);
sfree(ci->values);
sfree(ci);
sfree(td->index_keys[0].regex);
ci->children_num = 3;
ci->children = calloc(1, sizeof(*ci->children) * ci->children_num);
- ci->children[0].key = strdup("Source");
+ ci->children[0].key = "Source";
ci->children[0].parent = ci;
ci->children[0].values_num = 1;
ci->children[0].values = calloc(1, sizeof(*ci->children[0].values));
assert(ci->children[0].values != NULL);
- ci->children[0].values->value.string = strdup("PluginInstance");
+ ci->children[0].values->value.string = "PluginInstance";
ci->children[0].values->type = OCONFIG_TYPE_STRING;
- ci->children[1].key = strdup("Regex");
+ ci->children[1].key = "Regex";
ci->children[1].parent = ci;
ci->children[1].values_num = 1;
ci->children[1].values = calloc(1, sizeof(*ci->children[0].values));
assert(ci->children[1].values != NULL);
- ci->children[1].values->value.string = strdup("^([0-9])test[0-9]test[0-9]$");
+ ci->children[1].values->value.string = "^([0-9])test[0-9]test[0-9]$";
ci->children[1].values->type = OCONFIG_TYPE_STRING;
- ci->children[2].key = strdup("Group");
+ ci->children[2].key = "Group";
ci->children[2].parent = ci;
ci->children[2].values_num = 1;
ci->children[2].values = calloc(1, sizeof(*ci->children[0].values));
EXPECT_EQ_STR("^([0-9])test[0-9]test[0-9]$", td->index_keys[0].regex);
OK(td->tokens[INDEX_PLUGIN_INSTANCE] != NULL);
- sfree(ci->children[0].values->value.string);
sfree(ci->children[0].values);
- sfree(ci->children[0].key);
-
- sfree(ci->children[1].values->value.string);
sfree(ci->children[1].values);
- sfree(ci->children[1].key);
-
sfree(ci->children[2].values);
- sfree(ci->children[2].key);
sfree(ci->children);
sfree(ci);
}
DEF_TEST(parse_index_key) {
- char *regex = strdup("test-([0-9])-([0-9])");
- char *input = strdup("snmp-test-5-6");
+ const char regex[] = "test-([0-9])-([0-9])";
+ const char input[] = "snmp-test-5-6";
regex_t regex_info;
regmatch_t match;
- assert(regex != NULL);
- assert(input != NULL);
-
int ret = regcomp(®ex_info, regex, REG_EXTENDED);
EXPECT_EQ_INT(0, ret);
- ret = snmp_agent_parse_index_key(input, regex, ®ex_info, 0, &match);
+ ret = snmp_agent_parse_index_key(input, ®ex_info, 0, &match);
EXPECT_EQ_INT(0, ret);
EXPECT_EQ_INT(5, match.rm_so);
EXPECT_EQ_INT(13, match.rm_eo);
- ret = snmp_agent_parse_index_key(input, regex, ®ex_info, 1, &match);
+ ret = snmp_agent_parse_index_key(input, ®ex_info, 1, &match);
EXPECT_EQ_INT(0, ret);
EXPECT_EQ_INT(10, match.rm_so);
EXPECT_EQ_INT(11, match.rm_eo);
- ret = snmp_agent_parse_index_key(input, regex, ®ex_info, 2, &match);
+ ret = snmp_agent_parse_index_key(input, ®ex_info, 2, &match);
EXPECT_EQ_INT(0, ret);
EXPECT_EQ_INT(12, match.rm_so);
EXPECT_EQ_INT(13, match.rm_eo);
- sfree(regex);
- sfree(input);
regfree(®ex_info);
return 0;
DEF_TEST(create_token) {
c_avl_tree_t *tokens =
c_avl_create((int (*)(const void *, const void *))num_compare);
- char *input = strdup("testA1-testB2");
+ const char input[] = "testA1-testB2";
assert(tokens != NULL);
- assert(input != NULL);
int ret = snmp_agent_create_token(input, 0, 5, tokens, NULL);
EXPECT_EQ_INT(0, ret);
OK(ret != 0);
c_avl_destroy(tokens);
- sfree(input);
return 0;
}
DEF_TEST(delete_token) {
c_avl_tree_t *tokens =
c_avl_create((int (*)(const void *, const void *))num_compare);
- char *input = strdup("testA1-testB2-testC3");
+ const char input[] = "testA1-testB2-testC3";
assert(tokens != NULL);
- assert(input != NULL);
int ret = snmp_agent_create_token(input, 0, 5, tokens, NULL);
EXPECT_EQ_INT(0, ret);
OK(ret != 0);
c_avl_destroy(tokens);
- sfree(input);
return 0;
}
DEF_TEST(get_token) {
c_avl_tree_t *tokens =
c_avl_create((int (*)(const void *, const void *))num_compare);
- char *input = strdup("testA1-testB2-testC3");
+ const char input[] = "testA1-testB2-testC3";
assert(tokens != NULL);
- assert(input != NULL);
int ret = snmp_agent_create_token(input, 0, 5, tokens, NULL);
EXPECT_EQ_INT(0, ret);
}
c_avl_destroy(tokens);
- sfree(input);
return 0;
}
{19, 20}}; /* "3" */
c_avl_tree_t *tokens =
c_avl_create((int (*)(const void *, const void *))num_compare);
- char *input = strdup("testA1-testB2-testC3");
+ const char input[] = "testA1-testB2-testC3";
token_t *token;
int *offset;
c_avl_iterator_t *it;
int ret;
assert(tokens != NULL);
- assert(input != NULL);
/* First pass */
ret = snmp_agent_tokenize(input, tokens, &m[0], NULL);
}
c_avl_destroy(tokens);
- sfree(input);
return 0;
}
for (int i = 0; i < 3; i++) {
token = malloc(sizeof(*token));
token->str = t[i];
- token->key = snmp_varlist_add_variable(&td->index_list_cont, NULL, 0,
- ASN_INTEGER, &n[i], sizeof(n[i]));
+ token->key =
+ snmp_varlist_add_variable(&td->index_list_cont, NULL, 0, ASN_INTEGER,
+ (const u_char *)&n[i], sizeof(n[i]));
assert(token->key != NULL);
offset = &off[i];
ret = c_avl_insert(tokens, (void *)offset, (void *)token);