From: Pavel Rochnyack Date: Thu, 13 Jul 2017 11:51:31 +0000 (+0700) Subject: snmp_agent: Changed plugin initialization X-Git-Tag: collectd-5.8.0~120^2~1 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=d534734c9a114b376536f9cda2fb3611e7f4a85e;p=collectd.git snmp_agent: Changed plugin initialization * Do not start working thread if plugin not configured. That allows to avoid useless CPU consumption by polling loop. * Do not register 'write' / 'missing' callbacks if there was no configured. These callbacks do not do anything in this case. * Registration of 'shutdown' callback delayed to 'init' phase. --- diff --git a/src/snmp_agent.c b/src/snmp_agent.c index 456d43e0..6a196858 100644 --- a/src/snmp_agent.c +++ b/src/snmp_agent.c @@ -98,6 +98,7 @@ static snmp_agent_ctx_t *g_agent = NULL; (_dd->type ? !strcmp(_dd->type, _t) : 0) && \ (_dd->type_instance ? !strcmp(_dd->type_instance, _ti) : 1) +static int snmp_agent_shutdown(void); static void *snmp_agent_thread_run(void *arg); static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler); static int snmp_agent_set_vardata(void *dst_buf, size_t *dst_buf_len, @@ -1417,9 +1418,13 @@ static int snmp_agent_preinit(void) { static int snmp_agent_init(void) { int ret; - ret = snmp_agent_preinit(); - if (ret != 0) - return ret; + if (g_agent == NULL || ((llist_head(g_agent->scalars) == NULL) && + (llist_head(g_agent->tables) == NULL))) { + ERROR(PLUGIN_NAME ": snmp_agent_init: plugin not configured"); + return -EINVAL; + } + + plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown); ret = snmp_agent_register_scalar_oids(); if (ret != 0) @@ -1448,6 +1453,11 @@ static int snmp_agent_init(void) { return ret; } + if (llist_head(g_agent->tables) != NULL) { + plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL); + plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL); + } + return 0; } @@ -1593,7 +1603,4 @@ static int snmp_agent_config(oconfig_item_t *ci) { void module_register(void) { plugin_register_init(PLUGIN_NAME, snmp_agent_init); plugin_register_complex_config(PLUGIN_NAME, snmp_agent_config); - plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL); - plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL); - plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown); }