Only produce error message once, don't fill the screen with them...
[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         <depend>netsnmp</depend>
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                 ast_log(LOG_WARNING, "Could not load res_snmp.conf\n");
51                 return 0;
52         }
53         cat = ast_category_browse(cfg, NULL);
54         while (cat) {
55                 var = ast_variable_browse(cfg, cat);
56
57                 if (strcasecmp(cat, "general") == 0) {
58                         while (var) {
59                                 if (strcasecmp(var->name, "subagent") == 0) {
60                                         if (ast_true(var->value))
61                                                 res_snmp_agentx_subagent = 1;
62                                         else if (ast_false(var->value))
63                                                 res_snmp_agentx_subagent = 0;
64                                         else {
65                                                 ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n", var->value);
66                                                 ast_config_destroy(cfg);
67                                                 return 1;
68                                         }
69                                 } else if (strcasecmp(var->name, "enabled") == 0) {
70                                         res_snmp_enabled = ast_true(var->value);
71                                 } else {
72                                         ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);
73                                         ast_config_destroy(cfg);
74                                         return 1;
75                                 }
76                                 var = var->next;
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         return 1;
88 }
89
90 static int load_module(void)
91 {
92         if(!load_config())
93                 return AST_MODULE_LOAD_DECLINE;
94
95         ast_verbose(VERBOSE_PREFIX_1 "Loading [Sub]Agent Module\n");
96
97         res_snmp_dont_stop = 1;
98         if (res_snmp_enabled)
99                 return ast_pthread_create_background(&thread, NULL, agent_thread, NULL);
100         else
101                 return 0;
102 }
103
104 static int unload_module(void)
105 {
106         ast_verbose(VERBOSE_PREFIX_1 "Unloading [Sub]Agent Module\n");
107
108         res_snmp_dont_stop = 0;
109         return pthread_join(thread, NULL);
110 }
111
112 static int reload(void)
113 {
114         ast_verbose(VERBOSE_PREFIX_1 "Reloading [Sub]Agent Module\n");
115
116         res_snmp_dont_stop = 0;
117         if (thread != AST_PTHREADT_NULL)
118                 pthread_join(thread, NULL);
119         thread = AST_PTHREADT_NULL;
120         load_config();
121
122         res_snmp_dont_stop = 1;
123         if (res_snmp_enabled)
124                 return ast_pthread_create_background(&thread, NULL, agent_thread, NULL);
125         else
126                 return 0;
127 }
128
129 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "SNMP [Sub]Agent for Asterisk",
130                 .load = load_module,
131                 .unload = unload_module,
132                 .reload = reload,
133                 );