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>
32 #include "utils_avltree.h"
33 #include "utils_cache.h"
34 #include "utils_llist.h"
36 #include <net-snmp/net-snmp-config.h>
38 #include <net-snmp/net-snmp-includes.h>
40 #include <net-snmp/agent/net-snmp-agent-includes.h>
42 #define PLUGIN_NAME "snmp_agent"
43 #define ERR_BUF_SIZE 1024
44 #define TYPE_STRING -1
51 typedef struct oid_s oid_t;
53 struct table_definition_s {
58 c_avl_tree_t *instance_index;
59 c_avl_tree_t *index_instance;
61 typedef struct table_definition_s table_definition_t;
63 struct data_definition_s {
66 char *plugin_instance;
69 const table_definition_t *table;
76 typedef struct data_definition_s data_definition_t;
78 struct snmp_agent_ctx_s {
81 pthread_mutex_t agentx_lock;
87 typedef struct snmp_agent_ctx_s snmp_agent_ctx_t;
89 static snmp_agent_ctx_t *g_agent;
91 #define CHECK_DD_TYPE(_dd, _p, _pi, _t, _ti) \
92 (_dd->plugin ? !strcmp(_dd->plugin, _p) : 0) && \
93 (_dd->plugin_instance ? !strcmp(_dd->plugin_instance, _pi) : 1) && \
94 (_dd->type ? !strcmp(_dd->type, _t) : 0) && \
95 (_dd->type_instance ? !strcmp(_dd->type_instance, _ti) : 1)
97 static int snmp_agent_shutdown(void);
98 static void *snmp_agent_thread_run(void *arg);
99 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler);
100 static int snmp_agent_set_vardata(void *dst_buf, size_t *dst_buf_len,
101 u_char asn_type, double scale, double shift,
102 const void *value, size_t len, int type);
103 static int snmp_agent_unregister_oid_index(oid_t *oid, int index);
105 static u_char snmp_agent_get_asn_type(oid *oid, size_t oid_len) {
106 struct tree *node = get_tree(oid, oid_len, g_agent->tp);
108 return (node != NULL) ? mib_to_asn_type(node->type) : 0;
111 static char *snmp_agent_get_oid_name(oid *oid, size_t oid_len) {
112 struct tree *node = get_tree(oid, oid_len, g_agent->tp);
114 return (node != NULL) ? node->label : NULL;
117 static int snmp_agent_oid_to_string(char *buf, size_t buf_size,
119 char oid_str[MAX_OID_LEN][16];
120 char *oid_str_ptr[MAX_OID_LEN];
122 for (size_t i = 0; i < o->oid_len; i++) {
123 snprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
124 oid_str_ptr[i] = oid_str[i];
127 return strjoin(buf, buf_size, oid_str_ptr, o->oid_len, ".");
130 /* Prints a configuration storing list. It handles both table columns list
133 static void snmp_agent_dump_data(llist_t *list) {
134 char oid_str[DATA_MAX_NAME_LEN];
135 for (llentry_t *de = llist_head(list); de != NULL; de = de->next) {
136 data_definition_t *dd = de->value;
138 if (dd->table != NULL)
139 DEBUG(PLUGIN_NAME ": Column:");
141 DEBUG(PLUGIN_NAME ": Scalar:");
143 DEBUG(PLUGIN_NAME ": Name: %s", dd->name);
145 DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin);
146 if (dd->plugin_instance)
147 DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance);
149 DEBUG(PLUGIN_NAME ": Instance: true");
151 DEBUG(PLUGIN_NAME ": Type: %s", dd->type);
152 if (dd->type_instance)
153 DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance);
154 for (size_t i = 0; i < dd->oids_len; i++) {
155 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &dd->oids[i]);
156 DEBUG(PLUGIN_NAME ": OID[%" PRIsz "]: %s", i, oid_str);
158 DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale);
159 DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift);
163 /* Prints parsed configuration */
164 static void snmp_agent_dump_config(void) {
165 char oid_str[DATA_MAX_NAME_LEN];
167 /* Printing tables */
168 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
169 table_definition_t *td = te->value;
171 DEBUG(PLUGIN_NAME ": Table:");
172 DEBUG(PLUGIN_NAME ": Name: %s", td->name);
173 if (td->index_oid.oid_len != 0) {
174 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &td->index_oid);
175 DEBUG(PLUGIN_NAME ": IndexOID: %s", oid_str);
177 if (td->size_oid.oid_len != 0) {
178 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &td->size_oid);
179 DEBUG(PLUGIN_NAME ": SizeOID: %s", oid_str);
182 snmp_agent_dump_data(td->columns);
185 /* Printing scalars */
186 snmp_agent_dump_data(g_agent->scalars);
188 #endif /* COLLECT_DEBUG */
190 static int snmp_agent_validate_config(void) {
193 snmp_agent_dump_config();
196 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
197 table_definition_t *td = te->value;
199 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
200 data_definition_t *dd = de->value;
203 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'.'%s'", td->name,
208 if (dd->plugin_instance) {
209 ERROR(PLUGIN_NAME ": PluginInstance should not be defined for table "
210 "data type '%s'.'%s'",
215 if (dd->oids_len == 0) {
216 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'.'%s'", td->name,
221 if (dd->is_instance) {
223 if (dd->type || dd->type_instance) {
224 ERROR(PLUGIN_NAME ": Type and TypeInstance are not valid for "
225 "instance data '%s'.'%s'",
230 if (dd->oids_len > 1) {
233 ": Only one OID should be specified for instance data '%s'.'%s'",
240 ERROR(PLUGIN_NAME ": Type not defined for data '%s'.'%s'", td->name,
248 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
249 data_definition_t *dd = e->value;
252 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'", dd->name);
256 if (dd->oids_len == 0) {
257 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'", dd->name);
261 if (dd->is_instance) {
263 ": Instance flag can't be specified for scalar data '%s'",
269 ERROR(PLUGIN_NAME ": Type not defined for data '%s'", dd->name);
277 static void snmp_agent_generate_oid2string(oid_t *oid, size_t offset,
279 size_t key_len = oid->oid[offset];
282 for (i = 0; i < key_len && offset < oid->oid_len; i++)
283 key[i] = oid->oid[++offset];
288 static int snmp_agent_generate_string2oid(oid_t *oid, const char *key) {
289 size_t key_len = strlen(key);
291 oid->oid[oid->oid_len++] = key_len;
292 for (size_t i = 0; i < key_len; i++) {
293 oid->oid[oid->oid_len++] = key[i];
294 if (oid->oid_len >= MAX_OID_LEN) {
295 ERROR(PLUGIN_NAME ": Conversion key string %s to OID failed", key);
303 static int snmp_agent_register_oid_string(oid_t *oid, const char *key,
304 Netsnmp_Node_Handler *handler) {
307 memcpy(&new_oid, oid, sizeof(*oid));
308 int ret = snmp_agent_generate_string2oid(&new_oid, key);
312 return snmp_agent_register_oid(&new_oid, handler);
315 static int snmp_agent_unregister_oid_string(oid_t *oid, const char *key) {
318 memcpy(&new_oid, oid, sizeof(*oid));
319 int ret = snmp_agent_generate_string2oid(&new_oid, key);
323 return unregister_mib(new_oid.oid, new_oid.oid_len);
326 static int snmp_agent_table_row_remove(table_definition_t *td,
327 const char *instance) {
331 if (td->index_oid.oid_len) {
332 if ((c_avl_get(td->instance_index, instance, (void **)&index) != 0) ||
333 (c_avl_get(td->index_instance, index, (void **)&ins) != 0))
336 if (c_avl_get(td->instance_index, instance, (void **)&ins) != 0)
340 pthread_mutex_lock(&g_agent->agentx_lock);
342 if (td->index_oid.oid_len)
343 snmp_agent_unregister_oid_index(&td->index_oid, *index);
345 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
346 data_definition_t *dd = de->value;
348 for (size_t i = 0; i < dd->oids_len; i++)
349 if (td->index_oid.oid_len)
350 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
352 snmp_agent_unregister_oid_string(&dd->oids[i], ins);
355 pthread_mutex_unlock(&g_agent->agentx_lock);
357 DEBUG(PLUGIN_NAME ": Removed row for '%s' table [%d, %s]", td->name,
358 (index != NULL) ? *index : -1, ins);
361 .severity = NOTIF_WARNING, .time = cdtime(), .plugin = PLUGIN_NAME};
362 sstrncpy(n.host, hostname_g, sizeof(n.host));
363 sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance));
364 snprintf(n.message, sizeof(n.message),
365 "Removed data row from table %s instance %s index %d", td->name, ins,
366 (index != NULL) ? *index : -1);
367 plugin_dispatch_notification(&n);
369 if (td->index_oid.oid_len) {
370 c_avl_remove(td->index_instance, index, NULL, (void **)&ins);
371 c_avl_remove(td->instance_index, instance, NULL, (void **)&index);
375 c_avl_remove(td->instance_index, instance, NULL, (void **)&ins);
382 static int snmp_agent_clear_missing(const value_list_t *vl,
383 __attribute__((unused)) user_data_t *ud) {
387 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
388 table_definition_t *td = te->value;
390 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
391 data_definition_t *dd = de->value;
393 if (!dd->is_instance) {
394 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
395 vl->type_instance)) {
396 snmp_agent_table_row_remove(td, vl->plugin_instance);
406 static void snmp_agent_free_data(data_definition_t **dd) {
408 if (dd == NULL || *dd == NULL)
411 /* unregister scalar type OID */
412 if ((*dd)->table == NULL) {
413 for (size_t i = 0; i < (*dd)->oids_len; i++)
414 unregister_mib((*dd)->oids[i].oid, (*dd)->oids[i].oid_len);
418 sfree((*dd)->plugin);
419 sfree((*dd)->plugin_instance);
421 sfree((*dd)->type_instance);
429 static void snmp_agent_free_table_columns(table_definition_t *td) {
430 if (td->columns == NULL)
433 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
434 data_definition_t *dd = de->value;
436 if (td->index_oid.oid_len) {
440 c_avl_iterator_t *iter = c_avl_get_iterator(td->index_instance);
441 while (c_avl_iterator_next(iter, (void *)&index, (void *)&instance) ==
443 for (size_t i = 0; i < dd->oids_len; i++)
444 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
446 c_avl_iterator_destroy(iter);
450 c_avl_iterator_t *iter = c_avl_get_iterator(dd->table->instance_index);
451 while (c_avl_iterator_next(iter, (void *)&instance, (void *)&instance) ==
453 for (size_t i = 0; i < dd->oids_len; i++)
454 snmp_agent_unregister_oid_string(&dd->oids[i], instance);
456 c_avl_iterator_destroy(iter);
459 snmp_agent_free_data(&dd);
462 llist_destroy(td->columns);
464 } /* void snmp_agent_free_table_columns */
466 static void snmp_agent_free_table(table_definition_t **td) {
468 if (td == NULL || *td == NULL)
471 if ((*td)->size_oid.oid_len)
472 unregister_mib((*td)->size_oid.oid, (*td)->size_oid.oid_len);
474 /* Unregister Index OIDs */
475 if ((*td)->index_oid.oid_len) {
479 c_avl_iterator_t *iter = c_avl_get_iterator((*td)->index_instance);
480 while (c_avl_iterator_next(iter, (void *)&index, (void *)&instance) == 0)
481 snmp_agent_unregister_oid_index(&(*td)->index_oid, *index);
483 c_avl_iterator_destroy(iter);
486 /* Unregister all table columns and their registered OIDs */
487 snmp_agent_free_table_columns(*td);
492 /* index_instance and instance_index contain the same pointers */
493 c_avl_destroy((*td)->index_instance);
494 (*td)->index_instance = NULL;
496 if ((*td)->instance_index != NULL) {
497 while (c_avl_pick((*td)->instance_index, &key, &value) == 0) {
502 c_avl_destroy((*td)->instance_index);
503 (*td)->instance_index = NULL;
512 static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests,
513 data_definition_t *dd, char *instance,
515 char name[DATA_MAX_NAME_LEN];
516 format_name(name, sizeof(name), hostname_g, dd->plugin,
517 instance ? instance : dd->plugin_instance, dd->type,
519 DEBUG(PLUGIN_NAME ": Identifier '%s'", name);
523 const data_set_t *ds = plugin_get_ds(dd->type);
525 ERROR(PLUGIN_NAME ": Data set not found for '%s' type", dd->type);
526 return SNMP_NOSUCHINSTANCE;
529 int ret = uc_get_value_by_name(name, &values, &values_num);
532 ERROR(PLUGIN_NAME ": Failed to get value for '%s'", name);
533 return SNMP_NOSUCHINSTANCE;
536 assert(ds->ds_num == values_num);
537 assert(oid_index < (int)values_num);
539 char data[DATA_MAX_NAME_LEN];
540 size_t data_len = sizeof(data);
541 ret = snmp_agent_set_vardata(
542 data, &data_len, dd->oids[oid_index].type, dd->scale, dd->shift,
543 &values[oid_index], sizeof(values[oid_index]), ds->ds[oid_index].type);
548 ERROR(PLUGIN_NAME ": Failed to convert '%s' value to snmp data", name);
549 return SNMP_NOSUCHINSTANCE;
552 requests->requestvb->type = dd->oids[oid_index].type;
553 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
554 (const u_char *)data, data_len);
556 return SNMP_ERR_NOERROR;
560 snmp_agent_table_oid_handler(struct netsnmp_mib_handler_s *handler,
561 struct netsnmp_handler_registration_s *reginfo,
562 struct netsnmp_agent_request_info_s *reqinfo,
563 struct netsnmp_request_info_s *requests) {
565 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
566 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
567 return SNMP_ERR_NOERROR;
570 pthread_mutex_lock(&g_agent->lock);
573 memcpy(oid.oid, requests->requestvb->name,
574 sizeof(oid.oid[0]) * requests->requestvb->name_length);
575 oid.oid_len = requests->requestvb->name_length;
578 char oid_str[DATA_MAX_NAME_LEN];
579 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
580 DEBUG(PLUGIN_NAME ": Get request received for table OID '%s'", oid_str);
583 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
584 table_definition_t *td = te->value;
586 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
587 data_definition_t *dd = de->value;
589 for (size_t i = 0; i < dd->oids_len; i++) {
590 int ret = snmp_oid_ncompare(oid.oid, oid.oid_len, dd->oids[i].oid,
592 SNMP_MIN(oid.oid_len, dd->oids[i].oid_len));
598 if (!td->index_oid.oid_len) {
599 char key[MAX_OID_LEN];
601 memset(key, 0, sizeof(key));
602 snmp_agent_generate_oid2string(
603 &oid, SNMP_MIN(oid.oid_len, dd->oids[i].oid_len), key);
605 ret = c_avl_get(td->instance_index, key, (void **)&instance);
607 DEBUG(PLUGIN_NAME ": Nonexisting index string '%s' requested", key);
608 pthread_mutex_unlock(&g_agent->lock);
609 return SNMP_NOSUCHINSTANCE;
612 int index = oid.oid[oid.oid_len - 1];
614 ret = c_avl_get(td->index_instance, &index, (void **)&instance);
616 DEBUG(PLUGIN_NAME ": Nonexisting index '%d' requested", index);
617 pthread_mutex_unlock(&g_agent->lock);
618 return SNMP_NOSUCHINSTANCE;
622 if (dd->is_instance) {
623 requests->requestvb->type = ASN_OCTET_STR;
624 snmp_set_var_typed_value(
625 requests->requestvb, requests->requestvb->type,
626 (const u_char *)instance, strlen((instance)));
628 pthread_mutex_unlock(&g_agent->lock);
630 return SNMP_ERR_NOERROR;
633 ret = snmp_agent_form_reply(requests, dd, instance, i);
635 pthread_mutex_unlock(&g_agent->lock);
642 pthread_mutex_unlock(&g_agent->lock);
644 return SNMP_NOSUCHINSTANCE;
647 static int snmp_agent_table_index_oid_handler(
648 struct netsnmp_mib_handler_s *handler,
649 struct netsnmp_handler_registration_s *reginfo,
650 struct netsnmp_agent_request_info_s *reqinfo,
651 struct netsnmp_request_info_s *requests) {
653 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
654 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
655 return SNMP_ERR_NOERROR;
658 pthread_mutex_lock(&g_agent->lock);
661 memcpy(oid.oid, requests->requestvb->name,
662 sizeof(oid.oid[0]) * requests->requestvb->name_length);
663 oid.oid_len = requests->requestvb->name_length;
665 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
666 table_definition_t *td = te->value;
668 if (td->index_oid.oid_len &&
670 oid.oid, oid.oid_len, td->index_oid.oid, td->index_oid.oid_len,
671 SNMP_MIN(oid.oid_len, td->index_oid.oid_len)) == 0)) {
673 DEBUG(PLUGIN_NAME ": Handle '%s' table index OID", td->name);
675 int index = oid.oid[oid.oid_len - 1];
677 int ret = c_avl_get(td->index_instance, &index, &(void *){NULL});
679 /* nonexisting index requested */
680 pthread_mutex_unlock(&g_agent->lock);
681 return SNMP_NOSUCHINSTANCE;
684 requests->requestvb->type = ASN_INTEGER;
685 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
686 (const u_char *)&index, sizeof(index));
688 pthread_mutex_unlock(&g_agent->lock);
690 return SNMP_ERR_NOERROR;
694 pthread_mutex_unlock(&g_agent->lock);
696 return SNMP_NOSUCHINSTANCE;
699 static int snmp_agent_table_size_oid_handler(
700 struct netsnmp_mib_handler_s *handler,
701 struct netsnmp_handler_registration_s *reginfo,
702 struct netsnmp_agent_request_info_s *reqinfo,
703 struct netsnmp_request_info_s *requests) {
705 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
706 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
707 return SNMP_ERR_NOERROR;
710 pthread_mutex_lock(&g_agent->lock);
713 memcpy(oid.oid, requests->requestvb->name,
714 sizeof(oid.oid[0]) * requests->requestvb->name_length);
715 oid.oid_len = requests->requestvb->name_length;
717 DEBUG(PLUGIN_NAME ": Get request received for table size OID");
719 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
720 table_definition_t *td = te->value;
722 if (td->size_oid.oid_len &&
723 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->size_oid.oid,
724 td->size_oid.oid_len,
725 SNMP_MIN(oid.oid_len, td->size_oid.oid_len)) == 0)) {
726 DEBUG(PLUGIN_NAME ": Handle '%s' table size OID", td->name);
728 long size = c_avl_size(td->index_instance);
730 requests->requestvb->type = ASN_INTEGER;
731 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
732 (const u_char *)&size, sizeof(size));
734 pthread_mutex_unlock(&g_agent->lock);
736 return SNMP_ERR_NOERROR;
740 pthread_mutex_unlock(&g_agent->lock);
742 return SNMP_NOSUCHINSTANCE;
746 snmp_agent_scalar_oid_handler(struct netsnmp_mib_handler_s *handler,
747 struct netsnmp_handler_registration_s *reginfo,
748 struct netsnmp_agent_request_info_s *reqinfo,
749 struct netsnmp_request_info_s *requests) {
751 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
752 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
753 return SNMP_ERR_NOERROR;
756 pthread_mutex_lock(&g_agent->lock);
759 memcpy(oid.oid, requests->requestvb->name,
760 sizeof(oid.oid[0]) * requests->requestvb->name_length);
761 oid.oid_len = requests->requestvb->name_length;
764 char oid_str[DATA_MAX_NAME_LEN];
765 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
766 DEBUG(PLUGIN_NAME ": Get request received for scalar OID '%s'", oid_str);
769 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL;
771 data_definition_t *dd = de->value;
773 for (size_t i = 0; i < dd->oids_len; i++) {
775 int ret = snmp_oid_compare(oid.oid, oid.oid_len, dd->oids[i].oid,
776 dd->oids[i].oid_len);
780 ret = snmp_agent_form_reply(requests, dd, NULL, i);
782 pthread_mutex_unlock(&g_agent->lock);
788 pthread_mutex_unlock(&g_agent->lock);
790 return SNMP_NOSUCHINSTANCE;
793 static int snmp_agent_register_table_oids(void) {
795 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
796 table_definition_t *td = te->value;
798 if (td->size_oid.oid_len != 0) {
800 snmp_agent_get_asn_type(td->size_oid.oid, td->size_oid.oid_len);
801 td->size_oid.oid_len++;
802 int ret = snmp_agent_register_oid(&td->size_oid,
803 snmp_agent_table_size_oid_handler);
808 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
809 data_definition_t *dd = de->value;
811 for (size_t i = 0; i < dd->oids_len; i++) {
813 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
821 static int snmp_agent_register_scalar_oids(void) {
823 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
824 data_definition_t *dd = e->value;
826 for (size_t i = 0; i < dd->oids_len; i++) {
829 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
832 snmp_agent_register_oid(&dd->oids[i], snmp_agent_scalar_oid_handler);
841 static int snmp_agent_config_data_oids(data_definition_t *dd,
842 oconfig_item_t *ci) {
843 if (ci->values_num < 1) {
844 WARNING(PLUGIN_NAME ": `OIDs' needs at least one argument");
848 for (int i = 0; i < ci->values_num; i++)
849 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
850 WARNING(PLUGIN_NAME ": `OIDs' needs only string argument");
854 if (dd->oids != NULL)
857 dd->oids = calloc(ci->values_num, sizeof(*dd->oids));
858 if (dd->oids == NULL)
860 dd->oids_len = (size_t)ci->values_num;
862 for (int i = 0; i < ci->values_num; i++) {
863 dd->oids[i].oid_len = MAX_OID_LEN;
865 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->oids[i].oid,
866 &dd->oids[i].oid_len)) {
867 ERROR(PLUGIN_NAME ": snmp_parse_oid (%s) failed",
868 ci->values[i].value.string);
878 static int snmp_agent_config_table_size_oid(table_definition_t *td,
879 oconfig_item_t *ci) {
880 if (ci->values_num < 1) {
881 WARNING(PLUGIN_NAME ": `TableSizeOID' is empty");
885 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
886 WARNING(PLUGIN_NAME ": `TableSizeOID' needs only string argument");
890 td->size_oid.oid_len = MAX_OID_LEN;
892 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->size_oid.oid,
893 &td->size_oid.oid_len)) {
894 ERROR(PLUGIN_NAME ": Failed to parse table size OID (%s)",
895 ci->values[0].value.string);
896 td->size_oid.oid_len = 0;
903 static int snmp_agent_config_table_index_oid(table_definition_t *td,
904 oconfig_item_t *ci) {
906 if (ci->values_num < 1) {
907 WARNING(PLUGIN_NAME ": `IndexOID' is empty");
911 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
912 WARNING(PLUGIN_NAME ": `IndexOID' needs only string argument");
916 td->index_oid.oid_len = MAX_OID_LEN;
918 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->index_oid.oid,
919 &td->index_oid.oid_len)) {
920 ERROR(PLUGIN_NAME ": Failed to parse table index OID (%s)",
921 ci->values[0].value.string);
922 td->index_oid.oid_len = 0;
929 /* This function parses configuration of both scalar and table column
930 * because they have nearly the same structure */
931 static int snmp_agent_config_table_column(table_definition_t *td,
932 oconfig_item_t *ci) {
933 data_definition_t *dd;
938 dd = calloc(1, sizeof(*dd));
940 ERROR(PLUGIN_NAME ": Failed to allocate memory for table data definition");
944 ret = cf_util_get_string(ci, &dd->name);
952 /* NULL if it's a scalar */
955 for (int i = 0; i < ci->children_num; i++) {
956 oconfig_item_t *option = ci->children + i;
958 /* Instance option is reserved for table entry only */
959 if (strcasecmp("Instance", option->key) == 0 && td != NULL)
960 ret = cf_util_get_boolean(option, &dd->is_instance);
961 else if (strcasecmp("Plugin", option->key) == 0)
962 ret = cf_util_get_string(option, &dd->plugin);
963 else if (strcasecmp("PluginInstance", option->key) == 0)
964 ret = cf_util_get_string(option, &dd->plugin_instance);
965 else if (strcasecmp("Type", option->key) == 0)
966 ret = cf_util_get_string(option, &dd->type);
967 else if (strcasecmp("TypeInstance", option->key) == 0)
968 ret = cf_util_get_string(option, &dd->type_instance);
969 else if (strcasecmp("Shift", option->key) == 0)
970 ret = cf_util_get_double(option, &dd->shift);
971 else if (strcasecmp("Scale", option->key) == 0)
972 ret = cf_util_get_double(option, &dd->scale);
973 else if (strcasecmp("OIDs", option->key) == 0)
974 ret = snmp_agent_config_data_oids(dd, option);
976 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
981 snmp_agent_free_data(&dd);
986 llentry_t *entry = llentry_create(dd->name, dd);
988 snmp_agent_free_data(&dd);
992 /* Append to column list in parent table */
994 llist_append(td->columns, entry);
999 /* Parses scalar configuration entry */
1000 static int snmp_agent_config_scalar(oconfig_item_t *ci) {
1001 return snmp_agent_config_table_column(NULL, ci);
1004 static int num_compare(const int *a, const int *b) {
1005 assert((a != NULL) && (b != NULL));
1014 static int snmp_agent_config_table(oconfig_item_t *ci) {
1015 table_definition_t *td;
1020 td = calloc(1, sizeof(*td));
1022 ERROR(PLUGIN_NAME ": Failed to allocate memory for table definition");
1026 ret = cf_util_get_string(ci, &td->name);
1032 td->columns = llist_create();
1033 if (td->columns == NULL) {
1034 ERROR(PLUGIN_NAME ": Failed to allocate memory for columns list");
1035 snmp_agent_free_table(&td);
1039 for (int i = 0; i < ci->children_num; i++) {
1040 oconfig_item_t *option = ci->children + i;
1042 if (strcasecmp("IndexOID", option->key) == 0)
1043 ret = snmp_agent_config_table_index_oid(td, option);
1044 else if (strcasecmp("SizeOID", option->key) == 0)
1045 ret = snmp_agent_config_table_size_oid(td, option);
1046 else if (strcasecmp("Data", option->key) == 0)
1047 ret = snmp_agent_config_table_column(td, option);
1049 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
1054 snmp_agent_free_table(&td);
1059 td->instance_index =
1060 c_avl_create((int (*)(const void *, const void *))strcmp);
1061 if (td->instance_index == NULL) {
1062 snmp_agent_free_table(&td);
1066 td->index_instance =
1067 c_avl_create((int (*)(const void *, const void *))num_compare);
1068 if (td->index_instance == NULL) {
1069 snmp_agent_free_table(&td);
1073 llentry_t *entry = llentry_create(td->name, td);
1074 if (entry == NULL) {
1075 snmp_agent_free_table(&td);
1078 llist_append(g_agent->tables, entry);
1083 static int snmp_agent_get_value_from_ds_type(const value_t *val, int type,
1084 double scale, double shift,
1087 case DS_TYPE_COUNTER:
1088 *value = (long)((val->counter * scale) + shift);
1090 case DS_TYPE_ABSOLUTE:
1091 *value = (long)((val->absolute * scale) + shift);
1093 case DS_TYPE_DERIVE:
1094 *value = (long)((val->derive * scale) + shift);
1097 *value = (long)((val->gauge * scale) + shift);
1102 ERROR(PLUGIN_NAME ": Unknown data source type: %i", type);
1109 static int snmp_agent_set_vardata(void *data, size_t *data_len, u_char asn_type,
1110 double scale, double shift, const void *value,
1111 size_t len, int type) {
1114 netsnmp_vardata var;
1119 var.string = (u_char *)data;
1121 ret = snmp_agent_get_value_from_ds_type(val, type, scale, shift, &new_value);
1131 if (*data_len < sizeof(*var.integer))
1133 *var.integer = new_value;
1134 *data_len = sizeof(*var.integer);
1137 if (*data_len < sizeof(*var.counter64))
1139 var.counter64->high = (u_long)((int64_t)new_value >> 32);
1140 var.counter64->low = (u_long)((int64_t)new_value & 0xFFFFFFFF);
1141 *data_len = sizeof(*var.counter64);
1144 if (type == DS_TYPE_GAUGE) {
1145 char buf[DATA_MAX_NAME_LEN];
1146 snprintf(buf, sizeof(buf), "%.2f", val->gauge);
1147 if (*data_len < strlen(buf))
1149 *data_len = strlen(buf);
1150 memcpy(var.string, buf, *data_len);
1152 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1158 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1166 static int snmp_agent_register_oid_index(oid_t *oid, int index,
1167 Netsnmp_Node_Handler *handler) {
1169 memcpy(&new_oid, oid, sizeof(*oid));
1170 new_oid.oid[new_oid.oid_len++] = index;
1171 return snmp_agent_register_oid(&new_oid, handler);
1174 static int snmp_agent_unregister_oid_index(oid_t *oid, int index) {
1176 memcpy(&new_oid, oid, sizeof(*oid));
1177 new_oid.oid[new_oid.oid_len++] = index;
1178 return unregister_mib(new_oid.oid, new_oid.oid_len);
1181 static int snmp_agent_update_index(table_definition_t *td,
1182 const char *instance) {
1184 if (c_avl_get(td->instance_index, instance, NULL) == 0)
1191 ins = strdup(instance);
1195 /* need to generate index for the table */
1196 if (td->index_oid.oid_len) {
1197 index = calloc(1, sizeof(*index));
1198 if (index == NULL) {
1203 *index = c_avl_size(td->instance_index) + 1;
1205 ret = c_avl_insert(td->instance_index, ins, index);
1212 ret = c_avl_insert(td->index_instance, index, ins);
1214 DEBUG(PLUGIN_NAME ": Failed to update index_instance for '%s' table",
1216 c_avl_remove(td->instance_index, ins, NULL, (void **)&index);
1222 ret = snmp_agent_register_oid_index(&td->index_oid, *index,
1223 snmp_agent_table_index_oid_handler);
1227 /* instance as a key is required for any table */
1228 ret = c_avl_insert(td->instance_index, ins, ins);
1235 /* register new oids for all columns */
1236 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1237 data_definition_t *dd = de->value;
1239 for (size_t i = 0; i < dd->oids_len; i++) {
1240 if (td->index_oid.oid_len) {
1241 ret = snmp_agent_register_oid_index(&dd->oids[i], *index,
1242 snmp_agent_table_oid_handler);
1244 ret = snmp_agent_register_oid_string(&dd->oids[i], ins,
1245 snmp_agent_table_oid_handler);
1253 DEBUG(PLUGIN_NAME ": Updated index for '%s' table [%d, %s]", td->name,
1254 (index != NULL) ? *index : -1, ins);
1256 notification_t n = {
1257 .severity = NOTIF_OKAY, .time = cdtime(), .plugin = PLUGIN_NAME};
1258 sstrncpy(n.host, hostname_g, sizeof(n.host));
1259 sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance));
1260 snprintf(n.message, sizeof(n.message),
1261 "Data row added to table %s instance %s index %d", td->name, ins,
1262 (index != NULL) ? *index : -1);
1263 plugin_dispatch_notification(&n);
1268 static int snmp_agent_write(value_list_t const *vl) {
1273 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1274 table_definition_t *td = te->value;
1276 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1277 data_definition_t *dd = de->value;
1279 if (!dd->is_instance) {
1280 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
1281 vl->type_instance)) {
1282 snmp_agent_update_index(td, vl->plugin_instance);
1292 static int snmp_agent_collect(const data_set_t *ds, const value_list_t *vl,
1293 user_data_t __attribute__((unused)) * user_data) {
1295 pthread_mutex_lock(&g_agent->lock);
1297 snmp_agent_write(vl);
1299 pthread_mutex_unlock(&g_agent->lock);
1304 static int snmp_agent_preinit(void) {
1306 g_agent = calloc(1, sizeof(*g_agent));
1307 if (g_agent == NULL) {
1308 ERROR(PLUGIN_NAME ": Failed to allocate memory for snmp agent context");
1312 g_agent->tables = llist_create();
1313 g_agent->scalars = llist_create();
1315 if (g_agent->tables == NULL || g_agent->scalars == NULL) {
1316 ERROR(PLUGIN_NAME ": llist_create() failed");
1317 llist_destroy(g_agent->scalars);
1318 llist_destroy(g_agent->tables);
1323 /* make us an agentx client. */
1324 err = netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE,
1327 ERROR(PLUGIN_NAME ": Failed to set agent role (%d)", err);
1328 llist_destroy(g_agent->scalars);
1329 llist_destroy(g_agent->tables);
1334 * For SNMP debug purposes uses snmp_set_do_debugging(1);
1337 /* initialize the agent library */
1338 err = init_agent(PLUGIN_NAME);
1340 ERROR(PLUGIN_NAME ": Failed to initialize the agent library (%d)", err);
1341 llist_destroy(g_agent->scalars);
1342 llist_destroy(g_agent->tables);
1346 init_snmp(PLUGIN_NAME);
1348 g_agent->tp = read_all_mibs();
1353 static int snmp_agent_init(void) {
1356 if (g_agent == NULL || ((llist_head(g_agent->scalars) == NULL) &&
1357 (llist_head(g_agent->tables) == NULL))) {
1358 ERROR(PLUGIN_NAME ": snmp_agent_init: plugin not configured");
1362 plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown);
1364 ret = snmp_agent_register_scalar_oids();
1368 ret = snmp_agent_register_table_oids();
1372 ret = pthread_mutex_init(&g_agent->lock, NULL);
1374 ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret);
1378 ret = pthread_mutex_init(&g_agent->agentx_lock, NULL);
1380 ERROR(PLUGIN_NAME ": Failed to initialize AgentX mutex, err %u", ret);
1384 /* create a second thread to listen for requests from AgentX*/
1385 ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
1387 ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
1391 if (llist_head(g_agent->tables) != NULL) {
1392 plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL);
1393 plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL);
1399 static void *snmp_agent_thread_run(void __attribute__((unused)) * arg) {
1400 INFO(PLUGIN_NAME ": Thread is up and running");
1403 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1405 pthread_mutex_lock(&g_agent->agentx_lock);
1406 agent_check_and_process(0); /* 0 == don't block */
1407 pthread_mutex_unlock(&g_agent->agentx_lock);
1409 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
1416 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler) {
1417 netsnmp_handler_registration *reg;
1418 char *oid_name = snmp_agent_get_oid_name(oid->oid, oid->oid_len - 1);
1419 char oid_str[DATA_MAX_NAME_LEN];
1421 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), oid);
1423 if (oid_name == NULL) {
1425 ": Skipped registration: OID (%s) is not found in main tree",
1430 reg = netsnmp_create_handler_registration(oid_name, handler, oid->oid,
1431 oid->oid_len, HANDLER_CAN_RONLY);
1433 ERROR(PLUGIN_NAME ": Failed to create handler registration for OID (%s)",
1438 pthread_mutex_lock(&g_agent->agentx_lock);
1440 if (netsnmp_register_instance(reg) != MIB_REGISTERED_OK) {
1441 ERROR(PLUGIN_NAME ": Failed to register handler for OID (%s)", oid_str);
1442 pthread_mutex_unlock(&g_agent->agentx_lock);
1446 pthread_mutex_unlock(&g_agent->agentx_lock);
1448 DEBUG(PLUGIN_NAME ": Registered handler for OID (%s)", oid_str);
1453 static int snmp_agent_free_config(void) {
1455 if (g_agent == NULL)
1458 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next)
1459 snmp_agent_free_table((table_definition_t **)&te->value);
1460 llist_destroy(g_agent->tables);
1462 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL; de = de->next)
1463 snmp_agent_free_data((data_definition_t **)&de->value);
1464 llist_destroy(g_agent->scalars);
1469 static int snmp_agent_shutdown(void) {
1472 DEBUG(PLUGIN_NAME ": snmp_agent_shutdown");
1474 if (g_agent == NULL) {
1475 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: plugin not initialized");
1479 if (pthread_cancel(g_agent->thread) != 0)
1480 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to cancel the thread");
1482 if (pthread_join(g_agent->thread, NULL) != 0)
1483 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to join the thread");
1485 snmp_agent_free_config();
1487 snmp_shutdown(PLUGIN_NAME);
1489 pthread_mutex_destroy(&g_agent->lock);
1490 pthread_mutex_destroy(&g_agent->agentx_lock);
1497 static int snmp_agent_config(oconfig_item_t *ci) {
1499 int ret = snmp_agent_preinit();
1506 for (int i = 0; i < ci->children_num; i++) {
1507 oconfig_item_t *child = ci->children + i;
1508 if (strcasecmp("Data", child->key) == 0) {
1509 ret = snmp_agent_config_scalar(child);
1510 } else if (strcasecmp("Table", child->key) == 0) {
1511 ret = snmp_agent_config_table(child);
1513 ERROR(PLUGIN_NAME ": Unknown configuration option `%s'", child->key);
1518 ERROR(PLUGIN_NAME ": Failed to parse configuration");
1519 snmp_agent_free_config();
1520 snmp_shutdown(PLUGIN_NAME);
1526 ret = snmp_agent_validate_config();
1528 ERROR(PLUGIN_NAME ": Invalid configuration provided");
1529 snmp_agent_free_config();
1530 snmp_shutdown(PLUGIN_NAME);
1538 void module_register(void) {
1539 plugin_register_init(PLUGIN_NAME, snmp_agent_init);
1540 plugin_register_complex_config(PLUGIN_NAME, snmp_agent_config);