res_snmp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * \extref Uses the Net-SNMP libraries available at
  16. * http://net-snmp.sourceforge.net/
  17. */
  18. /*** MODULEINFO
  19. <depend>netsnmp</depend>
  20. <support_level>extended</support_level>
  21. ***/
  22. #include "asterisk.h"
  23. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  24. #include "asterisk/channel.h"
  25. #include "asterisk/module.h"
  26. #include "snmp/agent.h"
  27. #define MODULE_DESCRIPTION "SNMP [Sub]Agent for Asterisk"
  28. int res_snmp_agentx_subagent;
  29. int res_snmp_dont_stop;
  30. static int res_snmp_enabled;
  31. static pthread_t thread = AST_PTHREADT_NULL;
  32. /*!
  33. * \brief Load res_snmp.conf config file
  34. * \return 1 on load, 0 file does not exist
  35. */
  36. static int load_config(void)
  37. {
  38. struct ast_variable *var;
  39. struct ast_config *cfg;
  40. struct ast_flags config_flags = { 0 };
  41. char *cat;
  42. res_snmp_enabled = 0;
  43. res_snmp_agentx_subagent = 1;
  44. cfg = ast_config_load("res_snmp.conf", config_flags);
  45. if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
  46. ast_log(LOG_WARNING, "Could not load res_snmp.conf\n");
  47. return 0;
  48. }
  49. cat = ast_category_browse(cfg, NULL);
  50. while (cat) {
  51. var = ast_variable_browse(cfg, cat);
  52. if (strcasecmp(cat, "general") == 0) {
  53. while (var) {
  54. if (strcasecmp(var->name, "subagent") == 0) {
  55. if (ast_true(var->value))
  56. res_snmp_agentx_subagent = 1;
  57. else if (ast_false(var->value))
  58. res_snmp_agentx_subagent = 0;
  59. else {
  60. ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n", var->value);
  61. ast_config_destroy(cfg);
  62. return 1;
  63. }
  64. } else if (strcasecmp(var->name, "enabled") == 0) {
  65. res_snmp_enabled = ast_true(var->value);
  66. } else {
  67. ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);
  68. ast_config_destroy(cfg);
  69. return 1;
  70. }
  71. var = var->next;
  72. }
  73. } else {
  74. ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
  75. ast_config_destroy(cfg);
  76. return 1;
  77. }
  78. cat = ast_category_browse(cfg, cat);
  79. }
  80. ast_config_destroy(cfg);
  81. return 1;
  82. }
  83. static int load_module(void)
  84. {
  85. if(!load_config())
  86. return AST_MODULE_LOAD_DECLINE;
  87. ast_verb(1, "Loading [Sub]Agent Module\n");
  88. res_snmp_dont_stop = 1;
  89. if (res_snmp_enabled)
  90. return ast_pthread_create_background(&thread, NULL, agent_thread, NULL);
  91. else
  92. return 0;
  93. }
  94. static int unload_module(void)
  95. {
  96. ast_verb(1, "Unloading [Sub]Agent Module\n");
  97. res_snmp_dont_stop = 0;
  98. return ((thread != AST_PTHREADT_NULL) ? pthread_join(thread, NULL) : 0);
  99. }
  100. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "SNMP [Sub]Agent for Asterisk",
  101. .load = load_module,
  102. .unload = unload_module,
  103. );