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>
32 #include "utils_avltree.h"
33 #include "utils_cache.h"
34 #include "utils_llist.h"
36 #include <net-snmp/net-snmp-config.h>
37 #include <net-snmp/net-snmp-includes.h>
38 #include <net-snmp/agent/net-snmp-agent-includes.h>
40 #define PLUGIN_NAME "snmp_agent"
41 #define ERR_BUF_SIZE 1024
42 #define TYPE_STRING -1
45 #define MIN(x, y) ((x) < (y) ? (x) : (y))
53 typedef struct oid_s oid_t;
55 struct table_definition_s {
60 c_avl_tree_t *instance_index;
61 c_avl_tree_t *index_instance;
63 typedef struct table_definition_s table_definition_t;
65 struct data_definition_s {
68 char *plugin_instance;
71 const table_definition_t *table;
78 typedef struct data_definition_s data_definition_t;
80 struct snmp_agent_ctx_s {
83 pthread_mutex_t agentx_lock;
89 typedef struct snmp_agent_ctx_s snmp_agent_ctx_t;
91 snmp_agent_ctx_t *g_agent = NULL;
93 #define CHECK_DD_TYPE(_dd, _p, _pi, _t, _ti) \
94 (_dd->plugin ? !strcmp(_dd->plugin, _p) : 0) && \
95 (_dd->plugin_instance ? !strcmp(_dd->plugin_instance, _pi) : 1) && \
96 (_dd->type ? !strcmp(_dd->type, _t) : 0) && \
97 (_dd->type_instance ? !strcmp(_dd->type_instance, _ti) : 1)
99 static void *snmp_agent_thread_run(void *arg);
100 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler);
101 static int snmp_agent_set_vardata(void *dst_buf, size_t *dst_buf_len,
102 u_char asn_type, double scale, double shift,
103 const void *value, size_t len, int type);
104 static int snmp_agent_unregister_oid_index(oid_t *oid, int index);
106 static u_char snmp_agent_get_asn_type(oid *oid, size_t oid_len) {
107 struct tree *node = get_tree(oid, oid_len, g_agent->tp);
109 return (node != NULL ? mib_to_asn_type(node->type) : 0);
112 static char *snmp_agent_get_oid_name(oid *oid, size_t oid_len) {
113 struct tree *node = get_tree(oid, oid_len, g_agent->tp);
115 return (node != NULL ? node->label : NULL);
118 static int snmp_agent_oid_to_string(char *buf, size_t buf_size,
120 char oid_str[MAX_OID_LEN][16];
121 char *oid_str_ptr[MAX_OID_LEN];
123 for (size_t i = 0; i < o->oid_len; i++) {
124 ssnprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
125 oid_str_ptr[i] = oid_str[i];
128 return (strjoin(buf, buf_size, oid_str_ptr, o->oid_len, "."));
131 static void snmp_agent_dump_data(void) {
133 char oid_str[DATA_MAX_NAME_LEN];
135 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
136 table_definition_t *td = te->value;
138 DEBUG(PLUGIN_NAME ": Table:");
139 DEBUG(PLUGIN_NAME ": Name: %s", td->name);
140 if (td->index_oid.oid_len != 0) {
141 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &td->index_oid);
142 DEBUG(PLUGIN_NAME ": IndexOID: %s", oid_str);
144 if (td->size_oid.oid_len != 0) {
145 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &td->size_oid);
146 DEBUG(PLUGIN_NAME ": SizeOID: %s", oid_str);
149 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
150 data_definition_t *dd = de->value;
152 DEBUG(PLUGIN_NAME ": Column:");
153 DEBUG(PLUGIN_NAME ": Name: %s", dd->name);
155 DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin);
156 if (dd->plugin_instance)
157 DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance);
159 DEBUG(PLUGIN_NAME ": Instance: true");
161 DEBUG(PLUGIN_NAME ": Type: %s", dd->type);
162 if (dd->type_instance)
163 DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance);
164 for (int i = 0; i < dd->oids_len; i++) {
165 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &dd->oids[i]);
166 DEBUG(PLUGIN_NAME ": OID[%d]: %s", i, oid_str);
168 DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale);
169 DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift);
173 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
174 data_definition_t *dd = e->value;
176 DEBUG(PLUGIN_NAME ": Scalar:");
177 DEBUG(PLUGIN_NAME ": Name: %s", dd->name);
179 DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin);
180 if (dd->plugin_instance)
181 DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance);
183 DEBUG(PLUGIN_NAME ": Instance: true");
185 DEBUG(PLUGIN_NAME ": Type: %s", dd->type);
186 if (dd->type_instance)
187 DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance);
188 for (int i = 0; i < dd->oids_len; i++) {
189 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &dd->oids[i]);
190 DEBUG(PLUGIN_NAME ": OID[%d]: %s", i, oid_str);
192 DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale);
193 DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift);
195 #endif /* COLLECT_DEBUG */
198 static int snmp_agent_validate_data(void) {
200 snmp_agent_dump_data();
202 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
203 table_definition_t *td = te->value;
205 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
206 data_definition_t *dd = de->value;
209 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'.'%s'", td->name,
214 if (dd->plugin_instance) {
215 ERROR(PLUGIN_NAME ": PluginInstance should not be defined for table "
216 "data type '%s'.'%s'",
221 if (dd->oids_len == 0) {
222 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'.'%s'", td->name,
227 if (dd->is_instance) {
229 if (dd->type || dd->type_instance) {
230 ERROR(PLUGIN_NAME ": Type and TypeInstance are not valid for "
231 "instance data '%s'.'%s'",
236 if (dd->oids_len > 1) {
239 ": Only one OID should be specified for instance data '%s'.'%s'",
246 ERROR(PLUGIN_NAME ": Type not defined for data '%s'.'%s'", td->name,
254 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
255 data_definition_t *dd = e->value;
258 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'", dd->name);
262 if (dd->oids_len == 0) {
263 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'", dd->name);
267 if (dd->is_instance) {
269 ": Instance flag can't be specified for scalar data '%s'",
275 ERROR(PLUGIN_NAME ": Type not defined for data '%s'", dd->name);
283 static int snmp_agent_table_row_remove(table_definition_t *td,
284 const char *instance) {
288 if ((c_avl_get(td->instance_index, instance, (void **)&index) != 0) ||
289 (c_avl_get(td->index_instance, index, (void **)&ins) != 0))
292 pthread_mutex_lock(&g_agent->agentx_lock);
294 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
295 data_definition_t *dd = de->value;
297 for (int i = 0; i < dd->oids_len; i++)
298 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
301 snmp_agent_unregister_oid_index(&td->index_oid, *index);
303 pthread_mutex_unlock(&g_agent->agentx_lock);
305 DEBUG(PLUGIN_NAME ": Removed row for '%s' table [%d, %s]", td->name, *index,
309 .severity = NOTIF_WARNING,
311 .plugin = PLUGIN_NAME
313 sstrncpy(n.host, hostname_g, sizeof(n.host));
314 sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance));
315 ssnprintf(n.message, sizeof(n.message),
316 "Removed data row from table %s instance %s index %d", td->name,
318 plugin_dispatch_notification(&n);
320 c_avl_remove(td->index_instance, index, NULL, (void **)&ins);
321 c_avl_remove(td->instance_index, instance, NULL, (void **)&index);
328 static int snmp_agent_clear_missing(const value_list_t *vl,
329 __attribute__((unused)) user_data_t *ud) {
333 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
334 table_definition_t *td = te->value;
336 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
337 data_definition_t *dd = de->value;
339 if (!dd->is_instance) {
340 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
341 vl->type_instance)) {
342 snmp_agent_table_row_remove(td, vl->plugin_instance);
352 static void snmp_agent_free_data(data_definition_t **dd) {
354 if (dd == NULL || *dd == NULL)
357 /* unregister scalar type OID */
358 if ((*dd)->table == NULL) {
359 for (int i = 0; i < (*dd)->oids_len; i++)
360 unregister_mib((*dd)->oids[i].oid, (*dd)->oids[i].oid_len);
362 /* unregister all table OIDs */
366 c_avl_iterator_t *iter = c_avl_get_iterator((*dd)->table->index_instance);
367 while (c_avl_iterator_next(iter, (void *)&index, (void *)&value) == 0) {
368 for (int i = 0; i < (*dd)->oids_len; i++)
369 snmp_agent_unregister_oid_index(&(*dd)->oids[i], *index);
371 c_avl_iterator_destroy(iter);
375 sfree((*dd)->plugin);
376 sfree((*dd)->plugin_instance);
378 sfree((*dd)->type_instance);
386 static void snmp_agent_free_table(table_definition_t **td) {
388 if (td == NULL || *td == NULL)
391 if ((*td)->size_oid.oid_len)
392 unregister_mib((*td)->size_oid.oid, (*td)->size_oid.oid_len);
394 if ((*td)->index_oid.oid_len) {
398 c_avl_iterator_t *iter = c_avl_get_iterator((*td)->index_instance);
399 while (c_avl_iterator_next(iter, (void *)&index, (void *)&value) == 0)
400 snmp_agent_unregister_oid_index(&(*td)->index_oid, *index);
402 c_avl_iterator_destroy(iter);
405 for (llentry_t *de = llist_head((*td)->columns); de != NULL; de = de->next) {
406 data_definition_t *dd = de->value;
407 snmp_agent_free_data(&dd);
410 llist_destroy((*td)->columns);
415 /* index_instance and instance_index contain the same pointers */
416 c_avl_destroy((*td)->index_instance);
417 (*td)->index_instance = NULL;
419 while (c_avl_pick((*td)->instance_index, &key, &value) == 0) {
423 c_avl_destroy((*td)->instance_index);
424 (*td)->instance_index = NULL;
432 static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests,
433 data_definition_t *dd, char *instance,
435 char name[DATA_MAX_NAME_LEN];
436 format_name(name, sizeof(name), hostname_g, dd->plugin,
437 instance ? instance : dd->plugin_instance, dd->type,
439 DEBUG(PLUGIN_NAME ": Identifier '%s'", name);
443 const data_set_t *ds = plugin_get_ds(dd->type);
445 ERROR(PLUGIN_NAME ": Data set not found for '%s' type", dd->type);
446 return SNMP_NOSUCHINSTANCE;
449 int ret = uc_get_value_by_name(name, &values, &values_num);
452 ERROR(PLUGIN_NAME ": Failed to get value for '%s'", name);
453 return SNMP_NOSUCHINSTANCE;
456 assert(ds->ds_num == values_num);
457 assert(oid_index < values_num);
459 char data[DATA_MAX_NAME_LEN];
460 size_t data_len = sizeof(data);
461 ret = snmp_agent_set_vardata(
462 data, &data_len, dd->oids[oid_index].type, dd->scale, dd->shift,
463 &values[oid_index], sizeof(values[oid_index]), ds->ds[oid_index].type);
468 ERROR(PLUGIN_NAME ": Failed to convert '%s' value to snmp data", name);
469 return SNMP_NOSUCHINSTANCE;
472 requests->requestvb->type = dd->oids[oid_index].type;
473 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type, data,
476 return SNMP_ERR_NOERROR;
480 snmp_agent_table_oid_handler(struct netsnmp_mib_handler_s *handler,
481 struct netsnmp_handler_registration_s *reginfo,
482 struct netsnmp_agent_request_info_s *reqinfo,
483 struct netsnmp_request_info_s *requests) {
485 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
486 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
487 return SNMP_ERR_NOERROR;
490 pthread_mutex_lock(&g_agent->lock);
493 memcpy(oid.oid, requests->requestvb->name,
494 sizeof(oid.oid[0]) * requests->requestvb->name_length);
495 oid.oid_len = requests->requestvb->name_length;
498 char oid_str[DATA_MAX_NAME_LEN];
499 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
500 DEBUG(PLUGIN_NAME ": Get request received for table OID '%s'", oid_str);
503 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
504 table_definition_t *td = te->value;
506 if (!td->index_oid.oid_len) {
507 DEBUG(PLUGIN_NAME ": %s:%d NOT IMPLEMENTED", __FUNCTION__, __LINE__);
511 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
512 data_definition_t *dd = de->value;
514 for (int i = 0; i < dd->oids_len; i++) {
515 int ret = snmp_oid_ncompare(oid.oid, oid.oid_len, dd->oids[i].oid,
517 MIN(oid.oid_len, dd->oids[i].oid_len));
521 int index = oid.oid[oid.oid_len - 1];
524 ret = c_avl_get(td->index_instance, &index, (void **)&instance);
526 DEBUG(PLUGIN_NAME ": Nonexisting index '%d' requested", index);
527 pthread_mutex_unlock(&g_agent->lock);
528 return SNMP_NOSUCHINSTANCE;
531 if (dd->is_instance) {
532 requests->requestvb->type = ASN_OCTET_STR;
533 snmp_set_var_typed_value(requests->requestvb,
534 requests->requestvb->type, instance,
537 pthread_mutex_unlock(&g_agent->lock);
539 return SNMP_ERR_NOERROR;
542 ret = snmp_agent_form_reply(requests, dd, instance, i);
544 pthread_mutex_unlock(&g_agent->lock);
551 pthread_mutex_unlock(&g_agent->lock);
553 return SNMP_NOSUCHINSTANCE;
556 static int snmp_agent_table_index_oid_handler(
557 struct netsnmp_mib_handler_s *handler,
558 struct netsnmp_handler_registration_s *reginfo,
559 struct netsnmp_agent_request_info_s *reqinfo,
560 struct netsnmp_request_info_s *requests) {
562 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
563 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
564 return SNMP_ERR_NOERROR;
567 pthread_mutex_lock(&g_agent->lock);
570 memcpy(oid.oid, requests->requestvb->name,
571 sizeof(oid.oid[0]) * requests->requestvb->name_length);
572 oid.oid_len = requests->requestvb->name_length;
574 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
575 table_definition_t *td = te->value;
577 if (td->index_oid.oid_len &&
578 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->index_oid.oid,
579 td->index_oid.oid_len,
580 MIN(oid.oid_len, td->index_oid.oid_len)) == 0)) {
582 DEBUG(PLUGIN_NAME ": Handle '%s' table index OID", td->name);
584 int index = oid.oid[oid.oid_len - 1];
586 int ret = c_avl_get(td->index_instance, &index, &(void *){NULL});
588 /* nonexisting index requested */
589 pthread_mutex_unlock(&g_agent->lock);
590 return SNMP_NOSUCHINSTANCE;
593 requests->requestvb->type = ASN_INTEGER;
594 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
595 &index, sizeof(index));
597 pthread_mutex_unlock(&g_agent->lock);
599 return SNMP_ERR_NOERROR;
603 pthread_mutex_unlock(&g_agent->lock);
605 return SNMP_NOSUCHINSTANCE;
608 static int snmp_agent_table_size_oid_handler(
609 struct netsnmp_mib_handler_s *handler,
610 struct netsnmp_handler_registration_s *reginfo,
611 struct netsnmp_agent_request_info_s *reqinfo,
612 struct netsnmp_request_info_s *requests) {
614 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
615 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
616 return SNMP_ERR_NOERROR;
619 pthread_mutex_lock(&g_agent->lock);
622 memcpy(oid.oid, requests->requestvb->name,
623 sizeof(oid.oid[0]) * requests->requestvb->name_length);
624 oid.oid_len = requests->requestvb->name_length;
626 DEBUG(PLUGIN_NAME ": Get request received for table size OID");
628 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
629 table_definition_t *td = te->value;
631 if (td->size_oid.oid_len &&
632 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->size_oid.oid,
633 td->size_oid.oid_len,
634 MIN(oid.oid_len, td->size_oid.oid_len)) == 0)) {
635 DEBUG(PLUGIN_NAME ": Handle '%s' table size OID", td->name);
637 long size = c_avl_size(td->index_instance);
639 requests->requestvb->type = td->size_oid.type;
640 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
641 &size, sizeof(size));
643 pthread_mutex_unlock(&g_agent->lock);
645 return SNMP_ERR_NOERROR;
649 pthread_mutex_unlock(&g_agent->lock);
651 return SNMP_NOSUCHINSTANCE;
655 snmp_agent_scalar_oid_handler(struct netsnmp_mib_handler_s *handler,
656 struct netsnmp_handler_registration_s *reginfo,
657 struct netsnmp_agent_request_info_s *reqinfo,
658 struct netsnmp_request_info_s *requests) {
660 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
661 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
662 return SNMP_ERR_NOERROR;
665 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;
673 char oid_str[DATA_MAX_NAME_LEN];
674 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
675 DEBUG(PLUGIN_NAME ": Get request received for scalar OID '%s'", oid_str);
678 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL;
680 data_definition_t *dd = de->value;
682 for (int i = 0; i < dd->oids_len; i++) {
684 int ret = snmp_oid_compare(oid.oid, oid.oid_len, dd->oids[i].oid,
685 dd->oids[i].oid_len);
689 ret = snmp_agent_form_reply(requests, dd, NULL, i);
691 pthread_mutex_unlock(&g_agent->lock);
697 pthread_mutex_unlock(&g_agent->lock);
699 return SNMP_NOSUCHINSTANCE;
702 static int snmp_agent_register_table_oids(void) {
704 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
705 table_definition_t *td = te->value;
707 if (td->size_oid.oid_len != 0) {
709 snmp_agent_get_asn_type(td->size_oid.oid, td->size_oid.oid_len);
710 td->size_oid.oid_len++;
711 int ret = snmp_agent_register_oid(&td->size_oid,
712 snmp_agent_table_size_oid_handler);
717 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
718 data_definition_t *dd = de->value;
720 for (int i = 0; i < dd->oids_len; i++) {
722 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
730 static int snmp_agent_register_scalar_oids(void) {
732 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
733 data_definition_t *dd = e->value;
735 for (int i = 0; i < dd->oids_len; i++) {
738 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
741 snmp_agent_register_oid(&dd->oids[i], snmp_agent_scalar_oid_handler);
750 static int snmp_agent_config_data_oids(data_definition_t *dd,
751 oconfig_item_t *ci) {
752 if (ci->values_num < 1) {
753 WARNING(PLUGIN_NAME ": `OIDs' needs at least one argument");
757 for (int i = 0; i < ci->values_num; i++)
758 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
759 WARNING(PLUGIN_NAME ": `OIDs' needs only string argument");
763 if (dd->oids != NULL)
766 dd->oids = calloc(ci->values_num, sizeof(*dd->oids));
767 if (dd->oids == NULL)
769 dd->oids_len = (size_t)ci->values_num;
771 for (int i = 0; i < ci->values_num; i++) {
772 dd->oids[i].oid_len = MAX_OID_LEN;
774 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->oids[i].oid,
775 &dd->oids[i].oid_len)) {
776 ERROR(PLUGIN_NAME ": snmp_parse_oid (%s) failed",
777 ci->values[i].value.string);
787 static int snmp_agent_config_table_size_oid(table_definition_t *td,
788 oconfig_item_t *ci) {
789 if (ci->values_num < 1) {
790 WARNING(PLUGIN_NAME ": `TableSizeOID' is empty");
794 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
795 WARNING(PLUGIN_NAME ": `TableSizeOID' needs only string argument");
799 td->size_oid.oid_len = MAX_OID_LEN;
801 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->size_oid.oid,
802 &td->size_oid.oid_len)) {
803 ERROR(PLUGIN_NAME ": Failed to parse table size OID (%s)",
804 ci->values[0].value.string);
805 td->size_oid.oid_len = 0;
812 static int snmp_agent_config_table_index_oid(table_definition_t *td,
813 oconfig_item_t *ci) {
815 if (ci->values_num < 1) {
816 WARNING(PLUGIN_NAME ": `IndexOID' is empty");
820 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
821 WARNING(PLUGIN_NAME ": `IndexOID' needs only string argument");
825 td->index_oid.oid_len = MAX_OID_LEN;
827 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->index_oid.oid,
828 &td->index_oid.oid_len)) {
829 ERROR(PLUGIN_NAME ": Failed to parse table index OID (%s)",
830 ci->values[0].value.string);
831 td->index_oid.oid_len = 0;
838 static int snmp_agent_config_table_data(table_definition_t *td,
839 oconfig_item_t *ci) {
840 data_definition_t *dd;
845 dd = calloc(1, sizeof(*dd));
847 ERROR(PLUGIN_NAME ": Failed to allocate memory for table data definition");
851 ret = cf_util_get_string(ci, &dd->name);
862 for (int i = 0; i < ci->children_num; i++) {
863 oconfig_item_t *option = ci->children + i;
865 if (strcasecmp("Instance", option->key) == 0)
866 ret = cf_util_get_boolean(option, &dd->is_instance);
867 else if (strcasecmp("Plugin", option->key) == 0)
868 ret = cf_util_get_string(option, &dd->plugin);
869 else if (strcasecmp("PluginInstance", option->key) == 0)
870 ret = cf_util_get_string(option, &dd->plugin_instance);
871 else if (strcasecmp("Type", option->key) == 0)
872 ret = cf_util_get_string(option, &dd->type);
873 else if (strcasecmp("TypeInstance", option->key) == 0)
874 ret = cf_util_get_string(option, &dd->type_instance);
875 else if (strcasecmp("Shift", option->key) == 0)
876 ret = cf_util_get_double(option, &dd->shift);
877 else if (strcasecmp("Scale", option->key) == 0)
878 ret = cf_util_get_double(option, &dd->scale);
879 else if (strcasecmp("OIDs", option->key) == 0)
880 ret = snmp_agent_config_data_oids(dd, option);
882 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
887 snmp_agent_free_data(&dd);
892 llentry_t *entry = llentry_create(dd->name, dd);
894 snmp_agent_free_data(&dd);
898 llist_append(td->columns, entry);
903 static int snmp_agent_config_data(oconfig_item_t *ci) {
904 data_definition_t *dd;
909 dd = calloc(1, sizeof(*dd));
911 ERROR(PLUGIN_NAME ": Failed to allocate memory for data definition");
915 ret = cf_util_get_string(ci, &dd->name);
924 for (int i = 0; i < ci->children_num; i++) {
925 oconfig_item_t *option = ci->children + i;
927 if (strcasecmp("Instance", option->key) == 0)
928 ret = cf_util_get_boolean(option, &dd->is_instance);
929 else if (strcasecmp("Plugin", option->key) == 0)
930 ret = cf_util_get_string(option, &dd->plugin);
931 else if (strcasecmp("PluginInstance", option->key) == 0)
932 ret = cf_util_get_string(option, &dd->plugin_instance);
933 else if (strcasecmp("Type", option->key) == 0)
934 ret = cf_util_get_string(option, &dd->type);
935 else if (strcasecmp("TypeInstance", option->key) == 0)
936 ret = cf_util_get_string(option, &dd->type_instance);
937 else if (strcasecmp("Shift", option->key) == 0)
938 ret = cf_util_get_double(option, &dd->shift);
939 else if (strcasecmp("Scale", option->key) == 0)
940 ret = cf_util_get_double(option, &dd->scale);
941 else if (strcasecmp("OIDs", option->key) == 0)
942 ret = snmp_agent_config_data_oids(dd, option);
944 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
949 snmp_agent_free_data(&dd);
954 llentry_t *entry = llentry_create(dd->name, dd);
956 snmp_agent_free_data(&dd);
960 llist_append(g_agent->scalars, entry);
965 static int num_compare(const int *a, const int *b) {
966 assert((a != NULL) && (b != NULL));
975 static int snmp_agent_config_table(oconfig_item_t *ci) {
976 table_definition_t *td;
981 td = calloc(1, sizeof(*td));
983 ERROR(PLUGIN_NAME ": Failed to allocate memory for table definition");
987 ret = cf_util_get_string(ci, &td->name);
993 td->columns = llist_create();
994 if (td->columns == NULL) {
995 ERROR(PLUGIN_NAME ": Failed to allocate memory for columns list");
996 snmp_agent_free_table(&td);
1000 for (int i = 0; i < ci->children_num; i++) {
1001 oconfig_item_t *option = ci->children + i;
1003 if (strcasecmp("IndexOID", option->key) == 0)
1004 ret = snmp_agent_config_table_index_oid(td, option);
1005 else if (strcasecmp("SizeOID", option->key) == 0)
1006 ret = snmp_agent_config_table_size_oid(td, option);
1007 else if (strcasecmp("Data", option->key) == 0)
1008 ret = snmp_agent_config_table_data(td, option);
1010 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
1015 snmp_agent_free_table(&td);
1020 if (td->index_oid.oid_len == 0) {
1021 ERROR(PLUGIN_NAME ": Table %s Index OID is not specified", td->name);
1022 snmp_agent_free_table(&td);
1026 llentry_t *entry = llentry_create(td->name, td);
1027 if (entry == NULL) {
1028 snmp_agent_free_table(&td);
1032 td->instance_index =
1033 c_avl_create((int (*)(const void *, const void *))strcmp);
1034 if (td->instance_index == NULL) {
1035 snmp_agent_free_table(&td);
1039 td->index_instance =
1040 c_avl_create((int (*)(const void *, const void *))num_compare);
1041 if (td->index_instance == NULL) {
1042 snmp_agent_free_table(&td);
1046 llist_append(g_agent->tables, entry);
1051 static int snmp_agent_get_value_from_ds_type(const value_t *val, int type,
1052 double scale, double shift,
1055 case DS_TYPE_COUNTER:
1056 *value = (long)((val->counter * scale) + shift);
1058 case DS_TYPE_ABSOLUTE:
1059 *value = (long)((val->absolute * scale) + shift);
1061 case DS_TYPE_DERIVE:
1062 *value = (long)((val->derive * scale) + shift);
1065 *value = (long)((val->gauge * scale) + shift);
1070 ERROR(PLUGIN_NAME ": Unknown data source type: %i", type);
1077 static int snmp_agent_set_vardata(void *data, size_t *data_len, u_char asn_type,
1078 double scale, double shift, const void *value,
1079 size_t len, int type) {
1082 netsnmp_vardata var;
1087 var.string = (u_char *)data;
1089 ret = snmp_agent_get_value_from_ds_type(val, type, scale, shift, &new_value);
1099 if (*data_len < sizeof(*var.integer))
1101 *var.integer = new_value;
1102 *data_len = sizeof(*var.integer);
1105 if (*data_len < sizeof(*var.counter64))
1107 var.counter64->high = (u_long)((int64_t)new_value >> 32);
1108 var.counter64->low = (u_long)((int64_t)new_value & 0xFFFFFFFF);
1109 *data_len = sizeof(*var.counter64);
1112 if (type == DS_TYPE_GAUGE) {
1113 char buf[DATA_MAX_NAME_LEN];
1114 snprintf(buf, sizeof(buf), "%.2f", val->gauge);
1115 if (*data_len < strlen(buf))
1117 *data_len = strlen(buf);
1118 memcpy(var.string, buf, *data_len);
1120 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1126 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1134 static int snmp_agent_register_oid_index(oid_t *oid, int index,
1135 Netsnmp_Node_Handler *handler) {
1137 memcpy(&new_oid, oid, sizeof(*oid));
1138 new_oid.oid[new_oid.oid_len++] = index;
1139 return snmp_agent_register_oid(&new_oid, handler);
1142 static int snmp_agent_unregister_oid_index(oid_t *oid, int index) {
1144 memcpy(&new_oid, oid, sizeof(*oid));
1145 new_oid.oid[new_oid.oid_len++] = index;
1146 return unregister_mib(new_oid.oid, new_oid.oid_len);
1149 static int snmp_agent_update_index(table_definition_t *td,
1150 const char *instance) {
1152 if (c_avl_get(td->instance_index, instance, NULL) == 0)
1159 ins = strdup(instance);
1163 index = calloc(1, sizeof(*index));
1164 if (index == NULL) {
1169 *index = c_avl_size(td->instance_index) + 1;
1171 ret = c_avl_insert(td->instance_index, ins, index);
1178 ret = c_avl_insert(td->index_instance, index, ins);
1180 DEBUG(PLUGIN_NAME ": Failed to update index_instance for '%s' table",
1185 DEBUG(PLUGIN_NAME ": Updated index for '%s' table [%d, %s]", td->name, *index,
1188 /* need to generate index for the table */
1189 if (td->index_oid.oid_len) {
1191 ret = snmp_agent_register_oid_index(&td->index_oid, *index,
1192 snmp_agent_table_index_oid_handler);
1196 /* register new oids for all columns */
1197 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1198 data_definition_t *dd = de->value;
1200 for (int i = 0; i < dd->oids_len; i++) {
1201 ret = snmp_agent_register_oid_index(&dd->oids[i], *index,
1202 snmp_agent_table_oid_handler);
1209 notification_t n = {
1210 .severity = NOTIF_OKAY,
1212 .plugin = PLUGIN_NAME
1214 sstrncpy(n.host, hostname_g, sizeof(n.host));
1215 sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance));
1216 ssnprintf(n.message, sizeof(n.message),
1217 "Data row added to table %s instance %s index %d", td->name, ins,
1219 plugin_dispatch_notification(&n);
1224 static int snmp_agent_write(value_list_t const *vl) {
1229 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1230 table_definition_t *td = te->value;
1232 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1233 data_definition_t *dd = de->value;
1235 if (!dd->is_instance) {
1236 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
1237 vl->type_instance)) {
1238 snmp_agent_update_index(td, vl->plugin_instance);
1248 static int snmp_agent_collect(const data_set_t *ds, const value_list_t *vl,
1249 user_data_t __attribute__((unused)) * user_data) {
1251 pthread_mutex_lock(&g_agent->lock);
1253 snmp_agent_write(vl);
1255 pthread_mutex_unlock(&g_agent->lock);
1260 static int snmp_agent_preinit(void) {
1261 if (g_agent != NULL) {
1262 /* already initialized if config callback was called before init callback */
1266 g_agent = calloc(1, sizeof(*g_agent));
1267 if (g_agent == NULL) {
1268 ERROR(PLUGIN_NAME ": Failed to allocate memory for snmp agent context");
1272 g_agent->tables = llist_create();
1273 g_agent->scalars = llist_create();
1276 /* make us a agentx client. */
1277 err = netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE,
1280 ERROR(PLUGIN_NAME ": Failed to set agent role (%d)", err);
1285 * For SNMP debug purposes uses snmp_set_do_debugging(1);
1288 /* initialize the agent library */
1289 err = init_agent(PLUGIN_NAME);
1291 ERROR(PLUGIN_NAME ": Failed to initialize the agent library (%d)", err);
1295 init_snmp(PLUGIN_NAME);
1297 g_agent->tp = read_all_mibs();
1302 static int snmp_agent_init(void) {
1305 ret = snmp_agent_preinit();
1309 ret = snmp_agent_register_scalar_oids();
1313 ret = snmp_agent_register_table_oids();
1317 /* create a second thread to listen for requests from AgentX*/
1318 ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
1320 ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
1324 ret = pthread_mutex_init(&g_agent->lock, NULL);
1326 ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret);
1330 ret = pthread_mutex_init(&g_agent->agentx_lock, NULL);
1332 ERROR(PLUGIN_NAME ": Failed to initialize AgentX mutex, err %u", ret);
1339 static void *snmp_agent_thread_run(void __attribute__((unused)) * arg) {
1340 INFO(PLUGIN_NAME ": Thread is up and running");
1343 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1345 pthread_mutex_lock(&g_agent->agentx_lock);
1346 agent_check_and_process(0); /* 0 == don't block */
1347 pthread_mutex_unlock(&g_agent->agentx_lock);
1349 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
1356 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler) {
1357 netsnmp_handler_registration *reg;
1358 char *oid_name = snmp_agent_get_oid_name(oid->oid, oid->oid_len - 1);
1359 char oid_str[DATA_MAX_NAME_LEN];
1361 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), oid);
1363 if (oid_name == NULL) {
1365 ": Skipped registration: OID (%s) is not found in main tree",
1370 reg = netsnmp_create_handler_registration(oid_name, handler, oid->oid,
1371 oid->oid_len, HANDLER_CAN_RONLY);
1373 ERROR(PLUGIN_NAME ": Failed to create handler registration for OID (%s)",
1378 pthread_mutex_lock(&g_agent->agentx_lock);
1380 if (netsnmp_register_instance(reg) != MIB_REGISTERED_OK) {
1381 ERROR(PLUGIN_NAME ": Failed to register handler for OID (%s)", oid_str);
1382 pthread_mutex_unlock(&g_agent->agentx_lock);
1386 pthread_mutex_unlock(&g_agent->agentx_lock);
1388 DEBUG(PLUGIN_NAME ": Registered handler for OID (%s)", oid_str);
1393 static int snmp_agent_free_config(void) {
1395 if (g_agent == NULL)
1398 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next)
1399 snmp_agent_free_table((table_definition_t **)&te->value);
1400 llist_destroy(g_agent->tables);
1402 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL; de = de->next)
1403 snmp_agent_free_data((data_definition_t **)&de->value);
1404 llist_destroy(g_agent->scalars);
1409 static int snmp_agent_shutdown(void) {
1412 DEBUG(PLUGIN_NAME ": snmp_agent_shutdown");
1414 if (g_agent == NULL) {
1415 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: plugin not initialized");
1419 if (pthread_cancel(g_agent->thread) != 0)
1420 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to cancel the thread");
1422 if (pthread_join(g_agent->thread, NULL) != 0)
1423 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to join the thread");
1425 snmp_agent_free_config();
1427 snmp_shutdown(PLUGIN_NAME);
1429 pthread_mutex_destroy(&g_agent->lock);
1430 pthread_mutex_destroy(&g_agent->agentx_lock);
1437 static int snmp_agent_config(oconfig_item_t *ci) {
1439 int ret = snmp_agent_preinit();
1446 for (int i = 0; i < ci->children_num; i++) {
1447 oconfig_item_t *child = ci->children + i;
1448 if (strcasecmp("Data", child->key) == 0) {
1449 ret = snmp_agent_config_data(child);
1450 } else if (strcasecmp("Table", child->key) == 0) {
1451 ret = snmp_agent_config_table(child);
1453 ERROR(PLUGIN_NAME ": Unknown configuration option `%s'", child->key);
1458 ERROR(PLUGIN_NAME ": Failed to parse configuration");
1459 snmp_agent_free_config();
1460 snmp_shutdown(PLUGIN_NAME);
1466 ret = snmp_agent_validate_data();
1468 ERROR(PLUGIN_NAME ": Invalid configuration provided");
1469 snmp_agent_free_config();
1470 snmp_shutdown(PLUGIN_NAME);
1478 void module_register(void) {
1479 plugin_register_init(PLUGIN_NAME, snmp_agent_init);
1480 plugin_register_complex_config(PLUGIN_NAME, snmp_agent_config);
1481 plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL);
1482 plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL);
1483 plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown);