res_snmp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /*! \file
  10. *
  11. * \brief SNMP Agent / SubAgent support for Asterisk
  12. *
  13. * \author Thorsten Lockert <tholo@voop.as>
  14. *
  15. * Uses the Net-SNMP libraries available at
  16. * http://net-snmp.sourceforge.net/
  17. */
  18. /*! \li \ref res_snmp.c uses the configuration file \ref res_snmp.conf
  19. * \addtogroup configuration_file Configuration Files
  20. */
  21. /*!
  22. * \page res_snmp.conf res_snmp.conf
  23. * \verbinclude res_snmp.conf.sample
  24. */
  25. /*** MODULEINFO
  26. <depend>netsnmp</depend>
  27. <support_level>extended</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/channel.h"
  32. #include "asterisk/module.h"
  33. #include "snmp/agent.h"
  34. #define MODULE_DESCRIPTION "SNMP [Sub]Agent for Asterisk"
  35. int res_snmp_agentx_subagent;
  36. int res_snmp_dont_stop;
  37. static int res_snmp_enabled;
  38. static pthread_t thread = AST_PTHREADT_NULL;
  39. /*!
  40. * \brief Load res_snmp.conf config file
  41. * \return 1 on load, 0 file does not exist
  42. */
  43. static int load_config(void)
  44. {
  45. struct ast_variable *var;
  46. struct ast_config *cfg;
  47. struct ast_flags config_flags = { 0 };
  48. char *cat;
  49. res_snmp_enabled = 0;
  50. res_snmp_agentx_subagent = 1;
  51. cfg = ast_config_load("res_snmp.conf", config_flags);
  52. if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
  53. ast_log(LOG_WARNING, "Could not load res_snmp.conf\n");
  54. return 0;
  55. }
  56. cat = ast_category_browse(cfg, NULL);
  57. while (cat) {
  58. var = ast_variable_browse(cfg, cat);
  59. if (strcasecmp(cat, "general") == 0) {
  60. while (var) {
  61. if (strcasecmp(var->name, "subagent") == 0) {
  62. if (ast_true(var->value))
  63. res_snmp_agentx_subagent = 1;
  64. else if (ast_false(var->value))
  65. res_snmp_agentx_subagent = 0;
  66. else {
  67. ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n", var->value);
  68. ast_config_destroy(cfg);
  69. return 1;
  70. }
  71. } else if (strcasecmp(var->name, "enabled") == 0) {
  72. res_snmp_enabled = ast_true(var->value);
  73. } else {
  74. ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);
  75. ast_config_destroy(cfg);
  76. return 1;
  77. }
  78. var = var->next;
  79. }
  80. } else {
  81. ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
  82. ast_config_destroy(cfg);
  83. return 1;
  84. }
  85. cat = ast_category_browse(cfg, cat);
  86. }
  87. ast_config_destroy(cfg);
  88. return 1;
  89. }
  90. /*!
  91. * \brief Load the module
  92. *
  93. * Module loading including tests for configuration or dependencies.
  94. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  95. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  96. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  97. * configuration file or other non-critical problem return
  98. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  99. */
  100. static int load_module(void)
  101. {
  102. if(!load_config())
  103. return AST_MODULE_LOAD_DECLINE;
  104. ast_verb(1, "Loading [Sub]Agent Module\n");
  105. res_snmp_dont_stop = 1;
  106. if (res_snmp_enabled)
  107. return ast_pthread_create_background(&thread, NULL, agent_thread, NULL);
  108. else
  109. return 0;
  110. }
  111. static int unload_module(void)
  112. {
  113. 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);
  116. }
  117. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "SNMP [Sub]Agent for Asterisk",
  118. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  119. .load = load_module,
  120. .unload = unload_module,
  121. );