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
48 typedef struct oid_s oid_t;
50 struct table_definition_s {
55 c_avl_tree_t *instance_index;
56 c_avl_tree_t *index_instance;
58 typedef struct table_definition_s table_definition_t;
60 struct data_definition_s {
63 char *plugin_instance;
66 table_definition_t *table;
73 typedef struct data_definition_s data_definition_t;
75 struct snmp_agent_ctx_s {
78 pthread_mutex_t agentx_lock;
84 typedef struct snmp_agent_ctx_s snmp_agent_ctx_t;
86 snmp_agent_ctx_t *g_agent = NULL;
88 #define CHECK_DD_TYPE(_dd, _p, _pi, _t, _ti) \
89 (_dd->plugin ? !strcmp(_dd->plugin, _p) : 0) && \
90 (_dd->plugin_instance ? !strcmp(_dd->plugin_instance, _pi) : 1) && \
91 (_dd->type ? !strcmp(_dd->type, _t) : 1) && \
92 (_dd->type_instance ? !strcmp(_dd->type_instance, _ti) : 1)
94 static void *snmp_agent_thread_run(void *arg);
95 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler);
96 static int snmp_agent_set_vardata(void *dst_buf, size_t *dst_buf_len,
97 u_char asn_type, double scale, double shift,
98 const void *value, size_t len, int type);
99 static int snmp_agent_unregister_oid_index(oid_t *oid, int index);
101 static u_char snmp_agent_get_asn_type(oid *oid, size_t oid_len) {
102 struct tree *node = get_tree(oid, oid_len, g_agent->tp);
104 return (node != NULL ? mib_to_asn_type(node->type) : 0);
107 static char *snmp_agent_get_oid_name(oid *oid, size_t oid_len) {
108 struct tree *node = get_tree(oid, oid_len, g_agent->tp);
110 return (node != NULL ? node->label : NULL);
113 static int snmp_agent_oid_to_string(char *buf, size_t buf_size,
115 char oid_str[MAX_OID_LEN][16];
116 char *oid_str_ptr[MAX_OID_LEN];
118 for (size_t i = 0; i < o->oid_len; i++) {
119 ssnprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
120 oid_str_ptr[i] = oid_str[i];
123 return (strjoin(buf, buf_size, oid_str_ptr, o->oid_len, "."));
126 static void snmp_agent_dump_data(void) {
128 char temp[DATA_MAX_NAME_LEN];
130 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
131 table_definition_t *td = (table_definition_t *)te->value;
133 DEBUG(PLUGIN_NAME ": Table:");
134 DEBUG(PLUGIN_NAME ": Name: %s", td->name);
135 if (td->index_oid.oid_len != 0) {
136 snmp_agent_oid_to_string(temp, sizeof(temp), &td->index_oid);
137 DEBUG(PLUGIN_NAME ": IndexOID: %s", temp);
139 if (td->size_oid.oid_len != 0) {
140 snmp_agent_oid_to_string(temp, sizeof(temp), &td->size_oid);
141 DEBUG(PLUGIN_NAME ": SizeOID: %s", temp);
144 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
145 data_definition_t *dd = (data_definition_t *)de->value;
147 DEBUG(PLUGIN_NAME ": Column:");
148 DEBUG(PLUGIN_NAME ": Name: %s", dd->name);
150 DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin);
151 if (dd->plugin_instance)
152 DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance);
154 DEBUG(PLUGIN_NAME ": Instance: true");
156 DEBUG(PLUGIN_NAME ": Type: %s", dd->type);
157 if (dd->type_instance)
158 DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance);
159 for (int i = 0; i < dd->oids_len; i++) {
160 snmp_agent_oid_to_string(temp, sizeof(temp), &dd->oids[i]);
161 DEBUG(PLUGIN_NAME ": OID[%d]: %s", i, temp);
163 DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale);
164 DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift);
168 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
169 data_definition_t *dd = (data_definition_t *)e->value;
171 DEBUG(PLUGIN_NAME ": Scalar:");
172 DEBUG(PLUGIN_NAME ": Name: %s", dd->name);
174 DEBUG(PLUGIN_NAME ": Plugin: %s", dd->plugin);
175 if (dd->plugin_instance)
176 DEBUG(PLUGIN_NAME ": PluginInstance: %s", dd->plugin_instance);
178 DEBUG(PLUGIN_NAME ": Instance: true");
180 DEBUG(PLUGIN_NAME ": Type: %s", dd->type);
181 if (dd->type_instance)
182 DEBUG(PLUGIN_NAME ": TypeInstance: %s", dd->type_instance);
183 for (int i = 0; i < dd->oids_len; i++) {
184 snmp_agent_oid_to_string(temp, sizeof(temp), &dd->oids[i]);
185 DEBUG(PLUGIN_NAME ": OID[%d]: %s", i, temp);
187 DEBUG(PLUGIN_NAME ": Scale: %g", dd->scale);
188 DEBUG(PLUGIN_NAME ": Shift: %g", dd->shift);
190 #endif /* COLLECT_DEBUG */
193 static int snmp_agent_validate_data(void) {
195 snmp_agent_dump_data();
197 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
198 table_definition_t *td = (table_definition_t *)te->value;
200 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
201 data_definition_t *dd = (data_definition_t *)de->value;
204 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'.'%s'", td->name,
209 if (dd->plugin_instance) {
210 ERROR(PLUGIN_NAME ": PluginInstance should not be defined for table "
211 "data type '%s'.'%s'",
216 if (dd->oids_len == 0) {
217 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'.'%s'", td->name,
222 if (dd->is_instance) {
224 if (dd->type || dd->type_instance) {
225 ERROR(PLUGIN_NAME ": Type and TypeInstance are not valid for "
226 "instance data '%s'.'%s'",
231 if (dd->oids_len > 1) {
234 ": Only one OID should be specified for instance data '%s'.'%s'",
241 ERROR(PLUGIN_NAME ": Type not defined for data '%s'.'%s'", td->name,
249 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
250 data_definition_t *dd = (data_definition_t *)e->value;
253 ERROR(PLUGIN_NAME ": Plugin not defined for '%s'", dd->name);
257 if (dd->oids_len == 0) {
258 ERROR(PLUGIN_NAME ": No OIDs defined for '%s'", dd->name);
262 if (dd->is_instance) {
264 ": Instance flag can't be specified for scalar data '%s'",
270 ERROR(PLUGIN_NAME ": Type not defined for data '%s'", dd->name);
278 static int snmp_agent_table_row_remove(table_definition_t *td,
279 const char *instance) {
283 if ((c_avl_get(td->instance_index, instance, (void**) &index) != 0) ||
284 (c_avl_get(td->index_instance, index, (void**) &ins) != 0))
287 pthread_mutex_lock(&g_agent->agentx_lock);
289 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
290 data_definition_t *dd = (data_definition_t *)de->value;
292 for (int i = 0; i < dd->oids_len; i++)
293 snmp_agent_unregister_oid_index(&dd->oids[i], *index);
296 snmp_agent_unregister_oid_index(&td->index_oid, *index);
298 pthread_mutex_unlock(&g_agent->agentx_lock);
300 DEBUG(PLUGIN_NAME ": Removed row for '%s' table [%d, %s].", td->name, *index,
303 notification_t n = {NOTIF_WARNING, cdtime(), "", "", PLUGIN_NAME, "", "", "",
305 sstrncpy(n.host, hostname_g, sizeof(n.host));
306 sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance));
307 ssnprintf(n.message, sizeof(n.message),
308 "Removed data row from table %s instance %s index %d", td->name,
310 plugin_dispatch_notification(&n);
312 c_avl_remove(td->index_instance, index, NULL, (void**) &ins);
313 c_avl_remove(td->instance_index, instance, NULL, (void**) &index);
320 static int snmp_agent_clear_missing(const value_list_t *vl,
321 __attribute__((unused)) user_data_t *ud) {
325 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
326 table_definition_t *td = (table_definition_t *)te->value;
328 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
329 data_definition_t *dd = (data_definition_t *)de->value;
331 if (!dd->is_instance) {
332 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
333 vl->type_instance)) {
334 snmp_agent_table_row_remove(td, vl->plugin_instance);
344 static void snmp_agent_free_data(data_definition_t **dd) {
346 if (dd == NULL || *dd == NULL)
349 /* unregister scalar type OID */
350 if ((*dd)->table == NULL) {
351 for (int i = 0; i < (*dd)->oids_len; i++)
352 unregister_mib((*dd)->oids[i].oid, (*dd)->oids[i].oid_len);
354 /* unregister all table OIDs */
358 c_avl_iterator_t *iter = c_avl_get_iterator((*dd)->table->index_instance);
359 while (c_avl_iterator_next(iter, (void *)&index, (void *)&value) == 0) {
360 for (int i = 0; i < (*dd)->oids_len; i++)
361 snmp_agent_unregister_oid_index(&(*dd)->oids[i], *index);
363 c_avl_iterator_destroy(iter);
367 sfree((*dd)->plugin);
368 sfree((*dd)->plugin_instance);
370 sfree((*dd)->type_instance);
378 static void snmp_agent_free_table(table_definition_t **td) {
380 if (td == NULL || *td == NULL)
383 if ((*td)->size_oid.oid_len)
384 unregister_mib((*td)->size_oid.oid, (*td)->size_oid.oid_len);
386 if ((*td)->index_oid.oid_len) {
390 c_avl_iterator_t *iter = c_avl_get_iterator((*td)->index_instance);
391 while (c_avl_iterator_next(iter, (void *)&index, (void *)&value) == 0)
392 snmp_agent_unregister_oid_index(&(*td)->index_oid, *index);
394 c_avl_iterator_destroy(iter);
397 for (llentry_t *de = llist_head((*td)->columns); de != NULL; de = de->next) {
398 data_definition_t *dd = (data_definition_t *)de->value;
399 snmp_agent_free_data(&dd);
402 llist_destroy((*td)->columns);
407 c_avl_destroy((*td)->index_instance);
408 (*td)->index_instance = NULL;
410 while (c_avl_pick((*td)->instance_index, &key, &value) == 0) {
414 c_avl_destroy((*td)->instance_index);
415 (*td)->instance_index = NULL;
424 snmp_agent_table_oid_handler(struct netsnmp_mib_handler_s *handler,
425 struct netsnmp_handler_registration_s *reginfo,
426 struct netsnmp_agent_request_info_s *reqinfo,
427 struct netsnmp_request_info_s *requests) {
429 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
430 DEBUG(PLUGIN_NAME ": Not supported request mode (%d).", reqinfo->mode);
431 return SNMP_ERR_NOERROR;
435 char temp[DATA_MAX_NAME_LEN];
437 pthread_mutex_lock(&g_agent->lock);
439 memcpy(oid.oid, requests->requestvb->name,
440 sizeof(oid.oid[0]) * requests->requestvb->name_length);
441 oid.oid_len = requests->requestvb->name_length;
443 snmp_agent_oid_to_string(temp, sizeof(temp), &oid);
445 DEBUG(PLUGIN_NAME ": Get request received for table OID '%s'.", temp);
447 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
448 table_definition_t *td = (table_definition_t *)te->value;
450 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
451 data_definition_t *dd = (data_definition_t *)de->value;
453 for (int i = 0; i < dd->oids_len; i++) {
455 int ret = snmp_oid_ncompare(oid.oid, oid.oid_len,
456 dd->oids[i].oid, dd->oids[i].oid_len,
457 MIN(oid.oid_len, dd->oids[i].oid_len));
458 if (ret != 0) continue;
460 if (td->index_oid.oid_len) {
461 int index = oid.oid[oid.oid_len - 1];
464 ret = c_avl_get(td->index_instance, &index, (void **)&instance);
466 DEBUG(PLUGIN_NAME ": Nonexisting index '%d' requested.", index);
467 pthread_mutex_unlock(&g_agent->lock);
468 return SNMP_NOSUCHINSTANCE;
471 if (dd->is_instance) {
473 requests->requestvb->type = ASN_OCTET_STR;
474 snmp_set_var_typed_value(requests->requestvb,
475 requests->requestvb->type, instance,
478 pthread_mutex_unlock(&g_agent->lock);
480 return SNMP_ERR_NOERROR;
483 format_name(temp, sizeof(temp), hostname_g, dd->plugin, instance,
484 dd->type, dd->type_instance);
485 DEBUG(PLUGIN_NAME ": Identifier '%s'", temp);
489 const data_set_t *ds;
491 ds = plugin_get_ds(dd->type);
493 DEBUG(PLUGIN_NAME ": Data set not found for '%s' type.", dd->type);
494 pthread_mutex_unlock(&g_agent->lock);
495 return SNMP_NOSUCHINSTANCE;
498 ret = uc_get_value_by_name(temp, &values, &values_num);
501 ERROR(PLUGIN_NAME ": Failed to get value for '%s'.", temp);
502 pthread_mutex_unlock(&g_agent->lock);
503 return SNMP_NOSUCHINSTANCE;
506 assert(ds->ds_num == values_num);
507 assert(i < values_num);
509 char data[DATA_MAX_NAME_LEN];
510 size_t data_len = sizeof(data);
511 ret = snmp_agent_set_vardata(data, &data_len, dd->oids[i].type,
512 dd->scale, dd->shift, &values[i],
513 sizeof(values[i]), ds->ds[i].type);
519 ERROR(PLUGIN_NAME ": Failed to convert '%s' value to snmp data.",
521 pthread_mutex_unlock(&g_agent->lock);
522 return SNMP_NOSUCHINSTANCE;
525 requests->requestvb->type = dd->oids[i].type;
526 snmp_set_var_typed_value(requests->requestvb,
527 requests->requestvb->type, data, data_len);
529 pthread_mutex_unlock(&g_agent->lock);
531 return SNMP_ERR_NOERROR;
534 DEBUG(PLUGIN_NAME ": %s:%d NOT IMPLEMENTED.", __FUNCTION__, __LINE__);
540 pthread_mutex_unlock(&g_agent->lock);
542 return SNMP_NOSUCHINSTANCE;
546 snmp_agent_table_index_oid_handler(struct netsnmp_mib_handler_s *handler,
547 struct netsnmp_handler_registration_s *reginfo,
548 struct netsnmp_agent_request_info_s *reqinfo,
549 struct netsnmp_request_info_s *requests) {
551 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
552 DEBUG(PLUGIN_NAME ": Not supported request mode (%d).", reqinfo->mode);
553 return SNMP_ERR_NOERROR;
558 pthread_mutex_lock(&g_agent->lock);
560 memcpy(oid.oid, requests->requestvb->name,
561 sizeof(oid.oid[0]) * requests->requestvb->name_length);
562 oid.oid_len = requests->requestvb->name_length;
564 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
565 table_definition_t *td = (table_definition_t *)te->value;
567 if (td->index_oid.oid_len &&
568 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->index_oid.oid,
569 td->index_oid.oid_len,
570 MIN(oid.oid_len, td->index_oid.oid_len)) == 0)) {
572 DEBUG(PLUGIN_NAME ": Handle '%s' table index OID.", td->name);
574 int index = oid.oid[oid.oid_len - 1];
577 int ret = c_avl_get(td->index_instance, &index, (void **)&instance);
579 /* nonexisting index requested */
580 pthread_mutex_unlock(&g_agent->lock);
581 return SNMP_NOSUCHINSTANCE;
584 requests->requestvb->type = ASN_INTEGER;
585 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
586 &index, sizeof(index));
588 pthread_mutex_unlock(&g_agent->lock);
590 return SNMP_ERR_NOERROR;
594 pthread_mutex_unlock(&g_agent->lock);
596 return SNMP_NOSUCHINSTANCE;
600 snmp_agent_table_size_oid_handler(struct netsnmp_mib_handler_s *handler,
601 struct netsnmp_handler_registration_s *reginfo,
602 struct netsnmp_agent_request_info_s *reqinfo,
603 struct netsnmp_request_info_s *requests) {
605 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
606 DEBUG(PLUGIN_NAME ": Not supported request mode (%d).", reqinfo->mode);
607 return SNMP_ERR_NOERROR;
612 pthread_mutex_lock(&g_agent->lock);
614 memcpy(oid.oid, requests->requestvb->name,
615 sizeof(oid.oid[0]) * requests->requestvb->name_length);
616 oid.oid_len = requests->requestvb->name_length;
618 DEBUG(PLUGIN_NAME ": Get request received for table size OID.");
620 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
621 table_definition_t *td = (table_definition_t *)te->value;
623 if (td->size_oid.oid_len &&
624 (snmp_oid_ncompare(oid.oid, oid.oid_len, td->size_oid.oid,
625 td->size_oid.oid_len,
626 MIN(oid.oid_len, td->size_oid.oid_len)) == 0)) {
627 DEBUG(PLUGIN_NAME ": Handle '%s' table size OID.", td->name);
629 long size = c_avl_size(td->index_instance);
631 requests->requestvb->type = td->size_oid.type;
632 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
633 &size, sizeof(size));
635 pthread_mutex_unlock(&g_agent->lock);
637 return SNMP_ERR_NOERROR;
641 pthread_mutex_unlock(&g_agent->lock);
643 return SNMP_NOSUCHINSTANCE;
648 snmp_agent_scalar_oid_handler(struct netsnmp_mib_handler_s *handler,
649 struct netsnmp_handler_registration_s *reginfo,
650 struct netsnmp_agent_request_info_s *reqinfo,
651 struct netsnmp_request_info_s *requests) {
653 if (reqinfo->mode != MODE_GET && reqinfo->mode != MODE_GETNEXT) {
654 DEBUG(PLUGIN_NAME ": Not supported request mode (%d).", reqinfo->mode);
655 return SNMP_ERR_NOERROR;
660 char temp[DATA_MAX_NAME_LEN];
662 pthread_mutex_lock(&g_agent->lock);
664 memcpy(oid.oid, requests->requestvb->name,
665 sizeof(oid.oid[0]) * requests->requestvb->name_length);
666 oid.oid_len = requests->requestvb->name_length;
668 snmp_agent_oid_to_string(temp, sizeof(temp), &oid);
670 DEBUG(PLUGIN_NAME ": Get request received for scalar OID '%s'.", temp);
672 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL; de = de->next) {
673 data_definition_t *dd = (data_definition_t *)de->value;
675 for (int i = 0; i < dd->oids_len; i++) {
677 ret = snmp_oid_compare(oid.oid, oid.oid_len,
678 dd->oids[i].oid, dd->oids[i].oid_len);
679 if (ret != 0) continue;
681 format_name(temp, sizeof(temp), hostname_g, dd->plugin,
682 dd->plugin_instance, dd->type, dd->type_instance);
683 DEBUG(PLUGIN_NAME ": Identifier '%s'", temp);
687 const data_set_t *ds;
689 ds = plugin_get_ds(dd->type);
691 DEBUG(PLUGIN_NAME ": Data set not found for '%s' type", dd->type);
692 pthread_mutex_unlock(&g_agent->lock);
693 return SNMP_NOSUCHINSTANCE;
696 ret = uc_get_value_by_name(temp, &values, &values_num);
699 ERROR(PLUGIN_NAME ": Failed to get value for '%s'.", temp);
700 pthread_mutex_unlock(&g_agent->lock);
701 return SNMP_NOSUCHINSTANCE;
704 assert(ds->ds_num == values_num);
705 assert(i < values_num);
707 char data[DATA_MAX_NAME_LEN];
708 size_t data_len = sizeof(data);
709 ret = snmp_agent_set_vardata(data, &data_len, dd->oids[i].type,
710 dd->scale, dd->shift, &values[i],
711 sizeof(values[i]), ds->ds[i].type);
717 ERROR(PLUGIN_NAME ": Failed to convert '%s' value to snmp data.", temp);
718 pthread_mutex_unlock(&g_agent->lock);
719 return SNMP_NOSUCHINSTANCE;
722 requests->requestvb->type = dd->oids[i].type;
723 snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
726 pthread_mutex_unlock(&g_agent->lock);
728 return SNMP_ERR_NOERROR;
732 pthread_mutex_unlock(&g_agent->lock);
734 return SNMP_NOSUCHINSTANCE;
737 static int snmp_agent_register_table_oids(void) {
739 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
740 table_definition_t *td = (table_definition_t *)te->value;
742 if (td->size_oid.oid_len != 0) {
744 snmp_agent_get_asn_type(td->size_oid.oid, td->size_oid.oid_len);
745 td->size_oid.oid_len++;
746 int ret = snmp_agent_register_oid(&td->size_oid,
747 snmp_agent_table_size_oid_handler);
752 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
753 data_definition_t *dd = (data_definition_t *)de->value;
755 for (int i = 0; i < dd->oids_len; i++) {
757 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
765 static int snmp_agent_register_scalar_oids(void) {
767 for (llentry_t *e = llist_head(g_agent->scalars); e != NULL; e = e->next) {
768 data_definition_t *dd = (data_definition_t *)e->value;
770 for (int i = 0; i < dd->oids_len; i++) {
773 snmp_agent_get_asn_type(dd->oids[i].oid, dd->oids[i].oid_len);
776 snmp_agent_register_oid(&dd->oids[i], snmp_agent_scalar_oid_handler);
785 static int snmp_agent_config_data_oids(data_definition_t *dd,
786 oconfig_item_t *ci) {
787 if (ci->values_num < 1) {
788 WARNING(PLUGIN_NAME ": `OIDs' needs at least one argument.");
792 for (int i = 0; i < ci->values_num; i++)
793 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
794 WARNING(PLUGIN_NAME ": `OIDs' needs only string argument.");
798 if (dd->oids != NULL)
801 dd->oids = calloc(ci->values_num, sizeof(*dd->oids));
802 if (dd->oids == NULL)
804 dd->oids_len = (size_t)ci->values_num;
806 for (int i = 0; i < ci->values_num; i++) {
807 dd->oids[i].oid_len = MAX_OID_LEN;
809 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->oids[i].oid,
810 &dd->oids[i].oid_len)) {
811 ERROR(PLUGIN_NAME ": snmp_parse_oid (%s) failed.",
812 ci->values[i].value.string);
822 static int snmp_agent_config_table_size_oid(table_definition_t *td,
823 oconfig_item_t *ci) {
824 if (ci->values_num < 1) {
825 WARNING(PLUGIN_NAME ": `TableSizeOID' is empty.");
829 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
830 WARNING(PLUGIN_NAME ": `TableSizeOID' needs only string argument.");
834 td->size_oid.oid_len = MAX_OID_LEN;
836 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->size_oid.oid,
837 &td->size_oid.oid_len)) {
838 ERROR(PLUGIN_NAME ": Failed to parse table size OID (%s)",
839 ci->values[0].value.string);
840 td->size_oid.oid_len = 0;
847 static int snmp_agent_config_table_index_oid(table_definition_t *td,
848 oconfig_item_t *ci) {
850 if (ci->values_num < 1) {
851 WARNING(PLUGIN_NAME ": `IndexOID' is empty.");
855 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
856 WARNING(PLUGIN_NAME ": `IndexOID' needs only string argument.");
860 td->index_oid.oid_len = MAX_OID_LEN;
862 if (NULL == snmp_parse_oid(ci->values[0].value.string, td->index_oid.oid,
863 &td->index_oid.oid_len)) {
864 ERROR(PLUGIN_NAME ": Failed to parse table index OID (%s)",
865 ci->values[0].value.string);
866 td->index_oid.oid_len = 0;
873 static int snmp_agent_config_table_data(table_definition_t *td,
874 oconfig_item_t *ci) {
875 data_definition_t *dd;
880 dd = calloc(1, sizeof(*dd));
882 ERROR(PLUGIN_NAME ": Failed to allocate memory for table data definition");
886 ret = cf_util_get_string(ci, &dd->name);
897 for (int i = 0; i < ci->children_num; i++) {
898 oconfig_item_t *option = ci->children + i;
900 if (strcasecmp("Instance", option->key) == 0)
901 ret = cf_util_get_boolean(option, &dd->is_instance);
902 else if (strcasecmp("Plugin", option->key) == 0)
903 ret = cf_util_get_string(option, &dd->plugin);
904 else if (strcasecmp("PluginInstance", option->key) == 0)
905 ret = cf_util_get_string(option, &dd->plugin_instance);
906 else if (strcasecmp("Type", option->key) == 0)
907 ret = cf_util_get_string(option, &dd->type);
908 else if (strcasecmp("TypeInstance", option->key) == 0)
909 ret = cf_util_get_string(option, &dd->type_instance);
910 else if (strcasecmp("Shift", option->key) == 0)
911 ret = cf_util_get_double(option, &dd->shift);
912 else if (strcasecmp("Scale", option->key) == 0)
913 ret = cf_util_get_double(option, &dd->scale);
914 else if (strcasecmp("OIDs", option->key) == 0)
915 ret = snmp_agent_config_data_oids(dd, option);
917 WARNING(PLUGIN_NAME ": Option `%s' not allowed here.", option->key);
922 snmp_agent_free_data(&dd);
927 llentry_t *entry = llentry_create(dd->name, dd);
929 snmp_agent_free_data(&dd);
933 llist_append(td->columns, entry);
938 static int snmp_agent_config_data(oconfig_item_t *ci) {
939 data_definition_t *dd;
944 dd = calloc(1, sizeof(*dd));
946 ERROR(PLUGIN_NAME ": Failed to allocate memory for data definition");
950 ret = cf_util_get_string(ci, &dd->name);
959 for (int i = 0; i < ci->children_num; i++) {
960 oconfig_item_t *option = ci->children + i;
962 if (strcasecmp("Instance", option->key) == 0)
963 ret = cf_util_get_boolean(option, &dd->is_instance);
964 else if (strcasecmp("Plugin", option->key) == 0)
965 ret = cf_util_get_string(option, &dd->plugin);
966 else if (strcasecmp("PluginInstance", option->key) == 0)
967 ret = cf_util_get_string(option, &dd->plugin_instance);
968 else if (strcasecmp("Type", option->key) == 0)
969 ret = cf_util_get_string(option, &dd->type);
970 else if (strcasecmp("TypeInstance", option->key) == 0)
971 ret = cf_util_get_string(option, &dd->type_instance);
972 else if (strcasecmp("Shift", option->key) == 0)
973 ret = cf_util_get_double(option, &dd->shift);
974 else if (strcasecmp("Scale", option->key) == 0)
975 ret = cf_util_get_double(option, &dd->scale);
976 else if (strcasecmp("OIDs", option->key) == 0)
977 ret = snmp_agent_config_data_oids(dd, option);
979 WARNING(PLUGIN_NAME ": Option `%s' not allowed here.", option->key);
984 snmp_agent_free_data(&dd);
989 llentry_t *entry = llentry_create(dd->name, dd);
991 snmp_agent_free_data(&dd);
995 llist_append(g_agent->scalars, entry);
1000 static int num_compare(const int *a, const int *b) {
1001 assert((a != NULL) && (b != NULL));
1010 static int snmp_agent_config_table(oconfig_item_t *ci) {
1011 table_definition_t *td;
1016 td = calloc(1, sizeof(*td));
1018 ERROR(PLUGIN_NAME ": Failed to allocate memory for table definition.");
1022 ret = cf_util_get_string(ci, &td->name);
1028 td->columns = llist_create();
1029 if (td->columns == NULL) {
1030 ERROR(PLUGIN_NAME ": Failed to allocate memory for columns list.");
1031 snmp_agent_free_table(&td);
1035 for (int i = 0; i < ci->children_num; i++) {
1036 oconfig_item_t *option = ci->children + i;
1038 if (strcasecmp("IndexOID", option->key) == 0)
1039 ret = snmp_agent_config_table_index_oid(td, option);
1040 else if (strcasecmp("SizeOID", option->key) == 0)
1041 ret = snmp_agent_config_table_size_oid(td, option);
1042 else if (strcasecmp("Data", option->key) == 0)
1043 ret = snmp_agent_config_table_data(td, option);
1045 WARNING(PLUGIN_NAME ": Option `%s' not allowed here.", option->key);
1050 snmp_agent_free_table(&td);
1055 llentry_t *entry = llentry_create(td->name, td);
1056 if (entry == NULL) {
1057 snmp_agent_free_table(&td);
1061 td->instance_index =
1062 c_avl_create((int (*)(const void *, const void *))strcmp);
1063 if (td->instance_index == NULL) {
1064 snmp_agent_free_table(&td);
1068 td->index_instance =
1069 c_avl_create((int (*)(const void *, const void *))num_compare);
1070 if (td->index_instance == NULL) {
1071 snmp_agent_free_table(&td);
1075 llist_append(g_agent->tables, entry);
1080 static int snmp_agent_get_value_from_ds_type(const value_t *val, int type,
1081 double scale, double shift,
1084 case DS_TYPE_COUNTER:
1085 *value = (long)((val->counter * scale) + shift);
1087 case DS_TYPE_ABSOLUTE:
1088 *value = (long)((val->absolute * scale) + shift);
1090 case DS_TYPE_DERIVE:
1091 *value = (long)((val->derive * scale) + shift);
1094 *value = (long)((val->gauge * scale) + shift);
1099 ERROR(PLUGIN_NAME ": Unknown data source type: %i.", type);
1106 static int snmp_agent_set_vardata(void *data, size_t *data_len, u_char asn_type,
1107 double scale, double shift, const void *value,
1108 size_t len, int type) {
1111 netsnmp_vardata var;
1116 var.string = (u_char *)data;
1118 ret = snmp_agent_get_value_from_ds_type(val, type, scale, shift, &new_value);
1128 if (*data_len < sizeof(*var.integer))
1130 *var.integer = new_value;
1131 *data_len = sizeof(*var.integer);
1134 if (*data_len < sizeof(*var.counter64))
1136 var.counter64->high = (u_long)((int64_t)new_value >> 32);
1137 var.counter64->low = (u_long)((int64_t)new_value & 0xFFFFFFFF);
1138 *data_len = sizeof(*var.counter64);
1141 if (type == DS_TYPE_GAUGE) {
1142 char buf[DATA_MAX_NAME_LEN];
1143 snprintf(buf, sizeof(buf), "%.2f", val->gauge);
1144 if (*data_len < strlen(buf))
1146 *data_len = strlen(buf);
1147 memcpy(var.string, buf, *data_len);
1149 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type.", type,
1155 ERROR(PLUGIN_NAME ": Failed to convert %d ds type to %d asn type.", type,
1163 static int snmp_agent_register_oid_index(oid_t *oid, int index,
1164 Netsnmp_Node_Handler *handler) {
1166 memcpy(&new_oid, oid, sizeof(*oid));
1167 new_oid.oid[new_oid.oid_len++] = index;
1168 return snmp_agent_register_oid(&new_oid, handler);
1171 static int snmp_agent_unregister_oid_index(oid_t *oid, int index) {
1173 memcpy(&new_oid, oid, sizeof(*oid));
1174 new_oid.oid[new_oid.oid_len++] = index;
1175 return unregister_mib(new_oid.oid, new_oid.oid_len);
1178 static int snmp_agent_update_index(table_definition_t *td,
1179 const char *instance) {
1181 if (c_avl_get(td->instance_index, instance, NULL) == 0)
1188 ins = strdup(instance);
1192 index = calloc(1, sizeof(*index));
1193 if (index == NULL) {
1198 *index = c_avl_size(td->instance_index) + 1;
1200 ret = c_avl_insert(td->instance_index, ins, index);
1207 ret = c_avl_insert(td->index_instance, index, ins);
1209 DEBUG(PLUGIN_NAME ": Failed to update index_instance for '%s' table.",
1214 DEBUG(PLUGIN_NAME ": Updated index for '%s' table [%d, %s].", td->name,
1217 /* need to generate index for the table */
1218 if (td->index_oid.oid_len) {
1220 ret = snmp_agent_register_oid_index(&td->index_oid, *index,
1221 snmp_agent_table_index_oid_handler);
1225 /* register new oids for all columns */
1226 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1227 data_definition_t *dd = (data_definition_t *)de->value;
1229 for (int i = 0; i < dd->oids_len; i++) {
1230 ret = snmp_agent_register_oid_index(&dd->oids[i], *index,
1231 snmp_agent_table_oid_handler);
1238 notification_t n = {NOTIF_OKAY, cdtime(), "", "", PLUGIN_NAME, "", "", "",
1240 sstrncpy(n.host, hostname_g, sizeof(n.host));
1241 sstrncpy(n.plugin_instance, ins, sizeof(n.plugin_instance));
1242 ssnprintf(n.message, sizeof(n.message),
1243 "Data row added to table %s instance %s index %d", td->name, ins,
1245 plugin_dispatch_notification(&n);
1250 static int snmp_agent_write(value_list_t const *vl) {
1255 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next) {
1256 table_definition_t *td = (table_definition_t *)te->value;
1258 for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
1259 data_definition_t *dd = (data_definition_t *)de->value;
1261 if (!dd->is_instance) {
1262 if (CHECK_DD_TYPE(dd, vl->plugin, vl->plugin_instance, vl->type,
1263 vl->type_instance)) {
1264 snmp_agent_update_index(td, vl->plugin_instance);
1274 static int snmp_agent_collect(const data_set_t *ds, const value_list_t *vl,
1275 user_data_t __attribute__((unused)) * user_data) {
1277 pthread_mutex_lock(&g_agent->lock);
1279 snmp_agent_write(vl);
1281 pthread_mutex_unlock(&g_agent->lock);
1286 static int snmp_agent_preinit(void) {
1287 if (g_agent != NULL) {
1288 /* already initialized if config callback was called before init callback */
1292 g_agent = calloc(1, sizeof(*g_agent));
1293 if (g_agent == NULL) {
1294 ERROR(PLUGIN_NAME ": Failed to allocate memory for snmp agent context.");
1298 g_agent->tables = llist_create();
1299 g_agent->scalars = llist_create();
1302 /* make us a agentx client. */
1303 err = netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE,
1306 ERROR(PLUGIN_NAME ": Failed to set agent role (%d)", err);
1311 * For SNMP debug purposes uses snmp_set_do_debugging(1);
1314 /* initialize the agent library */
1315 err = init_agent(PLUGIN_NAME);
1317 ERROR(PLUGIN_NAME ": Failed to initialize the agent library (%d)", err);
1321 init_snmp(PLUGIN_NAME);
1323 g_agent->tp = read_all_mibs();
1328 static int snmp_agent_init(void) {
1331 ret = snmp_agent_preinit();
1335 ret = snmp_agent_register_scalar_oids();
1339 ret = snmp_agent_register_table_oids();
1343 /* create a second thread to listen for requests from AgentX*/
1344 ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
1346 ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
1350 ret = pthread_mutex_init(&g_agent->lock, NULL);
1352 ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret);
1356 ret = pthread_mutex_init(&g_agent->agentx_lock, NULL);
1358 ERROR(PLUGIN_NAME ": Failed to initialize AgentX mutex, err %u", ret);
1365 static void *snmp_agent_thread_run(void __attribute__((unused)) * arg) {
1366 INFO(PLUGIN_NAME ": Thread is up and running");
1369 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1371 pthread_mutex_lock(&g_agent->agentx_lock);
1372 agent_check_and_process(0); /* 0 == don't block */
1373 pthread_mutex_unlock(&g_agent->agentx_lock);
1375 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
1382 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler) {
1383 netsnmp_handler_registration *reg;
1384 char temp[DATA_MAX_NAME_LEN];
1385 char *oid_name = snmp_agent_get_oid_name(oid->oid, oid->oid_len - 1);
1387 snmp_agent_oid_to_string(temp, sizeof(temp), oid);
1389 if (oid_name == NULL) {
1391 ": Skipped registration: OID (%s) is not found in main tree", temp);
1395 reg = netsnmp_create_handler_registration(oid_name, handler, oid->oid,
1396 oid->oid_len, HANDLER_CAN_RONLY);
1398 ERROR(PLUGIN_NAME ": Failed to create handler registration for OID (%s)",
1403 pthread_mutex_lock(&g_agent->agentx_lock);
1405 if (netsnmp_register_instance(reg) != MIB_REGISTERED_OK) {
1406 ERROR(PLUGIN_NAME ": Failed to register handler for OID (%s)", temp);
1407 pthread_mutex_unlock(&g_agent->agentx_lock);
1411 pthread_mutex_unlock(&g_agent->agentx_lock);
1413 DEBUG(PLUGIN_NAME ": Registered handler for OID (%s)", temp);
1418 static int snmp_agent_free_config(void) {
1420 if (g_agent == NULL)
1423 for (llentry_t *te = llist_head(g_agent->tables); te != NULL; te = te->next)
1424 snmp_agent_free_table((table_definition_t **)&te->value);
1425 llist_destroy(g_agent->tables);
1427 for (llentry_t *de = llist_head(g_agent->scalars); de != NULL; de = de->next)
1428 snmp_agent_free_data((data_definition_t **)&de->value);
1429 llist_destroy(g_agent->scalars);
1434 static int snmp_agent_shutdown(void) {
1437 DEBUG(PLUGIN_NAME ": snmp_agent_shutdown.");
1439 if (g_agent == NULL) {
1440 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: plugin not initialized.");
1444 if (pthread_cancel(g_agent->thread) != 0)
1445 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to cancel the thread.");
1447 if (pthread_join(g_agent->thread, NULL) != 0)
1448 ERROR(PLUGIN_NAME ": snmp_agent_shutdown: failed to join the thread.");
1450 snmp_agent_free_config();
1452 snmp_shutdown(PLUGIN_NAME);
1454 pthread_mutex_destroy(&g_agent->lock);
1455 pthread_mutex_destroy(&g_agent->agentx_lock);
1462 static int snmp_agent_config(oconfig_item_t *ci) {
1464 int ret = snmp_agent_preinit();
1471 for (int i = 0; i < ci->children_num; i++) {
1472 oconfig_item_t *child = ci->children + i;
1473 if (strcasecmp("Data", child->key) == 0) {
1474 ret = snmp_agent_config_data(child);
1475 } else if (strcasecmp("Table", child->key) == 0) {
1476 ret = snmp_agent_config_table(child);
1478 ERROR(PLUGIN_NAME ": Unknown configuration option `%s'.", child->key);
1483 ERROR(PLUGIN_NAME ": Failed to parse configuration.");
1484 snmp_agent_free_config();
1485 snmp_shutdown(PLUGIN_NAME);
1491 ret = snmp_agent_validate_data();
1493 ERROR(PLUGIN_NAME ": Invalid configuration provided.");
1494 snmp_agent_free_config();
1495 snmp_shutdown(PLUGIN_NAME);
1503 void module_register(void) {
1504 plugin_register_init(PLUGIN_NAME, snmp_agent_init);
1505 plugin_register_complex_config(PLUGIN_NAME, snmp_agent_config);
1506 plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL);
1507 plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL);
1508 plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown);