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