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