Merged revisions 73316 via svnmerge from
[asterisk/asterisk.git] / res / res_snmp.c
1 /*
2  * Copyright (C) 2006 Voop as
3  * Thorsten Lockert <tholo@voop.as>
4  *
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  */
9
10 /*! \file
11  *
12  * \brief SNMP Agent / SubAgent support for Asterisk
13  *
14  * \author Thorsten Lockert <tholo@voop.as>
15  *
16  * \extref Uses the Net-SNMP libraries available at
17  *       http://net-snmp.sourceforge.net/
18  */
19
20 /*** MODULEINFO
21         <depend>netsnmp</depend>
22  ***/
23
24 #include "asterisk.h"
25
26 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
27
28 #include "asterisk/channel.h"
29 #include "asterisk/module.h"
30 #include "asterisk/logger.h"
31 #include "asterisk/options.h"
32
33 #include "snmp/agent.h"
34
35 #define MODULE_DESCRIPTION      "SNMP [Sub]Agent for Asterisk"
36
37 int res_snmp_agentx_subagent;
38 int res_snmp_dont_stop;
39 int res_snmp_enabled;
40
41 static pthread_t thread = AST_PTHREADT_NULL;
42
43 static int load_config(void)
44 {
45         struct ast_variable *var;
46         struct ast_config *cfg;
47         char *cat;
48
49         res_snmp_enabled = 0;
50         res_snmp_agentx_subagent = 1;
51         cfg = ast_config_load("res_snmp.conf");
52         if (!cfg) {
53                 ast_log(LOG_WARNING, "Could not load res_snmp.conf\n");
54                 return 0;
55         }
56         cat = ast_category_browse(cfg, NULL);
57         while (cat) {
58                 var = ast_variable_browse(cfg, cat);
59
60                 if (strcasecmp(cat, "general") == 0) {
61                         while (var) {
62                                 if (strcasecmp(var->name, "subagent") == 0) {
63                                         if (ast_true(var->value))
64                                                 res_snmp_agentx_subagent = 1;
65                                         else if (ast_false(var->value))
66                                                 res_snmp_agentx_subagent = 0;
67                                         else {
68                                                 ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n", var->value);
69                                                 ast_config_destroy(cfg);
70                                                 return 1;
71                                         }
72                                 } else if (strcasecmp(var->name, "enabled") == 0) {
73                                         res_snmp_enabled = ast_true(var->value);
74                                 } else {
75                                         ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);
76                                         ast_config_destroy(cfg);
77                                         return 1;
78                                 }
79                                 var = var->next;
80                         }
81                 } else {
82                         ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
83                         ast_config_destroy(cfg);
84                         return 1;
85                 }
86
87                 cat = ast_category_browse(cfg, cat);
88         }
89         ast_config_destroy(cfg);
90         return 1;
91 }
92
93 static int load_module(void)
94 {
95         if(!load_config())
96                 return AST_MODULE_LOAD_DECLINE;
97
98         ast_verbose(VERBOSE_PREFIX_1 "Loading [Sub]Agent Module\n");
99
100         res_snmp_dont_stop = 1;
101         if (res_snmp_enabled)
102                 return ast_pthread_create_background(&thread, NULL, agent_thread, NULL);
103         else
104                 return 0;
105 }
106
107 static int unload_module(void)
108 {
109         ast_verbose(VERBOSE_PREFIX_1 "Unloading [Sub]Agent Module\n");
110
111         res_snmp_dont_stop = 0;
112         return ((thread != AST_PTHREADT_NULL) ? pthread_join(thread, NULL) : 0);
113 }
114
115 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "SNMP [Sub]Agent for Asterisk",
116                 .load = load_module,
117                 .unload = unload_module,
118                 );