Asterisk - The Open Source Telephony Project GIT-master-2de1a68
res_snmp.c
Go to the documentation of this file.
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 * Uses the Net-SNMP libraries available at
17 * http://net-snmp.sourceforge.net/
18 */
19
20/*! \li \ref res_snmp.c uses the configuration file \ref res_snmp.conf
21 * \addtogroup configuration_file Configuration Files
22 */
23
24/*!
25 * \page res_snmp.conf res_snmp.conf
26 * \verbinclude res_snmp.conf.sample
27 */
28
29/*** MODULEINFO
30 <depend>netsnmp</depend>
31 <support_level>extended</support_level>
32 ***/
33
34#include "asterisk.h"
35
36#include "asterisk/channel.h"
37#include "asterisk/module.h"
38
39#include "snmp/agent.h"
40
41#define MODULE_DESCRIPTION "SNMP [Sub]Agent for Asterisk"
42
46
47static pthread_t thread = AST_PTHREADT_NULL;
48
49/*!
50 * \brief Load res_snmp.conf config file
51 * \return 1 on load, 0 file does not exist
52*/
53static int load_config(void)
54{
55 struct ast_variable *var;
56 struct ast_config *cfg;
57 struct ast_flags config_flags = { 0 };
58 char *cat;
59
62 cfg = ast_config_load("res_snmp.conf", config_flags);
64 ast_log(LOG_WARNING, "Could not load res_snmp.conf\n");
65 return 0;
66 }
67 cat = ast_category_browse(cfg, NULL);
68 while (cat) {
69 var = ast_variable_browse(cfg, cat);
70
71 if (strcasecmp(cat, "general") == 0) {
72 while (var) {
73 if (strcasecmp(var->name, "subagent") == 0) {
74 if (ast_true(var->value))
76 else if (ast_false(var->value))
78 else {
79 ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n", var->value);
81 return 1;
82 }
83 } else if (strcasecmp(var->name, "enabled") == 0) {
85 } else {
86 ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);
88 return 1;
89 }
90 var = var->next;
91 }
92 } else {
93 ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
95 return 1;
96 }
97
98 cat = ast_category_browse(cfg, cat);
99 }
101 return 1;
102}
103
104/*!
105 * \brief Load the module
106 *
107 * Module loading including tests for configuration or dependencies.
108 * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
109 * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
110 * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
111 * configuration file or other non-critical problem return
112 * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
113 */
114static int load_module(void)
115{
116 if(!load_config())
118
119 ast_verb(1, "Loading [Sub]Agent Module\n");
120
124 else
125 return 0;
126}
127
128static int unload_module(void)
129{
130 ast_verb(1, "Unloading [Sub]Agent Module\n");
131
133 return ((thread != AST_PTHREADT_NULL) ? pthread_join(thread, NULL) : 0);
134}
135
136AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "SNMP [Sub]Agent for Asterisk",
137 .support_level = AST_MODULE_SUPPORT_EXTENDED,
138 .load = load_module,
139 .unload = unload_module,
void * agent_thread(void *arg)
Definition: agent.c:186
SNMP Agent / SubAgent support for Asterisk.
#define var
Definition: ast_expr2f.c:605
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
#define ast_config_load(filename, flags)
Load a config file.
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
Definition: extconf.c:3326
#define CONFIG_STATUS_FILEMISSING
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1215
#define LOG_ERROR
#define ast_verb(level,...)
#define LOG_WARNING
#define AST_PTHREADT_NULL
Definition: lock.h:66
Asterisk module definitions.
@ AST_MODFLAG_DEFAULT
Definition: module.h:315
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODULE_SUPPORT_EXTENDED
Definition: module.h:122
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static pthread_t thread
Definition: res_snmp.c:47
int res_snmp_dont_stop
Definition: res_snmp.c:44
static int load_module(void)
Load the module.
Definition: res_snmp.c:114
int res_snmp_agentx_subagent
Definition: res_snmp.c:43
static int unload_module(void)
Definition: res_snmp.c:128
static int res_snmp_enabled
Definition: res_snmp.c:45
static int load_config(void)
Load res_snmp.conf config file.
Definition: res_snmp.c:53
#define NULL
Definition: resample.c:96
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: utils.c:2199
int attribute_pure ast_false(const char *val)
Make sure something is false. Determine if a string containing a boolean value is "false"....
Definition: utils.c:2216
Structure used to handle boolean flags.
Definition: utils.h:199
Structure for variables, used for configurations and for channel variables.
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:592