Merged revisions 29196 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
17 /*** MODULEINFO
18         <defaultenabled>no</defaultenabled>
19  ***/
20
21 #include "asterisk.h"
22
23 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
24
25 #include "asterisk/channel.h"
26 #include "asterisk/module.h"
27 #include "asterisk/logger.h"
28 #include "asterisk/options.h"
29
30 #include "snmp/agent.h"
31
32 #define MODULE_DESCRIPTION      "SNMP [Sub]Agent for Asterisk"
33
34 int res_snmp_agentx_subagent;
35 int res_snmp_dont_stop;
36 int res_snmp_enabled;
37
38 static pthread_t thread = AST_PTHREADT_NULL;
39
40 static int load_config(void)
41 {
42     struct ast_variable *var;
43     struct ast_config *cfg;
44     char *cat;
45         
46         res_snmp_enabled = 0;
47         res_snmp_agentx_subagent = 1;
48     cfg = ast_config_load("res_snmp.conf");
49     if (cfg) {
50                 cat = ast_category_browse(cfg, NULL);
51                 while (cat) {
52                         var = ast_variable_browse(cfg, cat);
53
54                         if (strcasecmp(cat, "general") == 0) {
55                                 while (var) {
56                                         if (strcasecmp(var->name, "subagent") == 0) {
57                                                 if (ast_true(var->value))
58                                                         res_snmp_agentx_subagent = 1;
59                                                 else if (ast_false(var->value))
60                                                         res_snmp_agentx_subagent = 0;
61                                                 else {
62                                                         ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n",
63                                                                         var->value);
64                                                         ast_config_destroy(cfg);
65                                                         return 1;
66                                                 }
67                                         } else if (strcasecmp(var->name, "enabled") == 0) {
68                                                 res_snmp_enabled = ast_true(var->value);
69                                         } else {
70                                                 ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n",
71                                                                 var->name, cat);
72                                                 ast_config_destroy(cfg);
73                                                 return 1;
74                                         }
75                                         var = var->next;
76                                 }
77                         }
78                         else {
79                                 ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
80                                 ast_config_destroy(cfg);
81                                 return 1;
82                         }
83
84                         cat = ast_category_browse(cfg, cat);
85                 }
86                 ast_config_destroy(cfg);
87     }
88
89     return 0;
90 }
91
92 int load_module(void)
93 {
94     load_config();
95
96     ast_verbose(VERBOSE_PREFIX_1 "Loading [Sub]Agent Module\n");
97
98     res_snmp_dont_stop = 1;
99         if (res_snmp_enabled)
100             return ast_pthread_create(&thread, NULL, agent_thread, NULL);
101         else
102                 return 0;
103 }
104
105 int unload_module(void)
106 {
107     ast_verbose(VERBOSE_PREFIX_1 "Unloading [Sub]Agent Module\n");
108
109     res_snmp_dont_stop = 0;
110     return pthread_join(thread, NULL);
111 }
112
113 int reload(void)
114 {
115     ast_verbose(VERBOSE_PREFIX_1 "Reloading [Sub]Agent Module\n");
116
117     res_snmp_dont_stop = 0;
118         if (thread != AST_PTHREADT_NULL)
119             pthread_join(thread, NULL);
120         thread = AST_PTHREADT_NULL;
121     load_config();
122
123     res_snmp_dont_stop = 1;
124         if (res_snmp_enabled)
125             return ast_pthread_create(&thread, NULL, agent_thread, NULL);
126         else
127                 return 0;
128 }
129
130 int usecount(void)
131 {
132     return 0;
133 }
134
135 const char *key(void)
136 {
137     return ASTERISK_GPL_KEY;
138 }
139
140 const char *description(void)
141 {
142     return MODULE_DESCRIPTION;
143 }
144
145 /*
146  * Local Variables:
147  * c-file-style: gnu
148  * c-basic-offset: 4
149  * c-file-offsets: ((case-label . 0))
150  * tab-width: 4
151  * indent-tabs-mode: t
152  * End:
153  */