Correction to commmit 120863, make sure proper destructor function is called as well...
[asterisk-bristuff.git] / res / res_snmp.c
blob6bbf231718935bc0c8f738e881dbb7cc7b54b2c2
1 /*
2 * Copyright (C) 2006 Voop as
3 * Thorsten Lockert <tholo@voop.as>
5 * This program is free software, distributed under the terms of
6 * the GNU General Public License Version 2. See the LICENSE file
7 * at the top of the source tree.
8 */
10 /*! \file
12 * \brief SNMP Agent / SubAgent support for Asterisk
14 * \author Thorsten Lockert <tholo@voop.as>
17 /*** MODULEINFO
18 <depend>netsnmp</depend>
19 ***/
21 #include "asterisk.h"
23 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
25 #include "asterisk/channel.h"
26 #include "asterisk/module.h"
27 #include "asterisk/logger.h"
28 #include "asterisk/options.h"
30 #include "snmp/agent.h"
32 #define MODULE_DESCRIPTION "SNMP [Sub]Agent for Asterisk"
34 int res_snmp_agentx_subagent;
35 int res_snmp_dont_stop;
36 int res_snmp_enabled;
38 static pthread_t thread = AST_PTHREADT_NULL;
40 static int load_config(void)
42 struct ast_variable *var;
43 struct ast_config *cfg;
44 char *cat;
46 res_snmp_enabled = 0;
47 res_snmp_agentx_subagent = 1;
48 cfg = ast_config_load("res_snmp.conf");
49 if (!cfg) {
50 ast_log(LOG_WARNING, "Could not load res_snmp.conf\n");
51 return 0;
53 cat = ast_category_browse(cfg, NULL);
54 while (cat) {
55 var = ast_variable_browse(cfg, cat);
57 if (strcasecmp(cat, "general") == 0) {
58 while (var) {
59 if (strcasecmp(var->name, "subagent") == 0) {
60 if (ast_true(var->value))
61 res_snmp_agentx_subagent = 1;
62 else if (ast_false(var->value))
63 res_snmp_agentx_subagent = 0;
64 else {
65 ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n", var->value);
66 ast_config_destroy(cfg);
67 return 1;
69 } else if (strcasecmp(var->name, "enabled") == 0) {
70 res_snmp_enabled = ast_true(var->value);
71 } else {
72 ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);
73 ast_config_destroy(cfg);
74 return 1;
76 var = var->next;
78 } else {
79 ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
80 ast_config_destroy(cfg);
81 return 1;
84 cat = ast_category_browse(cfg, cat);
86 ast_config_destroy(cfg);
87 return 1;
90 static int load_module(void)
92 if(!load_config())
93 return AST_MODULE_LOAD_DECLINE;
95 ast_verbose(VERBOSE_PREFIX_1 "Loading [Sub]Agent Module\n");
97 res_snmp_dont_stop = 1;
98 if (res_snmp_enabled)
99 return ast_pthread_create_background(&thread, NULL, agent_thread, NULL);
100 else
101 return 0;
104 static int unload_module(void)
106 ast_verbose(VERBOSE_PREFIX_1 "Unloading [Sub]Agent Module\n");
108 res_snmp_dont_stop = 0;
109 return ((thread != AST_PTHREADT_NULL) ? pthread_join(thread, NULL) : 0);
112 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "SNMP [Sub]Agent for Asterisk",
113 .load = load_module,
114 .unload = unload_module,