Wed Jan 8 2020 09:49:50

Asterisk developer's documentation


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  * \extref Uses the Net-SNMP libraries available at
17  * http://net-snmp.sourceforge.net/
18  */
19 
20 /*** MODULEINFO
21  <depend>netsnmp</depend>
22  <support_level>extended</support_level>
23  ***/
24 
25 #include "asterisk.h"
26 
27 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
28 
29 #include "asterisk/channel.h"
30 #include "asterisk/module.h"
31 
32 #include "snmp/agent.h"
33 
34 #define MODULE_DESCRIPTION "SNMP [Sub]Agent for Asterisk"
35 
38 static int res_snmp_enabled;
39 
40 static pthread_t thread = AST_PTHREADT_NULL;
41 
42 /*!
43  * \brief Load res_snmp.conf config file
44  * \return 1 on load, 0 file does not exist
45 */
46 static int load_config(void)
47 {
48  struct ast_variable *var;
49  struct ast_config *cfg;
50  struct ast_flags config_flags = { 0 };
51  char *cat;
52 
53  res_snmp_enabled = 0;
54  res_snmp_agentx_subagent = 1;
55  cfg = ast_config_load("res_snmp.conf", config_flags);
57  ast_log(LOG_WARNING, "Could not load res_snmp.conf\n");
58  return 0;
59  }
60  cat = ast_category_browse(cfg, NULL);
61  while (cat) {
62  var = ast_variable_browse(cfg, cat);
63 
64  if (strcasecmp(cat, "general") == 0) {
65  while (var) {
66  if (strcasecmp(var->name, "subagent") == 0) {
67  if (ast_true(var->value))
68  res_snmp_agentx_subagent = 1;
69  else if (ast_false(var->value))
70  res_snmp_agentx_subagent = 0;
71  else {
72  ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n", var->value);
73  ast_config_destroy(cfg);
74  return 1;
75  }
76  } else if (strcasecmp(var->name, "enabled") == 0) {
77  res_snmp_enabled = ast_true(var->value);
78  } else {
79  ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);
80  ast_config_destroy(cfg);
81  return 1;
82  }
83  var = var->next;
84  }
85  } else {
86  ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
87  ast_config_destroy(cfg);
88  return 1;
89  }
90 
91  cat = ast_category_browse(cfg, cat);
92  }
93  ast_config_destroy(cfg);
94  return 1;
95 }
96 
97 static int load_module(void)
98 {
99  if(!load_config())
101 
102  ast_verb(1, "Loading [Sub]Agent Module\n");
103 
104  res_snmp_dont_stop = 1;
105  if (res_snmp_enabled)
106  return ast_pthread_create_background(&thread, NULL, agent_thread, NULL);
107  else
108  return 0;
109 }
110 
111 static int unload_module(void)
112 {
113  ast_verb(1, "Unloading [Sub]Agent Module\n");
114 
115  res_snmp_dont_stop = 0;
116  return ((thread != AST_PTHREADT_NULL) ? pthread_join(thread, NULL) : 0);
117 }
118 
119 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "SNMP [Sub]Agent for Asterisk",
120  .load = load_module,
121  .unload = unload_module,
122  );
pthread_t thread
Definition: app_meetme.c:962
void * agent_thread(void *arg)
Definition: agent.c:185
Asterisk main include file. File version handling, generic pbx functions.
static int res_snmp_enabled
Definition: res_snmp.c:38
#define LOG_WARNING
Definition: logger.h:144
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category)
Goes through variables.
Definition: config.c:597
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
#define var
Definition: ast_expr2f.c:606
static int load_config(void)
Load res_snmp.conf config file.
Definition: res_snmp.c:46
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
#define ast_verb(level,...)
Definition: logger.h:243
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:426
#define CONFIG_STATUS_FILEMISSING
Definition: config.h:50
const char * value
Definition: config.h:79
#define ast_config_load(filename, flags)
Load a config file.
Definition: config.h:170
#define AST_PTHREADT_NULL
Definition: lock.h:65
char * ast_category_browse(struct ast_config *config, const char *prev)
Goes through categories.
Definition: config.c:810
const char * name
Definition: config.h:77
int res_snmp_dont_stop
Definition: res_snmp.c:37
#define LOG_ERROR
Definition: logger.h:155
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is &quot;true&quot;. This function checks to see whether a string passed to it is an indication of an &quot;true&quot; value. It checks to see if the string is &quot;yes&quot;, &quot;true&quot;, &quot;y&quot;, &quot;t&quot;, &quot;on&quot; or &quot;1&quot;.
Definition: utils.c:1533
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
Structure used to handle boolean flags.
Definition: utils.h:200
int res_snmp_agentx_subagent
Definition: res_snmp.c:36
SNMP Agent / SubAgent support for Asterisk.
int attribute_pure ast_false(const char *val)
Make sure something is false. Determine if a string containing a boolean value is &quot;false&quot;...
Definition: utils.c:1550
static int unload_module(void)
Definition: res_snmp.c:111
struct ast_variable * next
Definition: config.h:82
#define CONFIG_STATUS_FILEINVALID
Definition: config.h:52
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
static int load_module(void)
Definition: res_snmp.c:97
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180