res_fax.c: Add chan locked precondition comments.
[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  * Uses the Net-SNMP libraries available at
17  *       http://net-snmp.sourceforge.net/
18  */
19
20 /*! \li \ref res_snmp.c uses the configuration file \ref res_snmp.conf
21  * \addtogroup configuration_file Configuration Files
22  */
23
24 /*!
25  * \page res_snmp.conf res_snmp.conf
26  * \verbinclude res_snmp.conf.sample
27  */
28
29 /*** MODULEINFO
30         <depend>netsnmp</depend>
31         <support_level>extended</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_REGISTER_FILE()
37
38 #include "asterisk/channel.h"
39 #include "asterisk/module.h"
40
41 #include "snmp/agent.h"
42
43 #define MODULE_DESCRIPTION      "SNMP [Sub]Agent for Asterisk"
44
45 int res_snmp_agentx_subagent;
46 int res_snmp_dont_stop;
47 static int res_snmp_enabled;
48
49 static pthread_t thread = AST_PTHREADT_NULL;
50
51 /*!
52  * \brief Load res_snmp.conf config file
53  * \return 1 on load, 0 file does not exist
54 */
55 static int load_config(void)
56 {
57         struct ast_variable *var;
58         struct ast_config *cfg;
59         struct ast_flags config_flags = { 0 };
60         char *cat;
61
62         res_snmp_enabled = 0;
63         res_snmp_agentx_subagent = 1;
64         cfg = ast_config_load("res_snmp.conf", config_flags);
65         if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
66                 ast_log(LOG_WARNING, "Could not load res_snmp.conf\n");
67                 return 0;
68         }
69         cat = ast_category_browse(cfg, NULL);
70         while (cat) {
71                 var = ast_variable_browse(cfg, cat);
72
73                 if (strcasecmp(cat, "general") == 0) {
74                         while (var) {
75                                 if (strcasecmp(var->name, "subagent") == 0) {
76                                         if (ast_true(var->value))
77                                                 res_snmp_agentx_subagent = 1;
78                                         else if (ast_false(var->value))
79                                                 res_snmp_agentx_subagent = 0;
80                                         else {
81                                                 ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n", var->value);
82                                                 ast_config_destroy(cfg);
83                                                 return 1;
84                                         }
85                                 } else if (strcasecmp(var->name, "enabled") == 0) {
86                                         res_snmp_enabled = ast_true(var->value);
87                                 } else {
88                                         ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);
89                                         ast_config_destroy(cfg);
90                                         return 1;
91                                 }
92                                 var = var->next;
93                         }
94                 } else {
95                         ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
96                         ast_config_destroy(cfg);
97                         return 1;
98                 }
99
100                 cat = ast_category_browse(cfg, cat);
101         }
102         ast_config_destroy(cfg);
103         return 1;
104 }
105
106 /*!
107  * \brief Load the module
108  *
109  * Module loading including tests for configuration or dependencies.
110  * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
111  * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
112  * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the 
113  * configuration file or other non-critical problem return 
114  * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
115  */
116 static int load_module(void)
117 {
118         if(!load_config())
119                 return AST_MODULE_LOAD_DECLINE;
120
121         ast_verb(1, "Loading [Sub]Agent Module\n");
122
123         res_snmp_dont_stop = 1;
124         if (res_snmp_enabled)
125                 return ast_pthread_create_background(&thread, NULL, agent_thread, NULL);
126         else
127                 return 0;
128 }
129
130 static int unload_module(void)
131 {
132         ast_verb(1, "Unloading [Sub]Agent Module\n");
133
134         res_snmp_dont_stop = 0;
135         return ((thread != AST_PTHREADT_NULL) ? pthread_join(thread, NULL) : 0);
136 }
137
138 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "SNMP [Sub]Agent for Asterisk",
139         .support_level = AST_MODULE_SUPPORT_EXTENDED,
140         .load = load_module,
141         .unload = unload_module,
142 );