Merge another change from team/russell/events ...
[asterisk-bristuff.git] / res / res_snmp.c
blob71a79f2096905f88136b83d144adcc1e643642fc
1 /*
2 * Copyright (C) 2006 Voop as
3 * Thorsten Lockert <tholo@voop.as>
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 */
10 /*! \file
12 * \brief SNMP Agent / SubAgent support for Asterisk
14 * \author Thorsten Lockert <tholo@voop.as>
16 * \extref Uses the Net-SNMP libraries available at
17 * http://net-snmp.sourceforge.net/
20 /*** MODULEINFO
21 <depend>netsnmp</depend>
22 ***/
24 #include "asterisk.h"
26 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
28 #include "asterisk/channel.h"
29 #include "asterisk/module.h"
31 #include "snmp/agent.h"
33 #define MODULE_DESCRIPTION "SNMP [Sub]Agent for Asterisk"
35 int res_snmp_agentx_subagent;
36 int res_snmp_dont_stop;
37 int res_snmp_enabled;
39 static pthread_t thread = AST_PTHREADT_NULL;
41 /*!
42 * \brief Load res_snmp.conf config file
43 * \return 1 on load, 0 file does not exist
45 static int load_config(void)
47 struct ast_variable *var;
48 struct ast_config *cfg;
49 struct ast_flags config_flags = { 0 };
50 char *cat;
52 res_snmp_enabled = 0;
53 res_snmp_agentx_subagent = 1;
54 cfg = ast_config_load("res_snmp.conf", config_flags);
55 if (!cfg) {
56 ast_log(LOG_WARNING, "Could not load res_snmp.conf\n");
57 return 0;
59 cat = ast_category_browse(cfg, NULL);
60 while (cat) {
61 var = ast_variable_browse(cfg, cat);
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;
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;
82 var = var->next;
84 } else {
85 ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
86 ast_config_destroy(cfg);
87 return 1;
90 cat = ast_category_browse(cfg, cat);
92 ast_config_destroy(cfg);
93 return 1;
96 static int load_module(void)
98 if(!load_config())
99 return AST_MODULE_LOAD_DECLINE;
101 ast_verb(1, "Loading [Sub]Agent Module\n");
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;
110 static int unload_module(void)
112 ast_verb(1, "Unloading [Sub]Agent Module\n");
114 res_snmp_dont_stop = 0;
115 return ((thread != AST_PTHREADT_NULL) ? pthread_join(thread, NULL) : 0);
118 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "SNMP [Sub]Agent for Asterisk",
119 .load = load_module,
120 .unload = unload_module,