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
47 #define MIN(x, y) ((x) < (y) ? (x) : (y))
55 typedef struct oid_s oid_t;
57 struct table_definition_s {
62 c_avl_tree_t *instance_index;
63 c_avl_tree_t *index_instance;
65 typedef struct table_definition_s table_definition_t;
67 struct data_definition_s {
70 char *plugin_instance;
73 const table_definition_t *table;
80 typedef struct data_definition_s data_definition_t;
82 struct snmp_agent_ctx_s {
85 pthread_mutex_t agentx_lock;
91 typedef struct snmp_agent_ctx_s snmp_agent_ctx_t;
93 static snmp_agent_ctx_t *g_agent = NULL;
95 #define CHECK_DD_TYPE(_dd, _p, _pi, _t, _ti) \
96 (_dd->plugin ? !strcmp(_dd->plugin, _p) : 0) && \
97 (_dd->plugin_instance ? !strcmp(_dd->plugin_instance, _pi) : 1) && \
98 (_dd->type ? !strcmp(_dd->type, _t) : 0) && \
99 (_dd->type_instance ? !strcmp(_dd->type_instance, _ti) : 1)
101 static void *snmp_agent_thread_run(void *arg);
102 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler);
103 static int snmp_agent_set_vardata(void *dst_buf, size_t *dst_buf_len,
104 u_char asn_type, double scale, double shift,
105 const void *value, size_t len, int type);
106 static int snmp_agent_unregister_oid_index(oid_t *oid, int index);
108 static u_char snmp_agent_get_asn_type(oid *oid, size_t oid_len) {
109 struct tree *node = get_tree(oid, oid_len, g_agent->tp);
111 return (node != NULL) ? mib_to_asn_type(node->type) : 0;
114 static char *snmp_agent_get_oid_name(oid *oid, size_t oid_len) {
115 struct tree *node = get_tree(oid, oid_len, g_agent->tp);
117 return (node != NULL) ? node->label : NULL;
120 static int snmp_agent_oid_to_string(char *buf, size_t buf_size,
122 char oid_str[MAX_OID_LEN][16];
123 char *oid_str_ptr[MAX_OID_LEN];
125 for (size_t i = 0; i < o->oid_len; i++) {
126 snprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
127 oid_str_ptr[i] = oid_str[i];
130 return strjoin(buf, buf_size, oid_str_ptr, o->oid_len, ".");
133 static void snmp_agent_dump_data(void) {
135 char oid_str[DATA_MAX_NAME_LEN];
137 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
138 table_definition_t *td = te->value;
140 DEBUG(PLUGIN_NAME ": Table:");
141 DEBUG(PLUGIN_NAME ": Name: %s", td->name);
142 if (td->index_oid.oid_len != 0) {
143 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &td->index_oid);
144 DEBUG(PLUGIN_NAME ": IndexOID: %s", oid_str);
146 if (td->size_oid.oid_len != 0) {
147 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &td->size_oid);
148 DEBUG(PLUGIN_NAME ": SizeOID: %s", oid_str);
151 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
152 data_definition_t *dd = de->value;
154 DEBUG(PLUGIN_NAME ": Column:");
155 DEBUG(PLUGIN_NAME ": Name: %s", dd->name);
157 DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin);
158 if (dd->plugin_instance)
159 DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance);
161 DEBUG(PLUGIN_NAME ": Instance: true");
163 DEBUG(PLUGIN_NAME ": Type: %s", dd->type);
164 if (dd->type_instance)
165 DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance);
166 for (size_t i = 0; i < dd->oids_len; i++) {
167 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &dd->oids[i]);
168 DEBUG(PLUGIN_NAME ": OID[%d]: %s", i, oid_str);
170 DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale);
171 DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift);
175 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
176 data_definition_t *dd = e->value;
178 DEBUG(PLUGIN_NAME ": Scalar:");
179 DEBUG(PLUGIN_NAME ": Name: %s", dd->name);
181 DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin);
182 if (dd->plugin_instance)
183 DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance);
185 DEBUG(PLUGIN_NAME ": Instance: true");
187 DEBUG(PLUGIN_NAME ": Type: %s", dd->type);
188 if (dd->type_instance)
189 DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance);
190 for (size_t i = 0; i < dd->oids_len; i++) {
191 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &dd->oids[i]);
192 DEBUG(PLUGIN_NAME ": OID[%d]: %s", i, oid_str);
194 DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale);
195 DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift);
197 #endif /* COLLECT_DEBUG */
200 static int snmp_agent_validate_data(void) {
202 snmp_agent_dump_data();
204 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
205 table_definition_t *td = te->value;
207 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
208 data_definition_t *dd = de->value;
211 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'.'%s'", td->name,
216 if (dd->plugin_instance) {
217 ERROR(PLUGIN_NAME ": PluginInstance should not be defined for table "
218 "data type '%s'.'%s'",
223 if (dd->oids_len == 0) {
224 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'.'%s'", td->name,
229 if (dd->is_instance) {
231 if (dd->type || dd->type_instance) {
232 ERROR(PLUGIN_NAME ": Type and TypeInstance are not valid for "
233 "instance data '%s'.'%s'",
238 if (dd->oids_len > 1) {
241 ": Only one OID should be specified for instance data '%s'.'%s'",
248 ERROR(PLUGIN_NAME ": Type not defined for data '%s'.'%s'", td->name,
256 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
257 data_definition_t *dd = e->value;
260 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'", dd->name);
264 if (dd->oids_len == 0) {
265 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'", dd->name);
269 if (dd->is_instance) {
271 ": Instance flag can't be specified for scalar data '%s'",
277 ERROR(PLUGIN_NAME ": Type not defined for data '%s'", dd->name);
285 static void snmp_agent_generate_oid2string(oid_t *oid, size_t offset,
287 int key_len = oid->oid[offset];
290 for (i = 0; i < key_len && offset < oid->oid_len; i++)
291 key[i] = oid->oid[++offset];
296 static int snmp_agent_generate_string2oid(oid_t *oid, const char *key) {
297 int key_len = strlen(key);
299 oid->oid[oid->oid_len++] = key_len;
300 for (int i = 0; i < key_len; i++) {
301 oid->oid[oid->oid_len++] = key[i];
302 if (oid->oid_len >= MAX_OID_LEN) {
303 ERROR(PLUGIN_NAME ": Conversion key string %s to OID failed", key);
311 static int snmp_agent_register_oid_string(oid_t *oid, const char *key,
312 Netsnmp_Node_Handler *handler) {
315 memcpy(&new_oid, oid, sizeof(*oid));
316 int ret = snmp_agent_generate_string2oid(&new_oid, key);
320 return snmp_agent_register_oid(&new_oid, handler);
323 static int snmp_agent_unregister_oid_string(oid_t *oid, const char *key) {
326 memcpy(&new_oid, oid, sizeof(*oid));
327 int ret = snmp_agent_generate_string2oid(&new_oid, key);
331 return unregister_mib(new_oid.oid, new_oid.oid_len);
334 static int snmp_agent_table_row_remove(table_definition_t *td,
335 const char *instance) {
339 if (td->index_oid.oid_len) {
340 if ((c_avl_get(td->instance_index, instance, (void **)&index) != 0) ||
341 (c_avl_get(td->index_instance, index, (void **)&ins) != 0))
344 if (c_avl_get(td->instance_index, instance, (void **)&ins) != 0)
348 pthread_mutex_lock(&g_agent->agentx_lock);
350 if (td->index_oid.oid_len)
351 snmp_agent_unregister_oid_index(&td->index_oid, *index);
353 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
354 data_definition_t *dd = de->value;
356 for (size_t i = 0; i < dd->oids_len; i++)
357 if (td->index_oid.oid_len)
358 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
360 snmp_agent_unregister_oid_string(&dd->oids[i], ins);
363 pthread_mutex_unlock(&g_agent->agentx_lock);
365 DEBUG(PLUGIN_NAME ": Removed row for '%s' table [%d, %s]", td->name,
366 (index != NULL) ? *index : -1, ins);
369 .severity = NOTIF_WARNING, .time = cdtime(), .plugin = PLUGIN_NAME};
370 sstrncpy(n.host, hostname_g, sizeof(n.host));
371 sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance));
372 snprintf(n.message, sizeof(n.message),
373 "Removed data row from table %s instance %s index %d", td->name, ins,
374 (index != NULL) ? *index : -1);
375 plugin_dispatch_notification(&n);
377 if (td->index_oid.oid_len) {
378 c_avl_remove(td->index_instance, index, NULL, (void **)&ins);
379 c_avl_remove(td->instance_index, instance, NULL, (void **)&index);
383 c_avl_remove(td->instance_index, instance, NULL, (void **)&ins);
390 static int snmp_agent_clear_missing(const value_list_t *vl,
391 __attribute__((unused)) user_data_t *ud) {
395 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
396 table_definition_t *td = te->value;
398 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
399 data_definition_t *dd = de->value;
401 if (!dd->is_instance) {
402 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
403 vl->type_instance)) {
404 snmp_agent_table_row_remove(td, vl->plugin_instance);
414 static void snmp_agent_free_data(data_definition_t **dd) {
416 if (dd == NULL || *dd == NULL)
419 /* unregister scalar type OID */
420 if ((*dd)->table == NULL) {
421 for (size_t i = 0; i < (*dd)->oids_len; i++)
422 unregister_mib((*dd)->oids[i].oid, (*dd)->oids[i].oid_len);
424 if (!(*dd)->table->index_oid.oid_len) {
427 c_avl_iterator_t *iter = c_avl_get_iterator((*dd)->table->instance_index);
428 while (c_avl_iterator_next(iter, (void *)&instance, (void *)&instance) ==
430 for (size_t i = 0; i < (*dd)->oids_len; i++)
431 snmp_agent_unregister_oid_string(&(*dd)->oids[i], instance);
433 c_avl_iterator_destroy(iter);
435 /* unregister all table OIDs */
439 c_avl_iterator_t *iter = c_avl_get_iterator((*dd)->table->index_instance);
440 while (c_avl_iterator_next(iter, (void *)&index, (void *)&value) == 0) {
441 for (size_t i = 0; i < (*dd)->oids_len; i++)
442 snmp_agent_unregister_oid_index(&(*dd)->oids[i], *index);
444 c_avl_iterator_destroy(iter);
448 sfree((*dd)->plugin);
449 sfree((*dd)->plugin_instance);
451 sfree((*dd)->type_instance);
459 static void snmp_agent_free_table(table_definition_t **td) {
461 if (td == NULL || *td == NULL)
464 if ((*td)->size_oid.oid_len)
465 unregister_mib((*td)->size_oid.oid, (*td)->size_oid.oid_len);
467 if ((*td)->index_oid.oid_len) {
471 c_avl_iterator_t *iter = c_avl_get_iterator((*td)->index_instance);
472 while (c_avl_iterator_next(iter, (void *)&index, (void *)&value) == 0)
473 snmp_agent_unregister_oid_index(&(*td)->index_oid, *index);
475 c_avl_iterator_destroy(iter);
478 for (llentry_t *de = llist_head((*td)->columns); de != NULL; de = de->next) {
479 data_definition_t *dd = de->value;
480 snmp_agent_free_data(&dd);
483 llist_destroy((*td)->columns);
488 /* index_instance and instance_index contain the same pointers */
489 c_avl_destroy((*td)->index_instance);
490 (*td)->index_instance = NULL;
492 if ((*td)->instance_index != NULL) {
493 while (c_avl_pick((*td)->instance_index, &key, &value) == 0) {
498 c_avl_destroy((*td)->instance_index);
499 (*td)->instance_index = NULL;
508 static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests,
509 data_definition_t *dd, char *instance,
511 char name[DATA_MAX_NAME_LEN];
512 format_name(name, sizeof(name), hostname_g, dd->plugin,
513 instance ? instance : dd->plugin_instance, dd->type,
515 DEBUG(PLUGIN_NAME ": Identifier '%s'", name);
519 const data_set_t *ds = plugin_get_ds(dd->type);
521 ERROR(PLUGIN_NAME ": Data set not found for '%s' type", dd->type);
522 return SNMP_NOSUCHINSTANCE;
525 int ret = uc_get_value_by_name(name, &values, &values_num);
528 ERROR(PLUGIN_NAME ": Failed to get value for '%s'", name);
529 return SNMP_NOSUCHINSTANCE;
532 assert(ds->ds_num == values_num);
533 assert(oid_index < (int)values_num);
535 char data[DATA_MAX_NAME_LEN];
536 size_t data_len = sizeof(data);
537 ret = snmp_agent_set_vardata(
538 data, &data_len, dd->oids[oid_index].type, dd->scale, dd->shift,
539 &values[oid_index], sizeof(values[oid_index]), ds->ds[oid_index].type);
544 ERROR(PLUGIN_NAME ": Failed to convert '%s' value to snmp data", name);
545 return SNMP_NOSUCHINSTANCE;
548 requests->requestvb->type = dd->oids[oid_index].type;
549 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
550 (const u_char *)data, data_len);
552 return SNMP_ERR_NOERROR;
556 snmp_agent_table_oid_handler(struct netsnmp_mib_handler_s *handler,
557 struct netsnmp_handler_registration_s *reginfo,
558 struct netsnmp_agent_request_info_s *reqinfo,
559 struct netsnmp_request_info_s *requests) {
561 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
562 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
563 return SNMP_ERR_NOERROR;
566 pthread_mutex_lock(&g_agent->lock);
569 memcpy(oid.oid, requests->requestvb->name,
570 sizeof(oid.oid[0]) * requests->requestvb->name_length);
571 oid.oid_len = requests->requestvb->name_length;
574 char oid_str[DATA_MAX_NAME_LEN];
575 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
576 DEBUG(PLUGIN_NAME ": Get request received for table OID '%s'", oid_str);
579 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
580 table_definition_t *td = te->value;
582 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
583 data_definition_t *dd = de->value;
585 for (size_t i = 0; i < dd->oids_len; i++) {
586 int ret = snmp_oid_ncompare(oid.oid, oid.oid_len, dd->oids[i].oid,
588 MIN(oid.oid_len, dd->oids[i].oid_len));
594 if (!td->index_oid.oid_len) {
595 char key[MAX_OID_LEN];
597 memset(key, 0, sizeof(key));
598 snmp_agent_generate_oid2string(
599 &oid, MIN(oid.oid_len, dd->oids[i].oid_len), key);
601 ret = c_avl_get(td->instance_index, key, (void **)&instance);
603 DEBUG(PLUGIN_NAME ": Nonexisting index string '%s' requested", key);
604 pthread_mutex_unlock(&g_agent->lock);
605 return SNMP_NOSUCHINSTANCE;
608 int index = oid.oid[oid.oid_len - 1];
610 ret = c_avl_get(td->index_instance, &index, (void **)&instance);
612 DEBUG(PLUGIN_NAME ": Nonexisting index '%d' requested", index);
613 pthread_mutex_unlock(&g_agent->lock);
614 return SNMP_NOSUCHINSTANCE;
618 if (dd->is_instance) {
619 requests->requestvb->type = ASN_OCTET_STR;
620 snmp_set_var_typed_value(
621 requests->requestvb, requests->requestvb->type,
622 (const u_char *)instance, strlen((instance)));
624 pthread_mutex_unlock(&g_agent->lock);
626 return SNMP_ERR_NOERROR;
629 ret = snmp_agent_form_reply(requests, dd, instance, i);
631 pthread_mutex_unlock(&g_agent->lock);
638 pthread_mutex_unlock(&g_agent->lock);
640 return SNMP_NOSUCHINSTANCE;
643 static int snmp_agent_table_index_oid_handler(
644 struct netsnmp_mib_handler_s *handler,
645 struct netsnmp_handler_registration_s *reginfo,
646 struct netsnmp_agent_request_info_s *reqinfo,
647 struct netsnmp_request_info_s *requests) {
649 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
650 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
651 return SNMP_ERR_NOERROR;
654 pthread_mutex_lock(&g_agent->lock);
657 memcpy(oid.oid, requests->requestvb->name,
658 sizeof(oid.oid[0]) * requests->requestvb->name_length);
659 oid.oid_len = requests->requestvb->name_length;
661 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
662 table_definition_t *td = te->value;
664 if (td->index_oid.oid_len &&
665 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->index_oid.oid,
666 td->index_oid.oid_len,
667 MIN(oid.oid_len, td->index_oid.oid_len)) == 0)) {
669 DEBUG(PLUGIN_NAME ": Handle '%s' table index OID", td->name);
671 int index = oid.oid[oid.oid_len - 1];
673 int ret = c_avl_get(td->index_instance, &index, &(void *){NULL});
675 /* nonexisting index requested */
676 pthread_mutex_unlock(&g_agent->lock);
677 return SNMP_NOSUCHINSTANCE;
680 requests->requestvb->type = ASN_INTEGER;
681 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
682 (const u_char *)&index, sizeof(index));
684 pthread_mutex_unlock(&g_agent->lock);
686 return SNMP_ERR_NOERROR;
690 pthread_mutex_unlock(&g_agent->lock);
692 return SNMP_NOSUCHINSTANCE;
695 static int snmp_agent_table_size_oid_handler(
696 struct netsnmp_mib_handler_s *handler,
697 struct netsnmp_handler_registration_s *reginfo,
698 struct netsnmp_agent_request_info_s *reqinfo,
699 struct netsnmp_request_info_s *requests) {
701 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
702 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
703 return SNMP_ERR_NOERROR;
706 pthread_mutex_lock(&g_agent->lock);
709 memcpy(oid.oid, requests->requestvb->name,
710 sizeof(oid.oid[0]) * requests->requestvb->name_length);
711 oid.oid_len = requests->requestvb->name_length;
713 DEBUG(PLUGIN_NAME ": Get request received for table size OID");
715 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
716 table_definition_t *td = te->value;
718 if (td->size_oid.oid_len &&
719 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->size_oid.oid,
720 td->size_oid.oid_len,
721 MIN(oid.oid_len, td->size_oid.oid_len)) == 0)) {
722 DEBUG(PLUGIN_NAME ": Handle '%s' table size OID", td->name);
724 long size = c_avl_size(td->index_instance);
726 requests->requestvb->type = td->size_oid.type;
727 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
728 (const u_char *)&size, sizeof(size));
730 pthread_mutex_unlock(&g_agent->lock);
732 return SNMP_ERR_NOERROR;
736 pthread_mutex_unlock(&g_agent->lock);
738 return SNMP_NOSUCHINSTANCE;
742 snmp_agent_scalar_oid_handler(struct netsnmp_mib_handler_s *handler,
743 struct netsnmp_handler_registration_s *reginfo,
744 struct netsnmp_agent_request_info_s *reqinfo,
745 struct netsnmp_request_info_s *requests) {
747 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
748 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
749 return SNMP_ERR_NOERROR;
752 pthread_mutex_lock(&g_agent->lock);
755 memcpy(oid.oid, requests->requestvb->name,
756 sizeof(oid.oid[0]) * requests->requestvb->name_length);
757 oid.oid_len = requests->requestvb->name_length;
760 char oid_str[DATA_MAX_NAME_LEN];
761 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
762 DEBUG(PLUGIN_NAME ": Get request received for scalar OID '%s'", oid_str);
765 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL;
767 data_definition_t *dd = de->value;
769 for (size_t i = 0; i < dd->oids_len; i++) {
771 int ret = snmp_oid_compare(oid.oid, oid.oid_len, dd->oids[i].oid,
772 dd->oids[i].oid_len);
776 ret = snmp_agent_form_reply(requests, dd, NULL, i);
778 pthread_mutex_unlock(&g_agent->lock);
784 pthread_mutex_unlock(&g_agent->lock);
786 return SNMP_NOSUCHINSTANCE;
789 static int snmp_agent_register_table_oids(void) {
791 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
792 table_definition_t *td = te->value;
794 if (td->size_oid.oid_len != 0) {
796 snmp_agent_get_asn_type(td->size_oid.oid, td->size_oid.oid_len);
797 td->size_oid.oid_len++;
798 int ret = snmp_agent_register_oid(&td->size_oid,
799 snmp_agent_table_size_oid_handler);
804 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
805 data_definition_t *dd = de->value;
807 for (size_t i = 0; i < dd->oids_len; i++) {
809 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
817 static int snmp_agent_register_scalar_oids(void) {
819 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
820 data_definition_t *dd = e->value;
822 for (size_t i = 0; i < dd->oids_len; i++) {
825 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
828 snmp_agent_register_oid(&dd->oids[i], snmp_agent_scalar_oid_handler);
837 static int snmp_agent_config_data_oids(data_definition_t *dd,
838 oconfig_item_t *ci) {
839 if (ci->values_num < 1) {
840 WARNING(PLUGIN_NAME ": `OIDs' needs at least one argument");
844 for (int i = 0; i < ci->values_num; i++)
845 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
846 WARNING(PLUGIN_NAME ": `OIDs' needs only string argument");
850 if (dd->oids != NULL)
853 dd->oids = calloc(ci->values_num, sizeof(*dd->oids));
854 if (dd->oids == NULL)
856 dd->oids_len = (size_t)ci->values_num;
858 for (int i = 0; i < ci->values_num; i++) {
859 dd->oids[i].oid_len = MAX_OID_LEN;
861 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->oids[i].oid,
862 &dd->oids[i].oid_len)) {
863 ERROR(PLUGIN_NAME ": snmp_parse_oid (%s) failed",
864 ci->values[i].value.string);
874 static int snmp_agent_config_table_size_oid(table_definition_t *td,
875 oconfig_item_t *ci) {
876 if (ci->values_num < 1) {
877 WARNING(PLUGIN_NAME ": `TableSizeOID' is empty");
881 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
882 WARNING(PLUGIN_NAME ": `TableSizeOID' needs only string argument");
886 td->size_oid.oid_len = MAX_OID_LEN;
888 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->size_oid.oid,
889 &td->size_oid.oid_len)) {
890 ERROR(PLUGIN_NAME ": Failed to parse table size OID (%s)",
891 ci->values[0].value.string);
892 td->size_oid.oid_len = 0;
899 static int snmp_agent_config_table_index_oid(table_definition_t *td,
900 oconfig_item_t *ci) {
902 if (ci->values_num < 1) {
903 WARNING(PLUGIN_NAME ": `IndexOID' is empty");
907 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
908 WARNING(PLUGIN_NAME ": `IndexOID' needs only string argument");
912 td->index_oid.oid_len = MAX_OID_LEN;
914 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->index_oid.oid,
915 &td->index_oid.oid_len)) {
916 ERROR(PLUGIN_NAME ": Failed to parse table index OID (%s)",
917 ci->values[0].value.string);
918 td->index_oid.oid_len = 0;
925 static int snmp_agent_config_table_data(table_definition_t *td,
926 oconfig_item_t *ci) {
927 data_definition_t *dd;
932 dd = calloc(1, sizeof(*dd));
934 ERROR(PLUGIN_NAME ": Failed to allocate memory for table data definition");
938 ret = cf_util_get_string(ci, &dd->name);
949 for (int i = 0; i < ci->children_num; i++) {
950 oconfig_item_t *option = ci->children + i;
952 if (strcasecmp("Instance", option->key) == 0)
953 ret = cf_util_get_boolean(option, &dd->is_instance);
954 else if (strcasecmp("Plugin", option->key) == 0)
955 ret = cf_util_get_string(option, &dd->plugin);
956 else if (strcasecmp("PluginInstance", option->key) == 0)
957 ret = cf_util_get_string(option, &dd->plugin_instance);
958 else if (strcasecmp("Type", option->key) == 0)
959 ret = cf_util_get_string(option, &dd->type);
960 else if (strcasecmp("TypeInstance", option->key) == 0)
961 ret = cf_util_get_string(option, &dd->type_instance);
962 else if (strcasecmp("Shift", option->key) == 0)
963 ret = cf_util_get_double(option, &dd->shift);
964 else if (strcasecmp("Scale", option->key) == 0)
965 ret = cf_util_get_double(option, &dd->scale);
966 else if (strcasecmp("OIDs", option->key) == 0)
967 ret = snmp_agent_config_data_oids(dd, option);
969 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
974 snmp_agent_free_data(&dd);
979 llentry_t *entry = llentry_create(dd->name, dd);
981 snmp_agent_free_data(&dd);
985 llist_append(td->columns, entry);
990 static int snmp_agent_config_data(oconfig_item_t *ci) {
991 data_definition_t *dd;
996 dd = calloc(1, sizeof(*dd));
998 ERROR(PLUGIN_NAME ": Failed to allocate memory for data definition");
1002 ret = cf_util_get_string(ci, &dd->name);
1011 for (int i = 0; i < ci->children_num; i++) {
1012 oconfig_item_t *option = ci->children + i;
1014 if (strcasecmp("Instance", option->key) == 0)
1015 ret = cf_util_get_boolean(option, &dd->is_instance);
1016 else if (strcasecmp("Plugin", option->key) == 0)
1017 ret = cf_util_get_string(option, &dd->plugin);
1018 else if (strcasecmp("PluginInstance", option->key) == 0)
1019 ret = cf_util_get_string(option, &dd->plugin_instance);
1020 else if (strcasecmp("Type", option->key) == 0)
1021 ret = cf_util_get_string(option, &dd->type);
1022 else if (strcasecmp("TypeInstance", option->key) == 0)
1023 ret = cf_util_get_string(option, &dd->type_instance);
1024 else if (strcasecmp("Shift", option->key) == 0)
1025 ret = cf_util_get_double(option, &dd->shift);
1026 else if (strcasecmp("Scale", option->key) == 0)
1027 ret = cf_util_get_double(option, &dd->scale);
1028 else if (strcasecmp("OIDs", option->key) == 0)
1029 ret = snmp_agent_config_data_oids(dd, option);
1031 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
1036 snmp_agent_free_data(&dd);
1041 llentry_t *entry = llentry_create(dd->name, dd);
1042 if (entry == NULL) {
1043 snmp_agent_free_data(&dd);
1047 llist_append(g_agent->scalars, entry);
1052 static int num_compare(const int *a, const int *b) {
1053 assert((a != NULL) && (b != NULL));
1062 static int snmp_agent_config_table(oconfig_item_t *ci) {
1063 table_definition_t *td;
1068 td = calloc(1, sizeof(*td));
1070 ERROR(PLUGIN_NAME ": Failed to allocate memory for table definition");
1074 ret = cf_util_get_string(ci, &td->name);
1080 td->columns = llist_create();
1081 if (td->columns == NULL) {
1082 ERROR(PLUGIN_NAME ": Failed to allocate memory for columns list");
1083 snmp_agent_free_table(&td);
1087 for (int i = 0; i < ci->children_num; i++) {
1088 oconfig_item_t *option = ci->children + i;
1090 if (strcasecmp("IndexOID", option->key) == 0)
1091 ret = snmp_agent_config_table_index_oid(td, option);
1092 else if (strcasecmp("SizeOID", option->key) == 0)
1093 ret = snmp_agent_config_table_size_oid(td, option);
1094 else if (strcasecmp("Data", option->key) == 0)
1095 ret = snmp_agent_config_table_data(td, option);
1097 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
1102 snmp_agent_free_table(&td);
1107 llentry_t *entry = llentry_create(td->name, td);
1108 if (entry == NULL) {
1109 snmp_agent_free_table(&td);
1113 td->instance_index =
1114 c_avl_create((int (*)(const void *, const void *))strcmp);
1115 if (td->instance_index == NULL) {
1116 snmp_agent_free_table(&td);
1120 td->index_instance =
1121 c_avl_create((int (*)(const void *, const void *))num_compare);
1122 if (td->index_instance == NULL) {
1123 snmp_agent_free_table(&td);
1127 llist_append(g_agent->tables, entry);
1132 static int snmp_agent_get_value_from_ds_type(const value_t *val, int type,
1133 double scale, double shift,
1136 case DS_TYPE_COUNTER:
1137 *value = (long)((val->counter * scale) + shift);
1139 case DS_TYPE_ABSOLUTE:
1140 *value = (long)((val->absolute * scale) + shift);
1142 case DS_TYPE_DERIVE:
1143 *value = (long)((val->derive * scale) + shift);
1146 *value = (long)((val->gauge * scale) + shift);
1151 ERROR(PLUGIN_NAME ": Unknown data source type: %i", type);
1158 static int snmp_agent_set_vardata(void *data, size_t *data_len, u_char asn_type,
1159 double scale, double shift, const void *value,
1160 size_t len, int type) {
1163 netsnmp_vardata var;
1168 var.string = (u_char *)data;
1170 ret = snmp_agent_get_value_from_ds_type(val, type, scale, shift, &new_value);
1180 if (*data_len < sizeof(*var.integer))
1182 *var.integer = new_value;
1183 *data_len = sizeof(*var.integer);
1186 if (*data_len < sizeof(*var.counter64))
1188 var.counter64->high = (u_long)((int64_t)new_value >> 32);
1189 var.counter64->low = (u_long)((int64_t)new_value & 0xFFFFFFFF);
1190 *data_len = sizeof(*var.counter64);
1193 if (type == DS_TYPE_GAUGE) {
1194 char buf[DATA_MAX_NAME_LEN];
1195 snprintf(buf, sizeof(buf), "%.2f", val->gauge);
1196 if (*data_len < strlen(buf))
1198 *data_len = strlen(buf);
1199 memcpy(var.string, buf, *data_len);
1201 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1207 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1215 static int snmp_agent_register_oid_index(oid_t *oid, int index,
1216 Netsnmp_Node_Handler *handler) {
1218 memcpy(&new_oid, oid, sizeof(*oid));
1219 new_oid.oid[new_oid.oid_len++] = index;
1220 return snmp_agent_register_oid(&new_oid, handler);
1223 static int snmp_agent_unregister_oid_index(oid_t *oid, int index) {
1225 memcpy(&new_oid, oid, sizeof(*oid));
1226 new_oid.oid[new_oid.oid_len++] = index;
1227 return unregister_mib(new_oid.oid, new_oid.oid_len);
1230 static int snmp_agent_update_index(table_definition_t *td,
1231 const char *instance) {
1233 if (c_avl_get(td->instance_index, instance, NULL) == 0)
1240 ins = strdup(instance);
1244 /* need to generate index for the table */
1245 if (td->index_oid.oid_len) {
1246 index = calloc(1, sizeof(*index));
1247 if (index == NULL) {
1252 *index = c_avl_size(td->instance_index) + 1;
1254 ret = c_avl_insert(td->instance_index, ins, index);
1261 ret = c_avl_insert(td->index_instance, index, ins);
1263 DEBUG(PLUGIN_NAME ": Failed to update index_instance for '%s' table",
1265 c_avl_remove(td->instance_index, ins, NULL, (void **)&index);
1271 ret = snmp_agent_register_oid_index(&td->index_oid, *index,
1272 snmp_agent_table_index_oid_handler);
1276 /* instance as a key is required for any table */
1277 ret = c_avl_insert(td->instance_index, ins, ins);
1284 /* register new oids for all columns */
1285 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1286 data_definition_t *dd = de->value;
1288 for (size_t i = 0; i < dd->oids_len; i++) {
1289 if (td->index_oid.oid_len) {
1290 ret = snmp_agent_register_oid_index(&dd->oids[i], *index,
1291 snmp_agent_table_oid_handler);
1293 ret = snmp_agent_register_oid_string(&dd->oids[i], ins,
1294 snmp_agent_table_oid_handler);
1302 DEBUG(PLUGIN_NAME ": Updated index for '%s' table [%d, %s]", td->name,
1303 (index != NULL) ? *index : -1, ins);
1305 notification_t n = {
1306 .severity = NOTIF_OKAY, .time = cdtime(), .plugin = PLUGIN_NAME};
1307 sstrncpy(n.host, hostname_g, sizeof(n.host));
1308 sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance));
1309 snprintf(n.message, sizeof(n.message),
1310 "Data row added to table %s instance %s index %d", td->name, ins,
1311 (index != NULL) ? *index : -1);
1312 plugin_dispatch_notification(&n);
1317 static int snmp_agent_write(value_list_t const *vl) {
1322 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1323 table_definition_t *td = te->value;
1325 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1326 data_definition_t *dd = de->value;
1328 if (!dd->is_instance) {
1329 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
1330 vl->type_instance)) {
1331 snmp_agent_update_index(td, vl->plugin_instance);
1341 static int snmp_agent_collect(const data_set_t *ds, const value_list_t *vl,
1342 user_data_t __attribute__((unused)) * user_data) {
1344 pthread_mutex_lock(&g_agent->lock);
1346 snmp_agent_write(vl);
1348 pthread_mutex_unlock(&g_agent->lock);
1353 static int snmp_agent_preinit(void) {
1354 if (g_agent != NULL) {
1355 /* already initialized if config callback was called before init callback */
1359 g_agent = calloc(1, sizeof(*g_agent));
1360 if (g_agent == NULL) {
1361 ERROR(PLUGIN_NAME ": Failed to allocate memory for snmp agent context");
1365 g_agent->tables = llist_create();
1366 g_agent->scalars = llist_create();
1369 /* make us a agentx client. */
1370 err = netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE,
1373 ERROR(PLUGIN_NAME ": Failed to set agent role (%d)", err);
1378 * For SNMP debug purposes uses snmp_set_do_debugging(1);
1381 /* initialize the agent library */
1382 err = init_agent(PLUGIN_NAME);
1384 ERROR(PLUGIN_NAME ": Failed to initialize the agent library (%d)", err);
1388 init_snmp(PLUGIN_NAME);
1390 g_agent->tp = read_all_mibs();
1395 static int snmp_agent_init(void) {
1398 ret = snmp_agent_preinit();
1402 ret = snmp_agent_register_scalar_oids();
1406 ret = snmp_agent_register_table_oids();
1410 /* create a second thread to listen for requests from AgentX*/
1411 ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
1413 ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
1417 ret = pthread_mutex_init(&g_agent->lock, NULL);
1419 ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret);
1423 ret = pthread_mutex_init(&g_agent->agentx_lock, NULL);
1425 ERROR(PLUGIN_NAME ": Failed to initialize AgentX mutex, err %u", ret);
1432 static void *snmp_agent_thread_run(void __attribute__((unused)) * arg) {
1433 INFO(PLUGIN_NAME ": Thread is up and running");
1436 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1438 pthread_mutex_lock(&g_agent->agentx_lock);
1439 agent_check_and_process(0); /* 0 == don't block */
1440 pthread_mutex_unlock(&g_agent->agentx_lock);
1442 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
1449 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler) {
1450 netsnmp_handler_registration *reg;
1451 char *oid_name = snmp_agent_get_oid_name(oid->oid, oid->oid_len - 1);
1452 char oid_str[DATA_MAX_NAME_LEN];
1454 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), oid);
1456 if (oid_name == NULL) {
1458 ": Skipped registration: OID (%s) is not found in main tree",
1463 reg = netsnmp_create_handler_registration(oid_name, handler, oid->oid,
1464 oid->oid_len, HANDLER_CAN_RONLY);
1466 ERROR(PLUGIN_NAME ": Failed to create handler registration for OID (%s)",
1471 pthread_mutex_lock(&g_agent->agentx_lock);
1473 if (netsnmp_register_instance(reg) != MIB_REGISTERED_OK) {
1474 ERROR(PLUGIN_NAME ": Failed to register handler for OID (%s)", oid_str);
1475 pthread_mutex_unlock(&g_agent->agentx_lock);
1479 pthread_mutex_unlock(&g_agent->agentx_lock);
1481 DEBUG(PLUGIN_NAME ": Registered handler for OID (%s)", oid_str);
1486 static int snmp_agent_free_config(void) {
1488 if (g_agent == NULL)
1491 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next)
1492 snmp_agent_free_table((table_definition_t **)&te->value);
1493 llist_destroy(g_agent->tables);
1495 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL; de = de->next)
1496 snmp_agent_free_data((data_definition_t **)&de->value);
1497 llist_destroy(g_agent->scalars);
1502 static int snmp_agent_shutdown(void) {
1505 DEBUG(PLUGIN_NAME ": snmp_agent_shutdown");
1507 if (g_agent == NULL) {
1508 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: plugin not initialized");
1512 if (pthread_cancel(g_agent->thread) != 0)
1513 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to cancel the thread");
1515 if (pthread_join(g_agent->thread, NULL) != 0)
1516 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to join the thread");
1518 snmp_agent_free_config();
1520 snmp_shutdown(PLUGIN_NAME);
1522 pthread_mutex_destroy(&g_agent->lock);
1523 pthread_mutex_destroy(&g_agent->agentx_lock);
1530 static int snmp_agent_config(oconfig_item_t *ci) {
1532 int ret = snmp_agent_preinit();
1539 for (int i = 0; i < ci->children_num; i++) {
1540 oconfig_item_t *child = ci->children + i;
1541 if (strcasecmp("Data", child->key) == 0) {
1542 ret = snmp_agent_config_data(child);
1543 } else if (strcasecmp("Table", child->key) == 0) {
1544 ret = snmp_agent_config_table(child);
1546 ERROR(PLUGIN_NAME ": Unknown configuration option `%s'", child->key);
1551 ERROR(PLUGIN_NAME ": Failed to parse configuration");
1552 snmp_agent_free_config();
1553 snmp_shutdown(PLUGIN_NAME);
1559 ret = snmp_agent_validate_data();
1561 ERROR(PLUGIN_NAME ": Invalid configuration provided");
1562 snmp_agent_free_config();
1563 snmp_shutdown(PLUGIN_NAME);
1571 void module_register(void) {
1572 plugin_register_init(PLUGIN_NAME, snmp_agent_init);
1573 plugin_register_complex_config(PLUGIN_NAME, snmp_agent_config);
1574 plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL);
1575 plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL);
1576 plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown);