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