2 * collectd - src/snmp_agent.c
4 * Copyright(c) 2017-2018 Intel Corporation. All rights reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Roman Korynkevych <romanx.korynkevych@intel.com>
26 * Serhiy Pshyk <serhiyx.pshyk@intel.com>
27 * Marcin Mozejko <marcinx.mozejko@intel.com>
33 #include "utils_avltree.h"
34 #include "utils_cache.h"
35 #include "utils_llist.h"
38 #include <net-snmp/net-snmp-config.h>
40 #include <net-snmp/net-snmp-includes.h>
42 #include <net-snmp/agent/net-snmp-agent-includes.h>
44 #define PLUGIN_NAME "snmp_agent"
45 #define TYPE_STRING -1
46 #define GROUP_UNUSED -1
48 #define MAX_KEY_SOURCES 5
49 #define MAX_INDEX_KEYS 5
52 /* Identifies index key source */
53 enum index_key_src_e {
56 INDEX_PLUGIN_INSTANCE,
60 typedef enum index_key_src_e index_key_src_t;
63 index_key_src_t source;
65 char *regex; /* Pattern used to parse index key source string */
66 int group; /* If pattern gives more than one group we can specify which one
70 typedef struct index_key_s index_key_t;
77 typedef struct oid_s oid_t;
81 netsnmp_variable_list *key; /* Points to succeeding key */
83 typedef struct token_s token_t;
85 struct table_definition_s {
90 c_avl_tree_t *instance_index;
91 c_avl_tree_t *index_instance;
92 c_avl_tree_t *instance_oids; /* Tells us how many OIDs registered for every
94 index_key_t index_keys[MAX_INDEX_KEYS]; /* Stores information about what each
95 index key represents */
97 netsnmp_variable_list *index_list_cont; /* Index key container used for
98 generating as well as parsing
99 OIDs, not thread-safe */
100 c_avl_tree_t *tokens[MAX_KEY_SOURCES]; /* Input string after regex execution
101 will be split into sepearate
104 bool tokens_done; /* Set to true when all tokens are generated */
106 typedef struct table_definition_s table_definition_t;
108 struct data_definition_s {
111 char *plugin_instance;
114 const table_definition_t *table;
115 bool is_index_key; /* indicates if table column is an index key */
116 int index_key_pos; /* position in indexes list */
122 typedef struct data_definition_s data_definition_t;
124 struct snmp_agent_ctx_s {
126 pthread_mutex_t lock;
127 pthread_mutex_t agentx_lock;
132 c_avl_tree_t *registered_oids; /* AVL tree containing all registered OIDs */
134 typedef struct snmp_agent_ctx_s snmp_agent_ctx_t;
136 static snmp_agent_ctx_t *g_agent;
137 static const char *index_opts[MAX_KEY_SOURCES] = {
138 "Hostname", "Plugin", "PluginInstance", "Type", "TypeInstance"};
140 #define CHECK_DD_TYPE(_dd, _p, _pi, _t, _ti) \
141 (_dd->plugin ? !strcmp(_dd->plugin, _p) : 0) && \
142 (_dd->plugin_instance ? !strcmp(_dd->plugin_instance, _pi) : 1) && \
143 (_dd->type ? !strcmp(_dd->type, _t) : 0) && \
144 (_dd->type_instance ? !strcmp(_dd->type_instance, _ti) : 1)
146 static int snmp_agent_shutdown(void);
147 static void *snmp_agent_thread_run(void *arg);
148 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler);
149 static int snmp_agent_set_vardata(void *dst_buf, size_t *dst_buf_len,
150 u_char asn_type, double scale, double shift,
151 const void *value, size_t len, int type);
152 static int snmp_agent_unregister_oid_index(oid_t *oid, int index);
153 static int snmp_agent_update_instance_oids(c_avl_tree_t *tree, oid_t *index_oid,
155 static int num_compare(const int *a, const int *b);
157 static u_char snmp_agent_get_asn_type(oid *oid, size_t oid_len) {
158 struct tree *node = get_tree(oid, oid_len, g_agent->tp);
160 return (node != NULL) ? mib_to_asn_type(node->type) : 0;
163 static char *snmp_agent_get_oid_name(oid *oid, size_t oid_len) {
164 struct tree *node = get_tree(oid, oid_len, g_agent->tp);
166 return (node != NULL) ? node->label : NULL;
169 static int snmp_agent_oid_to_string(char *buf, size_t buf_size,
171 char oid_str[MAX_OID_LEN][16];
172 char *oid_str_ptr[MAX_OID_LEN];
174 for (size_t i = 0; i < o->oid_len; i++) {
175 snprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
176 oid_str_ptr[i] = oid_str[i];
179 return strjoin(buf, buf_size, oid_str_ptr, o->oid_len, ".");
182 /* Prints a configuration storing list. It handles both table columns list
185 static void snmp_agent_dump_data(llist_t *list) {
186 char oid_str[DATA_MAX_NAME_LEN];
187 for (llentry_t *de = llist_head(list); de != NULL; de = de->next) {
188 data_definition_t *dd = de->value;
189 table_definition_t const *td = dd->table;
191 if (dd->table != NULL)
192 DEBUG(PLUGIN_NAME ": Column:");
194 DEBUG(PLUGIN_NAME ": Scalar:");
196 DEBUG(PLUGIN_NAME ": Name: %s", dd->name);
198 DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin);
199 if (dd->plugin_instance)
200 DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance);
201 if (dd->is_index_key) {
202 index_key_t const *index_key = &td->index_keys[dd->index_key_pos];
204 DEBUG(PLUGIN_NAME ": IndexKey:");
205 DEBUG(PLUGIN_NAME ": Source: %s", index_opts[index_key->source]);
206 DEBUG(PLUGIN_NAME ": Type: %s",
207 (index_key->type == ASN_INTEGER) ? "Integer" : "String");
208 if (index_key->regex)
209 DEBUG(PLUGIN_NAME ": Regex: %s", index_key->regex);
210 if (index_key->group != GROUP_UNUSED)
211 DEBUG(PLUGIN_NAME ": Group: %d", index_key->group);
214 DEBUG(PLUGIN_NAME ": Type: %s", dd->type);
215 if (dd->type_instance)
216 DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance);
217 for (size_t i = 0; i < dd->oids_len; i++) {
218 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &dd->oids[i]);
219 DEBUG(PLUGIN_NAME ": OID[%" PRIsz "]: %s", i, oid_str);
221 DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale);
222 DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift);
226 /* Prints parsed configuration */
227 static void snmp_agent_dump_config(void) {
228 char oid_str[DATA_MAX_NAME_LEN];
230 /* Printing tables */
231 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
232 table_definition_t *td = te->value;
234 DEBUG(PLUGIN_NAME ": Table:");
235 DEBUG(PLUGIN_NAME ": Name: %s", td->name);
236 if (td->index_oid.oid_len != 0) {
237 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &td->index_oid);
238 DEBUG(PLUGIN_NAME ": IndexOID: %s", oid_str);
240 if (td->size_oid.oid_len != 0) {
241 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &td->size_oid);
242 DEBUG(PLUGIN_NAME ": SizeOID: %s", oid_str);
245 snmp_agent_dump_data(td->columns);
248 /* Printing scalars */
249 snmp_agent_dump_data(g_agent->scalars);
251 #endif /* COLLECT_DEBUG */
253 static int snmp_agent_validate_config(void) {
256 snmp_agent_dump_config();
259 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
260 table_definition_t *td = te->value;
262 if (!td->index_keys_len) {
263 ERROR(PLUGIN_NAME ": Index keys not defined for '%s'", td->name);
267 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
268 data_definition_t *dd = de->value;
271 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'.'%s'", td->name,
276 if (dd->plugin_instance) {
277 ERROR(PLUGIN_NAME ": PluginInstance should not be defined for table "
278 "data type '%s'.'%s'",
283 if (dd->oids_len == 0) {
284 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'.'%s'", td->name,
289 if (dd->is_index_key) {
290 if (dd->type || dd->type_instance) {
291 ERROR(PLUGIN_NAME ": Type and TypeInstance are not valid for "
292 "index data '%s'.'%s'",
297 if (dd->oids_len > 1) {
300 ": Only one OID should be specified for instance data '%s'.'%s'",
307 ERROR(PLUGIN_NAME ": Type not defined for data '%s'.'%s'", td->name,
315 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
316 data_definition_t *dd = e->value;
319 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'", dd->name);
323 if (dd->oids_len == 0) {
324 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'", dd->name);
328 if (dd->is_index_key) {
329 ERROR(PLUGIN_NAME ": Index field can't be specified for scalar data '%s'",
335 ERROR(PLUGIN_NAME ": Type not defined for data '%s'", dd->name);
343 static int snmp_agent_parse_index_key(const char *input, regex_t *regex_info,
344 int gi, regmatch_t *m) {
345 regmatch_t matches[MAX_MATCHES];
347 int ret = regexec(regex_info, input, MAX_MATCHES, matches, 0);
349 if (gi > regex_info->re_nsub) {
350 ERROR(PLUGIN_NAME ": Group index %d not found. Check regex config", gi);
354 } else if (ret == REG_NOMATCH) {
355 ERROR(PLUGIN_NAME ": No match found");
360 regerror(ret, regex_info, msgbuf, sizeof(msgbuf));
361 ERROR(PLUGIN_NAME ": Regex match failed: %s", msgbuf);
368 static int snmp_agent_create_token(char const *input, int t_off, int n,
370 netsnmp_variable_list *index_key) {
371 assert(tree != NULL);
373 token_t *token = malloc(sizeof(*token));
378 int *offset = malloc(sizeof(*offset));
381 goto free_token_error;
385 token->key = index_key;
387 size_t len = strlen(input);
392 token->str = malloc(len + 1);
394 if (token->str == NULL)
395 goto free_offset_error;
397 /* copy at most n bytes from input with offset t_off into token->str */
398 sstrncpy(token->str, input, len + 1);
400 ret = c_avl_insert(tree, (void *)offset, (void *)token);
414 ERROR(PLUGIN_NAME ": Could not allocate memory to create token");
419 static int snmp_agent_delete_token(int t_off, c_avl_tree_t *tree) {
420 token_t *token = NULL;
423 int ret = c_avl_remove(tree, &t_off, (void **)&offset, (void **)&token);
426 ERROR(PLUGIN_NAME ": Could not delete token");
436 static int snmp_agent_get_token(c_avl_tree_t *tree, int mpos) {
442 c_avl_iterator_t *it = c_avl_get_iterator(tree);
443 while (c_avl_iterator_next(it, (void **)&pos, (void **)&token) == 0) {
450 c_avl_iterator_destroy(it);
454 static int snmp_agent_tokenize(const char *input, c_avl_tree_t *tokens,
456 netsnmp_variable_list *key) {
457 assert(tokens != NULL);
460 int len = strlen(input);
462 /* Creating first token that is going to be split later */
463 if (c_avl_size(tokens) == 0) {
464 ret = snmp_agent_create_token(input, 0, len, tokens, NULL);
469 /* Divide token that contains current match into two */
470 int t_pos = snmp_agent_get_token(tokens, m->rm_so);
471 ret = snmp_agent_delete_token(t_pos, tokens);
476 ret = snmp_agent_create_token(input, t_pos, m->rm_so - t_pos, tokens, key);
481 if (len - m->rm_eo > 1) {
482 ret = snmp_agent_create_token(input, m->rm_eo, len - m->rm_eo + 1, tokens,
485 snmp_agent_delete_token(t_pos, tokens);
493 static int snmp_agent_fill_index_list(table_definition_t *td,
494 value_list_t const *vl) {
497 netsnmp_variable_list *key = td->index_list_cont;
500 for (i = 0; i < td->index_keys_len; i++) {
501 /* var should never be NULL */
504 const index_key_src_t source = td->index_keys[i].source;
505 c_avl_tree_t *const tokens = td->tokens[source];
506 /* Generating list filled with all data necessary to generate an OID */
514 case INDEX_PLUGIN_INSTANCE:
515 ptr = vl->plugin_instance;
520 case INDEX_TYPE_INSTANCE:
521 ptr = vl->type_instance;
524 ERROR(PLUGIN_NAME ": Unknown index key source provided");
528 /* Parsing input string if necessary */
529 if (td->index_keys[i].regex) {
532 /* Parsing input string */
533 ret = snmp_agent_parse_index_key(ptr, &td->index_keys[i].regex_info,
534 td->index_keys[i].group, &m);
536 ERROR(PLUGIN_NAME ": Error executing regex");
540 /* Tokenizing input string if not done yet */
541 if (td->tokens_done == false)
542 ret = snmp_agent_tokenize(ptr, tokens, &m, key);
547 if (td->index_keys[i].type == ASN_INTEGER) {
548 int val = strtol(ptr + m.rm_so, NULL, 0);
550 #ifdef HAVE_NETSNMP_OLD_API
551 ret = snmp_set_var_value(key, (const u_char *)&val, sizeof(val));
553 ret = snmp_set_var_value(key, &val, sizeof(val));
556 #ifdef HAVE_NETSNMP_OLD_API
557 ret = snmp_set_var_value(key, (const u_char *)(ptr + m.rm_so),
560 ret = snmp_set_var_value(key, ptr + m.rm_so, m.rm_eo - m.rm_so);
563 #ifdef HAVE_NETSNMP_OLD_API
564 ret = snmp_set_var_value(key, (const u_char *)ptr, strlen(ptr));
566 ret = snmp_set_var_value(key, ptr, strlen(ptr));
572 key = key->next_variable;
575 /* Tokens for all source strings are generated */
576 for (i = 0; i < MAX_KEY_SOURCES; i++)
577 td->tokens_done = true;
582 static int snmp_agent_prep_index_list(table_definition_t const *td,
583 netsnmp_variable_list **index_list) {
584 /* Generating list having only the structure (with no values) letting us
585 * know how to parse an OID*/
586 for (int i = 0; i < td->index_keys_len; i++) {
587 switch (td->index_keys[i].source) {
590 case INDEX_PLUGIN_INSTANCE:
592 case INDEX_TYPE_INSTANCE:
593 snmp_varlist_add_variable(index_list, NULL, 0, td->index_keys[i].type,
597 ERROR(PLUGIN_NAME ": Unknown index key source provided");
604 static int snmp_agent_generate_index(table_definition_t *td,
605 value_list_t const *vl, oid_t *index_oid) {
607 /* According to given information by index_keys list
608 * index OID is going to be built
610 int ret = snmp_agent_fill_index_list(td, vl);
614 /* Building only index part OID (without table prefix OID) */
615 ret = build_oid_noalloc(index_oid->oid, sizeof(index_oid->oid),
616 &index_oid->oid_len, NULL, 0, td->index_list_cont);
617 if (ret != SNMPERR_SUCCESS) {
618 ERROR(PLUGIN_NAME ": Error building index OID");
625 /* It appends one OID to the end of another */
626 static int snmp_agent_append_oid(oid_t *out, const oid_t *in) {
628 if (out->oid_len + in->oid_len > MAX_OID_LEN) {
629 ERROR(PLUGIN_NAME ": Cannot create OID. Output length is too long!");
632 memcpy(&out->oid[out->oid_len], in->oid, in->oid_len * sizeof(oid));
633 out->oid_len += in->oid_len;
638 static int snmp_agent_register_oid_string(const oid_t *oid,
639 const oid_t *index_oid,
640 Netsnmp_Node_Handler *handler) {
643 memcpy(&new_oid, oid, sizeof(*oid));
644 /* Concatenating two string oids */
645 int ret = snmp_agent_append_oid(&new_oid, index_oid);
649 return snmp_agent_register_oid(&new_oid, handler);
652 static int snmp_agent_unregister_oid(oid_t *oid) {
653 int ret = c_avl_remove(g_agent->registered_oids, (void *)oid, NULL, NULL);
656 ERROR(PLUGIN_NAME ": Could not delete registration info");
658 return unregister_mib(oid->oid, oid->oid_len);
661 static int snmp_agent_unregister_oid_string(oid_t *oid,
662 const oid_t *index_oid) {
664 char oid_str[DATA_MAX_NAME_LEN];
666 memcpy(&new_oid, oid, sizeof(*oid));
667 /* Concatenating two string oids */
668 int ret = snmp_agent_append_oid(&new_oid, index_oid);
672 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &new_oid);
673 DEBUG(PLUGIN_NAME ": Unregistered handler for OID (%s)", oid_str);
675 return snmp_agent_unregister_oid(&new_oid);
678 static void snmp_agent_table_data_remove(data_definition_t *dd,
679 table_definition_t *td,
682 oid_t *ind_oid = NULL;
684 if (td->index_oid.oid_len) {
685 if ((c_avl_get(td->instance_index, index_oid, (void **)&index) != 0) ||
686 (c_avl_get(td->index_instance, index, NULL) != 0))
689 if (c_avl_get(td->instance_index, index_oid, NULL) != 0)
693 pthread_mutex_lock(&g_agent->agentx_lock);
695 int reg_oids = -1; /* Number of registered oids for given instance */
697 for (size_t i = 0; i < dd->oids_len; i++) {
698 if (td->index_oid.oid_len)
699 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
701 snmp_agent_unregister_oid_string(&dd->oids[i], index_oid);
704 snmp_agent_update_instance_oids(td->instance_oids, index_oid, -1);
707 /* Checking if any metrics are left registered */
709 pthread_mutex_unlock(&g_agent->agentx_lock);
713 /* All metrics have been unregistered. Unregistering index key OIDs */
714 int keys_processed = 0;
715 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
716 data_definition_t *idd = de->value;
718 if (!idd->is_index_key)
721 for (size_t i = 0; i < idd->oids_len; i++)
722 if (td->index_oid.oid_len)
723 snmp_agent_unregister_oid_index(&idd->oids[i], *index);
725 snmp_agent_unregister_oid_string(&idd->oids[i], index_oid);
727 if (++keys_processed >= td->index_keys_len)
730 pthread_mutex_unlock(&g_agent->agentx_lock);
732 /* All OIDs have been unregistered so we dont need this instance registered
734 char index_str[DATA_MAX_NAME_LEN];
737 snmp_agent_oid_to_string(index_str, sizeof(index_str), index_oid);
739 snprintf(index_str, sizeof(index_str), "%d", *index);
742 .severity = NOTIF_WARNING, .time = cdtime(), .plugin = PLUGIN_NAME};
743 sstrncpy(n.host, hostname_g, sizeof(n.host));
744 snprintf(n.message, sizeof(n.message),
745 "Removed data row from table %s with index %s", td->name, index_str);
746 DEBUG(PLUGIN_NAME ": %s", n.message);
747 plugin_dispatch_notification(&n);
751 c_avl_remove(td->instance_oids, index_oid, NULL, (void **)&val);
755 pthread_mutex_lock(&g_agent->agentx_lock);
756 snmp_agent_unregister_oid_index(&td->index_oid, *index);
757 pthread_mutex_unlock(&g_agent->agentx_lock);
759 c_avl_remove(td->index_instance, index, NULL, (void **)&ind_oid);
760 c_avl_remove(td->instance_index, index_oid, NULL, (void **)&index);
764 c_avl_remove(td->instance_index, index_oid, NULL, NULL);
768 static int snmp_agent_clear_missing(const value_list_t *vl,
769 __attribute__((unused)) user_data_t *ud) {
773 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
774 table_definition_t *td = te->value;
776 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
777 data_definition_t *dd = de->value;
779 if (!dd->is_index_key) {
780 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
781 vl->type_instance)) {
782 oid_t *index_oid = calloc(1, sizeof(*index_oid));
784 if (index_oid == NULL) {
785 ERROR(PLUGIN_NAME ": Could not allocate memory for index_oid");
789 int ret = snmp_agent_generate_index(td, vl, index_oid);
792 snmp_agent_table_data_remove(dd, td, index_oid);
804 static void snmp_agent_free_data(data_definition_t **dd) {
806 if (dd == NULL || *dd == NULL)
809 /* unregister scalar type OID */
810 if ((*dd)->table == NULL) {
811 for (size_t i = 0; i < (*dd)->oids_len; i++)
812 unregister_mib((*dd)->oids[i].oid, (*dd)->oids[i].oid_len);
816 sfree((*dd)->plugin);
817 sfree((*dd)->plugin_instance);
819 sfree((*dd)->type_instance);
827 static void snmp_agent_free_table_columns(table_definition_t *td) {
828 if (td->columns == NULL)
831 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
832 data_definition_t *dd = de->value;
834 if (td->index_oid.oid_len) {
838 c_avl_iterator_t *iter = c_avl_get_iterator(td->index_instance);
839 while (c_avl_iterator_next(iter, (void *)&index, (void *)&index_oid) ==
841 for (size_t i = 0; i < dd->oids_len; i++)
842 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
844 c_avl_iterator_destroy(iter);
848 c_avl_iterator_t *iter = c_avl_get_iterator(dd->table->instance_index);
849 while (c_avl_iterator_next(iter, (void *)&index_oid, NULL) == 0) {
850 for (size_t i = 0; i < dd->oids_len; i++)
851 snmp_agent_unregister_oid_string(&dd->oids[i], index_oid);
853 c_avl_iterator_destroy(iter);
856 snmp_agent_free_data(&dd);
859 llist_destroy(td->columns);
861 } /* void snmp_agent_free_table_columns */
863 static void snmp_agent_free_table(table_definition_t **td) {
865 if (td == NULL || *td == NULL)
868 if ((*td)->size_oid.oid_len)
869 unregister_mib((*td)->size_oid.oid, (*td)->size_oid.oid_len);
873 /* Unregister Index OIDs */
874 if ((*td)->index_oid.oid_len) {
877 c_avl_iterator_t *iter = c_avl_get_iterator((*td)->index_instance);
878 while (c_avl_iterator_next(iter, (void **)&index, (void **)&index_oid) == 0)
879 snmp_agent_unregister_oid_index(&(*td)->index_oid, *index);
881 c_avl_iterator_destroy(iter);
884 /* Unregister all table columns and their registered OIDs */
885 snmp_agent_free_table_columns(*td);
891 /* Removing data from instance_oids, leaving key pointers since they are still
892 * used in other AVL trees */
893 c_avl_iterator_t *iter = c_avl_get_iterator((*td)->instance_oids);
894 while (c_avl_iterator_next(iter, (void **)&index_oid, (void **)&num) == 0)
896 c_avl_iterator_destroy(iter);
897 c_avl_destroy((*td)->instance_oids);
899 /* index_instance and instance_index contain the same pointers */
900 c_avl_destroy((*td)->index_instance);
901 (*td)->index_instance = NULL;
903 if ((*td)->instance_index != NULL) {
904 while (c_avl_pick((*td)->instance_index, &key, &value) == 0) {
909 c_avl_destroy((*td)->instance_index);
910 (*td)->instance_index = NULL;
912 snmp_free_varbind((*td)->index_list_cont);
917 for (i = 0; i < (*td)->index_keys_len; i++) {
918 sfree((*td)->index_keys[i].regex);
919 regfree(&(*td)->index_keys[i].regex_info);
921 for (i = 0; i < MAX_KEY_SOURCES; i++)
922 if ((*td)->tokens[i] != NULL) {
923 while (c_avl_pick((*td)->tokens[i], &key, (void **)&tok) == 0) {
928 c_avl_destroy((*td)->tokens[i]);
929 (*td)->tokens[i] = NULL;
937 static int snmp_agent_parse_oid_index_keys(const table_definition_t *td,
939 assert(index_oid != NULL);
940 int ret = parse_oid_indexes(index_oid->oid, index_oid->oid_len,
941 td->index_list_cont);
942 if (ret != SNMPERR_SUCCESS)
943 ERROR(PLUGIN_NAME ": index OID parse error!");
947 static int snmp_agent_build_name(char **name, c_avl_tree_t *tokens) {
950 char str[DATA_MAX_NAME_LEN];
951 char out[DATA_MAX_NAME_LEN] = {0};
952 c_avl_iterator_t *it = c_avl_get_iterator(tokens);
955 ERROR(PLUGIN_NAME ": Error getting tokens list iterator");
959 while (c_avl_iterator_next(it, (void **)&pos, (void **)&tok) == 0) {
960 strncat(out, tok->str, DATA_MAX_NAME_LEN - strlen(out) - 1);
961 if (tok->key != NULL) {
962 if (tok->key->type == ASN_INTEGER) {
963 snprintf(str, sizeof(str), "%ld", *tok->key->val.integer);
964 strncat(out, str, DATA_MAX_NAME_LEN - strlen(out) - 1);
965 } else /* OCTET_STR */
966 strncat(out, (char *)tok->key->val.string,
967 DATA_MAX_NAME_LEN - strlen(out) - 1);
971 c_avl_iterator_destroy(it);
975 ERROR(PLUGIN_NAME ": Could not allocate memory");
982 static int snmp_agent_format_name(char *name, int name_len,
983 data_definition_t *dd, oid_t *index_oid) {
987 if (index_oid == NULL) {
989 format_name(name, name_len, hostname_g, dd->plugin, dd->plugin_instance,
990 dd->type, dd->type_instance);
992 /* Need to parse string index OID */
993 const table_definition_t *td = dd->table;
994 ret = snmp_agent_parse_oid_index_keys(td, index_oid);
999 netsnmp_variable_list *key = td->index_list_cont;
1000 char str[DATA_MAX_NAME_LEN];
1001 char *fields[MAX_KEY_SOURCES] = {hostname_g, dd->plugin,
1002 dd->plugin_instance, dd->type,
1005 /* Looking for simple keys only */
1006 while (key != NULL) {
1007 if (!td->index_keys[i].regex) {
1008 index_key_src_t source = td->index_keys[i].source;
1010 if (source < INDEX_HOST || source > INDEX_TYPE_INSTANCE) {
1011 ERROR(PLUGIN_NAME ": Unkown index key source!");
1015 if (td->index_keys[i].type == ASN_INTEGER) {
1016 snprintf(str, sizeof(str), "%ld", *key->val.integer);
1017 fields[source] = str;
1018 } else /* OCTET_STR */
1019 fields[source] = (char *)key->val.string;
1021 key = key->next_variable;
1025 /* Keys with regexes */
1026 for (i = 0; i < MAX_KEY_SOURCES; i++) {
1027 if (td->tokens[i] == NULL)
1029 ret = snmp_agent_build_name(&fields[i], td->tokens[i]);
1033 format_name(name, name_len, fields[INDEX_HOST], fields[INDEX_PLUGIN],
1034 fields[INDEX_PLUGIN_INSTANCE], fields[INDEX_TYPE],
1035 fields[INDEX_TYPE_INSTANCE]);
1036 for (i = 0; i < MAX_KEY_SOURCES; i++) {
1045 static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests,
1046 data_definition_t *dd, oid_t *index_oid,
1050 if (dd->is_index_key) {
1051 const table_definition_t *td = dd->table;
1052 int ret = snmp_agent_parse_oid_index_keys(td, index_oid);
1057 netsnmp_variable_list *key = td->index_list_cont;
1058 /* Searching index key */
1059 for (int pos = 0; pos < dd->index_key_pos; pos++)
1060 key = key->next_variable;
1062 requests->requestvb->type = td->index_keys[dd->index_key_pos].type;
1064 if (requests->requestvb->type == ASN_INTEGER)
1065 #ifdef HAVE_NETSNMP_OLD_API
1066 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1067 (const u_char *)key->val.integer,
1068 sizeof(*key->val.integer));
1070 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1071 key->val.integer, sizeof(*key->val.integer));
1073 else /* OCTET_STR */
1074 #ifdef HAVE_NETSNMP_OLD_API
1075 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1076 (const u_char *)key->val.string,
1077 strlen((const char *)key->val.string));
1079 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1081 strlen((const char *)key->val.string));
1084 pthread_mutex_unlock(&g_agent->lock);
1086 return SNMP_ERR_NOERROR;
1089 char name[DATA_MAX_NAME_LEN];
1091 ret = snmp_agent_format_name(name, sizeof(name), dd, index_oid);
1095 DEBUG(PLUGIN_NAME ": Identifier '%s'", name);
1099 const data_set_t *ds = plugin_get_ds(dd->type);
1101 ERROR(PLUGIN_NAME ": Data set not found for '%s' type", dd->type);
1102 return SNMP_NOSUCHINSTANCE;
1105 ret = uc_get_value_by_name(name, &values, &values_num);
1108 ERROR(PLUGIN_NAME ": Failed to get value for '%s'", name);
1109 return SNMP_NOSUCHINSTANCE;
1112 assert(ds->ds_num == values_num);
1113 assert(oid_index < (int)values_num);
1115 char data[DATA_MAX_NAME_LEN];
1116 size_t data_len = sizeof(data);
1117 ret = snmp_agent_set_vardata(
1118 data, &data_len, dd->oids[oid_index].type, dd->scale, dd->shift,
1119 &values[oid_index], sizeof(values[oid_index]), ds->ds[oid_index].type);
1124 ERROR(PLUGIN_NAME ": Failed to convert '%s' value to snmp data", name);
1125 return SNMP_NOSUCHINSTANCE;
1128 requests->requestvb->type = dd->oids[oid_index].type;
1129 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1130 (const u_char *)data, data_len);
1132 return SNMP_ERR_NOERROR;
1136 snmp_agent_table_oid_handler(struct netsnmp_mib_handler_s *handler,
1137 struct netsnmp_handler_registration_s *reginfo,
1138 struct netsnmp_agent_request_info_s *reqinfo,
1139 struct netsnmp_request_info_s *requests) {
1141 if (reqinfo->mode != MODE_GET) {
1142 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
1143 return SNMP_ERR_NOERROR;
1146 pthread_mutex_lock(&g_agent->lock);
1148 oid_t oid; /* Requested OID */
1149 memcpy(oid.oid, requests->requestvb->name,
1150 sizeof(oid.oid[0]) * requests->requestvb->name_length);
1151 oid.oid_len = requests->requestvb->name_length;
1154 char oid_str[DATA_MAX_NAME_LEN];
1155 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
1156 DEBUG(PLUGIN_NAME ": Get request received for table OID '%s'", oid_str);
1158 oid_t index_oid; /* Index part of requested OID */
1160 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1161 table_definition_t *td = te->value;
1163 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1164 data_definition_t *dd = de->value;
1166 for (size_t i = 0; i < dd->oids_len; i++) {
1167 int ret = snmp_oid_ncompare(oid.oid, oid.oid_len, dd->oids[i].oid,
1168 dd->oids[i].oid_len,
1169 SNMP_MIN(oid.oid_len, dd->oids[i].oid_len));
1173 /* Calculating OID length for index part */
1174 index_oid.oid_len = oid.oid_len - dd->oids[i].oid_len;
1175 /* Fetching index part of the OID */
1176 memcpy(index_oid.oid, &oid.oid[dd->oids[i].oid_len],
1177 index_oid.oid_len * sizeof(*oid.oid));
1179 char index_str[DATA_MAX_NAME_LEN];
1180 snmp_agent_oid_to_string(index_str, sizeof(index_str), &index_oid);
1182 if (!td->index_oid.oid_len) {
1183 ret = c_avl_get(td->instance_index, &index_oid, NULL);
1187 assert(index_oid.oid_len == 1);
1188 ret = c_avl_get(td->index_instance, (int *)&index_oid.oid[0],
1189 (void **)&temp_oid);
1190 memcpy(&index_oid, temp_oid, sizeof(index_oid));
1194 INFO(PLUGIN_NAME ": Non-existing index (%s) requested", index_str);
1195 pthread_mutex_unlock(&g_agent->lock);
1196 return SNMP_NOSUCHINSTANCE;
1199 ret = snmp_agent_form_reply(requests, dd, &index_oid, i);
1200 pthread_mutex_unlock(&g_agent->lock);
1207 pthread_mutex_unlock(&g_agent->lock);
1209 return SNMP_NOSUCHINSTANCE;
1212 static int snmp_agent_table_index_oid_handler(
1213 struct netsnmp_mib_handler_s *handler,
1214 struct netsnmp_handler_registration_s *reginfo,
1215 struct netsnmp_agent_request_info_s *reqinfo,
1216 struct netsnmp_request_info_s *requests) {
1218 if (reqinfo->mode != MODE_GET) {
1219 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
1220 return SNMP_ERR_NOERROR;
1223 pthread_mutex_lock(&g_agent->lock);
1226 memcpy(oid.oid, requests->requestvb->name,
1227 sizeof(oid.oid[0]) * requests->requestvb->name_length);
1228 oid.oid_len = requests->requestvb->name_length;
1230 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1231 table_definition_t *td = te->value;
1233 if (td->index_oid.oid_len &&
1235 oid.oid, oid.oid_len, td->index_oid.oid, td->index_oid.oid_len,
1236 SNMP_MIN(oid.oid_len, td->index_oid.oid_len)) == 0)) {
1238 DEBUG(PLUGIN_NAME ": Handle '%s' table index OID", td->name);
1240 int index = oid.oid[oid.oid_len - 1];
1242 int ret = c_avl_get(td->index_instance, &index, NULL);
1244 /* nonexisting index requested */
1245 pthread_mutex_unlock(&g_agent->lock);
1246 return SNMP_NOSUCHINSTANCE;
1249 requests->requestvb->type = ASN_INTEGER;
1250 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1251 (const u_char *)&index, sizeof(index));
1253 pthread_mutex_unlock(&g_agent->lock);
1255 return SNMP_ERR_NOERROR;
1259 pthread_mutex_unlock(&g_agent->lock);
1261 return SNMP_NOSUCHINSTANCE;
1264 static int snmp_agent_table_size_oid_handler(
1265 struct netsnmp_mib_handler_s *handler,
1266 struct netsnmp_handler_registration_s *reginfo,
1267 struct netsnmp_agent_request_info_s *reqinfo,
1268 struct netsnmp_request_info_s *requests) {
1270 if (reqinfo->mode != MODE_GET) {
1271 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
1272 return SNMP_ERR_NOERROR;
1275 pthread_mutex_lock(&g_agent->lock);
1278 memcpy(oid.oid, requests->requestvb->name,
1279 sizeof(oid.oid[0]) * requests->requestvb->name_length);
1280 oid.oid_len = requests->requestvb->name_length;
1282 DEBUG(PLUGIN_NAME ": Get request received for table size OID");
1284 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1285 table_definition_t *td = te->value;
1287 if (td->size_oid.oid_len &&
1288 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->size_oid.oid,
1289 td->size_oid.oid_len,
1290 SNMP_MIN(oid.oid_len, td->size_oid.oid_len)) == 0)) {
1291 DEBUG(PLUGIN_NAME ": Handle '%s' table size OID", td->name);
1294 if (td->index_oid.oid_len)
1295 size = c_avl_size(td->index_instance);
1297 size = c_avl_size(td->instance_index);
1299 requests->requestvb->type = ASN_INTEGER;
1300 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1301 (const u_char *)&size, sizeof(size));
1303 pthread_mutex_unlock(&g_agent->lock);
1305 return SNMP_ERR_NOERROR;
1309 pthread_mutex_unlock(&g_agent->lock);
1311 return SNMP_NOSUCHINSTANCE;
1315 snmp_agent_scalar_oid_handler(struct netsnmp_mib_handler_s *handler,
1316 struct netsnmp_handler_registration_s *reginfo,
1317 struct netsnmp_agent_request_info_s *reqinfo,
1318 struct netsnmp_request_info_s *requests) {
1320 if (reqinfo->mode != MODE_GET) {
1321 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
1322 return SNMP_ERR_NOERROR;
1325 pthread_mutex_lock(&g_agent->lock);
1328 memcpy(oid.oid, requests->requestvb->name,
1329 sizeof(oid.oid[0]) * requests->requestvb->name_length);
1330 oid.oid_len = requests->requestvb->name_length;
1333 char oid_str[DATA_MAX_NAME_LEN];
1334 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
1335 DEBUG(PLUGIN_NAME ": Get request received for scalar OID '%s'", oid_str);
1338 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL;
1340 data_definition_t *dd = de->value;
1342 for (size_t i = 0; i < dd->oids_len; i++) {
1344 int ret = snmp_oid_compare(oid.oid, oid.oid_len, dd->oids[i].oid,
1345 dd->oids[i].oid_len);
1349 ret = snmp_agent_form_reply(requests, dd, NULL, i);
1351 pthread_mutex_unlock(&g_agent->lock);
1357 pthread_mutex_unlock(&g_agent->lock);
1359 return SNMP_NOSUCHINSTANCE;
1362 static int snmp_agent_register_table_oids(void) {
1364 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1365 table_definition_t *td = te->value;
1367 if (td->size_oid.oid_len != 0) {
1369 snmp_agent_get_asn_type(td->size_oid.oid, td->size_oid.oid_len);
1370 td->size_oid.oid_len++;
1371 int ret = snmp_agent_register_oid(&td->size_oid,
1372 snmp_agent_table_size_oid_handler);
1377 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1378 data_definition_t *dd = de->value;
1380 for (size_t i = 0; i < dd->oids_len; i++) {
1382 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
1390 static int snmp_agent_register_scalar_oids(void) {
1392 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
1393 data_definition_t *dd = e->value;
1395 for (size_t i = 0; i < dd->oids_len; i++) {
1398 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
1401 snmp_agent_register_oid(&dd->oids[i], snmp_agent_scalar_oid_handler);
1410 static int snmp_agent_config_data_oids(data_definition_t *dd,
1411 oconfig_item_t *ci) {
1412 if (ci->values_num < 1) {
1413 WARNING(PLUGIN_NAME ": `OIDs' needs at least one argument");
1417 for (int i = 0; i < ci->values_num; i++)
1418 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
1419 WARNING(PLUGIN_NAME ": `OIDs' needs only string argument");
1423 if (dd->oids != NULL) {
1424 WARNING(PLUGIN_NAME ": OIDs can be configured only once for each data");
1429 dd->oids = calloc(ci->values_num, sizeof(*dd->oids));
1431 if (dd->oids == NULL)
1433 dd->oids_len = (size_t)ci->values_num;
1435 for (int i = 0; i < ci->values_num; i++) {
1436 dd->oids[i].oid_len = MAX_OID_LEN;
1438 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->oids[i].oid,
1439 &dd->oids[i].oid_len)) {
1440 ERROR(PLUGIN_NAME ": snmp_parse_oid (%s) failed",
1441 ci->values[i].value.string);
1451 static int snmp_agent_config_table_size_oid(table_definition_t *td,
1452 oconfig_item_t *ci) {
1453 if (ci->values_num < 1) {
1454 WARNING(PLUGIN_NAME ": `TableSizeOID' is empty");
1458 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
1459 WARNING(PLUGIN_NAME ": `TableSizeOID' needs only string argument");
1463 td->size_oid.oid_len = MAX_OID_LEN;
1465 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->size_oid.oid,
1466 &td->size_oid.oid_len)) {
1467 ERROR(PLUGIN_NAME ": Failed to parse table size OID (%s)",
1468 ci->values[0].value.string);
1469 td->size_oid.oid_len = 0;
1476 static int snmp_agent_config_table_index_oid(table_definition_t *td,
1477 oconfig_item_t *ci) {
1479 if (ci->values_num < 1) {
1480 WARNING(PLUGIN_NAME ": `IndexOID' is empty");
1484 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
1485 WARNING(PLUGIN_NAME ": `IndexOID' needs only string argument");
1489 td->index_oid.oid_len = MAX_OID_LEN;
1491 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->index_oid.oid,
1492 &td->index_oid.oid_len)) {
1493 ERROR(PLUGIN_NAME ": Failed to parse table index OID (%s)",
1494 ci->values[0].value.string);
1495 td->index_oid.oid_len = 0;
1502 /* Getting index key source that will represent table row */
1503 static int snmp_agent_config_index_key_source(table_definition_t *td,
1504 data_definition_t *dd,
1505 oconfig_item_t *ci) {
1508 int ret = cf_util_get_string(ci, &val);
1514 for (int i = 0; i < MAX_KEY_SOURCES; i++) {
1515 if (strcasecmp(index_opts[i], (const char *)val) == 0) {
1516 td->index_keys[td->index_keys_len].source = i;
1517 td->index_keys[td->index_keys_len].group = GROUP_UNUSED;
1518 td->index_keys[td->index_keys_len].regex = NULL;
1525 ERROR(PLUGIN_NAME ": Failed to parse index key source: '%s'", val);
1531 dd->index_key_pos = td->index_keys_len++;
1532 dd->is_index_key = true;
1537 /* Getting format string used to parse values from index key source */
1538 static int snmp_agent_config_index_key_regex(table_definition_t *td,
1539 data_definition_t *dd,
1540 oconfig_item_t *ci) {
1541 index_key_t *index_key = &td->index_keys[dd->index_key_pos];
1543 int ret = cf_util_get_string(ci, &index_key->regex);
1547 ret = regcomp(&index_key->regex_info, index_key->regex, REG_EXTENDED);
1549 ERROR(PLUGIN_NAME ": Could not compile regex for %s", dd->name);
1553 index_key_src_t source = index_key->source;
1554 if (td->tokens[source] == NULL) {
1555 td->tokens[source] =
1556 c_avl_create((int (*)(const void *, const void *))num_compare);
1557 if (td->tokens[source] == NULL) {
1558 ERROR(PLUGIN_NAME ": Could not allocate memory for AVL tree");
1566 static int snmp_agent_config_index_key(table_definition_t *td,
1567 data_definition_t *dd,
1568 oconfig_item_t *ci) {
1571 for (int i = 0; (i < ci->children_num && ret == 0); i++) {
1572 oconfig_item_t *option = ci->children + i;
1574 if (strcasecmp("Source", option->key) == 0)
1575 ret = snmp_agent_config_index_key_source(td, dd, option);
1576 else if (strcasecmp("Regex", option->key) == 0)
1577 ret = snmp_agent_config_index_key_regex(td, dd, option);
1578 else if (strcasecmp("Group", option->key) == 0)
1579 ret = cf_util_get_int(option, &td->index_keys[dd->index_key_pos].group);
1585 /* This function parses configuration of both scalar and table column
1586 * because they have nearly the same structure */
1587 static int snmp_agent_config_table_column(table_definition_t *td,
1588 oconfig_item_t *ci) {
1589 data_definition_t *dd;
1591 oconfig_item_t *option_tmp = NULL;
1595 dd = calloc(1, sizeof(*dd));
1597 ERROR(PLUGIN_NAME ": Failed to allocate memory for table data definition");
1601 ret = cf_util_get_string(ci, &dd->name);
1609 /* NULL if it's a scalar */
1611 dd->is_index_key = false;
1613 for (int i = 0; i < ci->children_num; i++) {
1614 oconfig_item_t *option = ci->children + i;
1616 /* First 3 options are reserved for table entry only */
1617 if (td != NULL && strcasecmp("IndexKey", option->key) == 0) {
1618 dd->is_index_key = true;
1619 option_tmp = option;
1620 } else if (strcasecmp("Plugin", option->key) == 0)
1621 ret = cf_util_get_string(option, &dd->plugin);
1622 else if (strcasecmp("PluginInstance", option->key) == 0)
1623 ret = cf_util_get_string(option, &dd->plugin_instance);
1624 else if (strcasecmp("Type", option->key) == 0)
1625 ret = cf_util_get_string(option, &dd->type);
1626 else if (strcasecmp("TypeInstance", option->key) == 0)
1627 ret = cf_util_get_string(option, &dd->type_instance);
1628 else if (strcasecmp("Shift", option->key) == 0)
1629 ret = cf_util_get_double(option, &dd->shift);
1630 else if (strcasecmp("Scale", option->key) == 0)
1631 ret = cf_util_get_double(option, &dd->scale);
1632 else if (strcasecmp("OIDs", option->key) == 0)
1633 ret = snmp_agent_config_data_oids(dd, option);
1635 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
1640 snmp_agent_free_data(&dd);
1645 if (dd->is_index_key) {
1646 ret = snmp_agent_config_index_key(td, dd, option_tmp);
1647 td->index_keys[dd->index_key_pos].type =
1648 snmp_agent_get_asn_type(dd->oids[0].oid, dd->oids[0].oid_len);
1651 snmp_agent_free_data(&dd);
1656 llentry_t *entry = llentry_create(dd->name, dd);
1657 if (entry == NULL) {
1658 snmp_agent_free_data(&dd);
1662 /* Append to column list in parent table */
1664 llist_append(td->columns, entry);
1666 llentry_destroy(entry);
1671 /* Parses scalar configuration entry */
1672 static int snmp_agent_config_scalar(oconfig_item_t *ci) {
1673 return snmp_agent_config_table_column(NULL, ci);
1676 static int num_compare(const int *a, const int *b) {
1677 assert((a != NULL) && (b != NULL));
1686 static int oid_compare(const oid_t *a, const oid_t *b) {
1687 return snmp_oid_compare(a->oid, a->oid_len, b->oid, b->oid_len);
1690 static int snmp_agent_config_table(oconfig_item_t *ci) {
1691 table_definition_t *td;
1696 td = calloc(1, sizeof(*td));
1698 ERROR(PLUGIN_NAME ": Failed to allocate memory for table definition");
1702 ret = cf_util_get_string(ci, &td->name);
1708 td->columns = llist_create();
1709 if (td->columns == NULL) {
1710 ERROR(PLUGIN_NAME ": Failed to allocate memory for columns list");
1711 snmp_agent_free_table(&td);
1715 for (int i = 0; i < MAX_KEY_SOURCES; i++)
1716 td->tokens[i] = NULL;
1717 td->tokens_done = false;
1719 for (int i = 0; i < ci->children_num; i++) {
1720 oconfig_item_t *option = ci->children + i;
1722 if (strcasecmp("IndexOID", option->key) == 0)
1723 ret = snmp_agent_config_table_index_oid(td, option);
1724 else if (strcasecmp("SizeOID", option->key) == 0)
1725 ret = snmp_agent_config_table_size_oid(td, option);
1726 else if (strcasecmp("Data", option->key) == 0)
1727 ret = snmp_agent_config_table_column(td, option);
1729 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
1734 snmp_agent_free_table(&td);
1739 /* Preparing index list container */
1740 ret = snmp_agent_prep_index_list(td, &td->index_list_cont);
1744 td->instance_index =
1745 c_avl_create((int (*)(const void *, const void *))oid_compare);
1746 if (td->instance_index == NULL) {
1747 snmp_agent_free_table(&td);
1751 td->index_instance =
1752 c_avl_create((int (*)(const void *, const void *))num_compare);
1753 if (td->index_instance == NULL) {
1754 snmp_agent_free_table(&td);
1759 c_avl_create((int (*)(const void *, const void *))oid_compare);
1760 if (td->instance_oids == NULL) {
1761 snmp_agent_free_table(&td);
1765 llentry_t *entry = llentry_create(td->name, td);
1766 if (entry == NULL) {
1767 snmp_agent_free_table(&td);
1771 llist_append(g_agent->tables, entry);
1776 static int snmp_agent_get_value_from_ds_type(const value_t *val, int type,
1777 double scale, double shift,
1780 case DS_TYPE_COUNTER:
1781 *value = (long)((val->counter * scale) + shift);
1783 case DS_TYPE_ABSOLUTE:
1784 *value = (long)((val->absolute * scale) + shift);
1786 case DS_TYPE_DERIVE:
1787 *value = (long)((val->derive * scale) + shift);
1790 *value = (long)((val->gauge * scale) + shift);
1795 ERROR(PLUGIN_NAME ": Unknown data source type: %i", type);
1802 static int snmp_agent_set_vardata(void *data, size_t *data_len, u_char asn_type,
1803 double scale, double shift, const void *value,
1804 size_t len, int type) {
1807 netsnmp_vardata var;
1812 var.string = (u_char *)data;
1814 ret = snmp_agent_get_value_from_ds_type(val, type, scale, shift, &new_value);
1824 if (*data_len < sizeof(*var.integer))
1826 *var.integer = new_value;
1827 *data_len = sizeof(*var.integer);
1830 if (*data_len < sizeof(*var.counter64))
1832 var.counter64->high = (u_long)((int64_t)new_value >> 32);
1833 var.counter64->low = (u_long)((int64_t)new_value & 0xFFFFFFFF);
1834 *data_len = sizeof(*var.counter64);
1837 if (type == DS_TYPE_GAUGE) {
1838 char buf[DATA_MAX_NAME_LEN];
1839 snprintf(buf, sizeof(buf), "%.2f", val->gauge);
1840 if (*data_len < strlen(buf))
1842 *data_len = strlen(buf);
1843 memcpy(var.string, buf, *data_len);
1845 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1851 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1859 static int snmp_agent_register_oid_index(oid_t *oid, int index,
1860 Netsnmp_Node_Handler *handler) {
1862 memcpy(&new_oid, oid, sizeof(*oid));
1863 new_oid.oid[new_oid.oid_len++] = index;
1864 return snmp_agent_register_oid(&new_oid, handler);
1867 static int snmp_agent_unregister_oid_index(oid_t *oid, int index) {
1869 memcpy(&new_oid, oid, sizeof(*oid));
1870 new_oid.oid[new_oid.oid_len++] = index;
1871 return snmp_agent_unregister_oid(&new_oid);
1874 static int snmp_agent_update_instance_oids(c_avl_tree_t *tree, oid_t *index_oid,
1876 int *oids_num; /* number of oids registered for instance */
1878 if (c_avl_get(tree, index_oid, (void **)&oids_num) == 0) {
1882 ERROR(PLUGIN_NAME ": Error updating index data");
1887 static int snmp_agent_update_index(data_definition_t *dd,
1888 table_definition_t *td, oid_t *index_oid,
1889 bool *free_index_oid) {
1894 if (c_avl_get(td->instance_index, (void *)index_oid, (void **)&index) != 0) {
1895 /* We'll keep index_oid stored in AVL tree */
1896 *free_index_oid = false;
1898 /* need to generate index for the table */
1899 if (td->index_oid.oid_len) {
1900 index = calloc(1, sizeof(*index));
1901 if (index == NULL) {
1906 *index = c_avl_size(td->instance_index) + 1;
1908 ret = c_avl_insert(td->instance_index, index_oid, index);
1912 ret = c_avl_insert(td->index_instance, index, index_oid);
1914 DEBUG(PLUGIN_NAME ": Failed to update index_instance for '%s' table",
1916 goto remove_avl_index_oid;
1919 ret = snmp_agent_register_oid_index(&td->index_oid, *index,
1920 snmp_agent_table_index_oid_handler);
1922 goto remove_avl_index;
1924 /* instance as a key is required for any table */
1925 ret = c_avl_insert(td->instance_index, index_oid, NULL);
1930 value = calloc(1, sizeof(*value));
1932 if (value == NULL) {
1933 ERROR(PLUGIN_NAME ": Failed to allocate memory");
1935 goto unregister_index;
1938 ret = c_avl_insert(td->instance_oids, index_oid, value);
1941 DEBUG(PLUGIN_NAME ": Failed to update instance_oids for '%s' table",
1946 int keys_processed = 0;
1948 /* Registering index keys OIDs */
1949 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1950 data_definition_t *idd = de->value;
1951 if (!idd->is_index_key)
1954 for (size_t i = 0; i < idd->oids_len; i++) {
1955 if (td->index_oid.oid_len)
1956 ret = snmp_agent_register_oid_index(&idd->oids[i], *index,
1957 snmp_agent_table_oid_handler);
1959 ret = snmp_agent_register_oid_string(&idd->oids[i], index_oid,
1960 snmp_agent_table_oid_handler);
1963 ERROR(PLUGIN_NAME ": Could not register OID");
1968 if (++keys_processed >= td->index_keys_len)
1975 for (size_t i = 0; i < dd->oids_len; i++) {
1976 if (td->index_oid.oid_len)
1977 ret = snmp_agent_register_oid_index(&dd->oids[i], *index,
1978 snmp_agent_table_oid_handler);
1980 ret = snmp_agent_register_oid_string(&dd->oids[i], index_oid,
1981 snmp_agent_table_oid_handler);
1985 else if (ret == OID_EXISTS)
1987 else if (snmp_agent_update_instance_oids(td->instance_oids, index_oid, 1) <
1992 if (ret != OID_EXISTS) {
1993 char index_str[DATA_MAX_NAME_LEN];
1996 snmp_agent_oid_to_string(index_str, sizeof(index_str), index_oid);
1998 snprintf(index_str, sizeof(index_str), "%d", *index);
2000 notification_t n = {
2001 .severity = NOTIF_OKAY, .time = cdtime(), .plugin = PLUGIN_NAME};
2002 sstrncpy(n.host, hostname_g, sizeof(n.host));
2003 snprintf(n.message, sizeof(n.message),
2004 "Data added to table %s with index %s", td->name, index_str);
2005 DEBUG(PLUGIN_NAME ": %s", n.message);
2007 plugin_dispatch_notification(&n);
2015 if (td->index_oid.oid_len)
2016 snmp_agent_unregister_oid_index(index_oid, *index);
2018 if (td->index_oid.oid_len)
2019 c_avl_remove(td->index_instance, index, NULL, NULL);
2020 remove_avl_index_oid:
2021 c_avl_remove(td->instance_index, index_oid, NULL, NULL);
2026 *free_index_oid = true;
2031 static int snmp_agent_write(value_list_t const *vl) {
2035 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
2036 table_definition_t *td = te->value;
2038 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
2039 data_definition_t *dd = de->value;
2041 if (!dd->is_index_key) {
2042 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
2043 vl->type_instance)) {
2044 oid_t *index_oid = calloc(1, sizeof(*index_oid));
2045 bool free_index_oid = true;
2047 if (index_oid == NULL) {
2048 ERROR(PLUGIN_NAME ": Could not allocate memory for index_oid");
2052 int ret = snmp_agent_generate_index(td, vl, index_oid);
2055 ret = snmp_agent_update_index(dd, td, index_oid, &free_index_oid);
2057 /* Index exists or update failed */
2070 static int snmp_agent_collect(const data_set_t *ds, const value_list_t *vl,
2071 user_data_t __attribute__((unused)) * user_data) {
2073 pthread_mutex_lock(&g_agent->lock);
2075 snmp_agent_write(vl);
2077 pthread_mutex_unlock(&g_agent->lock);
2082 static int snmp_agent_preinit(void) {
2084 g_agent = calloc(1, sizeof(*g_agent));
2085 if (g_agent == NULL) {
2086 ERROR(PLUGIN_NAME ": Failed to allocate memory for snmp agent context");
2090 g_agent->tables = llist_create();
2091 g_agent->scalars = llist_create();
2092 g_agent->registered_oids =
2093 c_avl_create((int (*)(const void *, const void *))oid_compare);
2095 if (g_agent->tables == NULL || g_agent->scalars == NULL) {
2096 ERROR(PLUGIN_NAME ": llist_create() failed");
2097 llist_destroy(g_agent->scalars);
2098 llist_destroy(g_agent->tables);
2099 c_avl_destroy(g_agent->registered_oids);
2104 /* make us an agentx client. */
2105 err = netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE,
2108 ERROR(PLUGIN_NAME ": Failed to set agent role (%d)", err);
2109 llist_destroy(g_agent->scalars);
2110 llist_destroy(g_agent->tables);
2111 c_avl_destroy(g_agent->registered_oids);
2116 * For SNMP debug purposes uses snmp_set_do_debugging(1);
2119 /* initialize the agent library */
2120 err = init_agent(PLUGIN_NAME);
2122 ERROR(PLUGIN_NAME ": Failed to initialize the agent library (%d)", err);
2123 llist_destroy(g_agent->scalars);
2124 llist_destroy(g_agent->tables);
2125 c_avl_destroy(g_agent->registered_oids);
2129 init_snmp(PLUGIN_NAME);
2131 g_agent->tp = read_all_mibs();
2136 static int snmp_agent_init(void) {
2139 if (g_agent == NULL || ((llist_head(g_agent->scalars) == NULL) &&
2140 (llist_head(g_agent->tables) == NULL))) {
2141 ERROR(PLUGIN_NAME ": snmp_agent_init: plugin not configured");
2145 plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown);
2147 ret = snmp_agent_register_scalar_oids();
2151 ret = snmp_agent_register_table_oids();
2155 ret = pthread_mutex_init(&g_agent->lock, NULL);
2157 ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret);
2161 ret = pthread_mutex_init(&g_agent->agentx_lock, NULL);
2163 ERROR(PLUGIN_NAME ": Failed to initialize AgentX mutex, err %u", ret);
2167 /* create a second thread to listen for requests from AgentX*/
2168 ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
2170 ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
2174 if (llist_head(g_agent->tables) != NULL) {
2175 plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL);
2176 plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL);
2182 static void *snmp_agent_thread_run(void __attribute__((unused)) * arg) {
2183 INFO(PLUGIN_NAME ": Thread is up and running");
2186 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2188 pthread_mutex_lock(&g_agent->agentx_lock);
2189 agent_check_and_process(0); /* 0 == don't block */
2190 pthread_mutex_unlock(&g_agent->agentx_lock);
2192 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
2199 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler) {
2200 netsnmp_handler_registration *reg;
2202 if (c_avl_get(g_agent->registered_oids, (void *)oid, NULL) == 0)
2205 oid_t *new_oid = calloc(1, sizeof(*oid));
2207 if (new_oid == NULL) {
2208 ERROR(PLUGIN_NAME ": Could not allocate memory to register new OID");
2212 memcpy(new_oid, oid, sizeof(*oid));
2214 int ret = c_avl_insert(g_agent->registered_oids, (void *)new_oid, NULL);
2216 ERROR(PLUGIN_NAME ": Could not allocate memory to register new OID");
2222 char *oid_name = snmp_agent_get_oid_name(oid->oid, oid->oid_len - 1);
2223 char oid_str[DATA_MAX_NAME_LEN];
2225 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), oid);
2227 if (oid_name == NULL) {
2229 ": Skipped registration: OID (%s) is not found in main tree",
2234 reg = netsnmp_create_handler_registration(oid_name, handler, oid->oid,
2235 oid->oid_len, HANDLER_CAN_RONLY);
2237 ERROR(PLUGIN_NAME ": Failed to create handler registration for OID (%s)",
2242 pthread_mutex_lock(&g_agent->agentx_lock);
2244 if (netsnmp_register_instance(reg) != MIB_REGISTERED_OK) {
2245 ERROR(PLUGIN_NAME ": Failed to register handler for OID (%s)", oid_str);
2246 pthread_mutex_unlock(&g_agent->agentx_lock);
2250 pthread_mutex_unlock(&g_agent->agentx_lock);
2252 DEBUG(PLUGIN_NAME ": Registered handler for OID (%s)", oid_str);
2257 static int snmp_agent_free_config(void) {
2259 if (g_agent == NULL)
2262 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next)
2263 snmp_agent_free_table((table_definition_t **)&te->value);
2264 llist_destroy(g_agent->tables);
2266 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL; de = de->next)
2267 snmp_agent_free_data((data_definition_t **)&de->value);
2268 llist_destroy(g_agent->scalars);
2273 static int snmp_agent_shutdown(void) {
2276 DEBUG(PLUGIN_NAME ": snmp_agent_shutdown");
2278 if (g_agent == NULL) {
2279 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: plugin not initialized");
2283 if (pthread_cancel(g_agent->thread) != 0)
2284 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to cancel the thread");
2286 if (pthread_join(g_agent->thread, NULL) != 0)
2287 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to join the thread");
2289 snmp_agent_free_config();
2291 snmp_shutdown(PLUGIN_NAME);
2293 pthread_mutex_destroy(&g_agent->lock);
2294 pthread_mutex_destroy(&g_agent->agentx_lock);
2296 /* Freeing registered OIDs list */
2299 if (g_agent->registered_oids != NULL) {
2300 while (c_avl_pick(g_agent->registered_oids, &oid, NULL) == 0) {
2303 c_avl_destroy(g_agent->registered_oids);
2311 static int snmp_agent_config(oconfig_item_t *ci) {
2312 int ret = snmp_agent_preinit();
2319 for (int i = 0; i < ci->children_num; i++) {
2320 oconfig_item_t *child = ci->children + i;
2321 if (strcasecmp("Data", child->key) == 0) {
2322 ret = snmp_agent_config_scalar(child);
2323 } else if (strcasecmp("Table", child->key) == 0) {
2324 ret = snmp_agent_config_table(child);
2326 ERROR(PLUGIN_NAME ": Unknown configuration option `%s'", child->key);
2331 ERROR(PLUGIN_NAME ": Failed to parse configuration");
2332 snmp_agent_free_config();
2333 snmp_shutdown(PLUGIN_NAME);
2339 ret = snmp_agent_validate_config();
2341 ERROR(PLUGIN_NAME ": Invalid configuration provided");
2342 snmp_agent_free_config();
2343 snmp_shutdown(PLUGIN_NAME);
2351 void module_register(void) {
2352 plugin_register_init(PLUGIN_NAME, snmp_agent_init);
2353 plugin_register_complex_config(PLUGIN_NAME, snmp_agent_config);