res_endpoint_stats.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2015, Digium, Inc.
  5. *
  6. * Matthew Jordan <mjordan@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \brief Statsd Endpoint stats.
  20. *
  21. * This module subscribes to Stasis endpoints and send statistics
  22. * based on their state.
  23. *
  24. * \author Matthew Jordan <mjordan@digium.com>
  25. * \since 13.7.0
  26. */
  27. /*** MODULEINFO
  28. <depend>res_statsd</depend>
  29. <defaultenabled>no</defaultenabled>
  30. <support_level>extended</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include "asterisk/module.h"
  35. #include "asterisk/stasis_endpoints.h"
  36. #include "asterisk/stasis_message_router.h"
  37. #include "asterisk/statsd.h"
  38. /*! Stasis message router */
  39. static struct stasis_message_router *router;
  40. static void update_endpoint_state(struct ast_endpoint_snapshot *snapshot, const char *delta)
  41. {
  42. switch (snapshot->state) {
  43. case AST_ENDPOINT_UNKNOWN:
  44. ast_statsd_log_string("endpoints.state.unknown", AST_STATSD_GAUGE, delta, 1.0);
  45. break;
  46. case AST_ENDPOINT_OFFLINE:
  47. ast_statsd_log_string("endpoints.state.offline", AST_STATSD_GAUGE, delta, 1.0);
  48. break;
  49. case AST_ENDPOINT_ONLINE:
  50. ast_statsd_log_string("endpoints.state.online", AST_STATSD_GAUGE, delta, 1.0);
  51. break;
  52. }
  53. }
  54. static void handle_endpoint_update(struct ast_endpoint_snapshot *old_snapshot, struct ast_endpoint_snapshot *new_snapshot)
  55. {
  56. if (!old_snapshot && new_snapshot) {
  57. ast_statsd_log_string("endpoints.count", AST_STATSD_GAUGE, "+1", 1.0);
  58. update_endpoint_state(new_snapshot, "+1");
  59. } else if (old_snapshot && !new_snapshot) {
  60. ast_statsd_log_string("endpoints.count", AST_STATSD_GAUGE, "-1", 1.0);
  61. update_endpoint_state(old_snapshot, "-1");
  62. } else {
  63. if (old_snapshot->state != new_snapshot->state) {
  64. update_endpoint_state(old_snapshot, "-1");
  65. update_endpoint_state(new_snapshot, "+1");
  66. }
  67. ast_statsd_log_full_va("endpoints.%s.%s.channels", AST_STATSD_GAUGE, new_snapshot->num_channels, 1.0,
  68. new_snapshot->tech, new_snapshot->resource);
  69. }
  70. }
  71. static void cache_update_cb(void *data, struct stasis_subscription *sub,
  72. struct stasis_message *message)
  73. {
  74. struct stasis_cache_update *update = stasis_message_data(message);
  75. struct ast_endpoint_snapshot *old_snapshot;
  76. struct ast_endpoint_snapshot *new_snapshot;
  77. if (ast_endpoint_snapshot_type() != update->type) {
  78. return;
  79. }
  80. old_snapshot = stasis_message_data(update->old_snapshot);
  81. new_snapshot = stasis_message_data(update->new_snapshot);
  82. handle_endpoint_update(old_snapshot, new_snapshot);
  83. }
  84. static int dump_cache_load(void *obj, void *arg, int flags)
  85. {
  86. struct stasis_message *msg = obj;
  87. struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
  88. handle_endpoint_update(NULL, snapshot);
  89. return 0;
  90. }
  91. static int dump_cache_unload(void *obj, void *arg, int flags)
  92. {
  93. struct stasis_message *msg = obj;
  94. struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
  95. handle_endpoint_update(snapshot, NULL);
  96. return 0;
  97. }
  98. static int load_module(void)
  99. {
  100. struct ao2_container *endpoints;
  101. router = stasis_message_router_create(ast_endpoint_topic_all_cached());
  102. if (!router) {
  103. return AST_MODULE_LOAD_DECLINE;
  104. }
  105. stasis_message_router_add(router, stasis_cache_update_type(), cache_update_cb, NULL);
  106. endpoints = stasis_cache_dump(ast_endpoint_cache(), ast_endpoint_snapshot_type());
  107. if (endpoints) {
  108. ao2_callback(endpoints, OBJ_MULTIPLE | OBJ_NODATA | OBJ_NOLOCK, dump_cache_load, NULL);
  109. ao2_ref(endpoints, -1);
  110. }
  111. return AST_MODULE_LOAD_SUCCESS;
  112. }
  113. static int unload_module(void)
  114. {
  115. struct ao2_container *endpoints;
  116. endpoints = stasis_cache_dump(ast_endpoint_cache(), ast_endpoint_snapshot_type());
  117. if (endpoints) {
  118. ao2_callback(endpoints, OBJ_MULTIPLE | OBJ_NODATA | OBJ_NOLOCK, dump_cache_unload, NULL);
  119. ao2_ref(endpoints, -1);
  120. }
  121. stasis_message_router_unsubscribe_and_join(router);
  122. router = NULL;
  123. return 0;
  124. }
  125. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Endpoint statistics",
  126. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  127. .load = load_module,
  128. .unload = unload_module,
  129. );