2 * collectd - src/snmp_agent.c
4 * Copyright(c) 2017 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 1 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 const char *const 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, char *regex,
344 regex_t *regex_info, int gi,
346 regmatch_t matches[MAX_MATCHES];
348 int ret = regexec(regex_info, input, MAX_MATCHES, matches, 0);
350 if (gi > regex_info->re_nsub) {
351 ERROR(PLUGIN_NAME ": Group index %d not found. Check regex config", gi);
355 } else if (ret == REG_NOMATCH) {
356 ERROR(PLUGIN_NAME ": No match found");
361 regerror(ret, regex_info, msgbuf, sizeof(msgbuf));
362 ERROR(PLUGIN_NAME ": Regex match failed: %s", msgbuf);
369 static int snmp_agent_create_token(char const *input, int t_off, int n,
371 netsnmp_variable_list *index_key) {
372 assert(tree != NULL);
374 token_t *token = malloc(sizeof(*token));
379 int *offset = malloc(sizeof(*offset));
382 goto free_token_error;
386 token->key = index_key;
387 token->str = strndup(input + t_off, n);
389 if (token->str == NULL)
390 goto free_offset_error;
393 ret = c_avl_insert(tree, (void *)offset, (void *)token);
407 ERROR(PLUGIN_NAME ": Could not allocate memory to create token");
412 static int snmp_agent_delete_token(int t_off, c_avl_tree_t *tree) {
413 token_t *token = NULL;
416 int ret = c_avl_remove(tree, &t_off, (void **)&offset, (void **)&token);
419 ERROR(PLUGIN_NAME ": Could not delete token");
429 static int snmp_agent_get_token(c_avl_tree_t *tree, int mpos) {
435 c_avl_iterator_t *it = c_avl_get_iterator(tree);
436 while (c_avl_iterator_next(it, (void **)&pos, (void **)&token) == 0) {
443 c_avl_iterator_destroy(it);
447 static int snmp_agent_tokenize(const char *input, c_avl_tree_t *tokens,
449 netsnmp_variable_list *key) {
450 assert(tokens != NULL);
453 int len = strlen(input);
455 /* Creating first token that is going to be split later */
456 if (c_avl_size(tokens) == 0) {
457 ret = snmp_agent_create_token(input, 0, len, tokens, NULL);
462 /* Divide token that contains current match into two */
463 int t_pos = snmp_agent_get_token(tokens, m->rm_so);
464 ret = snmp_agent_delete_token(t_pos, tokens);
469 ret = snmp_agent_create_token(input, t_pos, m->rm_so - t_pos, tokens, key);
474 if (len - m->rm_eo > 1) {
475 ret = snmp_agent_create_token(input, m->rm_eo, len - m->rm_eo + 1, tokens,
478 snmp_agent_delete_token(t_pos, tokens);
486 static int snmp_agent_fill_index_list(table_definition_t *td,
487 value_list_t const *vl) {
490 netsnmp_variable_list *key = td->index_list_cont;
493 for (i = 0; i < td->index_keys_len; i++) {
494 /* var should never be NULL */
498 const index_key_src_t source = td->index_keys[i].source;
499 c_avl_tree_t *const tokens = td->tokens[source];
500 /* Generating list filled with all data necessary to generate an OID */
508 case INDEX_PLUGIN_INSTANCE:
509 ptr = vl->plugin_instance;
514 case INDEX_TYPE_INSTANCE:
515 ptr = vl->type_instance;
518 ERROR(PLUGIN_NAME ": Unknown index key source provided");
524 /* Parsing input string if necessary */
525 if (td->index_keys[i].regex) {
526 regmatch_t m = {-1, -1};
528 /* Parsing input string */
529 ret = snmp_agent_parse_index_key(ptr, td->index_keys[i].regex,
530 &td->index_keys[i].regex_info,
531 td->index_keys[i].group, &m);
533 ERROR(PLUGIN_NAME ": Error executing regex");
537 /* Tokenizing input string if not done yet */
538 if (td->tokens_done == 0)
539 ret = snmp_agent_tokenize(ptr, tokens, &m, key);
544 if (td->index_keys[i].type == ASN_INTEGER) {
545 int val = strtol(ptr + m.rm_so, NULL, 0);
546 ret = snmp_set_var_value(key, &val, sizeof(val));
548 ret = snmp_set_var_value(key, ptr + m.rm_so, m.rm_eo - m.rm_so);
550 ret = snmp_set_var_value(key, ptr, strlen(ptr));
551 key = key->next_variable;
554 /* Tokens for all source strings are generated */
555 for (i = 0; i < MAX_KEY_SOURCES; i++)
561 static int snmp_agent_prep_index_list(table_definition_t const *td,
562 netsnmp_variable_list **index_list) {
563 /* Generating list having only the structure (with no values) letting us
564 * know how to parse an OID*/
565 for (int i = 0; i < td->index_keys_len; i++) {
566 switch (td->index_keys[i].source) {
569 case INDEX_PLUGIN_INSTANCE:
571 case INDEX_TYPE_INSTANCE:
572 snmp_varlist_add_variable(index_list, NULL, 0, td->index_keys[i].type,
576 ERROR(PLUGIN_NAME ": Unknown index key source provided");
583 static int snmp_agent_generate_index(table_definition_t *td,
584 value_list_t const *vl, oid_t *index_oid) {
586 /* According to given information by index_keys list
587 * index OID is going to be built
589 int ret = snmp_agent_fill_index_list(td, vl);
593 /* Building only index part OID (without table prefix OID) */
594 ret = build_oid_noalloc(index_oid->oid, sizeof(index_oid->oid),
595 &index_oid->oid_len, NULL, 0, td->index_list_cont);
596 if (ret != SNMPERR_SUCCESS) {
597 ERROR(PLUGIN_NAME ": Error building index OID");
604 /* It appends one OID to the end of another */
605 static int snmp_agent_append_oid(oid_t *out, const oid_t *in) {
607 if (out->oid_len + in->oid_len > MAX_OID_LEN) {
608 ERROR(PLUGIN_NAME ": Cannot create OID. Output length is too long!");
611 memcpy(&out->oid[out->oid_len], in->oid, in->oid_len * sizeof(oid));
612 out->oid_len += in->oid_len;
617 static int snmp_agent_register_oid_string(const oid_t *oid,
618 const oid_t *index_oid,
619 Netsnmp_Node_Handler *handler) {
622 memcpy(&new_oid, oid, sizeof(*oid));
623 /* Concatenating two string oids */
624 int ret = snmp_agent_append_oid(&new_oid, index_oid);
628 return snmp_agent_register_oid(&new_oid, handler);
631 static int snmp_agent_unregister_oid(oid_t *oid) {
632 int ret = c_avl_remove(g_agent->registered_oids, (void *)oid, NULL, NULL);
635 ERROR(PLUGIN_NAME ": Could not delete registration info");
637 return unregister_mib(oid->oid, oid->oid_len);
640 static int snmp_agent_unregister_oid_string(oid_t *oid,
641 const oid_t *index_oid) {
643 char oid_str[DATA_MAX_NAME_LEN];
645 memcpy(&new_oid, oid, sizeof(*oid));
646 /* Concatenating two string oids */
647 int ret = snmp_agent_append_oid(&new_oid, index_oid);
651 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &new_oid);
652 DEBUG(PLUGIN_NAME ": Unregistered handler for OID (%s)", oid_str);
654 return snmp_agent_unregister_oid(&new_oid);
657 static void snmp_agent_table_data_remove(data_definition_t *dd,
658 table_definition_t *td,
661 oid_t *ind_oid = NULL;
663 if (td->index_oid.oid_len) {
664 if ((c_avl_get(td->instance_index, index_oid, (void **)&index) != 0) ||
665 (c_avl_get(td->index_instance, index, NULL) != 0))
668 if (c_avl_get(td->instance_index, index_oid, NULL) != 0)
672 pthread_mutex_lock(&g_agent->agentx_lock);
674 int reg_oids = -1; /* Number of registered oids for given instance */
676 for (size_t i = 0; i < dd->oids_len; i++) {
677 if (td->index_oid.oid_len)
678 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
680 snmp_agent_unregister_oid_string(&dd->oids[i], index_oid);
683 snmp_agent_update_instance_oids(td->instance_oids, index_oid, -1);
686 /* Checking if any metrics are left registered */
688 pthread_mutex_unlock(&g_agent->agentx_lock);
692 /* All metrics have been unregistered. Unregistering index key OIDs */
693 int keys_processed = 0;
694 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
695 data_definition_t *idd = de->value;
697 if (!idd->is_index_key)
700 for (size_t i = 0; i < idd->oids_len; i++)
701 if (td->index_oid.oid_len)
702 snmp_agent_unregister_oid_index(&idd->oids[i], *index);
704 snmp_agent_unregister_oid_string(&idd->oids[i], index_oid);
706 if (++keys_processed >= td->index_keys_len)
709 pthread_mutex_unlock(&g_agent->agentx_lock);
711 /* All OIDs have been unregistered so we dont need this instance registered
713 char index_str[DATA_MAX_NAME_LEN];
716 snmp_agent_oid_to_string(index_str, sizeof(index_str), index_oid);
718 snprintf(index_str, sizeof(index_str), "%d", *index);
721 .severity = NOTIF_WARNING, .time = cdtime(), .plugin = PLUGIN_NAME};
722 sstrncpy(n.host, hostname_g, sizeof(n.host));
723 snprintf(n.message, sizeof(n.message),
724 "Removed data row from table %s with index %s", td->name, index_str);
725 DEBUG(PLUGIN_NAME ": %s", n.message);
726 plugin_dispatch_notification(&n);
730 c_avl_remove(td->instance_oids, index_oid, NULL, (void **)&val);
734 pthread_mutex_lock(&g_agent->agentx_lock);
735 snmp_agent_unregister_oid_index(&td->index_oid, *index);
736 pthread_mutex_unlock(&g_agent->agentx_lock);
738 c_avl_remove(td->index_instance, index, NULL, (void **)&ind_oid);
739 c_avl_remove(td->instance_index, index_oid, NULL, (void **)&index);
743 c_avl_remove(td->instance_index, index_oid, NULL, NULL);
747 static int snmp_agent_clear_missing(const value_list_t *vl,
748 __attribute__((unused)) user_data_t *ud) {
752 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
753 table_definition_t *td = te->value;
755 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
756 data_definition_t *dd = de->value;
758 if (!dd->is_index_key) {
759 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
760 vl->type_instance)) {
761 oid_t *index_oid = calloc(1, sizeof(*index_oid));
763 if (index_oid == NULL) {
764 ERROR(PLUGIN_NAME ": Could not allocate memory for index_oid");
768 int ret = snmp_agent_generate_index(td, vl, index_oid);
771 snmp_agent_table_data_remove(dd, td, index_oid);
783 static void snmp_agent_free_data(data_definition_t **dd) {
785 if (dd == NULL || *dd == NULL)
788 /* unregister scalar type OID */
789 if ((*dd)->table == NULL) {
790 for (size_t i = 0; i < (*dd)->oids_len; i++)
791 unregister_mib((*dd)->oids[i].oid, (*dd)->oids[i].oid_len);
795 sfree((*dd)->plugin);
796 sfree((*dd)->plugin_instance);
798 sfree((*dd)->type_instance);
806 static void snmp_agent_free_table_columns(table_definition_t *td) {
807 if (td->columns == NULL)
810 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
811 data_definition_t *dd = de->value;
813 if (td->index_oid.oid_len) {
817 c_avl_iterator_t *iter = c_avl_get_iterator(td->index_instance);
818 while (c_avl_iterator_next(iter, (void *)&index, (void *)&index_oid) ==
820 for (size_t i = 0; i < dd->oids_len; i++)
821 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
823 c_avl_iterator_destroy(iter);
827 c_avl_iterator_t *iter = c_avl_get_iterator(dd->table->instance_index);
828 while (c_avl_iterator_next(iter, (void *)&index_oid, NULL) == 0) {
829 for (size_t i = 0; i < dd->oids_len; i++)
830 snmp_agent_unregister_oid_string(&dd->oids[i], index_oid);
832 c_avl_iterator_destroy(iter);
835 snmp_agent_free_data(&dd);
838 llist_destroy(td->columns);
840 } /* void snmp_agent_free_table_columns */
842 static void snmp_agent_free_table(table_definition_t **td) {
844 if (td == NULL || *td == NULL)
847 if ((*td)->size_oid.oid_len)
848 unregister_mib((*td)->size_oid.oid, (*td)->size_oid.oid_len);
852 /* Unregister Index OIDs */
853 if ((*td)->index_oid.oid_len) {
856 c_avl_iterator_t *iter = c_avl_get_iterator((*td)->index_instance);
857 while (c_avl_iterator_next(iter, (void **)&index, (void **)&index_oid) == 0)
858 snmp_agent_unregister_oid_index(&(*td)->index_oid, *index);
860 c_avl_iterator_destroy(iter);
863 /* Unregister all table columns and their registered OIDs */
864 snmp_agent_free_table_columns(*td);
870 /* Removing data from instance_oids, leaving key pointers since they are still
871 * used in other AVL trees */
872 c_avl_iterator_t *iter = c_avl_get_iterator((*td)->instance_oids);
873 while (c_avl_iterator_next(iter, (void **)&index_oid, (void **)&num) == 0)
875 c_avl_iterator_destroy(iter);
876 c_avl_destroy((*td)->instance_oids);
878 /* index_instance and instance_index contain the same pointers */
879 c_avl_destroy((*td)->index_instance);
880 (*td)->index_instance = NULL;
882 if ((*td)->instance_index != NULL) {
883 while (c_avl_pick((*td)->instance_index, &key, &value) == 0) {
888 c_avl_destroy((*td)->instance_index);
889 (*td)->instance_index = NULL;
891 snmp_free_varbind((*td)->index_list_cont);
896 for (i = 0; i < (*td)->index_keys_len; i++) {
897 sfree((*td)->index_keys[i].regex);
898 regfree(&(*td)->index_keys[i].regex_info);
900 for (i = 0; i < MAX_KEY_SOURCES; i++)
901 if ((*td)->tokens[i] != NULL) {
902 while (c_avl_pick((*td)->tokens[i], &key, (void **)&tok) == 0) {
907 c_avl_destroy((*td)->tokens[i]);
908 (*td)->tokens[i] = NULL;
916 static int snmp_agent_parse_oid_index_keys(const table_definition_t *td,
918 int ret = parse_oid_indexes(index_oid->oid, index_oid->oid_len,
919 td->index_list_cont);
920 if (ret != SNMPERR_SUCCESS)
921 ERROR(PLUGIN_NAME ": index OID parse error!");
925 static int snmp_agent_build_name(char **name, c_avl_tree_t *tokens) {
929 char str[DATA_MAX_NAME_LEN];
930 char out[DATA_MAX_NAME_LEN] = {0};
931 c_avl_iterator_t *it = c_avl_get_iterator(tokens);
934 ERROR(PLUGIN_NAME ": Error getting tokens list iterator");
938 while (c_avl_iterator_next(it, (void **)&pos, (void **)&tok) == 0) {
939 strncat(out, tok->str, strlen(tok->str));
940 if (tok->key != NULL) {
941 if (tok->key->type == ASN_INTEGER) {
942 snprintf(str, sizeof(str), "%ld", *tok->key->val.integer);
943 strncat(out, str, strlen(str));
944 } else { /* OCTET_STR */
945 strncat(out, (char *)tok->key->val.string,
946 strlen((char *)tok->key->val.string));
951 c_avl_iterator_destroy(it);
954 ERROR(PLUGIN_NAME ": Could not allocate memory");
961 static int snmp_agent_format_name(char *name, int name_len,
962 data_definition_t *dd, oid_t *index_oid) {
966 if (index_oid == NULL) {
968 format_name(name, name_len, hostname_g, dd->plugin, dd->plugin_instance,
969 dd->type, dd->type_instance);
971 /* Need to parse string index OID */
972 const table_definition_t *td = dd->table;
973 ret = snmp_agent_parse_oid_index_keys(td, index_oid);
978 netsnmp_variable_list *key = td->index_list_cont;
979 char str[DATA_MAX_NAME_LEN];
980 char *fields[MAX_KEY_SOURCES] = {hostname_g, dd->plugin,
981 dd->plugin_instance, dd->type,
984 /* Looking for simple keys only */
985 while (key != NULL) {
986 if (!td->index_keys[i].regex) {
987 index_key_src_t source = td->index_keys[i].source;
989 if (source < INDEX_HOST || source > INDEX_TYPE_INSTANCE) {
990 ERROR(PLUGIN_NAME ": Unkown index key source!");
994 if (td->index_keys[i].type == ASN_INTEGER) {
995 snprintf(str, sizeof(str), "%ld", *key->val.integer);
996 fields[source] = str;
997 } else /* OCTET_STR */
998 fields[source] = (char *)key->val.string;
1000 key = key->next_variable;
1004 /* Keys with regexes */
1005 for (i = 0; i < MAX_KEY_SOURCES; i++) {
1006 if (td->tokens[i] == NULL)
1008 ret = snmp_agent_build_name(&fields[i], td->tokens[i]);
1012 format_name(name, name_len, fields[INDEX_HOST], fields[INDEX_PLUGIN],
1013 fields[INDEX_PLUGIN_INSTANCE], fields[INDEX_TYPE],
1014 fields[INDEX_TYPE_INSTANCE]);
1015 for (i = 0; i < MAX_KEY_SOURCES; i++) {
1024 static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests,
1025 data_definition_t *dd, oid_t *index_oid,
1029 if (dd->is_index_key) {
1030 const table_definition_t *td = dd->table;
1031 int ret = snmp_agent_parse_oid_index_keys(td, index_oid);
1036 netsnmp_variable_list *key = td->index_list_cont;
1037 /* Searching index key */
1038 for (int pos = 0; pos < dd->index_key_pos; pos++)
1039 key = key->next_variable;
1041 requests->requestvb->type = td->index_keys[dd->index_key_pos].type;
1043 if (requests->requestvb->type == ASN_INTEGER)
1044 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1045 key->val.integer, sizeof(*key->val.integer));
1046 else /* OCTET_STR */
1047 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1048 (const u_char *)key->val.string,
1049 strlen((const char *)key->val.string));
1051 pthread_mutex_unlock(&g_agent->lock);
1053 return SNMP_ERR_NOERROR;
1056 char name[DATA_MAX_NAME_LEN];
1058 ret = snmp_agent_format_name(name, sizeof(name), dd, index_oid);
1062 DEBUG(PLUGIN_NAME ": Identifier '%s'", name);
1066 const data_set_t *ds = plugin_get_ds(dd->type);
1068 ERROR(PLUGIN_NAME ": Data set not found for '%s' type", dd->type);
1069 return SNMP_NOSUCHINSTANCE;
1072 ret = uc_get_value_by_name(name, &values, &values_num);
1075 ERROR(PLUGIN_NAME ": Failed to get value for '%s'", name);
1076 return SNMP_NOSUCHINSTANCE;
1079 assert(ds->ds_num == values_num);
1080 assert(oid_index < (int)values_num);
1082 char data[DATA_MAX_NAME_LEN];
1083 size_t data_len = sizeof(data);
1084 ret = snmp_agent_set_vardata(
1085 data, &data_len, dd->oids[oid_index].type, dd->scale, dd->shift,
1086 &values[oid_index], sizeof(values[oid_index]), ds->ds[oid_index].type);
1091 ERROR(PLUGIN_NAME ": Failed to convert '%s' value to snmp data", name);
1092 return SNMP_NOSUCHINSTANCE;
1095 requests->requestvb->type = dd->oids[oid_index].type;
1096 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1097 (const u_char *)data, data_len);
1099 return SNMP_ERR_NOERROR;
1103 snmp_agent_table_oid_handler(struct netsnmp_mib_handler_s *handler,
1104 struct netsnmp_handler_registration_s *reginfo,
1105 struct netsnmp_agent_request_info_s *reqinfo,
1106 struct netsnmp_request_info_s *requests) {
1108 if (reqinfo->mode != MODE_GET) {
1109 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
1110 return SNMP_ERR_NOERROR;
1113 pthread_mutex_lock(&g_agent->lock);
1115 oid_t oid; /* Requested OID */
1116 memcpy(oid.oid, requests->requestvb->name,
1117 sizeof(oid.oid[0]) * requests->requestvb->name_length);
1118 oid.oid_len = requests->requestvb->name_length;
1121 char oid_str[DATA_MAX_NAME_LEN];
1122 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
1123 DEBUG(PLUGIN_NAME ": Get request received for table OID '%s'", oid_str);
1125 oid_t index_oid; /* Index part of requested OID */
1127 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1128 table_definition_t *td = te->value;
1130 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1131 data_definition_t *dd = de->value;
1133 for (size_t i = 0; i < dd->oids_len; i++) {
1134 int ret = snmp_oid_ncompare(oid.oid, oid.oid_len, dd->oids[i].oid,
1135 dd->oids[i].oid_len,
1136 SNMP_MIN(oid.oid_len, dd->oids[i].oid_len));
1140 /* Calculating OID length for index part */
1141 index_oid.oid_len = oid.oid_len - dd->oids[i].oid_len;
1142 /* Fetching index part of the OID */
1143 memcpy(index_oid.oid, &oid.oid[dd->oids[i].oid_len],
1144 index_oid.oid_len * sizeof(*oid.oid));
1146 char index_str[DATA_MAX_NAME_LEN];
1147 snmp_agent_oid_to_string(index_str, sizeof(index_str), &index_oid);
1149 if (!td->index_oid.oid_len) {
1150 ret = c_avl_get(td->instance_index, &index_oid, NULL);
1154 assert(index_oid.oid_len == 1);
1155 ret = c_avl_get(td->index_instance, (int *)&index_oid.oid[0],
1156 (void **)&temp_oid);
1157 memcpy(&index_oid, temp_oid, sizeof(index_oid));
1161 INFO(PLUGIN_NAME ": Non-existing index (%s) requested", index_str);
1162 pthread_mutex_unlock(&g_agent->lock);
1163 return SNMP_NOSUCHINSTANCE;
1166 ret = snmp_agent_form_reply(requests, dd, &index_oid, i);
1167 pthread_mutex_unlock(&g_agent->lock);
1174 pthread_mutex_unlock(&g_agent->lock);
1176 return SNMP_NOSUCHINSTANCE;
1179 static int snmp_agent_table_index_oid_handler(
1180 struct netsnmp_mib_handler_s *handler,
1181 struct netsnmp_handler_registration_s *reginfo,
1182 struct netsnmp_agent_request_info_s *reqinfo,
1183 struct netsnmp_request_info_s *requests) {
1185 if (reqinfo->mode != MODE_GET) {
1186 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
1187 return SNMP_ERR_NOERROR;
1190 pthread_mutex_lock(&g_agent->lock);
1193 memcpy(oid.oid, requests->requestvb->name,
1194 sizeof(oid.oid[0]) * requests->requestvb->name_length);
1195 oid.oid_len = requests->requestvb->name_length;
1197 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1198 table_definition_t *td = te->value;
1200 if (td->index_oid.oid_len &&
1202 oid.oid, oid.oid_len, td->index_oid.oid, td->index_oid.oid_len,
1203 SNMP_MIN(oid.oid_len, td->index_oid.oid_len)) == 0)) {
1205 DEBUG(PLUGIN_NAME ": Handle '%s' table index OID", td->name);
1207 int index = oid.oid[oid.oid_len - 1];
1209 int ret = c_avl_get(td->index_instance, &index, NULL);
1211 /* nonexisting index requested */
1212 pthread_mutex_unlock(&g_agent->lock);
1213 return SNMP_NOSUCHINSTANCE;
1216 requests->requestvb->type = ASN_INTEGER;
1217 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1218 (const u_char *)&index, sizeof(index));
1220 pthread_mutex_unlock(&g_agent->lock);
1222 return SNMP_ERR_NOERROR;
1226 pthread_mutex_unlock(&g_agent->lock);
1228 return SNMP_NOSUCHINSTANCE;
1231 static int snmp_agent_table_size_oid_handler(
1232 struct netsnmp_mib_handler_s *handler,
1233 struct netsnmp_handler_registration_s *reginfo,
1234 struct netsnmp_agent_request_info_s *reqinfo,
1235 struct netsnmp_request_info_s *requests) {
1237 if (reqinfo->mode != MODE_GET) {
1238 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
1239 return SNMP_ERR_NOERROR;
1242 pthread_mutex_lock(&g_agent->lock);
1245 memcpy(oid.oid, requests->requestvb->name,
1246 sizeof(oid.oid[0]) * requests->requestvb->name_length);
1247 oid.oid_len = requests->requestvb->name_length;
1249 DEBUG(PLUGIN_NAME ": Get request received for table size OID");
1251 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1252 table_definition_t *td = te->value;
1254 if (td->size_oid.oid_len &&
1255 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->size_oid.oid,
1256 td->size_oid.oid_len,
1257 SNMP_MIN(oid.oid_len, td->size_oid.oid_len)) == 0)) {
1258 DEBUG(PLUGIN_NAME ": Handle '%s' table size OID", td->name);
1261 if (td->index_oid.oid_len)
1262 size = c_avl_size(td->index_instance);
1264 size = c_avl_size(td->instance_index);
1266 requests->requestvb->type = ASN_INTEGER;
1267 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1268 (const u_char *)&size, sizeof(size));
1270 pthread_mutex_unlock(&g_agent->lock);
1272 return SNMP_ERR_NOERROR;
1276 pthread_mutex_unlock(&g_agent->lock);
1278 return SNMP_NOSUCHINSTANCE;
1282 snmp_agent_scalar_oid_handler(struct netsnmp_mib_handler_s *handler,
1283 struct netsnmp_handler_registration_s *reginfo,
1284 struct netsnmp_agent_request_info_s *reqinfo,
1285 struct netsnmp_request_info_s *requests) {
1287 if (reqinfo->mode != MODE_GET) {
1288 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
1289 return SNMP_ERR_NOERROR;
1292 pthread_mutex_lock(&g_agent->lock);
1295 memcpy(oid.oid, requests->requestvb->name,
1296 sizeof(oid.oid[0]) * requests->requestvb->name_length);
1297 oid.oid_len = requests->requestvb->name_length;
1300 char oid_str[DATA_MAX_NAME_LEN];
1301 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
1302 DEBUG(PLUGIN_NAME ": Get request received for scalar OID '%s'", oid_str);
1305 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL;
1307 data_definition_t *dd = de->value;
1309 for (size_t i = 0; i < dd->oids_len; i++) {
1311 int ret = snmp_oid_compare(oid.oid, oid.oid_len, dd->oids[i].oid,
1312 dd->oids[i].oid_len);
1316 ret = snmp_agent_form_reply(requests, dd, NULL, i);
1318 pthread_mutex_unlock(&g_agent->lock);
1324 pthread_mutex_unlock(&g_agent->lock);
1326 return SNMP_NOSUCHINSTANCE;
1329 static int snmp_agent_register_table_oids(void) {
1331 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1332 table_definition_t *td = te->value;
1334 if (td->size_oid.oid_len != 0) {
1336 snmp_agent_get_asn_type(td->size_oid.oid, td->size_oid.oid_len);
1337 td->size_oid.oid_len++;
1338 int ret = snmp_agent_register_oid(&td->size_oid,
1339 snmp_agent_table_size_oid_handler);
1344 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1345 data_definition_t *dd = de->value;
1347 for (size_t i = 0; i < dd->oids_len; i++) {
1349 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
1357 static int snmp_agent_register_scalar_oids(void) {
1359 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
1360 data_definition_t *dd = e->value;
1362 for (size_t i = 0; i < dd->oids_len; i++) {
1365 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
1368 snmp_agent_register_oid(&dd->oids[i], snmp_agent_scalar_oid_handler);
1377 static int snmp_agent_config_data_oids(data_definition_t *dd,
1378 oconfig_item_t *ci) {
1379 if (ci->values_num < 1) {
1380 WARNING(PLUGIN_NAME ": `OIDs' needs at least one argument");
1384 for (int i = 0; i < ci->values_num; i++)
1385 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
1386 WARNING(PLUGIN_NAME ": `OIDs' needs only string argument");
1390 if (dd->oids != NULL) {
1391 WARNING(PLUGIN_NAME ": OIDs can be configured only once for each data");
1396 dd->oids = calloc(ci->values_num, sizeof(*dd->oids));
1398 if (dd->oids == NULL)
1400 dd->oids_len = (size_t)ci->values_num;
1402 for (int i = 0; i < ci->values_num; i++) {
1403 dd->oids[i].oid_len = MAX_OID_LEN;
1405 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->oids[i].oid,
1406 &dd->oids[i].oid_len)) {
1407 ERROR(PLUGIN_NAME ": snmp_parse_oid (%s) failed",
1408 ci->values[i].value.string);
1418 static int snmp_agent_config_table_size_oid(table_definition_t *td,
1419 oconfig_item_t *ci) {
1420 if (ci->values_num < 1) {
1421 WARNING(PLUGIN_NAME ": `TableSizeOID' is empty");
1425 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
1426 WARNING(PLUGIN_NAME ": `TableSizeOID' needs only string argument");
1430 td->size_oid.oid_len = MAX_OID_LEN;
1432 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->size_oid.oid,
1433 &td->size_oid.oid_len)) {
1434 ERROR(PLUGIN_NAME ": Failed to parse table size OID (%s)",
1435 ci->values[0].value.string);
1436 td->size_oid.oid_len = 0;
1443 static int snmp_agent_config_table_index_oid(table_definition_t *td,
1444 oconfig_item_t *ci) {
1446 if (ci->values_num < 1) {
1447 WARNING(PLUGIN_NAME ": `IndexOID' is empty");
1451 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
1452 WARNING(PLUGIN_NAME ": `IndexOID' needs only string argument");
1456 td->index_oid.oid_len = MAX_OID_LEN;
1458 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->index_oid.oid,
1459 &td->index_oid.oid_len)) {
1460 ERROR(PLUGIN_NAME ": Failed to parse table index OID (%s)",
1461 ci->values[0].value.string);
1462 td->index_oid.oid_len = 0;
1469 /* Getting index key source that will represent table row */
1470 static int snmp_agent_config_index_key_source(table_definition_t *td,
1471 data_definition_t *dd,
1472 oconfig_item_t *ci) {
1475 int ret = cf_util_get_string(ci, &val);
1481 for (int i = 0; i < MAX_KEY_SOURCES; i++) {
1482 if (strcasecmp(index_opts[i], (const char *)val) == 0) {
1483 td->index_keys[td->index_keys_len].source = i;
1484 td->index_keys[td->index_keys_len].group = GROUP_UNUSED;
1485 td->index_keys[td->index_keys_len].regex = NULL;
1492 ERROR(PLUGIN_NAME ": Failed to parse index key source: '%s'", val);
1498 dd->index_key_pos = td->index_keys_len++;
1499 dd->is_index_key = 1;
1504 /* Getting format string used to parse values from index key source */
1505 static int snmp_agent_config_index_key_regex(table_definition_t *td,
1506 data_definition_t *dd,
1507 oconfig_item_t *ci) {
1508 index_key_t *index_key = &td->index_keys[dd->index_key_pos];
1510 int ret = cf_util_get_string(ci, &index_key->regex);
1514 ret = regcomp(&index_key->regex_info, index_key->regex, REG_EXTENDED);
1516 ERROR(PLUGIN_NAME ": Could not compile regex for %s", dd->name);
1520 index_key_src_t source = index_key->source;
1521 if (td->tokens[source] == NULL) {
1522 td->tokens[source] =
1523 c_avl_create((int (*)(const void *, const void *))num_compare);
1524 if (td->tokens[source] == NULL) {
1525 ERROR(PLUGIN_NAME ": Could not allocate memory for AVL tree");
1533 static int snmp_agent_config_index_key(table_definition_t *td,
1534 data_definition_t *dd,
1535 oconfig_item_t *ci) {
1538 for (int i = 0; (i < ci->children_num && ret == 0); i++) {
1539 oconfig_item_t *option = ci->children + i;
1541 if (strcasecmp("Source", option->key) == 0)
1542 ret = snmp_agent_config_index_key_source(td, dd, option);
1543 else if (strcasecmp("Regex", option->key) == 0)
1544 ret = snmp_agent_config_index_key_regex(td, dd, option);
1545 else if (strcasecmp("Group", option->key) == 0)
1546 ret = cf_util_get_int(option, &td->index_keys[dd->index_key_pos].group);
1552 /* This function parses configuration of both scalar and table column
1553 * because they have nearly the same structure */
1554 static int snmp_agent_config_table_column(table_definition_t *td,
1555 oconfig_item_t *ci) {
1556 data_definition_t *dd;
1558 oconfig_item_t *option_tmp = NULL;
1562 dd = calloc(1, sizeof(*dd));
1564 ERROR(PLUGIN_NAME ": Failed to allocate memory for table data definition");
1568 ret = cf_util_get_string(ci, &dd->name);
1576 /* NULL if it's a scalar */
1578 dd->is_index_key = 0;
1580 for (int i = 0; i < ci->children_num; i++) {
1581 oconfig_item_t *option = ci->children + i;
1583 /* First 3 options are reserved for table entry only */
1584 if (td != NULL && strcasecmp("IndexKey", option->key) == 0) {
1585 dd->is_index_key = 1;
1586 option_tmp = option;
1587 } else if (strcasecmp("Plugin", option->key) == 0)
1588 ret = cf_util_get_string(option, &dd->plugin);
1589 else if (strcasecmp("PluginInstance", option->key) == 0)
1590 ret = cf_util_get_string(option, &dd->plugin_instance);
1591 else if (strcasecmp("Type", option->key) == 0)
1592 ret = cf_util_get_string(option, &dd->type);
1593 else if (strcasecmp("TypeInstance", option->key) == 0)
1594 ret = cf_util_get_string(option, &dd->type_instance);
1595 else if (strcasecmp("Shift", option->key) == 0)
1596 ret = cf_util_get_double(option, &dd->shift);
1597 else if (strcasecmp("Scale", option->key) == 0)
1598 ret = cf_util_get_double(option, &dd->scale);
1599 else if (strcasecmp("OIDs", option->key) == 0)
1600 ret = snmp_agent_config_data_oids(dd, option);
1602 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
1607 snmp_agent_free_data(&dd);
1612 if (dd->is_index_key) {
1613 ret = snmp_agent_config_index_key(td, dd, option_tmp);
1614 td->index_keys[dd->index_key_pos].type =
1615 snmp_agent_get_asn_type(dd->oids[0].oid, dd->oids[0].oid_len);
1618 snmp_agent_free_data(&dd);
1623 llentry_t *entry = llentry_create(dd->name, dd);
1624 if (entry == NULL) {
1625 snmp_agent_free_data(&dd);
1629 /* Append to column list in parent table */
1631 llist_append(td->columns, entry);
1633 llentry_destroy(entry);
1638 /* Parses scalar configuration entry */
1639 static int snmp_agent_config_scalar(oconfig_item_t *ci) {
1640 return snmp_agent_config_table_column(NULL, ci);
1643 static int num_compare(const int *a, const int *b) {
1644 assert((a != NULL) && (b != NULL));
1653 static int oid_compare(const oid_t *a, const oid_t *b) {
1654 return snmp_oid_compare(a->oid, a->oid_len, b->oid, b->oid_len);
1657 static int snmp_agent_config_table(oconfig_item_t *ci) {
1658 table_definition_t *td;
1663 td = calloc(1, sizeof(*td));
1665 ERROR(PLUGIN_NAME ": Failed to allocate memory for table definition");
1669 ret = cf_util_get_string(ci, &td->name);
1675 td->columns = llist_create();
1676 if (td->columns == NULL) {
1677 ERROR(PLUGIN_NAME ": Failed to allocate memory for columns list");
1678 snmp_agent_free_table(&td);
1682 for (int i = 0; i < MAX_KEY_SOURCES; i++)
1683 td->tokens[i] = NULL;
1684 td->tokens_done = 0;
1686 for (int i = 0; i < ci->children_num; i++) {
1687 oconfig_item_t *option = ci->children + i;
1689 if (strcasecmp("IndexOID", option->key) == 0)
1690 ret = snmp_agent_config_table_index_oid(td, option);
1691 else if (strcasecmp("SizeOID", option->key) == 0)
1692 ret = snmp_agent_config_table_size_oid(td, option);
1693 else if (strcasecmp("Data", option->key) == 0)
1694 ret = snmp_agent_config_table_column(td, option);
1696 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
1701 snmp_agent_free_table(&td);
1706 /* Preparing index list container */
1707 ret = snmp_agent_prep_index_list(td, &td->index_list_cont);
1711 td->instance_index =
1712 c_avl_create((int (*)(const void *, const void *))oid_compare);
1713 if (td->instance_index == NULL) {
1714 snmp_agent_free_table(&td);
1718 td->index_instance =
1719 c_avl_create((int (*)(const void *, const void *))num_compare);
1720 if (td->index_instance == NULL) {
1721 snmp_agent_free_table(&td);
1726 c_avl_create((int (*)(const void *, const void *))oid_compare);
1727 if (td->instance_oids == NULL) {
1728 snmp_agent_free_table(&td);
1732 llentry_t *entry = llentry_create(td->name, td);
1733 if (entry == NULL) {
1734 snmp_agent_free_table(&td);
1738 llist_append(g_agent->tables, entry);
1743 static int snmp_agent_get_value_from_ds_type(const value_t *val, int type,
1744 double scale, double shift,
1747 case DS_TYPE_COUNTER:
1748 *value = (long)((val->counter * scale) + shift);
1750 case DS_TYPE_ABSOLUTE:
1751 *value = (long)((val->absolute * scale) + shift);
1753 case DS_TYPE_DERIVE:
1754 *value = (long)((val->derive * scale) + shift);
1757 *value = (long)((val->gauge * scale) + shift);
1762 ERROR(PLUGIN_NAME ": Unknown data source type: %i", type);
1769 static int snmp_agent_set_vardata(void *data, size_t *data_len, u_char asn_type,
1770 double scale, double shift, const void *value,
1771 size_t len, int type) {
1774 netsnmp_vardata var;
1779 var.string = (u_char *)data;
1781 ret = snmp_agent_get_value_from_ds_type(val, type, scale, shift, &new_value);
1791 if (*data_len < sizeof(*var.integer))
1793 *var.integer = new_value;
1794 *data_len = sizeof(*var.integer);
1797 if (*data_len < sizeof(*var.counter64))
1799 var.counter64->high = (u_long)((int64_t)new_value >> 32);
1800 var.counter64->low = (u_long)((int64_t)new_value & 0xFFFFFFFF);
1801 *data_len = sizeof(*var.counter64);
1804 if (type == DS_TYPE_GAUGE) {
1805 char buf[DATA_MAX_NAME_LEN];
1806 snprintf(buf, sizeof(buf), "%.2f", val->gauge);
1807 if (*data_len < strlen(buf))
1809 *data_len = strlen(buf);
1810 memcpy(var.string, buf, *data_len);
1812 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1818 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1826 static int snmp_agent_register_oid_index(oid_t *oid, int index,
1827 Netsnmp_Node_Handler *handler) {
1829 memcpy(&new_oid, oid, sizeof(*oid));
1830 new_oid.oid[new_oid.oid_len++] = index;
1831 return snmp_agent_register_oid(&new_oid, handler);
1834 static int snmp_agent_unregister_oid_index(oid_t *oid, int index) {
1836 memcpy(&new_oid, oid, sizeof(*oid));
1837 new_oid.oid[new_oid.oid_len++] = index;
1838 return snmp_agent_unregister_oid(&new_oid);
1841 static int snmp_agent_update_instance_oids(c_avl_tree_t *tree, oid_t *index_oid,
1843 int *oids_num; /* number of oids registered for instance */
1845 if (c_avl_get(tree, index_oid, (void **)&oids_num) == 0) {
1849 ERROR(PLUGIN_NAME ": Error updating index data");
1854 static int snmp_agent_update_index(data_definition_t *dd,
1855 table_definition_t *td, oid_t **index_oid) {
1859 _Bool free_index_oid = 1;
1861 if (c_avl_get(td->instance_index, (void *)*index_oid, (void **)&index) != 0) {
1862 /* Processing new instance */
1865 /* need to generate index for the table */
1866 if (td->index_oid.oid_len) {
1867 index = calloc(1, sizeof(*index));
1868 if (index == NULL) {
1870 goto free_index_oid;
1873 *index = c_avl_size(td->instance_index) + 1;
1875 ret = c_avl_insert(td->instance_index, *index_oid, index);
1879 ret = c_avl_insert(td->index_instance, index, *index_oid);
1881 DEBUG(PLUGIN_NAME ": Failed to update index_instance for '%s' table",
1883 goto remove_avl_index_oid;
1886 ret = snmp_agent_register_oid_index(&td->index_oid, *index,
1887 snmp_agent_table_index_oid_handler);
1889 goto remove_avl_index;
1891 /* instance as a key is required for any table */
1892 ret = c_avl_insert(td->instance_index, *index_oid, NULL);
1894 goto free_index_oid;
1897 value = calloc(1, sizeof(*value));
1899 if (value == NULL) {
1900 ERROR(PLUGIN_NAME ": Failed to allocate memory");
1902 goto unregister_index;
1905 ret = c_avl_insert(td->instance_oids, *index_oid, value);
1908 DEBUG(PLUGIN_NAME ": Failed to update instance_oids for '%s' table",
1913 int keys_processed = 0;
1915 /* Registering index keys OIDs */
1916 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1917 data_definition_t *idd = de->value;
1918 if (!idd->is_index_key)
1921 for (size_t i = 0; i < idd->oids_len; i++) {
1922 if (td->index_oid.oid_len)
1923 ret = snmp_agent_register_oid_index(&idd->oids[i], *index,
1924 snmp_agent_table_oid_handler);
1926 ret = snmp_agent_register_oid_string(&idd->oids[i], *index_oid,
1927 snmp_agent_table_oid_handler);
1930 ERROR(PLUGIN_NAME ": Could not register OID");
1935 if (++keys_processed >= td->index_keys_len)
1942 for (size_t i = 0; i < dd->oids_len; i++) {
1943 if (td->index_oid.oid_len)
1944 ret = snmp_agent_register_oid_index(&dd->oids[i], *index,
1945 snmp_agent_table_oid_handler);
1947 ret = snmp_agent_register_oid_string(&dd->oids[i], *index_oid,
1948 snmp_agent_table_oid_handler);
1952 else if (ret == OID_EXISTS)
1954 else if (snmp_agent_update_instance_oids(td->instance_oids, *index_oid, 1) <
1959 if (ret != OID_EXISTS) {
1960 char index_str[DATA_MAX_NAME_LEN];
1963 snmp_agent_oid_to_string(index_str, sizeof(index_str), *index_oid);
1965 snprintf(index_str, sizeof(index_str), "%d", *index);
1967 notification_t n = {
1968 .severity = NOTIF_OKAY, .time = cdtime(), .plugin = PLUGIN_NAME};
1969 sstrncpy(n.host, hostname_g, sizeof(n.host));
1970 snprintf(n.message, sizeof(n.message),
1971 "Data added to table %s with index %s", td->name, index_str);
1972 DEBUG(PLUGIN_NAME ": %s", n.message);
1974 plugin_dispatch_notification(&n);
1985 if (td->index_oid.oid_len)
1986 snmp_agent_unregister_oid_index(*index_oid, *index);
1988 if (td->index_oid.oid_len)
1989 c_avl_remove(td->index_instance, index, NULL, NULL);
1990 remove_avl_index_oid:
1991 c_avl_remove(td->instance_index, *index_oid, NULL, NULL);
2001 static int snmp_agent_write(value_list_t const *vl) {
2005 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
2006 table_definition_t *td = te->value;
2008 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
2009 data_definition_t *dd = de->value;
2011 if (!dd->is_index_key) {
2012 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
2013 vl->type_instance)) {
2014 oid_t *index_oid = calloc(1, sizeof(*index_oid));
2016 if (index_oid == NULL) {
2017 ERROR(PLUGIN_NAME ": Could not allocate memory for index_oid");
2021 int ret = snmp_agent_generate_index(td, vl, index_oid);
2024 ret = snmp_agent_update_index(dd, td, &index_oid);
2035 static int snmp_agent_collect(const data_set_t *ds, const value_list_t *vl,
2036 user_data_t __attribute__((unused)) * user_data) {
2038 pthread_mutex_lock(&g_agent->lock);
2040 snmp_agent_write(vl);
2042 pthread_mutex_unlock(&g_agent->lock);
2047 static int snmp_agent_preinit(void) {
2049 g_agent = calloc(1, sizeof(*g_agent));
2050 if (g_agent == NULL) {
2051 ERROR(PLUGIN_NAME ": Failed to allocate memory for snmp agent context");
2055 g_agent->tables = llist_create();
2056 g_agent->scalars = llist_create();
2057 g_agent->registered_oids =
2058 c_avl_create((int (*)(const void *, const void *))oid_compare);
2060 if (g_agent->tables == NULL || g_agent->scalars == NULL) {
2061 ERROR(PLUGIN_NAME ": llist_create() failed");
2062 llist_destroy(g_agent->scalars);
2063 llist_destroy(g_agent->tables);
2064 c_avl_destroy(g_agent->registered_oids);
2069 /* make us an agentx client. */
2070 err = netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE,
2073 ERROR(PLUGIN_NAME ": Failed to set agent role (%d)", err);
2074 llist_destroy(g_agent->scalars);
2075 llist_destroy(g_agent->tables);
2076 c_avl_destroy(g_agent->registered_oids);
2081 * For SNMP debug purposes uses snmp_set_do_debugging(1);
2084 /* initialize the agent library */
2085 err = init_agent(PLUGIN_NAME);
2087 ERROR(PLUGIN_NAME ": Failed to initialize the agent library (%d)", err);
2088 llist_destroy(g_agent->scalars);
2089 llist_destroy(g_agent->tables);
2090 c_avl_destroy(g_agent->registered_oids);
2094 init_snmp(PLUGIN_NAME);
2096 g_agent->tp = read_all_mibs();
2101 static int snmp_agent_init(void) {
2104 if (g_agent == NULL || ((llist_head(g_agent->scalars) == NULL) &&
2105 (llist_head(g_agent->tables) == NULL))) {
2106 ERROR(PLUGIN_NAME ": snmp_agent_init: plugin not configured");
2110 plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown);
2112 ret = snmp_agent_register_scalar_oids();
2116 ret = snmp_agent_register_table_oids();
2120 ret = pthread_mutex_init(&g_agent->lock, NULL);
2122 ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret);
2126 ret = pthread_mutex_init(&g_agent->agentx_lock, NULL);
2128 ERROR(PLUGIN_NAME ": Failed to initialize AgentX mutex, err %u", ret);
2132 /* create a second thread to listen for requests from AgentX*/
2133 ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
2135 ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
2139 if (llist_head(g_agent->tables) != NULL) {
2140 plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL);
2141 plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL);
2147 static void *snmp_agent_thread_run(void __attribute__((unused)) * arg) {
2148 INFO(PLUGIN_NAME ": Thread is up and running");
2151 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2153 pthread_mutex_lock(&g_agent->agentx_lock);
2154 agent_check_and_process(0); /* 0 == don't block */
2155 pthread_mutex_unlock(&g_agent->agentx_lock);
2157 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
2164 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler) {
2165 netsnmp_handler_registration *reg;
2167 if (c_avl_get(g_agent->registered_oids, (void *)oid, NULL) == 0)
2170 oid_t *new_oid = calloc(1, sizeof(*oid));
2172 if (new_oid == NULL) {
2173 ERROR(PLUGIN_NAME ": Could not allocate memory to register new OID");
2177 memcpy(new_oid, oid, sizeof(*oid));
2179 int ret = c_avl_insert(g_agent->registered_oids, (void *)new_oid, NULL);
2181 ERROR(PLUGIN_NAME ": Could not allocate memory to register new OID");
2187 char *oid_name = snmp_agent_get_oid_name(oid->oid, oid->oid_len - 1);
2188 char oid_str[DATA_MAX_NAME_LEN];
2190 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), oid);
2192 if (oid_name == NULL) {
2194 ": Skipped registration: OID (%s) is not found in main tree",
2199 reg = netsnmp_create_handler_registration(oid_name, handler, oid->oid,
2200 oid->oid_len, HANDLER_CAN_RONLY);
2202 ERROR(PLUGIN_NAME ": Failed to create handler registration for OID (%s)",
2207 pthread_mutex_lock(&g_agent->agentx_lock);
2209 if (netsnmp_register_instance(reg) != MIB_REGISTERED_OK) {
2210 ERROR(PLUGIN_NAME ": Failed to register handler for OID (%s)", oid_str);
2211 pthread_mutex_unlock(&g_agent->agentx_lock);
2215 pthread_mutex_unlock(&g_agent->agentx_lock);
2217 DEBUG(PLUGIN_NAME ": Registered handler for OID (%s)", oid_str);
2222 static int snmp_agent_free_config(void) {
2224 if (g_agent == NULL)
2227 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next)
2228 snmp_agent_free_table((table_definition_t **)&te->value);
2229 llist_destroy(g_agent->tables);
2231 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL; de = de->next)
2232 snmp_agent_free_data((data_definition_t **)&de->value);
2233 llist_destroy(g_agent->scalars);
2238 static int snmp_agent_shutdown(void) {
2241 DEBUG(PLUGIN_NAME ": snmp_agent_shutdown");
2243 if (g_agent == NULL) {
2244 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: plugin not initialized");
2248 if (pthread_cancel(g_agent->thread) != 0)
2249 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to cancel the thread");
2251 if (pthread_join(g_agent->thread, NULL) != 0)
2252 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to join the thread");
2254 snmp_agent_free_config();
2256 snmp_shutdown(PLUGIN_NAME);
2258 pthread_mutex_destroy(&g_agent->lock);
2259 pthread_mutex_destroy(&g_agent->agentx_lock);
2261 /* Freeing registered OIDs list */
2264 if (g_agent->registered_oids != NULL) {
2265 while (c_avl_pick(g_agent->registered_oids, &oid, NULL) == 0) {
2268 c_avl_destroy(g_agent->registered_oids);
2276 static int snmp_agent_config(oconfig_item_t *ci) {
2277 int ret = snmp_agent_preinit();
2284 for (int i = 0; i < ci->children_num; i++) {
2285 oconfig_item_t *child = ci->children + i;
2286 if (strcasecmp("Data", child->key) == 0) {
2287 ret = snmp_agent_config_scalar(child);
2288 } else if (strcasecmp("Table", child->key) == 0) {
2289 ret = snmp_agent_config_table(child);
2291 ERROR(PLUGIN_NAME ": Unknown configuration option `%s'", child->key);
2296 ERROR(PLUGIN_NAME ": Failed to parse configuration");
2297 snmp_agent_free_config();
2298 snmp_shutdown(PLUGIN_NAME);
2304 ret = snmp_agent_validate_config();
2306 ERROR(PLUGIN_NAME ": Invalid configuration provided");
2307 snmp_agent_free_config();
2308 snmp_shutdown(PLUGIN_NAME);
2316 void module_register(void) {
2317 plugin_register_init(PLUGIN_NAME, snmp_agent_init);
2318 plugin_register_complex_config(PLUGIN_NAME, snmp_agent_config);