(closes issue #10271)
[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 /*!
44  * \brief Load res_snmp.conf config file
45  * \return 1 on load, 0 file does not exist
46 */
47 static int load_config(void)
48 {
49         struct ast_variable *var;
50         struct ast_config *cfg;
51         char *cat;
52
53         res_snmp_enabled = 0;
54         res_snmp_agentx_subagent = 1;
55         cfg = ast_config_load("res_snmp.conf");
56         if (!cfg) {
57                 ast_log(LOG_WARNING, "Could not load res_snmp.conf\n");
58                 return 0;
59         }
60         cat = ast_category_browse(cfg, NULL);
61         while (cat) {
62                 var = ast_variable_browse(cfg, cat);
63
64                 if (strcasecmp(cat, "general") == 0) {
65                         while (var) {
66                                 if (strcasecmp(var->name, "subagent") == 0) {
67                                         if (ast_true(var->value))
68                                                 res_snmp_agentx_subagent = 1;
69                                         else if (ast_false(var->value))
70                                                 res_snmp_agentx_subagent = 0;
71                                         else {
72                                                 ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n", var->value);
73                                                 ast_config_destroy(cfg);
74                                                 return 1;
75                                         }
76                                 } else if (strcasecmp(var->name, "enabled") == 0) {
77                                         res_snmp_enabled = ast_true(var->value);
78                                 } else {
79                                         ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);
80                                         ast_config_destroy(cfg);
81                                         return 1;
82                                 }
83                                 var = var->next;
84                         }
85                 } else {
86                         ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
87                         ast_config_destroy(cfg);
88                         return 1;
89                 }
90
91                 cat = ast_category_browse(cfg, cat);
92         }
93         ast_config_destroy(cfg);
94         return 1;
95 }
96
97 static int load_module(void)
98 {
99         if(!load_config())
100                 return AST_MODULE_LOAD_DECLINE;
101
102         ast_verbose(VERBOSE_PREFIX_1 "Loading [Sub]Agent Module\n");
103
104         res_snmp_dont_stop = 1;
105         if (res_snmp_enabled)
106                 return ast_pthread_create_background(&thread, NULL, agent_thread, NULL);
107         else
108                 return 0;
109 }
110
111 static int unload_module(void)
112 {
113         ast_verbose(VERBOSE_PREFIX_1 "Unloading [Sub]Agent Module\n");
114
115         res_snmp_dont_stop = 0;
116         return ((thread != AST_PTHREADT_NULL) ? pthread_join(thread, NULL) : 0);
117 }
118
119 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "SNMP [Sub]Agent for Asterisk",
120                 .load = load_module,
121                 .unload = unload_module,
122                 );