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 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 if ((*td)->instance_index != NULL) {
420 while (c_avl_pick((*td)->instance_index, &key, &value) == 0) {
424 c_avl_destroy((*td)->instance_index);
425 (*td)->instance_index = NULL;
434 static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests,
435 data_definition_t *dd, char *instance,
437 char name[DATA_MAX_NAME_LEN];
438 format_name(name, sizeof(name), hostname_g, dd->plugin,
439 instance ? instance : dd->plugin_instance, dd->type,
441 DEBUG(PLUGIN_NAME ": Identifier '%s'", name);
445 const data_set_t *ds = plugin_get_ds(dd->type);
447 ERROR(PLUGIN_NAME ": Data set not found for '%s' type", dd->type);
448 return SNMP_NOSUCHINSTANCE;
451 int ret = uc_get_value_by_name(name, &values, &values_num);
454 ERROR(PLUGIN_NAME ": Failed to get value for '%s'", name);
455 return SNMP_NOSUCHINSTANCE;
458 assert(ds->ds_num == values_num);
459 assert(oid_index < values_num);
461 char data[DATA_MAX_NAME_LEN];
462 size_t data_len = sizeof(data);
463 ret = snmp_agent_set_vardata(
464 data, &data_len, dd->oids[oid_index].type, dd->scale, dd->shift,
465 &values[oid_index], sizeof(values[oid_index]), ds->ds[oid_index].type);
470 ERROR(PLUGIN_NAME ": Failed to convert '%s' value to snmp data", name);
471 return SNMP_NOSUCHINSTANCE;
474 requests->requestvb->type = dd->oids[oid_index].type;
475 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type, data,
478 return SNMP_ERR_NOERROR;
482 snmp_agent_table_oid_handler(struct netsnmp_mib_handler_s *handler,
483 struct netsnmp_handler_registration_s *reginfo,
484 struct netsnmp_agent_request_info_s *reqinfo,
485 struct netsnmp_request_info_s *requests) {
487 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
488 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
489 return SNMP_ERR_NOERROR;
492 pthread_mutex_lock(&g_agent->lock);
495 memcpy(oid.oid, requests->requestvb->name,
496 sizeof(oid.oid[0]) * requests->requestvb->name_length);
497 oid.oid_len = requests->requestvb->name_length;
500 char oid_str[DATA_MAX_NAME_LEN];
501 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
502 DEBUG(PLUGIN_NAME ": Get request received for table OID '%s'", oid_str);
505 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
506 table_definition_t *td = te->value;
508 if (!td->index_oid.oid_len) {
509 DEBUG(PLUGIN_NAME ": %s:%d NOT IMPLEMENTED", __FUNCTION__, __LINE__);
513 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
514 data_definition_t *dd = de->value;
516 for (int i = 0; i < dd->oids_len; i++) {
517 int ret = snmp_oid_ncompare(oid.oid, oid.oid_len, dd->oids[i].oid,
519 MIN(oid.oid_len, dd->oids[i].oid_len));
523 int index = oid.oid[oid.oid_len - 1];
526 ret = c_avl_get(td->index_instance, &index, (void **)&instance);
528 DEBUG(PLUGIN_NAME ": Nonexisting index '%d' requested", index);
529 pthread_mutex_unlock(&g_agent->lock);
530 return SNMP_NOSUCHINSTANCE;
533 if (dd->is_instance) {
534 requests->requestvb->type = ASN_OCTET_STR;
535 snmp_set_var_typed_value(requests->requestvb,
536 requests->requestvb->type, instance,
539 pthread_mutex_unlock(&g_agent->lock);
541 return SNMP_ERR_NOERROR;
544 ret = snmp_agent_form_reply(requests, dd, instance, i);
546 pthread_mutex_unlock(&g_agent->lock);
553 pthread_mutex_unlock(&g_agent->lock);
555 return SNMP_NOSUCHINSTANCE;
558 static int snmp_agent_table_index_oid_handler(
559 struct netsnmp_mib_handler_s *handler,
560 struct netsnmp_handler_registration_s *reginfo,
561 struct netsnmp_agent_request_info_s *reqinfo,
562 struct netsnmp_request_info_s *requests) {
564 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
565 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
566 return SNMP_ERR_NOERROR;
569 pthread_mutex_lock(&g_agent->lock);
572 memcpy(oid.oid, requests->requestvb->name,
573 sizeof(oid.oid[0]) * requests->requestvb->name_length);
574 oid.oid_len = requests->requestvb->name_length;
576 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
577 table_definition_t *td = te->value;
579 if (td->index_oid.oid_len &&
580 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->index_oid.oid,
581 td->index_oid.oid_len,
582 MIN(oid.oid_len, td->index_oid.oid_len)) == 0)) {
584 DEBUG(PLUGIN_NAME ": Handle '%s' table index OID", td->name);
586 int index = oid.oid[oid.oid_len - 1];
588 int ret = c_avl_get(td->index_instance, &index, &(void *){NULL});
590 /* nonexisting index requested */
591 pthread_mutex_unlock(&g_agent->lock);
592 return SNMP_NOSUCHINSTANCE;
595 requests->requestvb->type = ASN_INTEGER;
596 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
597 &index, sizeof(index));
599 pthread_mutex_unlock(&g_agent->lock);
601 return SNMP_ERR_NOERROR;
605 pthread_mutex_unlock(&g_agent->lock);
607 return SNMP_NOSUCHINSTANCE;
610 static int snmp_agent_table_size_oid_handler(
611 struct netsnmp_mib_handler_s *handler,
612 struct netsnmp_handler_registration_s *reginfo,
613 struct netsnmp_agent_request_info_s *reqinfo,
614 struct netsnmp_request_info_s *requests) {
616 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
617 DEBUG(PLUGIN_NAME ": Not supported request mode (%d)", reqinfo->mode);
618 return SNMP_ERR_NOERROR;
621 pthread_mutex_lock(&g_agent->lock);
624 memcpy(oid.oid, requests->requestvb->name,
625 sizeof(oid.oid[0]) * requests->requestvb->name_length);
626 oid.oid_len = requests->requestvb->name_length;
628 DEBUG(PLUGIN_NAME ": Get request received for table size OID");
630 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
631 table_definition_t *td = te->value;
633 if (td->size_oid.oid_len &&
634 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->size_oid.oid,
635 td->size_oid.oid_len,
636 MIN(oid.oid_len, td->size_oid.oid_len)) == 0)) {
637 DEBUG(PLUGIN_NAME ": Handle '%s' table size OID", td->name);
639 long size = c_avl_size(td->index_instance);
641 requests->requestvb->type = td->size_oid.type;
642 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
643 &size, sizeof(size));
645 pthread_mutex_unlock(&g_agent->lock);
647 return SNMP_ERR_NOERROR;
651 pthread_mutex_unlock(&g_agent->lock);
653 return SNMP_NOSUCHINSTANCE;
657 snmp_agent_scalar_oid_handler(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;
675 char oid_str[DATA_MAX_NAME_LEN];
676 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), &oid);
677 DEBUG(PLUGIN_NAME ": Get request received for scalar OID '%s'", oid_str);
680 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL;
682 data_definition_t *dd = de->value;
684 for (int i = 0; i < dd->oids_len; i++) {
686 int ret = snmp_oid_compare(oid.oid, oid.oid_len, dd->oids[i].oid,
687 dd->oids[i].oid_len);
691 ret = snmp_agent_form_reply(requests, dd, NULL, i);
693 pthread_mutex_unlock(&g_agent->lock);
699 pthread_mutex_unlock(&g_agent->lock);
701 return SNMP_NOSUCHINSTANCE;
704 static int snmp_agent_register_table_oids(void) {
706 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
707 table_definition_t *td = te->value;
709 if (td->size_oid.oid_len != 0) {
711 snmp_agent_get_asn_type(td->size_oid.oid, td->size_oid.oid_len);
712 td->size_oid.oid_len++;
713 int ret = snmp_agent_register_oid(&td->size_oid,
714 snmp_agent_table_size_oid_handler);
719 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
720 data_definition_t *dd = de->value;
722 for (int i = 0; i < dd->oids_len; i++) {
724 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
732 static int snmp_agent_register_scalar_oids(void) {
734 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
735 data_definition_t *dd = e->value;
737 for (int i = 0; i < dd->oids_len; i++) {
740 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
743 snmp_agent_register_oid(&dd->oids[i], snmp_agent_scalar_oid_handler);
752 static int snmp_agent_config_data_oids(data_definition_t *dd,
753 oconfig_item_t *ci) {
754 if (ci->values_num < 1) {
755 WARNING(PLUGIN_NAME ": `OIDs' needs at least one argument");
759 for (int i = 0; i < ci->values_num; i++)
760 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
761 WARNING(PLUGIN_NAME ": `OIDs' needs only string argument");
765 if (dd->oids != NULL)
768 dd->oids = calloc(ci->values_num, sizeof(*dd->oids));
769 if (dd->oids == NULL)
771 dd->oids_len = (size_t)ci->values_num;
773 for (int i = 0; i < ci->values_num; i++) {
774 dd->oids[i].oid_len = MAX_OID_LEN;
776 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->oids[i].oid,
777 &dd->oids[i].oid_len)) {
778 ERROR(PLUGIN_NAME ": snmp_parse_oid (%s) failed",
779 ci->values[i].value.string);
789 static int snmp_agent_config_table_size_oid(table_definition_t *td,
790 oconfig_item_t *ci) {
791 if (ci->values_num < 1) {
792 WARNING(PLUGIN_NAME ": `TableSizeOID' is empty");
796 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
797 WARNING(PLUGIN_NAME ": `TableSizeOID' needs only string argument");
801 td->size_oid.oid_len = MAX_OID_LEN;
803 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->size_oid.oid,
804 &td->size_oid.oid_len)) {
805 ERROR(PLUGIN_NAME ": Failed to parse table size OID (%s)",
806 ci->values[0].value.string);
807 td->size_oid.oid_len = 0;
814 static int snmp_agent_config_table_index_oid(table_definition_t *td,
815 oconfig_item_t *ci) {
817 if (ci->values_num < 1) {
818 WARNING(PLUGIN_NAME ": `IndexOID' is empty");
822 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
823 WARNING(PLUGIN_NAME ": `IndexOID' needs only string argument");
827 td->index_oid.oid_len = MAX_OID_LEN;
829 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->index_oid.oid,
830 &td->index_oid.oid_len)) {
831 ERROR(PLUGIN_NAME ": Failed to parse table index OID (%s)",
832 ci->values[0].value.string);
833 td->index_oid.oid_len = 0;
840 static int snmp_agent_config_table_data(table_definition_t *td,
841 oconfig_item_t *ci) {
842 data_definition_t *dd;
847 dd = calloc(1, sizeof(*dd));
849 ERROR(PLUGIN_NAME ": Failed to allocate memory for table data definition");
853 ret = cf_util_get_string(ci, &dd->name);
864 for (int i = 0; i < ci->children_num; i++) {
865 oconfig_item_t *option = ci->children + i;
867 if (strcasecmp("Instance", option->key) == 0)
868 ret = cf_util_get_boolean(option, &dd->is_instance);
869 else if (strcasecmp("Plugin", option->key) == 0)
870 ret = cf_util_get_string(option, &dd->plugin);
871 else if (strcasecmp("PluginInstance", option->key) == 0)
872 ret = cf_util_get_string(option, &dd->plugin_instance);
873 else if (strcasecmp("Type", option->key) == 0)
874 ret = cf_util_get_string(option, &dd->type);
875 else if (strcasecmp("TypeInstance", option->key) == 0)
876 ret = cf_util_get_string(option, &dd->type_instance);
877 else if (strcasecmp("Shift", option->key) == 0)
878 ret = cf_util_get_double(option, &dd->shift);
879 else if (strcasecmp("Scale", option->key) == 0)
880 ret = cf_util_get_double(option, &dd->scale);
881 else if (strcasecmp("OIDs", option->key) == 0)
882 ret = snmp_agent_config_data_oids(dd, option);
884 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
889 snmp_agent_free_data(&dd);
894 llentry_t *entry = llentry_create(dd->name, dd);
896 snmp_agent_free_data(&dd);
900 llist_append(td->columns, entry);
905 static int snmp_agent_config_data(oconfig_item_t *ci) {
906 data_definition_t *dd;
911 dd = calloc(1, sizeof(*dd));
913 ERROR(PLUGIN_NAME ": Failed to allocate memory for data definition");
917 ret = cf_util_get_string(ci, &dd->name);
926 for (int i = 0; i < ci->children_num; i++) {
927 oconfig_item_t *option = ci->children + i;
929 if (strcasecmp("Instance", option->key) == 0)
930 ret = cf_util_get_boolean(option, &dd->is_instance);
931 else if (strcasecmp("Plugin", option->key) == 0)
932 ret = cf_util_get_string(option, &dd->plugin);
933 else if (strcasecmp("PluginInstance", option->key) == 0)
934 ret = cf_util_get_string(option, &dd->plugin_instance);
935 else if (strcasecmp("Type", option->key) == 0)
936 ret = cf_util_get_string(option, &dd->type);
937 else if (strcasecmp("TypeInstance", option->key) == 0)
938 ret = cf_util_get_string(option, &dd->type_instance);
939 else if (strcasecmp("Shift", option->key) == 0)
940 ret = cf_util_get_double(option, &dd->shift);
941 else if (strcasecmp("Scale", option->key) == 0)
942 ret = cf_util_get_double(option, &dd->scale);
943 else if (strcasecmp("OIDs", option->key) == 0)
944 ret = snmp_agent_config_data_oids(dd, option);
946 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
951 snmp_agent_free_data(&dd);
956 llentry_t *entry = llentry_create(dd->name, dd);
958 snmp_agent_free_data(&dd);
962 llist_append(g_agent->scalars, entry);
967 static int num_compare(const int *a, const int *b) {
968 assert((a != NULL) && (b != NULL));
977 static int snmp_agent_config_table(oconfig_item_t *ci) {
978 table_definition_t *td;
983 td = calloc(1, sizeof(*td));
985 ERROR(PLUGIN_NAME ": Failed to allocate memory for table definition");
989 ret = cf_util_get_string(ci, &td->name);
995 td->columns = llist_create();
996 if (td->columns == NULL) {
997 ERROR(PLUGIN_NAME ": Failed to allocate memory for columns list");
998 snmp_agent_free_table(&td);
1002 for (int i = 0; i < ci->children_num; i++) {
1003 oconfig_item_t *option = ci->children + i;
1005 if (strcasecmp("IndexOID", option->key) == 0)
1006 ret = snmp_agent_config_table_index_oid(td, option);
1007 else if (strcasecmp("SizeOID", option->key) == 0)
1008 ret = snmp_agent_config_table_size_oid(td, option);
1009 else if (strcasecmp("Data", option->key) == 0)
1010 ret = snmp_agent_config_table_data(td, option);
1012 WARNING(PLUGIN_NAME ": Option `%s' not allowed here", option->key);
1017 snmp_agent_free_table(&td);
1022 if (td->index_oid.oid_len == 0) {
1023 ERROR(PLUGIN_NAME ": Table %s Index OID is not specified", td->name);
1024 snmp_agent_free_table(&td);
1028 llentry_t *entry = llentry_create(td->name, td);
1029 if (entry == NULL) {
1030 snmp_agent_free_table(&td);
1034 td->instance_index =
1035 c_avl_create((int (*)(const void *, const void *))strcmp);
1036 if (td->instance_index == NULL) {
1037 snmp_agent_free_table(&td);
1041 td->index_instance =
1042 c_avl_create((int (*)(const void *, const void *))num_compare);
1043 if (td->index_instance == NULL) {
1044 snmp_agent_free_table(&td);
1048 llist_append(g_agent->tables, entry);
1053 static int snmp_agent_get_value_from_ds_type(const value_t *val, int type,
1054 double scale, double shift,
1057 case DS_TYPE_COUNTER:
1058 *value = (long)((val->counter * scale) + shift);
1060 case DS_TYPE_ABSOLUTE:
1061 *value = (long)((val->absolute * scale) + shift);
1063 case DS_TYPE_DERIVE:
1064 *value = (long)((val->derive * scale) + shift);
1067 *value = (long)((val->gauge * scale) + shift);
1072 ERROR(PLUGIN_NAME ": Unknown data source type: %i", type);
1079 static int snmp_agent_set_vardata(void *data, size_t *data_len, u_char asn_type,
1080 double scale, double shift, const void *value,
1081 size_t len, int type) {
1084 netsnmp_vardata var;
1089 var.string = (u_char *)data;
1091 ret = snmp_agent_get_value_from_ds_type(val, type, scale, shift, &new_value);
1101 if (*data_len < sizeof(*var.integer))
1103 *var.integer = new_value;
1104 *data_len = sizeof(*var.integer);
1107 if (*data_len < sizeof(*var.counter64))
1109 var.counter64->high = (u_long)((int64_t)new_value >> 32);
1110 var.counter64->low = (u_long)((int64_t)new_value & 0xFFFFFFFF);
1111 *data_len = sizeof(*var.counter64);
1114 if (type == DS_TYPE_GAUGE) {
1115 char buf[DATA_MAX_NAME_LEN];
1116 snprintf(buf, sizeof(buf), "%.2f", val->gauge);
1117 if (*data_len < strlen(buf))
1119 *data_len = strlen(buf);
1120 memcpy(var.string, buf, *data_len);
1122 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1128 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type", type,
1136 static int snmp_agent_register_oid_index(oid_t *oid, int index,
1137 Netsnmp_Node_Handler *handler) {
1139 memcpy(&new_oid, oid, sizeof(*oid));
1140 new_oid.oid[new_oid.oid_len++] = index;
1141 return snmp_agent_register_oid(&new_oid, handler);
1144 static int snmp_agent_unregister_oid_index(oid_t *oid, int index) {
1146 memcpy(&new_oid, oid, sizeof(*oid));
1147 new_oid.oid[new_oid.oid_len++] = index;
1148 return unregister_mib(new_oid.oid, new_oid.oid_len);
1151 static int snmp_agent_update_index(table_definition_t *td,
1152 const char *instance) {
1154 if (c_avl_get(td->instance_index, instance, NULL) == 0)
1161 ins = strdup(instance);
1165 index = calloc(1, sizeof(*index));
1166 if (index == NULL) {
1171 *index = c_avl_size(td->instance_index) + 1;
1173 ret = c_avl_insert(td->instance_index, ins, index);
1180 ret = c_avl_insert(td->index_instance, index, ins);
1182 DEBUG(PLUGIN_NAME ": Failed to update index_instance for '%s' table",
1187 DEBUG(PLUGIN_NAME ": Updated index for '%s' table [%d, %s]", td->name, *index,
1190 /* need to generate index for the table */
1191 if (td->index_oid.oid_len) {
1193 ret = snmp_agent_register_oid_index(&td->index_oid, *index,
1194 snmp_agent_table_index_oid_handler);
1198 /* register new oids for all columns */
1199 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1200 data_definition_t *dd = de->value;
1202 for (int i = 0; i < dd->oids_len; i++) {
1203 ret = snmp_agent_register_oid_index(&dd->oids[i], *index,
1204 snmp_agent_table_oid_handler);
1211 notification_t n = {
1212 .severity = NOTIF_OKAY,
1214 .plugin = PLUGIN_NAME
1216 sstrncpy(n.host, hostname_g, sizeof(n.host));
1217 sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance));
1218 ssnprintf(n.message, sizeof(n.message),
1219 "Data row added to table %s instance %s index %d", td->name, ins,
1221 plugin_dispatch_notification(&n);
1226 static int snmp_agent_write(value_list_t const *vl) {
1231 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1232 table_definition_t *td = te->value;
1234 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1235 data_definition_t *dd = de->value;
1237 if (!dd->is_instance) {
1238 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
1239 vl->type_instance)) {
1240 snmp_agent_update_index(td, vl->plugin_instance);
1250 static int snmp_agent_collect(const data_set_t *ds, const value_list_t *vl,
1251 user_data_t __attribute__((unused)) * user_data) {
1253 pthread_mutex_lock(&g_agent->lock);
1255 snmp_agent_write(vl);
1257 pthread_mutex_unlock(&g_agent->lock);
1262 static int snmp_agent_preinit(void) {
1263 if (g_agent != NULL) {
1264 /* already initialized if config callback was called before init callback */
1268 g_agent = calloc(1, sizeof(*g_agent));
1269 if (g_agent == NULL) {
1270 ERROR(PLUGIN_NAME ": Failed to allocate memory for snmp agent context");
1274 g_agent->tables = llist_create();
1275 g_agent->scalars = llist_create();
1278 /* make us a agentx client. */
1279 err = netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE,
1282 ERROR(PLUGIN_NAME ": Failed to set agent role (%d)", err);
1287 * For SNMP debug purposes uses snmp_set_do_debugging(1);
1290 /* initialize the agent library */
1291 err = init_agent(PLUGIN_NAME);
1293 ERROR(PLUGIN_NAME ": Failed to initialize the agent library (%d)", err);
1297 init_snmp(PLUGIN_NAME);
1299 g_agent->tp = read_all_mibs();
1304 static int snmp_agent_init(void) {
1307 ret = snmp_agent_preinit();
1311 ret = snmp_agent_register_scalar_oids();
1315 ret = snmp_agent_register_table_oids();
1319 /* create a second thread to listen for requests from AgentX*/
1320 ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
1322 ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
1326 ret = pthread_mutex_init(&g_agent->lock, NULL);
1328 ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret);
1332 ret = pthread_mutex_init(&g_agent->agentx_lock, NULL);
1334 ERROR(PLUGIN_NAME ": Failed to initialize AgentX mutex, err %u", ret);
1341 static void *snmp_agent_thread_run(void __attribute__((unused)) * arg) {
1342 INFO(PLUGIN_NAME ": Thread is up and running");
1345 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1347 pthread_mutex_lock(&g_agent->agentx_lock);
1348 agent_check_and_process(0); /* 0 == don't block */
1349 pthread_mutex_unlock(&g_agent->agentx_lock);
1351 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
1358 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler) {
1359 netsnmp_handler_registration *reg;
1360 char *oid_name = snmp_agent_get_oid_name(oid->oid, oid->oid_len - 1);
1361 char oid_str[DATA_MAX_NAME_LEN];
1363 snmp_agent_oid_to_string(oid_str, sizeof(oid_str), oid);
1365 if (oid_name == NULL) {
1367 ": Skipped registration: OID (%s) is not found in main tree",
1372 reg = netsnmp_create_handler_registration(oid_name, handler, oid->oid,
1373 oid->oid_len, HANDLER_CAN_RONLY);
1375 ERROR(PLUGIN_NAME ": Failed to create handler registration for OID (%s)",
1380 pthread_mutex_lock(&g_agent->agentx_lock);
1382 if (netsnmp_register_instance(reg) != MIB_REGISTERED_OK) {
1383 ERROR(PLUGIN_NAME ": Failed to register handler for OID (%s)", oid_str);
1384 pthread_mutex_unlock(&g_agent->agentx_lock);
1388 pthread_mutex_unlock(&g_agent->agentx_lock);
1390 DEBUG(PLUGIN_NAME ": Registered handler for OID (%s)", oid_str);
1395 static int snmp_agent_free_config(void) {
1397 if (g_agent == NULL)
1400 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next)
1401 snmp_agent_free_table((table_definition_t **)&te->value);
1402 llist_destroy(g_agent->tables);
1404 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL; de = de->next)
1405 snmp_agent_free_data((data_definition_t **)&de->value);
1406 llist_destroy(g_agent->scalars);
1411 static int snmp_agent_shutdown(void) {
1414 DEBUG(PLUGIN_NAME ": snmp_agent_shutdown");
1416 if (g_agent == NULL) {
1417 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: plugin not initialized");
1421 if (pthread_cancel(g_agent->thread) != 0)
1422 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to cancel the thread");
1424 if (pthread_join(g_agent->thread, NULL) != 0)
1425 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to join the thread");
1427 snmp_agent_free_config();
1429 snmp_shutdown(PLUGIN_NAME);
1431 pthread_mutex_destroy(&g_agent->lock);
1432 pthread_mutex_destroy(&g_agent->agentx_lock);
1439 static int snmp_agent_config(oconfig_item_t *ci) {
1441 int ret = snmp_agent_preinit();
1448 for (int i = 0; i < ci->children_num; i++) {
1449 oconfig_item_t *child = ci->children + i;
1450 if (strcasecmp("Data", child->key) == 0) {
1451 ret = snmp_agent_config_data(child);
1452 } else if (strcasecmp("Table", child->key) == 0) {
1453 ret = snmp_agent_config_table(child);
1455 ERROR(PLUGIN_NAME ": Unknown configuration option `%s'", child->key);
1460 ERROR(PLUGIN_NAME ": Failed to parse configuration");
1461 snmp_agent_free_config();
1462 snmp_shutdown(PLUGIN_NAME);
1468 ret = snmp_agent_validate_data();
1470 ERROR(PLUGIN_NAME ": Invalid configuration provided");
1471 snmp_agent_free_config();
1472 snmp_shutdown(PLUGIN_NAME);
1480 void module_register(void) {
1481 plugin_register_init(PLUGIN_NAME, snmp_agent_init);
1482 plugin_register_complex_config(PLUGIN_NAME, snmp_agent_config);
1483 plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL);
1484 plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL);
1485 plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown);