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 int snmp_agent_shutdown(void);
102 static void *snmp_agent_thread_run(void *arg);
103 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler);
104 static int snmp_agent_set_vardata(void *dst_buf, size_t *dst_buf_len,
105 u_char asn_type, double scale, double shift,
106 const void *value, size_t len, int type);
107 static int snmp_agent_unregister_oid_index(oid_t *oid, int index);
109 static u_char snmp_agent_get_asn_type(oid *oid, size_t oid_len) {
110 struct tree *node = get_tree(oid, oid_len, g_agent->tp);
112 return (node != NULL) ? mib_to_asn_type(node->type) : 0;
115 static char *snmp_agent_get_oid_name(oid *oid, size_t oid_len) {
116 struct tree *node = get_tree(oid, oid_len, g_agent->tp);
118 return (node != NULL) ? node->label : NULL;
121 static int snmp_agent_oid_to_string(char *buf, size_t buf_size,
123 char oid_str[MAX_OID_LEN][16];
124 char *oid_str_ptr[MAX_OID_LEN];
126 for (size_t i = 0; i < o->oid_len; i++) {
127 snprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
128 oid_str_ptr[i] = oid_str[i];
131 return strjoin(buf, buf_size, oid_str_ptr, o->oid_len, ".");
134 static void snmp_agent_dump_data(void) {
136 char oid_str[DATA_MAX_NAME_LEN];
138 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
139 table_definition_t *td = te->value;
141 DEBUG(PLUGIN_NAME ": Table:");
142 DEBUG(PLUGIN_NAME ": Name: %s", td->name);
143 if (td->index_oid.oid_len != 0) {
144 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &td->index_oid);
145 DEBUG(PLUGIN_NAME ": IndexOID: %s", oid_str);
147 if (td->size_oid.oid_len != 0) {
148 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &td->size_oid);
149 DEBUG(PLUGIN_NAME ": SizeOID: %s", oid_str);
152 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
153 data_definition_t *dd = de->value;
155 DEBUG(PLUGIN_NAME ": Column:");
156 DEBUG(PLUGIN_NAME ": Name: %s", dd->name);
158 DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin);
159 if (dd->plugin_instance)
160 DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance);
162 DEBUG(PLUGIN_NAME ": Instance: true");
164 DEBUG(PLUGIN_NAME ": Type: %s", dd->type);
165 if (dd->type_instance)
166 DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance);
167 for (size_t i = 0; i < dd->oids_len; i++) {
168 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &dd->oids[i]);
169 DEBUG(PLUGIN_NAME ": OID[%" PRIsz "]: %s", i, oid_str);
171 DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale);
172 DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift);
176 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
177 data_definition_t *dd = e->value;
179 DEBUG(PLUGIN_NAME ": Scalar:");
180 DEBUG(PLUGIN_NAME ": Name: %s", dd->name);
182 DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin);
183 if (dd->plugin_instance)
184 DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance);
186 DEBUG(PLUGIN_NAME ": Instance: true");
188 DEBUG(PLUGIN_NAME ": Type: %s", dd->type);
189 if (dd->type_instance)
190 DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance);
191 for (size_t i = 0; i < dd->oids_len; i++) {
192 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &dd->oids[i]);
193 DEBUG(PLUGIN_NAME ": OID[%" PRIsz "]: %s", i, oid_str);
195 DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale);
196 DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift);
198 #endif /* COLLECT_DEBUG */
201 static int snmp_agent_validate_data(void) {
203 snmp_agent_dump_data();
205 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
206 table_definition_t *td = te->value;
208 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
209 data_definition_t *dd = de->value;
212 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'.'%s'", td->name,
217 if (dd->plugin_instance) {
218 ERROR(PLUGIN_NAME ": PluginInstance should not be defined for table "
219 "data type '%s'.'%s'",
224 if (dd->oids_len == 0) {
225 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'.'%s'", td->name,
230 if (dd->is_instance) {
232 if (dd->type || dd->type_instance) {
233 ERROR(PLUGIN_NAME ": Type and TypeInstance are not valid for "
234 "instance data '%s'.'%s'",
239 if (dd->oids_len > 1) {
242 ": Only one OID should be specified for instance data '%s'.'%s'",
249 ERROR(PLUGIN_NAME ": Type not defined for data '%s'.'%s'", td->name,
257 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
258 data_definition_t *dd = e->value;
261 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'", dd->name);
265 if (dd->oids_len == 0) {
266 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'", dd->name);
270 if (dd->is_instance) {
272 ": Instance flag can't be specified for scalar data '%s'",
278 ERROR(PLUGIN_NAME ": Type not defined for data '%s'", dd->name);
286 static void snmp_agent_generate_oid2string(oid_t *oid, size_t offset,
288 int key_len = oid->oid[offset];
291 for (i = 0; i < key_len && offset < oid->oid_len; i++)
292 key[i] = oid->oid[++offset];
297 static int snmp_agent_generate_string2oid(oid_t *oid, const char *key) {
298 int key_len = strlen(key);
300 oid->oid[oid->oid_len++] = key_len;
301 for (int i = 0; i < key_len; i++) {
302 oid->oid[oid->oid_len++] = key[i];
303 if (oid->oid_len >= MAX_OID_LEN) {
304 ERROR(PLUGIN_NAME ": Conversion key string %s to OID failed", key);
312 static int snmp_agent_register_oid_string(oid_t *oid, const char *key,
313 Netsnmp_Node_Handler *handler) {
316 memcpy(&new_oid, oid, sizeof(*oid));
317 int ret = snmp_agent_generate_string2oid(&new_oid, key);
321 return snmp_agent_register_oid(&new_oid, handler);
324 static int snmp_agent_unregister_oid_string(oid_t *oid, const char *key) {
327 memcpy(&new_oid, oid, sizeof(*oid));
328 int ret = snmp_agent_generate_string2oid(&new_oid, key);
332 return unregister_mib(new_oid.oid, new_oid.oid_len);
335 static int snmp_agent_table_row_remove(table_definition_t *td,
336 const char *instance) {
340 if (td->index_oid.oid_len) {
341 if ((c_avl_get(td->instance_index, instance, (void **)&index) != 0) ||
342 (c_avl_get(td->index_instance, index, (void **)&ins) != 0))
345 if (c_avl_get(td->instance_index, instance, (void **)&ins) != 0)
349 pthread_mutex_lock(&g_agent->agentx_lock);
351 if (td->index_oid.oid_len)
352 snmp_agent_unregister_oid_index(&td->index_oid, *index);
354 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
355 data_definition_t *dd = de->value;
357 for (size_t i = 0; i < dd->oids_len; i++)
358 if (td->index_oid.oid_len)
359 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
361 snmp_agent_unregister_oid_string(&dd->oids[i], ins);
364 pthread_mutex_unlock(&g_agent->agentx_lock);
366 DEBUG(PLUGIN_NAME ": Removed row for '%s' table [%d, %s]", td->name,
367 (index != NULL) ? *index : -1, ins);
370 .severity = NOTIF_WARNING, .time = cdtime(), .plugin = PLUGIN_NAME};
371 sstrncpy(n.host, hostname_g, sizeof(n.host));
372 sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance));
373 snprintf(n.message, sizeof(n.message),
374 "Removed data row from table %s instance %s index %d", td->name, ins,
375 (index != NULL) ? *index : -1);
376 plugin_dispatch_notification(&n);
378 if (td->index_oid.oid_len) {
379 c_avl_remove(td->index_instance, index, NULL, (void **)&ins);
380 c_avl_remove(td->instance_index, instance, NULL, (void **)&index);
384 c_avl_remove(td->instance_index, instance, NULL, (void **)&ins);
391 static int snmp_agent_clear_missing(const value_list_t *vl,
392 __attribute__((unused)) user_data_t *ud) {
396 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
397 table_definition_t *td = te->value;
399 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
400 data_definition_t *dd = de->value;
402 if (!dd->is_instance) {
403 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
404 vl->type_instance)) {
405 snmp_agent_table_row_remove(td, vl->plugin_instance);
415 static void snmp_agent_free_data(data_definition_t **dd) {
417 if (dd == NULL || *dd == NULL)
420 /* unregister scalar type OID */
421 if ((*dd)->table == NULL) {
422 for (size_t i = 0; i < (*dd)->oids_len; i++)
423 unregister_mib((*dd)->oids[i].oid, (*dd)->oids[i].oid_len);
427 sfree((*dd)->plugin);
428 sfree((*dd)->plugin_instance);
430 sfree((*dd)->type_instance);
438 static void snmp_agent_free_table_columns(table_definition_t *td) {
439 if (td->columns == NULL)
442 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
443 data_definition_t *dd = de->value;
445 if (td->index_oid.oid_len) {
449 c_avl_iterator_t *iter = c_avl_get_iterator(td->index_instance);
450 while (c_avl_iterator_next(iter, (void *)&index, (void *)&instance) ==
452 for (size_t i = 0; i < dd->oids_len; i++)
453 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
455 c_avl_iterator_destroy(iter);
459 c_avl_iterator_t *iter = c_avl_get_iterator(dd->table->instance_index);
460 while (c_avl_iterator_next(iter, (void *)&instance, (void *)&instance) ==
462 for (size_t i = 0; i < dd->oids_len; i++)
463 snmp_agent_unregister_oid_string(&dd->oids[i], instance);
465 c_avl_iterator_destroy(iter);
468 snmp_agent_free_data(&dd);
471 llist_destroy(td->columns);
473 } /* void snmp_agent_free_table_columns */
475 static void snmp_agent_free_table(table_definition_t **td) {
477 if (td == NULL || *td == NULL)
480 if ((*td)->size_oid.oid_len)
481 unregister_mib((*td)->size_oid.oid, (*td)->size_oid.oid_len);
483 /* Unregister Index OIDs */
484 if ((*td)->index_oid.oid_len) {
488 c_avl_iterator_t *iter = c_avl_get_iterator((*td)->index_instance);
489 while (c_avl_iterator_next(iter, (void *)&index, (void *)&instance) == 0)
490 snmp_agent_unregister_oid_index(&(*td)->index_oid, *index);
492 c_avl_iterator_destroy(iter);
495 /* Unregister all table columns and their registered OIDs */
496 snmp_agent_free_table_columns(*td);
501 /* index_instance and instance_index contain the same pointers */
502 c_avl_destroy((*td)->index_instance);
503 (*td)->index_instance = NULL;
505 if ((*td)->instance_index != NULL) {
506 while (c_avl_pick((*td)->instance_index, &key, &value) == 0) {
511 c_avl_destroy((*td)->instance_index);
512 (*td)->instance_index = NULL;
521 static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests,
522 data_definition_t *dd, char *instance,
524 char name[DATA_MAX_NAME_LEN];
525 format_name(name, sizeof(name), hostname_g, dd->plugin,
526 instance ? instance : dd->plugin_instance, dd->type,
528 DEBUG(PLUGIN_NAME ": Identifier '%s'", name);
532 const data_set_t *ds = plugin_get_ds(dd->type);
534 ERROR(PLUGIN_NAME ": Data set not found for '%s' type", dd->type);
535 return SNMP_NOSUCHINSTANCE;
538 int ret = uc_get_value_by_name(name, &values, &values_num);
541 ERROR(PLUGIN_NAME ": Failed to get value for '%s'", name);
542 return SNMP_NOSUCHINSTANCE;
545 assert(ds->ds_num == values_num);
546 assert(oid_index < (int)values_num);
548 char data[DATA_MAX_NAME_LEN];
549 size_t data_len = sizeof(data);
550 ret = snmp_agent_set_vardata(
551 data, &data_len, dd->oids[oid_index].type, dd->scale, dd->shift,
552 &values[oid_index], sizeof(values[oid_index]), ds->ds[oid_index].type);
557 ERROR(PLUGIN_NAME ": Failed to convert '%s' value to snmp data", name);
558 return SNMP_NOSUCHINSTANCE;
561 requests->requestvb->type = dd->oids[oid_index].type;
562 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
563 (const u_char *)data, data_len);
565 return SNMP_ERR_NOERROR;
569 snmp_agent_table_oid_handler(struct netsnmp_mib_handler_s *handler,
570 struct netsnmp_handler_registration_s *reginfo,
571 struct netsnmp_agent_request_info_s *reqinfo,
572 struct netsnmp_request_info_s *requests) {
574 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
575 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
576 return SNMP_ERR_NOERROR;
579 pthread_mutex_lock(&g_agent->lock);
582 memcpy(oid.oid, requests->requestvb->name,
583 sizeof(oid.oid[0]) * requests->requestvb->name_length);
584 oid.oid_len = requests->requestvb->name_length;
587 char oid_str[DATA_MAX_NAME_LEN];
588 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
589 DEBUG(PLUGIN_NAME ": Get request received for table OID '%s'", oid_str);
592 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
593 table_definition_t *td = te->value;
595 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
596 data_definition_t *dd = de->value;
598 for (size_t i = 0; i < dd->oids_len; i++) {
599 int ret = snmp_oid_ncompare(oid.oid, oid.oid_len, dd->oids[i].oid,
601 MIN(oid.oid_len, dd->oids[i].oid_len));
607 if (!td->index_oid.oid_len) {
608 char key[MAX_OID_LEN];
610 memset(key, 0, sizeof(key));
611 snmp_agent_generate_oid2string(
612 &oid, MIN(oid.oid_len, dd->oids[i].oid_len), key);
614 ret = c_avl_get(td->instance_index, key, (void **)&instance);
616 DEBUG(PLUGIN_NAME ": Nonexisting index string '%s' requested", key);
617 pthread_mutex_unlock(&g_agent->lock);
618 return SNMP_NOSUCHINSTANCE;
621 int index = oid.oid[oid.oid_len - 1];
623 ret = c_avl_get(td->index_instance, &index, (void **)&instance);
625 DEBUG(PLUGIN_NAME ": Nonexisting index '%d' requested", index);
626 pthread_mutex_unlock(&g_agent->lock);
627 return SNMP_NOSUCHINSTANCE;
631 if (dd->is_instance) {
632 requests->requestvb->type = ASN_OCTET_STR;
633 snmp_set_var_typed_value(
634 requests->requestvb, requests->requestvb->type,
635 (const u_char *)instance, strlen((instance)));
637 pthread_mutex_unlock(&g_agent->lock);
639 return SNMP_ERR_NOERROR;
642 ret = snmp_agent_form_reply(requests, dd, instance, i);
644 pthread_mutex_unlock(&g_agent->lock);
651 pthread_mutex_unlock(&g_agent->lock);
653 return SNMP_NOSUCHINSTANCE;
656 static int snmp_agent_table_index_oid_handler(
657 struct netsnmp_mib_handler_s *handler,
658 struct netsnmp_handler_registration_s *reginfo,
659 struct netsnmp_agent_request_info_s *reqinfo,
660 struct netsnmp_request_info_s *requests) {
662 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
663 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
664 return SNMP_ERR_NOERROR;
667 pthread_mutex_lock(&g_agent->lock);
670 memcpy(oid.oid, requests->requestvb->name,
671 sizeof(oid.oid[0]) * requests->requestvb->name_length);
672 oid.oid_len = requests->requestvb->name_length;
674 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
675 table_definition_t *td = te->value;
677 if (td->index_oid.oid_len &&
678 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->index_oid.oid,
679 td->index_oid.oid_len,
680 MIN(oid.oid_len, td->index_oid.oid_len)) == 0)) {
682 DEBUG(PLUGIN_NAME ": Handle '%s' table index OID", td->name);
684 int index = oid.oid[oid.oid_len - 1];
686 int ret = c_avl_get(td->index_instance, &index, &(void *){NULL});
688 /* nonexisting index requested */
689 pthread_mutex_unlock(&g_agent->lock);
690 return SNMP_NOSUCHINSTANCE;
693 requests->requestvb->type = ASN_INTEGER;
694 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
695 (const u_char *)&index, sizeof(index));
697 pthread_mutex_unlock(&g_agent->lock);
699 return SNMP_ERR_NOERROR;
703 pthread_mutex_unlock(&g_agent->lock);
705 return SNMP_NOSUCHINSTANCE;
708 static int snmp_agent_table_size_oid_handler(
709 struct netsnmp_mib_handler_s *handler,
710 struct netsnmp_handler_registration_s *reginfo,
711 struct netsnmp_agent_request_info_s *reqinfo,
712 struct netsnmp_request_info_s *requests) {
714 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
715 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
716 return SNMP_ERR_NOERROR;
719 pthread_mutex_lock(&g_agent->lock);
722 memcpy(oid.oid, requests->requestvb->name,
723 sizeof(oid.oid[0]) * requests->requestvb->name_length);
724 oid.oid_len = requests->requestvb->name_length;
726 DEBUG(PLUGIN_NAME ": Get request received for table size OID");
728 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
729 table_definition_t *td = te->value;
731 if (td->size_oid.oid_len &&
732 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->size_oid.oid,
733 td->size_oid.oid_len,
734 MIN(oid.oid_len, td->size_oid.oid_len)) == 0)) {
735 DEBUG(PLUGIN_NAME ": Handle '%s' table size OID", td->name);
737 long size = c_avl_size(td->index_instance);
739 requests->requestvb->type = td->size_oid.type;
740 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
741 (const u_char *)&size, sizeof(size));
743 pthread_mutex_unlock(&g_agent->lock);
745 return SNMP_ERR_NOERROR;
749 pthread_mutex_unlock(&g_agent->lock);
751 return SNMP_NOSUCHINSTANCE;
755 snmp_agent_scalar_oid_handler(struct netsnmp_mib_handler_s *handler,
756 struct netsnmp_handler_registration_s *reginfo,
757 struct netsnmp_agent_request_info_s *reqinfo,
758 struct netsnmp_request_info_s *requests) {
760 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
761 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
762 return SNMP_ERR_NOERROR;
765 pthread_mutex_lock(&g_agent->lock);
768 memcpy(oid.oid, requests->requestvb->name,
769 sizeof(oid.oid[0]) * requests->requestvb->name_length);
770 oid.oid_len = requests->requestvb->name_length;
773 char oid_str[DATA_MAX_NAME_LEN];
774 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
775 DEBUG(PLUGIN_NAME ": Get request received for scalar OID '%s'", oid_str);
778 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL;
780 data_definition_t *dd = de->value;
782 for (size_t i = 0; i < dd->oids_len; i++) {
784 int ret = snmp_oid_compare(oid.oid, oid.oid_len, dd->oids[i].oid,
785 dd->oids[i].oid_len);
789 ret = snmp_agent_form_reply(requests, dd, NULL, i);
791 pthread_mutex_unlock(&g_agent->lock);
797 pthread_mutex_unlock(&g_agent->lock);
799 return SNMP_NOSUCHINSTANCE;
802 static int snmp_agent_register_table_oids(void) {
804 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
805 table_definition_t *td = te->value;
807 if (td->size_oid.oid_len != 0) {
809 snmp_agent_get_asn_type(td->size_oid.oid, td->size_oid.oid_len);
810 td->size_oid.oid_len++;
811 int ret = snmp_agent_register_oid(&td->size_oid,
812 snmp_agent_table_size_oid_handler);
817 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
818 data_definition_t *dd = de->value;
820 for (size_t i = 0; i < dd->oids_len; i++) {
822 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
830 static int snmp_agent_register_scalar_oids(void) {
832 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
833 data_definition_t *dd = e->value;
835 for (size_t i = 0; i < dd->oids_len; i++) {
838 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
841 snmp_agent_register_oid(&dd->oids[i], snmp_agent_scalar_oid_handler);
850 static int snmp_agent_config_data_oids(data_definition_t *dd,
851 oconfig_item_t *ci) {
852 if (ci->values_num < 1) {
853 WARNING(PLUGIN_NAME ": `OIDs' needs at least one argument");
857 for (int i = 0; i < ci->values_num; i++)
858 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
859 WARNING(PLUGIN_NAME ": `OIDs' needs only string argument");
863 if (dd->oids != NULL)
866 dd->oids = calloc(ci->values_num, sizeof(*dd->oids));
867 if (dd->oids == NULL)
869 dd->oids_len = (size_t)ci->values_num;
871 for (int i = 0; i < ci->values_num; i++) {
872 dd->oids[i].oid_len = MAX_OID_LEN;
874 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->oids[i].oid,
875 &dd->oids[i].oid_len)) {
876 ERROR(PLUGIN_NAME ": snmp_parse_oid (%s) failed",
877 ci->values[i].value.string);
887 static int snmp_agent_config_table_size_oid(table_definition_t *td,
888 oconfig_item_t *ci) {
889 if (ci->values_num < 1) {
890 WARNING(PLUGIN_NAME ": `TableSizeOID' is empty");
894 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
895 WARNING(PLUGIN_NAME ": `TableSizeOID' needs only string argument");
899 td->size_oid.oid_len = MAX_OID_LEN;
901 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->size_oid.oid,
902 &td->size_oid.oid_len)) {
903 ERROR(PLUGIN_NAME ": Failed to parse table size OID (%s)",
904 ci->values[0].value.string);
905 td->size_oid.oid_len = 0;
912 static int snmp_agent_config_table_index_oid(table_definition_t *td,
913 oconfig_item_t *ci) {
915 if (ci->values_num < 1) {
916 WARNING(PLUGIN_NAME ": `IndexOID' is empty");
920 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
921 WARNING(PLUGIN_NAME ": `IndexOID' needs only string argument");
925 td->index_oid.oid_len = MAX_OID_LEN;
927 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->index_oid.oid,
928 &td->index_oid.oid_len)) {
929 ERROR(PLUGIN_NAME ": Failed to parse table index OID (%s)",
930 ci->values[0].value.string);
931 td->index_oid.oid_len = 0;
938 static int snmp_agent_config_table_data(table_definition_t *td,
939 oconfig_item_t *ci) {
940 data_definition_t *dd;
945 dd = calloc(1, sizeof(*dd));
947 ERROR(PLUGIN_NAME ": Failed to allocate memory for table data definition");
951 ret = cf_util_get_string(ci, &dd->name);
962 for (int i = 0; i < ci->children_num; i++) {
963 oconfig_item_t *option = ci->children + i;
965 if (strcasecmp("Instance", option->key) == 0)
966 ret = cf_util_get_boolean(option, &dd->is_instance);
967 else if (strcasecmp("Plugin", option->key) == 0)
968 ret = cf_util_get_string(option, &dd->plugin);
969 else if (strcasecmp("PluginInstance", option->key) == 0)
970 ret = cf_util_get_string(option, &dd->plugin_instance);
971 else if (strcasecmp("Type", option->key) == 0)
972 ret = cf_util_get_string(option, &dd->type);
973 else if (strcasecmp("TypeInstance", option->key) == 0)
974 ret = cf_util_get_string(option, &dd->type_instance);
975 else if (strcasecmp("Shift", option->key) == 0)
976 ret = cf_util_get_double(option, &dd->shift);
977 else if (strcasecmp("Scale", option->key) == 0)
978 ret = cf_util_get_double(option, &dd->scale);
979 else if (strcasecmp("OIDs", option->key) == 0)
980 ret = snmp_agent_config_data_oids(dd, option);
982 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
987 snmp_agent_free_data(&dd);
992 llentry_t *entry = llentry_create(dd->name, dd);
994 snmp_agent_free_data(&dd);
998 llist_append(td->columns, entry);
1003 static int snmp_agent_config_data(oconfig_item_t *ci) {
1004 data_definition_t *dd;
1009 dd = calloc(1, sizeof(*dd));
1011 ERROR(PLUGIN_NAME ": Failed to allocate memory for data definition");
1015 ret = cf_util_get_string(ci, &dd->name);
1024 for (int i = 0; i < ci->children_num; i++) {
1025 oconfig_item_t *option = ci->children + i;
1027 if (strcasecmp("Instance", option->key) == 0)
1028 ret = cf_util_get_boolean(option, &dd->is_instance);
1029 else if (strcasecmp("Plugin", option->key) == 0)
1030 ret = cf_util_get_string(option, &dd->plugin);
1031 else if (strcasecmp("PluginInstance", option->key) == 0)
1032 ret = cf_util_get_string(option, &dd->plugin_instance);
1033 else if (strcasecmp("Type", option->key) == 0)
1034 ret = cf_util_get_string(option, &dd->type);
1035 else if (strcasecmp("TypeInstance", option->key) == 0)
1036 ret = cf_util_get_string(option, &dd->type_instance);
1037 else if (strcasecmp("Shift", option->key) == 0)
1038 ret = cf_util_get_double(option, &dd->shift);
1039 else if (strcasecmp("Scale", option->key) == 0)
1040 ret = cf_util_get_double(option, &dd->scale);
1041 else if (strcasecmp("OIDs", option->key) == 0)
1042 ret = snmp_agent_config_data_oids(dd, option);
1044 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
1049 snmp_agent_free_data(&dd);
1054 llentry_t *entry = llentry_create(dd->name, dd);
1055 if (entry == NULL) {
1056 snmp_agent_free_data(&dd);
1060 llist_append(g_agent->scalars, entry);
1065 static int num_compare(const int *a, const int *b) {
1066 assert((a != NULL) && (b != NULL));
1075 static int snmp_agent_config_table(oconfig_item_t *ci) {
1076 table_definition_t *td;
1081 td = calloc(1, sizeof(*td));
1083 ERROR(PLUGIN_NAME ": Failed to allocate memory for table definition");
1087 ret = cf_util_get_string(ci, &td->name);
1093 td->columns = llist_create();
1094 if (td->columns == NULL) {
1095 ERROR(PLUGIN_NAME ": Failed to allocate memory for columns list");
1096 snmp_agent_free_table(&td);
1100 for (int i = 0; i < ci->children_num; i++) {
1101 oconfig_item_t *option = ci->children + i;
1103 if (strcasecmp("IndexOID", option->key) == 0)
1104 ret = snmp_agent_config_table_index_oid(td, option);
1105 else if (strcasecmp("SizeOID", option->key) == 0)
1106 ret = snmp_agent_config_table_size_oid(td, option);
1107 else if (strcasecmp("Data", option->key) == 0)
1108 ret = snmp_agent_config_table_data(td, option);
1110 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
1115 snmp_agent_free_table(&td);
1120 td->instance_index =
1121 c_avl_create((int (*)(const void *, const void *))strcmp);
1122 if (td->instance_index == NULL) {
1123 snmp_agent_free_table(&td);
1127 td->index_instance =
1128 c_avl_create((int (*)(const void *, const void *))num_compare);
1129 if (td->index_instance == NULL) {
1130 snmp_agent_free_table(&td);
1134 llentry_t *entry = llentry_create(td->name, td);
1135 if (entry == NULL) {
1136 snmp_agent_free_table(&td);
1139 llist_append(g_agent->tables, entry);
1144 static int snmp_agent_get_value_from_ds_type(const value_t *val, int type,
1145 double scale, double shift,
1148 case DS_TYPE_COUNTER:
1149 *value = (long)((val->counter * scale) + shift);
1151 case DS_TYPE_ABSOLUTE:
1152 *value = (long)((val->absolute * scale) + shift);
1154 case DS_TYPE_DERIVE:
1155 *value = (long)((val->derive * scale) + shift);
1158 *value = (long)((val->gauge * scale) + shift);
1163 ERROR(PLUGIN_NAME ": Unknown data source type: %i", type);
1170 static int snmp_agent_set_vardata(void *data, size_t *data_len, u_char asn_type,
1171 double scale, double shift, const void *value,
1172 size_t len, int type) {
1175 netsnmp_vardata var;
1180 var.string = (u_char *)data;
1182 ret = snmp_agent_get_value_from_ds_type(val, type, scale, shift, &new_value);
1192 if (*data_len < sizeof(*var.integer))
1194 *var.integer = new_value;
1195 *data_len = sizeof(*var.integer);
1198 if (*data_len < sizeof(*var.counter64))
1200 var.counter64->high = (u_long)((int64_t)new_value >> 32);
1201 var.counter64->low = (u_long)((int64_t)new_value & 0xFFFFFFFF);
1202 *data_len = sizeof(*var.counter64);
1205 if (type == DS_TYPE_GAUGE) {
1206 char buf[DATA_MAX_NAME_LEN];
1207 snprintf(buf, sizeof(buf), "%.2f", val->gauge);
1208 if (*data_len < strlen(buf))
1210 *data_len = strlen(buf);
1211 memcpy(var.string, buf, *data_len);
1213 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1219 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1227 static int snmp_agent_register_oid_index(oid_t *oid, int index,
1228 Netsnmp_Node_Handler *handler) {
1230 memcpy(&new_oid, oid, sizeof(*oid));
1231 new_oid.oid[new_oid.oid_len++] = index;
1232 return snmp_agent_register_oid(&new_oid, handler);
1235 static int snmp_agent_unregister_oid_index(oid_t *oid, int index) {
1237 memcpy(&new_oid, oid, sizeof(*oid));
1238 new_oid.oid[new_oid.oid_len++] = index;
1239 return unregister_mib(new_oid.oid, new_oid.oid_len);
1242 static int snmp_agent_update_index(table_definition_t *td,
1243 const char *instance) {
1245 if (c_avl_get(td->instance_index, instance, NULL) == 0)
1252 ins = strdup(instance);
1256 /* need to generate index for the table */
1257 if (td->index_oid.oid_len) {
1258 index = calloc(1, sizeof(*index));
1259 if (index == NULL) {
1264 *index = c_avl_size(td->instance_index) + 1;
1266 ret = c_avl_insert(td->instance_index, ins, index);
1273 ret = c_avl_insert(td->index_instance, index, ins);
1275 DEBUG(PLUGIN_NAME ": Failed to update index_instance for '%s' table",
1277 c_avl_remove(td->instance_index, ins, NULL, (void **)&index);
1283 ret = snmp_agent_register_oid_index(&td->index_oid, *index,
1284 snmp_agent_table_index_oid_handler);
1288 /* instance as a key is required for any table */
1289 ret = c_avl_insert(td->instance_index, ins, ins);
1296 /* register new oids for all columns */
1297 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1298 data_definition_t *dd = de->value;
1300 for (size_t i = 0; i < dd->oids_len; i++) {
1301 if (td->index_oid.oid_len) {
1302 ret = snmp_agent_register_oid_index(&dd->oids[i], *index,
1303 snmp_agent_table_oid_handler);
1305 ret = snmp_agent_register_oid_string(&dd->oids[i], ins,
1306 snmp_agent_table_oid_handler);
1314 DEBUG(PLUGIN_NAME ": Updated index for '%s' table [%d, %s]", td->name,
1315 (index != NULL) ? *index : -1, ins);
1317 notification_t n = {
1318 .severity = NOTIF_OKAY, .time = cdtime(), .plugin = PLUGIN_NAME};
1319 sstrncpy(n.host, hostname_g, sizeof(n.host));
1320 sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance));
1321 snprintf(n.message, sizeof(n.message),
1322 "Data row added to table %s instance %s index %d", td->name, ins,
1323 (index != NULL) ? *index : -1);
1324 plugin_dispatch_notification(&n);
1329 static int snmp_agent_write(value_list_t const *vl) {
1334 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1335 table_definition_t *td = te->value;
1337 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1338 data_definition_t *dd = de->value;
1340 if (!dd->is_instance) {
1341 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
1342 vl->type_instance)) {
1343 snmp_agent_update_index(td, vl->plugin_instance);
1353 static int snmp_agent_collect(const data_set_t *ds, const value_list_t *vl,
1354 user_data_t __attribute__((unused)) * user_data) {
1356 pthread_mutex_lock(&g_agent->lock);
1358 snmp_agent_write(vl);
1360 pthread_mutex_unlock(&g_agent->lock);
1365 static int snmp_agent_preinit(void) {
1366 if (g_agent != NULL) {
1367 /* already initialized if config callback was called before init callback */
1371 g_agent = calloc(1, sizeof(*g_agent));
1372 if (g_agent == NULL) {
1373 ERROR(PLUGIN_NAME ": Failed to allocate memory for snmp agent context");
1377 g_agent->tables = llist_create();
1378 g_agent->scalars = llist_create();
1380 if (g_agent->tables == NULL || g_agent->scalars == NULL) {
1381 ERROR(PLUGIN_NAME ": llist_create() failed");
1382 llist_destroy(g_agent->scalars);
1383 llist_destroy(g_agent->tables);
1388 /* make us a agentx client. */
1389 err = netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE,
1392 ERROR(PLUGIN_NAME ": Failed to set agent role (%d)", err);
1393 llist_destroy(g_agent->scalars);
1394 llist_destroy(g_agent->tables);
1399 * For SNMP debug purposes uses snmp_set_do_debugging(1);
1402 /* initialize the agent library */
1403 err = init_agent(PLUGIN_NAME);
1405 ERROR(PLUGIN_NAME ": Failed to initialize the agent library (%d)", err);
1406 llist_destroy(g_agent->scalars);
1407 llist_destroy(g_agent->tables);
1411 init_snmp(PLUGIN_NAME);
1413 g_agent->tp = read_all_mibs();
1418 static int snmp_agent_init(void) {
1421 if (g_agent == NULL || ((llist_head(g_agent->scalars) == NULL) &&
1422 (llist_head(g_agent->tables) == NULL))) {
1423 ERROR(PLUGIN_NAME ": snmp_agent_init: plugin not configured");
1427 plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown);
1429 ret = snmp_agent_register_scalar_oids();
1433 ret = snmp_agent_register_table_oids();
1437 ret = pthread_mutex_init(&g_agent->lock, NULL);
1439 ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret);
1443 ret = pthread_mutex_init(&g_agent->agentx_lock, NULL);
1445 ERROR(PLUGIN_NAME ": Failed to initialize AgentX mutex, err %u", ret);
1449 /* create a second thread to listen for requests from AgentX*/
1450 ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
1452 ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
1456 if (llist_head(g_agent->tables) != NULL) {
1457 plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL);
1458 plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL);
1464 static void *snmp_agent_thread_run(void __attribute__((unused)) * arg) {
1465 INFO(PLUGIN_NAME ": Thread is up and running");
1468 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1470 pthread_mutex_lock(&g_agent->agentx_lock);
1471 agent_check_and_process(0); /* 0 == don't block */
1472 pthread_mutex_unlock(&g_agent->agentx_lock);
1474 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
1481 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler) {
1482 netsnmp_handler_registration *reg;
1483 char *oid_name = snmp_agent_get_oid_name(oid->oid, oid->oid_len - 1);
1484 char oid_str[DATA_MAX_NAME_LEN];
1486 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), oid);
1488 if (oid_name == NULL) {
1490 ": Skipped registration: OID (%s) is not found in main tree",
1495 reg = netsnmp_create_handler_registration(oid_name, handler, oid->oid,
1496 oid->oid_len, HANDLER_CAN_RONLY);
1498 ERROR(PLUGIN_NAME ": Failed to create handler registration for OID (%s)",
1503 pthread_mutex_lock(&g_agent->agentx_lock);
1505 if (netsnmp_register_instance(reg) != MIB_REGISTERED_OK) {
1506 ERROR(PLUGIN_NAME ": Failed to register handler for OID (%s)", oid_str);
1507 pthread_mutex_unlock(&g_agent->agentx_lock);
1511 pthread_mutex_unlock(&g_agent->agentx_lock);
1513 DEBUG(PLUGIN_NAME ": Registered handler for OID (%s)", oid_str);
1518 static int snmp_agent_free_config(void) {
1520 if (g_agent == NULL)
1523 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next)
1524 snmp_agent_free_table((table_definition_t **)&te->value);
1525 llist_destroy(g_agent->tables);
1527 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL; de = de->next)
1528 snmp_agent_free_data((data_definition_t **)&de->value);
1529 llist_destroy(g_agent->scalars);
1534 static int snmp_agent_shutdown(void) {
1537 DEBUG(PLUGIN_NAME ": snmp_agent_shutdown");
1539 if (g_agent == NULL) {
1540 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: plugin not initialized");
1544 if (pthread_cancel(g_agent->thread) != 0)
1545 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to cancel the thread");
1547 if (pthread_join(g_agent->thread, NULL) != 0)
1548 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to join the thread");
1550 snmp_agent_free_config();
1552 snmp_shutdown(PLUGIN_NAME);
1554 pthread_mutex_destroy(&g_agent->lock);
1555 pthread_mutex_destroy(&g_agent->agentx_lock);
1562 static int snmp_agent_config(oconfig_item_t *ci) {
1564 int ret = snmp_agent_preinit();
1571 for (int i = 0; i < ci->children_num; i++) {
1572 oconfig_item_t *child = ci->children + i;
1573 if (strcasecmp("Data", child->key) == 0) {
1574 ret = snmp_agent_config_data(child);
1575 } else if (strcasecmp("Table", child->key) == 0) {
1576 ret = snmp_agent_config_table(child);
1578 ERROR(PLUGIN_NAME ": Unknown configuration option `%s'", child->key);
1583 ERROR(PLUGIN_NAME ": Failed to parse configuration");
1584 snmp_agent_free_config();
1585 snmp_shutdown(PLUGIN_NAME);
1591 ret = snmp_agent_validate_data();
1593 ERROR(PLUGIN_NAME ": Invalid configuration provided");
1594 snmp_agent_free_config();
1595 snmp_shutdown(PLUGIN_NAME);
1603 void module_register(void) {
1604 plugin_register_init(PLUGIN_NAME, snmp_agent_init);
1605 plugin_register_complex_config(PLUGIN_NAME, snmp_agent_config);