2 * collectd - src/snmp_agent.c
4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * 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>
31 #include "utils_avltree.h"
32 #include "utils_llist.h"
33 #include "utils_cache.h"
35 #include <net-snmp/net-snmp-config.h>
36 #include <net-snmp/net-snmp-includes.h>
37 #include <net-snmp/agent/net-snmp-agent-includes.h>
39 #define PLUGIN_NAME "snmp_agent"
40 #define ERR_BUF_SIZE 1024
41 #define TYPE_STRING -1
44 #define MIN(x, y) ((x) < (y) ? (x) : (y))
52 typedef struct oid_s oid_t;
54 struct table_definition_s {
59 c_avl_tree_t *instance_index;
60 c_avl_tree_t *index_instance;
62 typedef struct table_definition_s table_definition_t;
64 struct data_definition_s {
67 char *plugin_instance;
70 table_definition_t *table;
77 typedef struct data_definition_s data_definition_t;
79 struct snmp_agent_ctx_s {
82 pthread_mutex_t agentx_lock;
88 typedef struct snmp_agent_ctx_s snmp_agent_ctx_t;
90 snmp_agent_ctx_t *g_agent = NULL;
92 #define CHECK_DD_TYPE(_dd, _p, _pi, _t, _ti) \
93 (_dd->plugin ? !strcmp(_dd->plugin, _p) : 0) && \
94 (_dd->plugin_instance ? !strcmp(_dd->plugin_instance, _pi) : 1) && \
95 (_dd->type ? !strcmp(_dd->type, _t) : 1) && \
96 (_dd->type_instance ? !strcmp(_dd->type_instance, _ti) : 1)
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 ssnprintf(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 static void snmp_agent_dump_data(void) {
132 char temp[DATA_MAX_NAME_LEN];
134 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
135 table_definition_t *td = (table_definition_t *)te->value;
137 DEBUG(PLUGIN_NAME ": Table:");
138 DEBUG(PLUGIN_NAME ": Name: %s", td->name);
139 if (td->index_oid.oid_len != 0) {
140 snmp_agent_oid_to_string(temp, sizeof(temp), &td->index_oid);
141 DEBUG(PLUGIN_NAME ": IndexOID: %s", temp);
143 if (td->size_oid.oid_len != 0) {
144 snmp_agent_oid_to_string(temp, sizeof(temp), &td->size_oid);
145 DEBUG(PLUGIN_NAME ": SizeOID: %s", temp);
148 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
149 data_definition_t *dd = (data_definition_t *)de->value;
151 DEBUG(PLUGIN_NAME ": Column:");
152 DEBUG(PLUGIN_NAME ": Name: %s", dd->name);
154 DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin);
155 if (dd->plugin_instance)
156 DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance);
158 DEBUG(PLUGIN_NAME ": Instance: true");
160 DEBUG(PLUGIN_NAME ": Type: %s", dd->type);
161 if (dd->type_instance)
162 DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance);
163 for (int i = 0; i < dd->oids_len; i++) {
164 snmp_agent_oid_to_string(temp, sizeof(temp), &dd->oids[i]);
165 DEBUG(PLUGIN_NAME ": OID[%d]: %s", i, temp);
167 DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale);
168 DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift);
172 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
173 data_definition_t *dd = (data_definition_t *)e->value;
175 DEBUG(PLUGIN_NAME ": Scalar:");
176 DEBUG(PLUGIN_NAME ": Name: %s", dd->name);
178 DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin);
179 if (dd->plugin_instance)
180 DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance);
182 DEBUG(PLUGIN_NAME ": Instance: true");
184 DEBUG(PLUGIN_NAME ": Type: %s", dd->type);
185 if (dd->type_instance)
186 DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance);
187 for (int i = 0; i < dd->oids_len; i++) {
188 snmp_agent_oid_to_string(temp, sizeof(temp), &dd->oids[i]);
189 DEBUG(PLUGIN_NAME ": OID[%d]: %s", i, temp);
191 DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale);
192 DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift);
194 #endif /* COLLECT_DEBUG */
197 static int snmp_agent_validate_data(void) {
199 snmp_agent_dump_data();
201 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
202 table_definition_t *td = (table_definition_t *)te->value;
204 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
205 data_definition_t *dd = (data_definition_t *)de->value;
208 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'.'%s'", td->name,
213 if (dd->plugin_instance) {
214 ERROR(PLUGIN_NAME ": PluginInstance should not be defined for table "
215 "data type '%s'.'%s'",
220 if (dd->oids_len == 0) {
221 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'.'%s'", td->name,
226 if (dd->is_instance) {
228 if (dd->type || dd->type_instance) {
229 ERROR(PLUGIN_NAME ": Type and TypeInstance are not valid for "
230 "instance data '%s'.'%s'",
235 if (dd->oids_len > 1) {
238 ": Only one OID should be specified for instance data '%s'.'%s'",
245 ERROR(PLUGIN_NAME ": Type not defined for data '%s'.'%s'", td->name,
253 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
254 data_definition_t *dd = (data_definition_t *)e->value;
257 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'", dd->name);
261 if (dd->oids_len == 0) {
262 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'", dd->name);
266 if (dd->is_instance) {
268 ": Instance flag can't be specified for scalar data '%s'",
274 ERROR(PLUGIN_NAME ": Type not defined for data '%s'", dd->name);
282 static int snmp_agent_table_row_remove(table_definition_t *td,
283 const char *instance) {
287 if ((c_avl_get(td->instance_index, instance, (void**) &index) != 0) ||
288 (c_avl_get(td->index_instance, index, (void**) &ins) != 0))
291 pthread_mutex_lock(&g_agent->agentx_lock);
293 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
294 data_definition_t *dd = (data_definition_t *)de->value;
296 for (int i = 0; i < dd->oids_len; i++)
297 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
300 snmp_agent_unregister_oid_index(&td->index_oid, *index);
302 pthread_mutex_unlock(&g_agent->agentx_lock);
304 DEBUG(PLUGIN_NAME ": Removed row for '%s' table [%d, %s].", td->name, *index,
307 notification_t n = {NOTIF_WARNING, cdtime(), "", "", PLUGIN_NAME, "", "", "",
309 sstrncpy(n.host, hostname_g, sizeof(n.host));
310 sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance));
311 ssnprintf(n.message, sizeof(n.message),
312 "Removed data row from table %s instance %s index %d", td->name,
314 plugin_dispatch_notification(&n);
316 c_avl_remove(td->index_instance, index, NULL, (void**) &ins);
317 c_avl_remove(td->instance_index, instance, NULL, (void**) &index);
324 static int snmp_agent_clear_missing(const value_list_t *vl,
325 __attribute__((unused)) user_data_t *ud) {
329 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
330 table_definition_t *td = (table_definition_t *)te->value;
332 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
333 data_definition_t *dd = (data_definition_t *)de->value;
335 if (!dd->is_instance) {
336 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
337 vl->type_instance)) {
338 snmp_agent_table_row_remove(td, vl->plugin_instance);
348 static void snmp_agent_free_data(data_definition_t **dd) {
350 if (dd == NULL || *dd == NULL)
353 /* unregister scalar type OID */
354 if ((*dd)->table == NULL) {
355 for (int i = 0; i < (*dd)->oids_len; i++)
356 unregister_mib((*dd)->oids[i].oid, (*dd)->oids[i].oid_len);
358 /* unregister all table OIDs */
362 c_avl_iterator_t *iter = c_avl_get_iterator((*dd)->table->index_instance);
363 while (c_avl_iterator_next(iter, (void *)&index, (void *)&value) == 0) {
364 for (int i = 0; i < (*dd)->oids_len; i++)
365 snmp_agent_unregister_oid_index(&(*dd)->oids[i], *index);
367 c_avl_iterator_destroy(iter);
371 sfree((*dd)->plugin);
372 sfree((*dd)->plugin_instance);
374 sfree((*dd)->type_instance);
382 static void snmp_agent_free_table(table_definition_t **td) {
384 if (td == NULL || *td == NULL)
387 if ((*td)->size_oid.oid_len)
388 unregister_mib((*td)->size_oid.oid, (*td)->size_oid.oid_len);
390 if ((*td)->index_oid.oid_len) {
394 c_avl_iterator_t *iter = c_avl_get_iterator((*td)->index_instance);
395 while (c_avl_iterator_next(iter, (void *)&index, (void *)&value) == 0)
396 snmp_agent_unregister_oid_index(&(*td)->index_oid, *index);
398 c_avl_iterator_destroy(iter);
401 for (llentry_t *de = llist_head((*td)->columns); de != NULL; de = de->next) {
402 data_definition_t *dd = (data_definition_t *)de->value;
403 snmp_agent_free_data(&dd);
406 llist_destroy((*td)->columns);
411 c_avl_destroy((*td)->index_instance);
412 (*td)->index_instance = NULL;
414 while (c_avl_pick((*td)->instance_index, &key, &value) == 0) {
418 c_avl_destroy((*td)->instance_index);
419 (*td)->instance_index = NULL;
428 snmp_agent_table_oid_handler(struct netsnmp_mib_handler_s *handler,
429 struct netsnmp_handler_registration_s *reginfo,
430 struct netsnmp_agent_request_info_s *reqinfo,
431 struct netsnmp_request_info_s *requests) {
433 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
434 DEBUG(PLUGIN_NAME ": Not supported request mode (%d).", reqinfo->mode);
435 return SNMP_ERR_NOERROR;
439 char temp[DATA_MAX_NAME_LEN];
441 pthread_mutex_lock(&g_agent->lock);
443 memcpy(oid.oid, requests->requestvb->name,
444 sizeof(oid.oid[0]) * requests->requestvb->name_length);
445 oid.oid_len = requests->requestvb->name_length;
447 snmp_agent_oid_to_string(temp, sizeof(temp), &oid);
449 DEBUG(PLUGIN_NAME ": Get request received for table OID '%s'.", temp);
451 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
452 table_definition_t *td = (table_definition_t *)te->value;
454 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
455 data_definition_t *dd = (data_definition_t *)de->value;
457 for (int i = 0; i < dd->oids_len; i++) {
459 int ret = snmp_oid_ncompare(oid.oid, oid.oid_len,
460 dd->oids[i].oid, dd->oids[i].oid_len,
461 MIN(oid.oid_len, dd->oids[i].oid_len));
462 if (ret != 0) continue;
464 if (td->index_oid.oid_len) {
465 int index = oid.oid[oid.oid_len - 1];
468 ret = c_avl_get(td->index_instance, &index, (void **)&instance);
470 DEBUG(PLUGIN_NAME ": Nonexisting index '%d' requested.", index);
471 pthread_mutex_unlock(&g_agent->lock);
472 return SNMP_NOSUCHINSTANCE;
475 if (dd->is_instance) {
477 requests->requestvb->type = ASN_OCTET_STR;
478 snmp_set_var_typed_value(requests->requestvb,
479 requests->requestvb->type, instance,
482 pthread_mutex_unlock(&g_agent->lock);
484 return SNMP_ERR_NOERROR;
487 format_name(temp, sizeof(temp), hostname_g, dd->plugin, instance,
488 dd->type, dd->type_instance);
489 DEBUG(PLUGIN_NAME ": Identifier '%s'", temp);
493 const data_set_t *ds;
495 ds = plugin_get_ds(dd->type);
497 DEBUG(PLUGIN_NAME ": Data set not found for '%s' type.", dd->type);
498 pthread_mutex_unlock(&g_agent->lock);
499 return SNMP_NOSUCHINSTANCE;
502 ret = uc_get_value_by_name(temp, &values, &values_num);
505 ERROR(PLUGIN_NAME ": Failed to get value for '%s'.", temp);
506 pthread_mutex_unlock(&g_agent->lock);
507 return SNMP_NOSUCHINSTANCE;
510 assert(ds->ds_num == values_num);
511 assert(i < values_num);
513 char data[DATA_MAX_NAME_LEN];
514 size_t data_len = sizeof(data);
515 ret = snmp_agent_set_vardata(data, &data_len, dd->oids[i].type,
516 dd->scale, dd->shift, &values[i],
517 sizeof(values[i]), ds->ds[i].type);
523 ERROR(PLUGIN_NAME ": Failed to convert '%s' value to snmp data.",
525 pthread_mutex_unlock(&g_agent->lock);
526 return SNMP_NOSUCHINSTANCE;
529 requests->requestvb->type = dd->oids[i].type;
530 snmp_set_var_typed_value(requests->requestvb,
531 requests->requestvb->type, data, data_len);
533 pthread_mutex_unlock(&g_agent->lock);
535 return SNMP_ERR_NOERROR;
538 DEBUG(PLUGIN_NAME ": %s:%d NOT IMPLEMENTED.", __FUNCTION__, __LINE__);
544 pthread_mutex_unlock(&g_agent->lock);
546 return SNMP_NOSUCHINSTANCE;
550 snmp_agent_table_index_oid_handler(struct netsnmp_mib_handler_s *handler,
551 struct netsnmp_handler_registration_s *reginfo,
552 struct netsnmp_agent_request_info_s *reqinfo,
553 struct netsnmp_request_info_s *requests) {
555 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
556 DEBUG(PLUGIN_NAME ": Not supported request mode (%d).", reqinfo->mode);
557 return SNMP_ERR_NOERROR;
562 pthread_mutex_lock(&g_agent->lock);
564 memcpy(oid.oid, requests->requestvb->name,
565 sizeof(oid.oid[0]) * requests->requestvb->name_length);
566 oid.oid_len = requests->requestvb->name_length;
568 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
569 table_definition_t *td = (table_definition_t *)te->value;
571 if (td->index_oid.oid_len &&
572 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->index_oid.oid,
573 td->index_oid.oid_len,
574 MIN(oid.oid_len, td->index_oid.oid_len)) == 0)) {
576 DEBUG(PLUGIN_NAME ": Handle '%s' table index OID.", td->name);
578 int index = oid.oid[oid.oid_len - 1];
581 int ret = c_avl_get(td->index_instance, &index, (void **)&instance);
583 /* nonexisting index requested */
584 pthread_mutex_unlock(&g_agent->lock);
585 return SNMP_NOSUCHINSTANCE;
588 requests->requestvb->type = ASN_INTEGER;
589 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
590 &index, sizeof(index));
592 pthread_mutex_unlock(&g_agent->lock);
594 return SNMP_ERR_NOERROR;
598 pthread_mutex_unlock(&g_agent->lock);
600 return SNMP_NOSUCHINSTANCE;
604 snmp_agent_table_size_oid_handler(struct netsnmp_mib_handler_s *handler,
605 struct netsnmp_handler_registration_s *reginfo,
606 struct netsnmp_agent_request_info_s *reqinfo,
607 struct netsnmp_request_info_s *requests) {
609 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
610 DEBUG(PLUGIN_NAME ": Not supported request mode (%d).", reqinfo->mode);
611 return SNMP_ERR_NOERROR;
616 pthread_mutex_lock(&g_agent->lock);
618 memcpy(oid.oid, requests->requestvb->name,
619 sizeof(oid.oid[0]) * requests->requestvb->name_length);
620 oid.oid_len = requests->requestvb->name_length;
622 DEBUG(PLUGIN_NAME ": Get request received for table size OID.");
624 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
625 table_definition_t *td = (table_definition_t *)te->value;
627 if (td->size_oid.oid_len &&
628 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->size_oid.oid,
629 td->size_oid.oid_len,
630 MIN(oid.oid_len, td->size_oid.oid_len)) == 0)) {
631 DEBUG(PLUGIN_NAME ": Handle '%s' table size OID.", td->name);
633 long size = c_avl_size(td->index_instance);
635 requests->requestvb->type = td->size_oid.type;
636 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
637 &size, sizeof(size));
639 pthread_mutex_unlock(&g_agent->lock);
641 return SNMP_ERR_NOERROR;
645 pthread_mutex_unlock(&g_agent->lock);
647 return SNMP_NOSUCHINSTANCE;
652 snmp_agent_scalar_oid_handler(struct netsnmp_mib_handler_s *handler,
653 struct netsnmp_handler_registration_s *reginfo,
654 struct netsnmp_agent_request_info_s *reqinfo,
655 struct netsnmp_request_info_s *requests) {
657 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
658 DEBUG(PLUGIN_NAME ": Not supported request mode (%d).", reqinfo->mode);
659 return SNMP_ERR_NOERROR;
664 char temp[DATA_MAX_NAME_LEN];
666 pthread_mutex_lock(&g_agent->lock);
668 memcpy(oid.oid, requests->requestvb->name,
669 sizeof(oid.oid[0]) * requests->requestvb->name_length);
670 oid.oid_len = requests->requestvb->name_length;
672 snmp_agent_oid_to_string(temp, sizeof(temp), &oid);
674 DEBUG(PLUGIN_NAME ": Get request received for scalar OID '%s'.", temp);
676 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL; de = de->next) {
677 data_definition_t *dd = (data_definition_t *)de->value;
679 for (int i = 0; i < dd->oids_len; i++) {
681 ret = snmp_oid_compare(oid.oid, oid.oid_len,
682 dd->oids[i].oid, dd->oids[i].oid_len);
683 if (ret != 0) continue;
685 format_name(temp, sizeof(temp), hostname_g, dd->plugin,
686 dd->plugin_instance, dd->type, dd->type_instance);
687 DEBUG(PLUGIN_NAME ": Identifier '%s'", temp);
691 const data_set_t *ds;
693 ds = plugin_get_ds(dd->type);
695 DEBUG(PLUGIN_NAME ": Data set not found for '%s' type", dd->type);
696 pthread_mutex_unlock(&g_agent->lock);
697 return SNMP_NOSUCHINSTANCE;
700 ret = uc_get_value_by_name(temp, &values, &values_num);
703 ERROR(PLUGIN_NAME ": Failed to get value for '%s'.", temp);
704 pthread_mutex_unlock(&g_agent->lock);
705 return SNMP_NOSUCHINSTANCE;
708 assert(ds->ds_num == values_num);
709 assert(i < values_num);
711 char data[DATA_MAX_NAME_LEN];
712 size_t data_len = sizeof(data);
713 ret = snmp_agent_set_vardata(data, &data_len, dd->oids[i].type,
714 dd->scale, dd->shift, &values[i],
715 sizeof(values[i]), ds->ds[i].type);
721 ERROR(PLUGIN_NAME ": Failed to convert '%s' value to snmp data.", temp);
722 pthread_mutex_unlock(&g_agent->lock);
723 return SNMP_NOSUCHINSTANCE;
726 requests->requestvb->type = dd->oids[i].type;
727 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
730 pthread_mutex_unlock(&g_agent->lock);
732 return SNMP_ERR_NOERROR;
736 pthread_mutex_unlock(&g_agent->lock);
738 return SNMP_NOSUCHINSTANCE;
741 static int snmp_agent_register_table_oids(void) {
743 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
744 table_definition_t *td = (table_definition_t *)te->value;
746 if (td->size_oid.oid_len != 0) {
748 snmp_agent_get_asn_type(td->size_oid.oid, td->size_oid.oid_len);
749 td->size_oid.oid_len++;
750 int ret = snmp_agent_register_oid(&td->size_oid,
751 snmp_agent_table_size_oid_handler);
756 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
757 data_definition_t *dd = (data_definition_t *)de->value;
759 for (int i = 0; i < dd->oids_len; i++) {
761 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
769 static int snmp_agent_register_scalar_oids(void) {
771 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
772 data_definition_t *dd = (data_definition_t *)e->value;
774 for (int i = 0; i < dd->oids_len; i++) {
777 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
780 snmp_agent_register_oid(&dd->oids[i], snmp_agent_scalar_oid_handler);
789 static int snmp_agent_config_data_oids(data_definition_t *dd,
790 oconfig_item_t *ci) {
791 if (ci->values_num < 1) {
792 WARNING(PLUGIN_NAME ": `OIDs' needs at least one argument.");
796 for (int i = 0; i < ci->values_num; i++)
797 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
798 WARNING(PLUGIN_NAME ": `OIDs' needs only string argument.");
802 if (dd->oids != NULL)
805 dd->oids = calloc(ci->values_num, sizeof(*dd->oids));
806 if (dd->oids == NULL)
808 dd->oids_len = (size_t)ci->values_num;
810 for (int i = 0; i < ci->values_num; i++) {
811 dd->oids[i].oid_len = MAX_OID_LEN;
813 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->oids[i].oid,
814 &dd->oids[i].oid_len)) {
815 ERROR(PLUGIN_NAME ": snmp_parse_oid (%s) failed.",
816 ci->values[i].value.string);
826 static int snmp_agent_config_table_size_oid(table_definition_t *td,
827 oconfig_item_t *ci) {
828 if (ci->values_num < 1) {
829 WARNING(PLUGIN_NAME ": `TableSizeOID' is empty.");
833 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
834 WARNING(PLUGIN_NAME ": `TableSizeOID' needs only string argument.");
838 td->size_oid.oid_len = MAX_OID_LEN;
840 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->size_oid.oid,
841 &td->size_oid.oid_len)) {
842 ERROR(PLUGIN_NAME ": Failed to parse table size OID (%s)",
843 ci->values[0].value.string);
844 td->size_oid.oid_len = 0;
851 static int snmp_agent_config_table_index_oid(table_definition_t *td,
852 oconfig_item_t *ci) {
854 if (ci->values_num < 1) {
855 WARNING(PLUGIN_NAME ": `IndexOID' is empty.");
859 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
860 WARNING(PLUGIN_NAME ": `IndexOID' needs only string argument.");
864 td->index_oid.oid_len = MAX_OID_LEN;
866 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->index_oid.oid,
867 &td->index_oid.oid_len)) {
868 ERROR(PLUGIN_NAME ": Failed to parse table index OID (%s)",
869 ci->values[0].value.string);
870 td->index_oid.oid_len = 0;
877 static int snmp_agent_config_table_data(table_definition_t *td,
878 oconfig_item_t *ci) {
879 data_definition_t *dd;
884 dd = calloc(1, sizeof(*dd));
886 ERROR(PLUGIN_NAME ": Failed to allocate memory for table data definition");
890 ret = cf_util_get_string(ci, &dd->name);
901 for (int i = 0; i < ci->children_num; i++) {
902 oconfig_item_t *option = ci->children + i;
904 if (strcasecmp("Instance", option->key) == 0)
905 ret = cf_util_get_boolean(option, &dd->is_instance);
906 else if (strcasecmp("Plugin", option->key) == 0)
907 ret = cf_util_get_string(option, &dd->plugin);
908 else if (strcasecmp("PluginInstance", option->key) == 0)
909 ret = cf_util_get_string(option, &dd->plugin_instance);
910 else if (strcasecmp("Type", option->key) == 0)
911 ret = cf_util_get_string(option, &dd->type);
912 else if (strcasecmp("TypeInstance", option->key) == 0)
913 ret = cf_util_get_string(option, &dd->type_instance);
914 else if (strcasecmp("Shift", option->key) == 0)
915 ret = cf_util_get_double(option, &dd->shift);
916 else if (strcasecmp("Scale", option->key) == 0)
917 ret = cf_util_get_double(option, &dd->scale);
918 else if (strcasecmp("OIDs", option->key) == 0)
919 ret = snmp_agent_config_data_oids(dd, option);
921 WARNING(PLUGIN_NAME ": Option `%s' not allowed here.", option->key);
926 snmp_agent_free_data(&dd);
931 llentry_t *entry = llentry_create(dd->name, dd);
933 snmp_agent_free_data(&dd);
937 llist_append(td->columns, entry);
942 static int snmp_agent_config_data(oconfig_item_t *ci) {
943 data_definition_t *dd;
948 dd = calloc(1, sizeof(*dd));
950 ERROR(PLUGIN_NAME ": Failed to allocate memory for data definition");
954 ret = cf_util_get_string(ci, &dd->name);
963 for (int i = 0; i < ci->children_num; i++) {
964 oconfig_item_t *option = ci->children + i;
966 if (strcasecmp("Instance", option->key) == 0)
967 ret = cf_util_get_boolean(option, &dd->is_instance);
968 else if (strcasecmp("Plugin", option->key) == 0)
969 ret = cf_util_get_string(option, &dd->plugin);
970 else if (strcasecmp("PluginInstance", option->key) == 0)
971 ret = cf_util_get_string(option, &dd->plugin_instance);
972 else if (strcasecmp("Type", option->key) == 0)
973 ret = cf_util_get_string(option, &dd->type);
974 else if (strcasecmp("TypeInstance", option->key) == 0)
975 ret = cf_util_get_string(option, &dd->type_instance);
976 else if (strcasecmp("Shift", option->key) == 0)
977 ret = cf_util_get_double(option, &dd->shift);
978 else if (strcasecmp("Scale", option->key) == 0)
979 ret = cf_util_get_double(option, &dd->scale);
980 else if (strcasecmp("OIDs", option->key) == 0)
981 ret = snmp_agent_config_data_oids(dd, option);
983 WARNING(PLUGIN_NAME ": Option `%s' not allowed here.", option->key);
988 snmp_agent_free_data(&dd);
993 llentry_t *entry = llentry_create(dd->name, dd);
995 snmp_agent_free_data(&dd);
999 llist_append(g_agent->scalars, entry);
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_data(td, option);
1049 WARNING(PLUGIN_NAME ": Option `%s' not allowed here.", option->key);
1054 snmp_agent_free_table(&td);
1059 llentry_t *entry = llentry_create(td->name, td);
1060 if (entry == NULL) {
1061 snmp_agent_free_table(&td);
1065 td->instance_index =
1066 c_avl_create((int (*)(const void *, const void *))strcmp);
1067 if (td->instance_index == NULL) {
1068 snmp_agent_free_table(&td);
1072 td->index_instance =
1073 c_avl_create((int (*)(const void *, const void *))num_compare);
1074 if (td->index_instance == NULL) {
1075 snmp_agent_free_table(&td);
1079 llist_append(g_agent->tables, entry);
1084 static int snmp_agent_get_value_from_ds_type(const value_t *val, int type,
1085 double scale, double shift,
1088 case DS_TYPE_COUNTER:
1089 *value = (long)((val->counter * scale) + shift);
1091 case DS_TYPE_ABSOLUTE:
1092 *value = (long)((val->absolute * scale) + shift);
1094 case DS_TYPE_DERIVE:
1095 *value = (long)((val->derive * scale) + shift);
1098 *value = (long)((val->gauge * scale) + shift);
1103 ERROR(PLUGIN_NAME ": Unknown data source type: %i.", type);
1110 static int snmp_agent_set_vardata(void *data, size_t *data_len, u_char asn_type,
1111 double scale, double shift, const void *value,
1112 size_t len, int type) {
1115 netsnmp_vardata var;
1120 var.string = (u_char *)data;
1122 ret = snmp_agent_get_value_from_ds_type(val, type, scale, shift, &new_value);
1132 if (*data_len < sizeof(*var.integer))
1134 *var.integer = new_value;
1135 *data_len = sizeof(*var.integer);
1138 if (*data_len < sizeof(*var.counter64))
1140 var.counter64->high = (u_long)((int64_t)new_value >> 32);
1141 var.counter64->low = (u_long)((int64_t)new_value & 0xFFFFFFFF);
1142 *data_len = sizeof(*var.counter64);
1145 if (type == DS_TYPE_GAUGE) {
1146 char buf[DATA_MAX_NAME_LEN];
1147 snprintf(buf, sizeof(buf), "%.2f", val->gauge);
1148 if (*data_len < strlen(buf))
1150 *data_len = strlen(buf);
1151 memcpy(var.string, buf, *data_len);
1153 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type.", type,
1159 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type.", type,
1167 static int snmp_agent_register_oid_index(oid_t *oid, int index,
1168 Netsnmp_Node_Handler *handler) {
1170 memcpy(&new_oid, oid, sizeof(*oid));
1171 new_oid.oid[new_oid.oid_len++] = index;
1172 return snmp_agent_register_oid(&new_oid, handler);
1175 static int snmp_agent_unregister_oid_index(oid_t *oid, int index) {
1177 memcpy(&new_oid, oid, sizeof(*oid));
1178 new_oid.oid[new_oid.oid_len++] = index;
1179 return unregister_mib(new_oid.oid, new_oid.oid_len);
1182 static int snmp_agent_update_index(table_definition_t *td,
1183 const char *instance) {
1185 if (c_avl_get(td->instance_index, instance, NULL) == 0)
1192 ins = strdup(instance);
1196 index = calloc(1, sizeof(*index));
1197 if (index == NULL) {
1202 *index = c_avl_size(td->instance_index) + 1;
1204 ret = c_avl_insert(td->instance_index, ins, index);
1211 ret = c_avl_insert(td->index_instance, index, ins);
1213 DEBUG(PLUGIN_NAME ": Failed to update index_instance for '%s' table.",
1218 DEBUG(PLUGIN_NAME ": Updated index for '%s' table [%d, %s].", td->name,
1221 /* need to generate index for the table */
1222 if (td->index_oid.oid_len) {
1224 ret = snmp_agent_register_oid_index(&td->index_oid, *index,
1225 snmp_agent_table_index_oid_handler);
1229 /* register new oids for all columns */
1230 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1231 data_definition_t *dd = (data_definition_t *)de->value;
1233 for (int i = 0; i < dd->oids_len; i++) {
1234 ret = snmp_agent_register_oid_index(&dd->oids[i], *index,
1235 snmp_agent_table_oid_handler);
1242 notification_t n = {NOTIF_OKAY, cdtime(), "", "", PLUGIN_NAME, "", "", "",
1244 sstrncpy(n.host, hostname_g, sizeof(n.host));
1245 sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance));
1246 ssnprintf(n.message, sizeof(n.message),
1247 "Data row added to table %s instance %s index %d", td->name, ins,
1249 plugin_dispatch_notification(&n);
1254 static int snmp_agent_write(value_list_t const *vl) {
1259 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1260 table_definition_t *td = (table_definition_t *)te->value;
1262 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1263 data_definition_t *dd = (data_definition_t *)de->value;
1265 if (!dd->is_instance) {
1266 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
1267 vl->type_instance)) {
1268 snmp_agent_update_index(td, vl->plugin_instance);
1278 static int snmp_agent_collect(const data_set_t *ds, const value_list_t *vl,
1279 user_data_t __attribute__((unused)) * user_data) {
1281 pthread_mutex_lock(&g_agent->lock);
1283 snmp_agent_write(vl);
1285 pthread_mutex_unlock(&g_agent->lock);
1290 static int snmp_agent_preinit(void) {
1291 if (g_agent != NULL) {
1292 /* already initialized if config callback was called before init callback */
1296 g_agent = calloc(1, sizeof(*g_agent));
1297 if (g_agent == NULL) {
1298 ERROR(PLUGIN_NAME ": Failed to allocate memory for snmp agent context.");
1302 g_agent->tables = llist_create();
1303 g_agent->scalars = llist_create();
1306 /* make us a agentx client. */
1307 err = netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE,
1310 ERROR(PLUGIN_NAME ": Failed to set agent role (%d)", err);
1315 * For SNMP debug purposes uses snmp_set_do_debugging(1);
1318 /* initialize the agent library */
1319 err = init_agent(PLUGIN_NAME);
1321 ERROR(PLUGIN_NAME ": Failed to initialize the agent library (%d)", err);
1325 init_snmp(PLUGIN_NAME);
1327 g_agent->tp = read_all_mibs();
1332 static int snmp_agent_init(void) {
1335 ret = snmp_agent_preinit();
1339 ret = snmp_agent_register_scalar_oids();
1343 ret = snmp_agent_register_table_oids();
1347 /* create a second thread to listen for requests from AgentX*/
1348 ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
1350 ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
1354 ret = pthread_mutex_init(&g_agent->lock, NULL);
1356 ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret);
1360 ret = pthread_mutex_init(&g_agent->agentx_lock, NULL);
1362 ERROR(PLUGIN_NAME ": Failed to initialize AgentX mutex, err %u", ret);
1369 static void *snmp_agent_thread_run(void __attribute__((unused)) * arg) {
1370 INFO(PLUGIN_NAME ": Thread is up and running");
1373 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1375 pthread_mutex_lock(&g_agent->agentx_lock);
1376 agent_check_and_process(0); /* 0 == don't block */
1377 pthread_mutex_unlock(&g_agent->agentx_lock);
1379 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
1386 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler) {
1387 netsnmp_handler_registration *reg;
1388 char temp[DATA_MAX_NAME_LEN];
1389 char *oid_name = snmp_agent_get_oid_name(oid->oid, oid->oid_len - 1);
1391 snmp_agent_oid_to_string(temp, sizeof(temp), oid);
1393 if (oid_name == NULL) {
1395 ": Skipped registration: OID (%s) is not found in main tree", temp);
1399 reg = netsnmp_create_handler_registration(oid_name, handler, oid->oid,
1400 oid->oid_len, HANDLER_CAN_RONLY);
1402 ERROR(PLUGIN_NAME ": Failed to create handler registration for OID (%s)",
1407 pthread_mutex_lock(&g_agent->agentx_lock);
1409 if (netsnmp_register_instance(reg) != MIB_REGISTERED_OK) {
1410 ERROR(PLUGIN_NAME ": Failed to register handler for OID (%s)", temp);
1411 pthread_mutex_unlock(&g_agent->agentx_lock);
1415 pthread_mutex_unlock(&g_agent->agentx_lock);
1417 DEBUG(PLUGIN_NAME ": Registered handler for OID (%s)", temp);
1422 static int snmp_agent_free_config(void) {
1424 if (g_agent == NULL)
1427 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next)
1428 snmp_agent_free_table((table_definition_t **)&te->value);
1429 llist_destroy(g_agent->tables);
1431 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL; de = de->next)
1432 snmp_agent_free_data((data_definition_t **)&de->value);
1433 llist_destroy(g_agent->scalars);
1438 static int snmp_agent_shutdown(void) {
1441 DEBUG(PLUGIN_NAME ": snmp_agent_shutdown.");
1443 if (g_agent == NULL) {
1444 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: plugin not initialized.");
1448 if (pthread_cancel(g_agent->thread) != 0)
1449 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to cancel the thread.");
1451 if (pthread_join(g_agent->thread, NULL) != 0)
1452 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to join the thread.");
1454 snmp_agent_free_config();
1456 snmp_shutdown(PLUGIN_NAME);
1458 pthread_mutex_destroy(&g_agent->lock);
1459 pthread_mutex_destroy(&g_agent->agentx_lock);
1466 static int snmp_agent_config(oconfig_item_t *ci) {
1468 int ret = snmp_agent_preinit();
1475 for (int i = 0; i < ci->children_num; i++) {
1476 oconfig_item_t *child = ci->children + i;
1477 if (strcasecmp("Data", child->key) == 0) {
1478 ret = snmp_agent_config_data(child);
1479 } else if (strcasecmp("Table", child->key) == 0) {
1480 ret = snmp_agent_config_table(child);
1482 ERROR(PLUGIN_NAME ": Unknown configuration option `%s'.", child->key);
1487 ERROR(PLUGIN_NAME ": Failed to parse configuration.");
1488 snmp_agent_free_config();
1489 snmp_shutdown(PLUGIN_NAME);
1495 ret = snmp_agent_validate_data();
1497 ERROR(PLUGIN_NAME ": Invalid configuration provided.");
1498 snmp_agent_free_config();
1499 snmp_shutdown(PLUGIN_NAME);
1507 void module_register(void) {
1508 plugin_register_init(PLUGIN_NAME, snmp_agent_init);
1509 plugin_register_complex_config(PLUGIN_NAME, snmp_agent_config);
1510 plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL);
1511 plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL);
1512 plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown);