Thu Oct 11 06:47:20 2012

Asterisk developer's documentation


res_snmp.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Voop as
00003  * Thorsten Lockert <tholo@voop.as>
00004  *
00005  * This program is free software, distributed under the terms of
00006  * the GNU General Public License Version 2. See the LICENSE file
00007  * at the top of the source tree.
00008  */
00009 
00010 /*! \file
00011  *
00012  * \brief SNMP Agent / SubAgent support for Asterisk
00013  *
00014  * \author Thorsten Lockert <tholo@voop.as>
00015  *
00016  * \extref Uses the Net-SNMP libraries available at
00017  *  http://net-snmp.sourceforge.net/
00018  */
00019 
00020 /*** MODULEINFO
00021    <depend>netsnmp</depend>
00022  ***/
00023 
00024 #include "asterisk.h"
00025 
00026 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 142992 $")
00027 
00028 #include "asterisk/channel.h"
00029 #include "asterisk/module.h"
00030 
00031 #include "snmp/agent.h"
00032 
00033 #define  MODULE_DESCRIPTION   "SNMP [Sub]Agent for Asterisk"
00034 
00035 int res_snmp_agentx_subagent;
00036 int res_snmp_dont_stop;
00037 int res_snmp_enabled;
00038 
00039 static pthread_t thread = AST_PTHREADT_NULL;
00040 
00041 /*!
00042  * \brief Load res_snmp.conf config file
00043  * \return 1 on load, 0 file does not exist
00044 */
00045 static int load_config(void)
00046 {
00047    struct ast_variable *var;
00048    struct ast_config *cfg;
00049    struct ast_flags config_flags = { 0 };
00050    char *cat;
00051 
00052    res_snmp_enabled = 0;
00053    res_snmp_agentx_subagent = 1;
00054    cfg = ast_config_load("res_snmp.conf", config_flags);
00055    if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
00056       ast_log(LOG_WARNING, "Could not load res_snmp.conf\n");
00057       return 0;
00058    }
00059    cat = ast_category_browse(cfg, NULL);
00060    while (cat) {
00061       var = ast_variable_browse(cfg, cat);
00062 
00063       if (strcasecmp(cat, "general") == 0) {
00064          while (var) {
00065             if (strcasecmp(var->name, "subagent") == 0) {
00066                if (ast_true(var->value))
00067                   res_snmp_agentx_subagent = 1;
00068                else if (ast_false(var->value))
00069                   res_snmp_agentx_subagent = 0;
00070                else {
00071                   ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n", var->value);
00072                   ast_config_destroy(cfg);
00073                   return 1;
00074                }
00075             } else if (strcasecmp(var->name, "enabled") == 0) {
00076                res_snmp_enabled = ast_true(var->value);
00077             } else {
00078                ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);
00079                ast_config_destroy(cfg);
00080                return 1;
00081             }
00082             var = var->next;
00083          }
00084       } else {
00085          ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
00086          ast_config_destroy(cfg);
00087          return 1;
00088       }
00089 
00090       cat = ast_category_browse(cfg, cat);
00091    }
00092    ast_config_destroy(cfg);
00093    return 1;
00094 }
00095 
00096 static int load_module(void)
00097 {
00098    if(!load_config())
00099       return AST_MODULE_LOAD_DECLINE;
00100 
00101    ast_verb(1, "Loading [Sub]Agent Module\n");
00102 
00103    res_snmp_dont_stop = 1;
00104    if (res_snmp_enabled)
00105       return ast_pthread_create_background(&thread, NULL, agent_thread, NULL);
00106    else
00107       return 0;
00108 }
00109 
00110 static int unload_module(void)
00111 {
00112    ast_verb(1, "Unloading [Sub]Agent Module\n");
00113 
00114    res_snmp_dont_stop = 0;
00115    return ((thread != AST_PTHREADT_NULL) ? pthread_join(thread, NULL) : 0);
00116 }
00117 
00118 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "SNMP [Sub]Agent for Asterisk",
00119       .load = load_module,
00120       .unload = unload_module,
00121       );

Generated on Thu Oct 11 06:47:20 2012 for Asterisk - the Open Source PBX by  doxygen 1.5.6