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 for (size_t i = 0; i < dd->oids_len; i++) {
675 if (td->index_oid.oid_len)
676 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
678 snmp_agent_unregister_oid_string(&dd->oids[i], index_oid);
681 /* Checking if any metrics are left registered */
682 if (snmp_agent_update_instance_oids(td->instance_oids, index_oid, -1) > 0) {
683 pthread_mutex_unlock(&g_agent->agentx_lock);
687 /* All metrics have been unregistered. Unregistering index key OIDs */
688 int keys_processed = 0;
689 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
690 data_definition_t *idd = de->value;
692 if (!idd->is_index_key)
695 for (size_t i = 0; i < idd->oids_len; i++)
696 if (td->index_oid.oid_len)
697 snmp_agent_unregister_oid_index(&idd->oids[i], *index);
699 snmp_agent_unregister_oid_string(&idd->oids[i], index_oid);
701 if (++keys_processed >= td->index_keys_len)
704 pthread_mutex_unlock(&g_agent->agentx_lock);
706 /* All OIDs have been unregistered so we dont need this instance registered
708 char index_str[DATA_MAX_NAME_LEN];
711 snmp_agent_oid_to_string(index_str, sizeof(index_str), index_oid);
713 snprintf(index_str, sizeof(index_str), "%d", *index);
716 .severity = NOTIF_WARNING, .time = cdtime(), .plugin = PLUGIN_NAME};
717 sstrncpy(n.host, hostname_g, sizeof(n.host));
718 snprintf(n.message, sizeof(n.message),
719 "Removed data row from table %s with index %s", td->name, index_str);
720 DEBUG(PLUGIN_NAME ": %s", n.message);
721 plugin_dispatch_notification(&n);
725 c_avl_remove(td->instance_oids, index_oid, NULL, (void **)&val);
729 pthread_mutex_lock(&g_agent->agentx_lock);
730 snmp_agent_unregister_oid_index(&td->index_oid, *index);
731 pthread_mutex_unlock(&g_agent->agentx_lock);
733 c_avl_remove(td->index_instance, index, NULL, (void **)&ind_oid);
734 c_avl_remove(td->instance_index, index_oid, NULL, (void **)&index);
738 c_avl_remove(td->instance_index, index_oid, NULL, NULL);
742 static int snmp_agent_clear_missing(const value_list_t *vl,
743 __attribute__((unused)) user_data_t *ud) {
747 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
748 table_definition_t *td = te->value;
750 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
751 data_definition_t *dd = de->value;
753 if (!dd->is_index_key) {
754 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
755 vl->type_instance)) {
756 oid_t *index_oid = calloc(1, sizeof(*index_oid));
758 if (index_oid == NULL) {
759 ERROR(PLUGIN_NAME ": Could not allocate memory for index_oid");
763 int ret = snmp_agent_generate_index(td, vl, index_oid);
766 snmp_agent_table_data_remove(dd, td, index_oid);
778 static void snmp_agent_free_data(data_definition_t **dd) {
780 if (dd == NULL || *dd == NULL)
783 /* unregister scalar type OID */
784 if ((*dd)->table == NULL) {
785 for (size_t i = 0; i < (*dd)->oids_len; i++)
786 unregister_mib((*dd)->oids[i].oid, (*dd)->oids[i].oid_len);
790 sfree((*dd)->plugin);
791 sfree((*dd)->plugin_instance);
793 sfree((*dd)->type_instance);
801 static void snmp_agent_free_table_columns(table_definition_t *td) {
802 if (td->columns == NULL)
805 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
806 data_definition_t *dd = de->value;
808 if (td->index_oid.oid_len) {
812 c_avl_iterator_t *iter = c_avl_get_iterator(td->index_instance);
813 while (c_avl_iterator_next(iter, (void *)&index, (void *)&index_oid) ==
815 for (size_t i = 0; i < dd->oids_len; i++)
816 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
818 c_avl_iterator_destroy(iter);
822 c_avl_iterator_t *iter = c_avl_get_iterator(dd->table->instance_index);
823 while (c_avl_iterator_next(iter, (void *)&index_oid, NULL) == 0) {
824 for (size_t i = 0; i < dd->oids_len; i++)
825 snmp_agent_unregister_oid_string(&dd->oids[i], index_oid);
827 c_avl_iterator_destroy(iter);
830 snmp_agent_free_data(&dd);
833 llist_destroy(td->columns);
835 } /* void snmp_agent_free_table_columns */
837 static void snmp_agent_free_table(table_definition_t **td) {
839 if (td == NULL || *td == NULL)
842 if ((*td)->size_oid.oid_len)
843 unregister_mib((*td)->size_oid.oid, (*td)->size_oid.oid_len);
847 /* Unregister Index OIDs */
848 if ((*td)->index_oid.oid_len) {
851 c_avl_iterator_t *iter = c_avl_get_iterator((*td)->index_instance);
852 while (c_avl_iterator_next(iter, (void **)&index, (void **)&index_oid) == 0)
853 snmp_agent_unregister_oid_index(&(*td)->index_oid, *index);
855 c_avl_iterator_destroy(iter);
858 /* Unregister all table columns and their registered OIDs */
859 snmp_agent_free_table_columns(*td);
865 /* Removing data from instance_oids, leaving key pointers since they are still
866 * used in other AVL trees */
867 c_avl_iterator_t *iter = c_avl_get_iterator((*td)->instance_oids);
868 while (c_avl_iterator_next(iter, (void **)&index_oid, (void **)&num) == 0)
870 c_avl_iterator_destroy(iter);
871 c_avl_destroy((*td)->instance_oids);
873 /* index_instance and instance_index contain the same pointers */
874 c_avl_destroy((*td)->index_instance);
875 (*td)->index_instance = NULL;
877 if ((*td)->instance_index != NULL) {
878 while (c_avl_pick((*td)->instance_index, &key, &value) == 0) {
883 c_avl_destroy((*td)->instance_index);
884 (*td)->instance_index = NULL;
886 snmp_free_varbind((*td)->index_list_cont);
891 for (i = 0; i < (*td)->index_keys_len; i++) {
892 sfree((*td)->index_keys[i].regex);
893 regfree(&(*td)->index_keys[i].regex_info);
895 for (i = 0; i < MAX_KEY_SOURCES; i++)
896 if ((*td)->tokens[i] != NULL) {
897 while (c_avl_pick((*td)->tokens[i], &key, (void **)&tok) == 0) {
902 c_avl_destroy((*td)->tokens[i]);
903 (*td)->tokens[i] = NULL;
911 static int snmp_agent_parse_oid_index_keys(const table_definition_t *td,
913 int ret = parse_oid_indexes(index_oid->oid, index_oid->oid_len,
914 td->index_list_cont);
915 if (ret != SNMPERR_SUCCESS)
916 ERROR(PLUGIN_NAME ": index OID parse error!");
920 static int snmp_agent_build_name(char **name, c_avl_tree_t *tokens) {
924 char str[DATA_MAX_NAME_LEN];
925 char out[DATA_MAX_NAME_LEN] = {0};
926 c_avl_iterator_t *it = c_avl_get_iterator(tokens);
929 ERROR(PLUGIN_NAME ": Error getting tokens list iterator");
933 while (c_avl_iterator_next(it, (void **)&pos, (void **)&tok) == 0) {
934 strncat(out, tok->str, strlen(tok->str));
935 if (tok->key != NULL) {
936 if (tok->key->type == ASN_INTEGER) {
937 snprintf(str, sizeof(str), "%ld", *tok->key->val.integer);
938 strncat(out, str, strlen(str));
939 } else { /* OCTET_STR */
940 strncat(out, (char *)tok->key->val.string,
941 strlen((char *)tok->key->val.string));
946 c_avl_iterator_destroy(it);
949 ERROR(PLUGIN_NAME ": Could not allocate memory");
956 static int snmp_agent_format_name(char *name, int name_len,
957 data_definition_t *dd, oid_t *index_oid) {
961 if (index_oid == NULL) {
963 format_name(name, name_len, hostname_g, dd->plugin, dd->plugin_instance,
964 dd->type, dd->type_instance);
966 /* Need to parse string index OID */
967 const table_definition_t *td = dd->table;
968 ret = snmp_agent_parse_oid_index_keys(td, index_oid);
973 netsnmp_variable_list *key = td->index_list_cont;
974 char str[DATA_MAX_NAME_LEN];
975 char *fields[MAX_KEY_SOURCES] = {hostname_g, dd->plugin,
976 dd->plugin_instance, dd->type,
979 /* Looking for simple keys only */
980 while (key != NULL) {
981 if (!td->index_keys[i].regex) {
982 index_key_src_t source = td->index_keys[i].source;
984 if (source < INDEX_HOST || source > INDEX_TYPE_INSTANCE) {
985 ERROR(PLUGIN_NAME ": Unkown index key source!");
989 if (td->index_keys[i].type == ASN_INTEGER) {
990 snprintf(str, sizeof(str), "%ld", *key->val.integer);
991 fields[source] = str;
992 } else /* OCTET_STR */
993 fields[source] = (char *)key->val.string;
995 key = key->next_variable;
999 /* Keys with regexes */
1000 for (i = 0; i < MAX_KEY_SOURCES; i++) {
1001 if (td->tokens[i] == NULL)
1003 ret = snmp_agent_build_name(&fields[i], td->tokens[i]);
1007 format_name(name, name_len, fields[INDEX_HOST], fields[INDEX_PLUGIN],
1008 fields[INDEX_PLUGIN_INSTANCE], fields[INDEX_TYPE],
1009 fields[INDEX_TYPE_INSTANCE]);
1010 for (i = 0; i < MAX_KEY_SOURCES; i++) {
1019 static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests,
1020 data_definition_t *dd, oid_t *index_oid,
1024 if (dd->is_index_key) {
1025 const table_definition_t *td = dd->table;
1026 int ret = snmp_agent_parse_oid_index_keys(td, index_oid);
1031 netsnmp_variable_list *key = td->index_list_cont;
1032 /* Searching index key */
1033 for (int pos = 0; pos < dd->index_key_pos; pos++)
1034 key = key->next_variable;
1036 requests->requestvb->type = td->index_keys[dd->index_key_pos].type;
1038 if (requests->requestvb->type == ASN_INTEGER)
1039 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1040 key->val.integer, sizeof(*key->val.integer));
1041 else /* OCTET_STR */
1042 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1043 (const u_char *)key->val.string,
1044 strlen((const char *)key->val.string));
1046 pthread_mutex_unlock(&g_agent->lock);
1048 return SNMP_ERR_NOERROR;
1051 char name[DATA_MAX_NAME_LEN];
1053 ret = snmp_agent_format_name(name, sizeof(name), dd, index_oid);
1057 DEBUG(PLUGIN_NAME ": Identifier '%s'", name);
1061 const data_set_t *ds = plugin_get_ds(dd->type);
1063 ERROR(PLUGIN_NAME ": Data set not found for '%s' type", dd->type);
1064 return SNMP_NOSUCHINSTANCE;
1067 ret = uc_get_value_by_name(name, &values, &values_num);
1070 ERROR(PLUGIN_NAME ": Failed to get value for '%s'", name);
1071 return SNMP_NOSUCHINSTANCE;
1074 assert(ds->ds_num == values_num);
1075 assert(oid_index < (int)values_num);
1077 char data[DATA_MAX_NAME_LEN];
1078 size_t data_len = sizeof(data);
1079 ret = snmp_agent_set_vardata(
1080 data, &data_len, dd->oids[oid_index].type, dd->scale, dd->shift,
1081 &values[oid_index], sizeof(values[oid_index]), ds->ds[oid_index].type);
1086 ERROR(PLUGIN_NAME ": Failed to convert '%s' value to snmp data", name);
1087 return SNMP_NOSUCHINSTANCE;
1090 requests->requestvb->type = dd->oids[oid_index].type;
1091 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1092 (const u_char *)data, data_len);
1094 return SNMP_ERR_NOERROR;
1098 snmp_agent_table_oid_handler(struct netsnmp_mib_handler_s *handler,
1099 struct netsnmp_handler_registration_s *reginfo,
1100 struct netsnmp_agent_request_info_s *reqinfo,
1101 struct netsnmp_request_info_s *requests) {
1103 if (reqinfo->mode != MODE_GET) {
1104 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
1105 return SNMP_ERR_NOERROR;
1108 pthread_mutex_lock(&g_agent->lock);
1110 oid_t oid; /* Requested OID */
1111 memcpy(oid.oid, requests->requestvb->name,
1112 sizeof(oid.oid[0]) * requests->requestvb->name_length);
1113 oid.oid_len = requests->requestvb->name_length;
1116 char oid_str[DATA_MAX_NAME_LEN];
1117 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
1118 DEBUG(PLUGIN_NAME ": Get request received for table OID '%s'", oid_str);
1120 oid_t index_oid; /* Index part of requested OID */
1122 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1123 table_definition_t *td = te->value;
1125 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1126 data_definition_t *dd = de->value;
1128 for (size_t i = 0; i < dd->oids_len; i++) {
1129 int ret = snmp_oid_ncompare(oid.oid, oid.oid_len, dd->oids[i].oid,
1130 dd->oids[i].oid_len,
1131 SNMP_MIN(oid.oid_len, dd->oids[i].oid_len));
1135 /* Calculating OID length for index part */
1136 index_oid.oid_len = oid.oid_len - dd->oids[i].oid_len;
1137 /* Fetching index part of the OID */
1138 memcpy(index_oid.oid, &oid.oid[dd->oids[i].oid_len],
1139 index_oid.oid_len * sizeof(*oid.oid));
1141 char index_str[DATA_MAX_NAME_LEN];
1142 snmp_agent_oid_to_string(index_str, sizeof(index_str), &index_oid);
1144 if (!td->index_oid.oid_len) {
1145 ret = c_avl_get(td->instance_index, &index_oid, NULL);
1149 assert(index_oid.oid_len == 1);
1150 ret = c_avl_get(td->index_instance, (int *)&index_oid.oid[0],
1151 (void **)&temp_oid);
1152 memcpy(&index_oid, temp_oid, sizeof(index_oid));
1156 INFO(PLUGIN_NAME ": Non-existing index (%s) requested", index_str);
1157 pthread_mutex_unlock(&g_agent->lock);
1158 return SNMP_NOSUCHINSTANCE;
1161 ret = snmp_agent_form_reply(requests, dd, &index_oid, i);
1162 pthread_mutex_unlock(&g_agent->lock);
1169 pthread_mutex_unlock(&g_agent->lock);
1171 return SNMP_NOSUCHINSTANCE;
1174 static int snmp_agent_table_index_oid_handler(
1175 struct netsnmp_mib_handler_s *handler,
1176 struct netsnmp_handler_registration_s *reginfo,
1177 struct netsnmp_agent_request_info_s *reqinfo,
1178 struct netsnmp_request_info_s *requests) {
1180 if (reqinfo->mode != MODE_GET) {
1181 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
1182 return SNMP_ERR_NOERROR;
1185 pthread_mutex_lock(&g_agent->lock);
1188 memcpy(oid.oid, requests->requestvb->name,
1189 sizeof(oid.oid[0]) * requests->requestvb->name_length);
1190 oid.oid_len = requests->requestvb->name_length;
1192 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1193 table_definition_t *td = te->value;
1195 if (td->index_oid.oid_len &&
1197 oid.oid, oid.oid_len, td->index_oid.oid, td->index_oid.oid_len,
1198 SNMP_MIN(oid.oid_len, td->index_oid.oid_len)) == 0)) {
1200 DEBUG(PLUGIN_NAME ": Handle '%s' table index OID", td->name);
1202 int index = oid.oid[oid.oid_len - 1];
1204 int ret = c_avl_get(td->index_instance, &index, NULL);
1206 /* nonexisting index requested */
1207 pthread_mutex_unlock(&g_agent->lock);
1208 return SNMP_NOSUCHINSTANCE;
1211 requests->requestvb->type = ASN_INTEGER;
1212 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1213 (const u_char *)&index, sizeof(index));
1215 pthread_mutex_unlock(&g_agent->lock);
1217 return SNMP_ERR_NOERROR;
1221 pthread_mutex_unlock(&g_agent->lock);
1223 return SNMP_NOSUCHINSTANCE;
1226 static int snmp_agent_table_size_oid_handler(
1227 struct netsnmp_mib_handler_s *handler,
1228 struct netsnmp_handler_registration_s *reginfo,
1229 struct netsnmp_agent_request_info_s *reqinfo,
1230 struct netsnmp_request_info_s *requests) {
1232 if (reqinfo->mode != MODE_GET) {
1233 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
1234 return SNMP_ERR_NOERROR;
1237 pthread_mutex_lock(&g_agent->lock);
1240 memcpy(oid.oid, requests->requestvb->name,
1241 sizeof(oid.oid[0]) * requests->requestvb->name_length);
1242 oid.oid_len = requests->requestvb->name_length;
1244 DEBUG(PLUGIN_NAME ": Get request received for table size OID");
1246 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1247 table_definition_t *td = te->value;
1249 if (td->size_oid.oid_len &&
1250 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->size_oid.oid,
1251 td->size_oid.oid_len,
1252 SNMP_MIN(oid.oid_len, td->size_oid.oid_len)) == 0)) {
1253 DEBUG(PLUGIN_NAME ": Handle '%s' table size OID", td->name);
1256 if (td->index_oid.oid_len)
1257 size = c_avl_size(td->index_instance);
1259 size = c_avl_size(td->instance_index);
1261 requests->requestvb->type = ASN_INTEGER;
1262 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
1263 (const u_char *)&size, sizeof(size));
1265 pthread_mutex_unlock(&g_agent->lock);
1267 return SNMP_ERR_NOERROR;
1271 pthread_mutex_unlock(&g_agent->lock);
1273 return SNMP_NOSUCHINSTANCE;
1277 snmp_agent_scalar_oid_handler(struct netsnmp_mib_handler_s *handler,
1278 struct netsnmp_handler_registration_s *reginfo,
1279 struct netsnmp_agent_request_info_s *reqinfo,
1280 struct netsnmp_request_info_s *requests) {
1282 if (reqinfo->mode != MODE_GET) {
1283 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
1284 return SNMP_ERR_NOERROR;
1287 pthread_mutex_lock(&g_agent->lock);
1290 memcpy(oid.oid, requests->requestvb->name,
1291 sizeof(oid.oid[0]) * requests->requestvb->name_length);
1292 oid.oid_len = requests->requestvb->name_length;
1295 char oid_str[DATA_MAX_NAME_LEN];
1296 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
1297 DEBUG(PLUGIN_NAME ": Get request received for scalar OID '%s'", oid_str);
1300 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL;
1302 data_definition_t *dd = de->value;
1304 for (size_t i = 0; i < dd->oids_len; i++) {
1306 int ret = snmp_oid_compare(oid.oid, oid.oid_len, dd->oids[i].oid,
1307 dd->oids[i].oid_len);
1311 ret = snmp_agent_form_reply(requests, dd, NULL, i);
1313 pthread_mutex_unlock(&g_agent->lock);
1319 pthread_mutex_unlock(&g_agent->lock);
1321 return SNMP_NOSUCHINSTANCE;
1324 static int snmp_agent_register_table_oids(void) {
1326 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1327 table_definition_t *td = te->value;
1329 if (td->size_oid.oid_len != 0) {
1331 snmp_agent_get_asn_type(td->size_oid.oid, td->size_oid.oid_len);
1332 td->size_oid.oid_len++;
1333 int ret = snmp_agent_register_oid(&td->size_oid,
1334 snmp_agent_table_size_oid_handler);
1339 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1340 data_definition_t *dd = de->value;
1342 for (size_t i = 0; i < dd->oids_len; i++) {
1344 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
1352 static int snmp_agent_register_scalar_oids(void) {
1354 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
1355 data_definition_t *dd = e->value;
1357 for (size_t i = 0; i < dd->oids_len; i++) {
1360 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
1363 snmp_agent_register_oid(&dd->oids[i], snmp_agent_scalar_oid_handler);
1372 static int snmp_agent_config_data_oids(data_definition_t *dd,
1373 oconfig_item_t *ci) {
1374 if (ci->values_num < 1) {
1375 WARNING(PLUGIN_NAME ": `OIDs' needs at least one argument");
1379 for (int i = 0; i < ci->values_num; i++)
1380 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
1381 WARNING(PLUGIN_NAME ": `OIDs' needs only string argument");
1385 if (dd->oids != NULL) {
1386 WARNING(PLUGIN_NAME ": OIDs can be configured only once for each data");
1391 dd->oids = calloc(ci->values_num, sizeof(*dd->oids));
1393 if (dd->oids == NULL)
1395 dd->oids_len = (size_t)ci->values_num;
1397 for (int i = 0; i < ci->values_num; i++) {
1398 dd->oids[i].oid_len = MAX_OID_LEN;
1400 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->oids[i].oid,
1401 &dd->oids[i].oid_len)) {
1402 ERROR(PLUGIN_NAME ": snmp_parse_oid (%s) failed",
1403 ci->values[i].value.string);
1413 static int snmp_agent_config_table_size_oid(table_definition_t *td,
1414 oconfig_item_t *ci) {
1415 if (ci->values_num < 1) {
1416 WARNING(PLUGIN_NAME ": `TableSizeOID' is empty");
1420 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
1421 WARNING(PLUGIN_NAME ": `TableSizeOID' needs only string argument");
1425 td->size_oid.oid_len = MAX_OID_LEN;
1427 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->size_oid.oid,
1428 &td->size_oid.oid_len)) {
1429 ERROR(PLUGIN_NAME ": Failed to parse table size OID (%s)",
1430 ci->values[0].value.string);
1431 td->size_oid.oid_len = 0;
1438 static int snmp_agent_config_table_index_oid(table_definition_t *td,
1439 oconfig_item_t *ci) {
1441 if (ci->values_num < 1) {
1442 WARNING(PLUGIN_NAME ": `IndexOID' is empty");
1446 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
1447 WARNING(PLUGIN_NAME ": `IndexOID' needs only string argument");
1451 td->index_oid.oid_len = MAX_OID_LEN;
1453 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->index_oid.oid,
1454 &td->index_oid.oid_len)) {
1455 ERROR(PLUGIN_NAME ": Failed to parse table index OID (%s)",
1456 ci->values[0].value.string);
1457 td->index_oid.oid_len = 0;
1464 /* Getting index key source that will represent table row */
1465 static int snmp_agent_config_index_key_source(table_definition_t *td,
1466 data_definition_t *dd,
1467 oconfig_item_t *ci) {
1470 int ret = cf_util_get_string(ci, &val);
1476 for (int i = 0; i < MAX_KEY_SOURCES; i++) {
1477 if (strcasecmp(index_opts[i], (const char *)val) == 0) {
1478 td->index_keys[td->index_keys_len].source = i;
1479 td->index_keys[td->index_keys_len].group = GROUP_UNUSED;
1480 td->index_keys[td->index_keys_len].regex = NULL;
1487 ERROR(PLUGIN_NAME ": Failed to parse index key source: '%s'", val);
1493 dd->index_key_pos = td->index_keys_len++;
1494 dd->is_index_key = 1;
1499 /* Getting format string used to parse values from index key source */
1500 static int snmp_agent_config_index_key_regex(table_definition_t *td,
1501 data_definition_t *dd,
1502 oconfig_item_t *ci) {
1503 index_key_t *index_key = &td->index_keys[dd->index_key_pos];
1505 int ret = cf_util_get_string(ci, &index_key->regex);
1509 ret = regcomp(&index_key->regex_info, index_key->regex, REG_EXTENDED);
1511 ERROR(PLUGIN_NAME ": Could not compile regex for %s", dd->name);
1515 index_key_src_t source = index_key->source;
1516 if (td->tokens[source] == NULL) {
1517 td->tokens[source] =
1518 c_avl_create((int (*)(const void *, const void *))num_compare);
1519 if (td->tokens[source] == NULL) {
1520 ERROR(PLUGIN_NAME ": Could not allocate memory for AVL tree");
1528 static int snmp_agent_config_index_key(table_definition_t *td,
1529 data_definition_t *dd,
1530 oconfig_item_t *ci) {
1533 for (int i = 0; (i < ci->children_num && ret == 0); i++) {
1534 oconfig_item_t *option = ci->children + i;
1536 if (strcasecmp("Source", option->key) == 0)
1537 ret = snmp_agent_config_index_key_source(td, dd, option);
1538 else if (strcasecmp("Regex", option->key) == 0)
1539 ret = snmp_agent_config_index_key_regex(td, dd, option);
1540 else if (strcasecmp("Group", option->key) == 0)
1541 ret = cf_util_get_int(option, &td->index_keys[dd->index_key_pos].group);
1547 /* This function parses configuration of both scalar and table column
1548 * because they have nearly the same structure */
1549 static int snmp_agent_config_table_column(table_definition_t *td,
1550 oconfig_item_t *ci) {
1551 data_definition_t *dd;
1553 oconfig_item_t *option_tmp = NULL;
1557 dd = calloc(1, sizeof(*dd));
1559 ERROR(PLUGIN_NAME ": Failed to allocate memory for table data definition");
1563 ret = cf_util_get_string(ci, &dd->name);
1571 /* NULL if it's a scalar */
1573 dd->is_index_key = 0;
1575 for (int i = 0; i < ci->children_num; i++) {
1576 oconfig_item_t *option = ci->children + i;
1578 /* First 3 options are reserved for table entry only */
1579 if (td != NULL && strcasecmp("IndexKey", option->key) == 0) {
1580 dd->is_index_key = 1;
1581 option_tmp = option;
1582 } else if (strcasecmp("Plugin", option->key) == 0)
1583 ret = cf_util_get_string(option, &dd->plugin);
1584 else if (strcasecmp("PluginInstance", option->key) == 0)
1585 ret = cf_util_get_string(option, &dd->plugin_instance);
1586 else if (strcasecmp("Type", option->key) == 0)
1587 ret = cf_util_get_string(option, &dd->type);
1588 else if (strcasecmp("TypeInstance", option->key) == 0)
1589 ret = cf_util_get_string(option, &dd->type_instance);
1590 else if (strcasecmp("Shift", option->key) == 0)
1591 ret = cf_util_get_double(option, &dd->shift);
1592 else if (strcasecmp("Scale", option->key) == 0)
1593 ret = cf_util_get_double(option, &dd->scale);
1594 else if (strcasecmp("OIDs", option->key) == 0)
1595 ret = snmp_agent_config_data_oids(dd, option);
1597 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
1602 snmp_agent_free_data(&dd);
1607 if (dd->is_index_key) {
1608 ret = snmp_agent_config_index_key(td, dd, option_tmp);
1609 td->index_keys[dd->index_key_pos].type =
1610 snmp_agent_get_asn_type(dd->oids[0].oid, dd->oids[0].oid_len);
1613 snmp_agent_free_data(&dd);
1618 llentry_t *entry = llentry_create(dd->name, dd);
1619 if (entry == NULL) {
1620 snmp_agent_free_data(&dd);
1624 /* Append to column list in parent table */
1626 llist_append(td->columns, entry);
1628 llentry_destroy(entry);
1633 /* Parses scalar configuration entry */
1634 static int snmp_agent_config_scalar(oconfig_item_t *ci) {
1635 return snmp_agent_config_table_column(NULL, ci);
1638 static int num_compare(const int *a, const int *b) {
1639 assert((a != NULL) && (b != NULL));
1648 static int oid_compare(const oid_t *a, const oid_t *b) {
1649 return snmp_oid_compare(a->oid, a->oid_len, b->oid, b->oid_len);
1652 static int snmp_agent_config_table(oconfig_item_t *ci) {
1653 table_definition_t *td;
1658 td = calloc(1, sizeof(*td));
1660 ERROR(PLUGIN_NAME ": Failed to allocate memory for table definition");
1664 ret = cf_util_get_string(ci, &td->name);
1670 td->columns = llist_create();
1671 if (td->columns == NULL) {
1672 ERROR(PLUGIN_NAME ": Failed to allocate memory for columns list");
1673 snmp_agent_free_table(&td);
1677 for (int i = 0; i < MAX_KEY_SOURCES; i++)
1678 td->tokens[i] = NULL;
1679 td->tokens_done = 0;
1681 for (int i = 0; i < ci->children_num; i++) {
1682 oconfig_item_t *option = ci->children + i;
1684 if (strcasecmp("IndexOID", option->key) == 0)
1685 ret = snmp_agent_config_table_index_oid(td, option);
1686 else if (strcasecmp("SizeOID", option->key) == 0)
1687 ret = snmp_agent_config_table_size_oid(td, option);
1688 else if (strcasecmp("Data", option->key) == 0)
1689 ret = snmp_agent_config_table_column(td, option);
1691 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
1696 snmp_agent_free_table(&td);
1701 /* Preparing index list container */
1702 ret = snmp_agent_prep_index_list(td, &td->index_list_cont);
1706 td->instance_index =
1707 c_avl_create((int (*)(const void *, const void *))oid_compare);
1708 if (td->instance_index == NULL) {
1709 snmp_agent_free_table(&td);
1713 td->index_instance =
1714 c_avl_create((int (*)(const void *, const void *))num_compare);
1715 if (td->index_instance == NULL) {
1716 snmp_agent_free_table(&td);
1721 c_avl_create((int (*)(const void *, const void *))oid_compare);
1722 if (td->instance_oids == NULL) {
1723 snmp_agent_free_table(&td);
1727 llentry_t *entry = llentry_create(td->name, td);
1728 if (entry == NULL) {
1729 snmp_agent_free_table(&td);
1733 llist_append(g_agent->tables, entry);
1738 static int snmp_agent_get_value_from_ds_type(const value_t *val, int type,
1739 double scale, double shift,
1742 case DS_TYPE_COUNTER:
1743 *value = (long)((val->counter * scale) + shift);
1745 case DS_TYPE_ABSOLUTE:
1746 *value = (long)((val->absolute * scale) + shift);
1748 case DS_TYPE_DERIVE:
1749 *value = (long)((val->derive * scale) + shift);
1752 *value = (long)((val->gauge * scale) + shift);
1757 ERROR(PLUGIN_NAME ": Unknown data source type: %i", type);
1764 static int snmp_agent_set_vardata(void *data, size_t *data_len, u_char asn_type,
1765 double scale, double shift, const void *value,
1766 size_t len, int type) {
1769 netsnmp_vardata var;
1774 var.string = (u_char *)data;
1776 ret = snmp_agent_get_value_from_ds_type(val, type, scale, shift, &new_value);
1786 if (*data_len < sizeof(*var.integer))
1788 *var.integer = new_value;
1789 *data_len = sizeof(*var.integer);
1792 if (*data_len < sizeof(*var.counter64))
1794 var.counter64->high = (u_long)((int64_t)new_value >> 32);
1795 var.counter64->low = (u_long)((int64_t)new_value & 0xFFFFFFFF);
1796 *data_len = sizeof(*var.counter64);
1799 if (type == DS_TYPE_GAUGE) {
1800 char buf[DATA_MAX_NAME_LEN];
1801 snprintf(buf, sizeof(buf), "%.2f", val->gauge);
1802 if (*data_len < strlen(buf))
1804 *data_len = strlen(buf);
1805 memcpy(var.string, buf, *data_len);
1807 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1813 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1821 static int snmp_agent_register_oid_index(oid_t *oid, int index,
1822 Netsnmp_Node_Handler *handler) {
1824 memcpy(&new_oid, oid, sizeof(*oid));
1825 new_oid.oid[new_oid.oid_len++] = index;
1826 return snmp_agent_register_oid(&new_oid, handler);
1829 static int snmp_agent_unregister_oid_index(oid_t *oid, int index) {
1831 memcpy(&new_oid, oid, sizeof(*oid));
1832 new_oid.oid[new_oid.oid_len++] = index;
1833 return snmp_agent_unregister_oid(&new_oid);
1836 static int snmp_agent_update_instance_oids(c_avl_tree_t *tree, oid_t *index_oid,
1838 int *oids_num; /* number of oids registered for instance */
1840 if (c_avl_get(tree, index_oid, (void **)&oids_num) == 0) {
1844 ERROR(PLUGIN_NAME ": Error updating index data");
1849 static int snmp_agent_update_index(data_definition_t *dd,
1850 table_definition_t *td, oid_t **index_oid) {
1854 _Bool free_index_oid = 1;
1856 if (c_avl_get(td->instance_index, (void *)*index_oid, (void **)&index) != 0) {
1857 /* Processing new instance */
1860 /* need to generate index for the table */
1861 if (td->index_oid.oid_len) {
1862 index = calloc(1, sizeof(*index));
1863 if (index == NULL) {
1865 goto free_index_oid;
1868 *index = c_avl_size(td->instance_index) + 1;
1870 ret = c_avl_insert(td->instance_index, *index_oid, index);
1874 ret = c_avl_insert(td->index_instance, index, *index_oid);
1876 DEBUG(PLUGIN_NAME ": Failed to update index_instance for '%s' table",
1878 goto remove_avl_index_oid;
1881 ret = snmp_agent_register_oid_index(&td->index_oid, *index,
1882 snmp_agent_table_index_oid_handler);
1884 goto remove_avl_index;
1886 /* instance as a key is required for any table */
1887 ret = c_avl_insert(td->instance_index, *index_oid, NULL);
1889 goto free_index_oid;
1892 value = calloc(1, sizeof(*value));
1894 if (value == NULL) {
1895 ERROR(PLUGIN_NAME ": Failed to allocate memory");
1897 goto unregister_index;
1900 ret = c_avl_insert(td->instance_oids, *index_oid, value);
1903 DEBUG(PLUGIN_NAME ": Failed to update instance_oids for '%s' table",
1908 int keys_processed = 0;
1910 /* Registering index keys OIDs */
1911 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1912 data_definition_t *idd = de->value;
1913 if (!idd->is_index_key)
1916 for (size_t i = 0; i < idd->oids_len; i++) {
1917 if (td->index_oid.oid_len)
1918 ret = snmp_agent_register_oid_index(&idd->oids[i], *index,
1919 snmp_agent_table_oid_handler);
1921 ret = snmp_agent_register_oid_string(&idd->oids[i], *index_oid,
1922 snmp_agent_table_oid_handler);
1925 ERROR(PLUGIN_NAME ": Could not register OID");
1930 if (++keys_processed >= td->index_keys_len)
1937 for (size_t i = 0; i < dd->oids_len; i++) {
1938 if (td->index_oid.oid_len)
1939 ret = snmp_agent_register_oid_index(&dd->oids[i], *index,
1940 snmp_agent_table_oid_handler);
1942 ret = snmp_agent_register_oid_string(&dd->oids[i], *index_oid,
1943 snmp_agent_table_oid_handler);
1947 else if (ret == OID_EXISTS)
1949 else if (snmp_agent_update_instance_oids(td->instance_oids, *index_oid, 1) <
1954 if (ret != OID_EXISTS) {
1955 char index_str[DATA_MAX_NAME_LEN];
1958 snmp_agent_oid_to_string(index_str, sizeof(index_str), *index_oid);
1960 snprintf(index_str, sizeof(index_str), "%d", *index);
1962 notification_t n = {
1963 .severity = NOTIF_OKAY, .time = cdtime(), .plugin = PLUGIN_NAME};
1964 sstrncpy(n.host, hostname_g, sizeof(n.host));
1965 snprintf(n.message, sizeof(n.message),
1966 "Data added to table %s with index %s", td->name, index_str);
1967 DEBUG(PLUGIN_NAME ": %s", n.message);
1969 plugin_dispatch_notification(&n);
1980 if (td->index_oid.oid_len)
1981 snmp_agent_unregister_oid_index(*index_oid, *index);
1983 if (td->index_oid.oid_len)
1984 c_avl_remove(td->index_instance, index, NULL, NULL);
1985 remove_avl_index_oid:
1986 c_avl_remove(td->instance_index, *index_oid, NULL, NULL);
1996 static int snmp_agent_write(value_list_t const *vl) {
2000 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
2001 table_definition_t *td = te->value;
2003 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
2004 data_definition_t *dd = de->value;
2006 if (!dd->is_index_key) {
2007 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
2008 vl->type_instance)) {
2009 oid_t *index_oid = calloc(1, sizeof(*index_oid));
2011 if (index_oid == NULL) {
2012 ERROR(PLUGIN_NAME ": Could not allocate memory for index_oid");
2016 int ret = snmp_agent_generate_index(td, vl, index_oid);
2019 ret = snmp_agent_update_index(dd, td, &index_oid);
2030 static int snmp_agent_collect(const data_set_t *ds, const value_list_t *vl,
2031 user_data_t __attribute__((unused)) * user_data) {
2033 pthread_mutex_lock(&g_agent->lock);
2035 snmp_agent_write(vl);
2037 pthread_mutex_unlock(&g_agent->lock);
2042 static int snmp_agent_preinit(void) {
2044 g_agent = calloc(1, sizeof(*g_agent));
2045 if (g_agent == NULL) {
2046 ERROR(PLUGIN_NAME ": Failed to allocate memory for snmp agent context");
2050 g_agent->tables = llist_create();
2051 g_agent->scalars = llist_create();
2052 g_agent->registered_oids =
2053 c_avl_create((int (*)(const void *, const void *))oid_compare);
2055 if (g_agent->tables == NULL || g_agent->scalars == NULL) {
2056 ERROR(PLUGIN_NAME ": llist_create() failed");
2057 llist_destroy(g_agent->scalars);
2058 llist_destroy(g_agent->tables);
2059 c_avl_destroy(g_agent->registered_oids);
2064 /* make us an agentx client. */
2065 err = netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE,
2068 ERROR(PLUGIN_NAME ": Failed to set agent role (%d)", err);
2069 llist_destroy(g_agent->scalars);
2070 llist_destroy(g_agent->tables);
2071 c_avl_destroy(g_agent->registered_oids);
2076 * For SNMP debug purposes uses snmp_set_do_debugging(1);
2079 /* initialize the agent library */
2080 err = init_agent(PLUGIN_NAME);
2082 ERROR(PLUGIN_NAME ": Failed to initialize the agent library (%d)", err);
2083 llist_destroy(g_agent->scalars);
2084 llist_destroy(g_agent->tables);
2085 c_avl_destroy(g_agent->registered_oids);
2089 init_snmp(PLUGIN_NAME);
2091 g_agent->tp = read_all_mibs();
2096 static int snmp_agent_init(void) {
2099 if (g_agent == NULL || ((llist_head(g_agent->scalars) == NULL) &&
2100 (llist_head(g_agent->tables) == NULL))) {
2101 ERROR(PLUGIN_NAME ": snmp_agent_init: plugin not configured");
2105 plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown);
2107 ret = snmp_agent_register_scalar_oids();
2111 ret = snmp_agent_register_table_oids();
2115 ret = pthread_mutex_init(&g_agent->lock, NULL);
2117 ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret);
2121 ret = pthread_mutex_init(&g_agent->agentx_lock, NULL);
2123 ERROR(PLUGIN_NAME ": Failed to initialize AgentX mutex, err %u", ret);
2127 /* create a second thread to listen for requests from AgentX*/
2128 ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
2130 ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
2134 if (llist_head(g_agent->tables) != NULL) {
2135 plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL);
2136 plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL);
2142 static void *snmp_agent_thread_run(void __attribute__((unused)) * arg) {
2143 INFO(PLUGIN_NAME ": Thread is up and running");
2146 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2148 pthread_mutex_lock(&g_agent->agentx_lock);
2149 agent_check_and_process(0); /* 0 == don't block */
2150 pthread_mutex_unlock(&g_agent->agentx_lock);
2152 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
2159 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler) {
2160 netsnmp_handler_registration *reg;
2162 if (c_avl_get(g_agent->registered_oids, (void *)oid, NULL) == 0)
2165 oid_t *new_oid = calloc(1, sizeof(*oid));
2167 if (new_oid == NULL) {
2168 ERROR(PLUGIN_NAME ": Could not allocate memory to register new OID");
2172 memcpy(new_oid, oid, sizeof(*oid));
2174 int ret = c_avl_insert(g_agent->registered_oids, (void *)new_oid, NULL);
2176 ERROR(PLUGIN_NAME ": Could not allocate memory to register new OID");
2182 char *oid_name = snmp_agent_get_oid_name(oid->oid, oid->oid_len - 1);
2183 char oid_str[DATA_MAX_NAME_LEN];
2185 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), oid);
2187 if (oid_name == NULL) {
2189 ": Skipped registration: OID (%s) is not found in main tree",
2194 reg = netsnmp_create_handler_registration(oid_name, handler, oid->oid,
2195 oid->oid_len, HANDLER_CAN_RONLY);
2197 ERROR(PLUGIN_NAME ": Failed to create handler registration for OID (%s)",
2202 pthread_mutex_lock(&g_agent->agentx_lock);
2204 if (netsnmp_register_instance(reg) != MIB_REGISTERED_OK) {
2205 ERROR(PLUGIN_NAME ": Failed to register handler for OID (%s)", oid_str);
2206 pthread_mutex_unlock(&g_agent->agentx_lock);
2210 pthread_mutex_unlock(&g_agent->agentx_lock);
2212 DEBUG(PLUGIN_NAME ": Registered handler for OID (%s)", oid_str);
2217 static int snmp_agent_free_config(void) {
2219 if (g_agent == NULL)
2222 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next)
2223 snmp_agent_free_table((table_definition_t **)&te->value);
2224 llist_destroy(g_agent->tables);
2226 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL; de = de->next)
2227 snmp_agent_free_data((data_definition_t **)&de->value);
2228 llist_destroy(g_agent->scalars);
2233 static int snmp_agent_shutdown(void) {
2236 DEBUG(PLUGIN_NAME ": snmp_agent_shutdown");
2238 if (g_agent == NULL) {
2239 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: plugin not initialized");
2243 if (pthread_cancel(g_agent->thread) != 0)
2244 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to cancel the thread");
2246 if (pthread_join(g_agent->thread, NULL) != 0)
2247 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to join the thread");
2249 snmp_agent_free_config();
2251 snmp_shutdown(PLUGIN_NAME);
2253 pthread_mutex_destroy(&g_agent->lock);
2254 pthread_mutex_destroy(&g_agent->agentx_lock);
2256 /* Freeing registered OIDs list */
2259 if (g_agent->registered_oids != NULL) {
2260 while (c_avl_pick(g_agent->registered_oids, &oid, NULL) == 0) {
2263 c_avl_destroy(g_agent->registered_oids);
2271 static int snmp_agent_config(oconfig_item_t *ci) {
2272 int ret = snmp_agent_preinit();
2279 for (int i = 0; i < ci->children_num; i++) {
2280 oconfig_item_t *child = ci->children + i;
2281 if (strcasecmp("Data", child->key) == 0) {
2282 ret = snmp_agent_config_scalar(child);
2283 } else if (strcasecmp("Table", child->key) == 0) {
2284 ret = snmp_agent_config_table(child);
2286 ERROR(PLUGIN_NAME ": Unknown configuration option `%s'", child->key);
2291 ERROR(PLUGIN_NAME ": Failed to parse configuration");
2292 snmp_agent_free_config();
2293 snmp_shutdown(PLUGIN_NAME);
2299 ret = snmp_agent_validate_config();
2301 ERROR(PLUGIN_NAME ": Invalid configuration provided");
2302 snmp_agent_free_config();
2303 snmp_shutdown(PLUGIN_NAME);
2311 void module_register(void) {
2312 plugin_register_init(PLUGIN_NAME, snmp_agent_init);
2313 plugin_register_complex_config(PLUGIN_NAME, snmp_agent_config);