res_statsd.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@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 Support for publishing to a statsd server.
  20. *
  21. * \author David M. Lee, II <dlee@digium.com>
  22. * \since 12
  23. */
  24. /*** MODULEINFO
  25. <support_level>extended</support_level>
  26. ***/
  27. /*** DOCUMENTATION
  28. <configInfo name="res_statsd" language="en_US">
  29. <synopsis>Statsd client.</synopsis>
  30. <configFile name="statsd.conf">
  31. <configObject name="global">
  32. <synopsis>Global configuration settings</synopsis>
  33. <configOption name="enabled">
  34. <synopsis>Enable/disable the statsd module</synopsis>
  35. </configOption>
  36. <configOption name="server">
  37. <synopsis>Address of the statsd server</synopsis>
  38. </configOption>
  39. <configOption name="prefix">
  40. <synopsis>Prefix to prepend to every metric</synopsis>
  41. </configOption>
  42. <configOption name="add_newline">
  43. <synopsis>Append a newline to every event. This is useful if you want to fake out a server using netcat (nc -lu 8125)</synopsis>
  44. </configOption>
  45. </configObject>
  46. </configFile>
  47. </configInfo>
  48. ***/
  49. #include "asterisk.h"
  50. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  51. #include "asterisk/config_options.h"
  52. #include "asterisk/module.h"
  53. #include "asterisk/netsock2.h"
  54. #define AST_API_MODULE
  55. #include "asterisk/statsd.h"
  56. #define DEFAULT_STATSD_PORT 8125
  57. #define MAX_PREFIX 40
  58. /*! Socket for sending statd messages */
  59. static int socket_fd = -1;
  60. /*! \brief Global configuration options for statsd client. */
  61. struct conf_global_options {
  62. /*! Enabled by default, disabled if false. */
  63. int enabled;
  64. /*! Disabled by default, appends newlines to all messages when enabled. */
  65. int add_newline;
  66. /*! Statsd server address[:port]. */
  67. struct ast_sockaddr statsd_server;
  68. /*! Prefix to put on every stat. */
  69. char prefix[MAX_PREFIX + 1];
  70. };
  71. /*! \brief All configuration options for statsd client. */
  72. struct conf {
  73. /*! The general section configuration options. */
  74. struct conf_global_options *global;
  75. };
  76. /*! \brief Locking container for safe configuration access. */
  77. static AO2_GLOBAL_OBJ_STATIC(confs);
  78. static void conf_server(const struct conf *cfg, struct ast_sockaddr *addr)
  79. {
  80. *addr = cfg->global->statsd_server;
  81. if (ast_sockaddr_port(addr) == 0) {
  82. ast_sockaddr_set_port(addr, DEFAULT_STATSD_PORT);
  83. }
  84. }
  85. void AST_OPTIONAL_API_NAME(ast_statsd_log_full)(const char *metric_name,
  86. const char *metric_type, intmax_t value, double sample_rate)
  87. {
  88. RAII_VAR(struct conf *, cfg, NULL, ao2_cleanup);
  89. RAII_VAR(struct ast_str *, msg, NULL, ast_free);
  90. size_t len;
  91. struct ast_sockaddr statsd_server;
  92. if (socket_fd == -1) {
  93. return;
  94. }
  95. cfg = ao2_global_obj_ref(confs);
  96. conf_server(cfg, &statsd_server);
  97. /* Rates <= 0.0 never get logged.
  98. * Rates >= 1.0 always get logged.
  99. * All others leave it to chance.
  100. */
  101. if (sample_rate <= 0.0 ||
  102. (sample_rate < 1.0 && sample_rate < ast_random_double())) {
  103. return;
  104. }
  105. cfg = ao2_global_obj_ref(confs);
  106. msg = ast_str_create(40);
  107. if (!msg) {
  108. return;
  109. }
  110. if (!ast_strlen_zero(cfg->global->prefix)) {
  111. ast_str_append(&msg, 0, "%s.", cfg->global->prefix);
  112. }
  113. ast_str_append(&msg, 0, "%s:%jd|%s", metric_name, value, metric_type);
  114. if (sample_rate < 1.0) {
  115. ast_str_append(&msg, 0, "|@%.2f", sample_rate);
  116. }
  117. if (cfg->global->add_newline) {
  118. ast_str_append(&msg, 0, "\n");
  119. }
  120. len = ast_str_strlen(msg);
  121. ast_debug(6, "send: %s\n", ast_str_buffer(msg));
  122. ast_sendto(socket_fd, ast_str_buffer(msg), len, 0, &statsd_server);
  123. }
  124. void AST_OPTIONAL_API_NAME(ast_statsd_log)(const char *metric_name,
  125. const char *metric_type, intmax_t value)
  126. {
  127. ast_statsd_log_full(metric_name, metric_type, value, 1.0);
  128. }
  129. void AST_OPTIONAL_API_NAME(ast_statsd_log_sample)(const char *metric_name,
  130. intmax_t value, double sample_rate)
  131. {
  132. ast_statsd_log_full(metric_name, AST_STATSD_COUNTER, value,
  133. sample_rate);
  134. }
  135. /*! \brief Mapping of the statsd conf struct's globals to the
  136. * general context in the config file. */
  137. static struct aco_type global_option = {
  138. .type = ACO_GLOBAL,
  139. .name = "global",
  140. .item_offset = offsetof(struct conf, global),
  141. .category = "^general$",
  142. .category_match = ACO_WHITELIST
  143. };
  144. static struct aco_type *global_options[] = ACO_TYPES(&global_option);
  145. /*! \brief Disposes of the statsd conf object */
  146. static void conf_destructor(void *obj)
  147. {
  148. struct conf *cfg = obj;
  149. ao2_cleanup(cfg->global);
  150. }
  151. /*! \brief Creates the statis http conf object. */
  152. static void *conf_alloc(void)
  153. {
  154. struct conf *cfg;
  155. if (!(cfg = ao2_alloc(sizeof(*cfg), conf_destructor))) {
  156. return NULL;
  157. }
  158. if (!(cfg->global = ao2_alloc(sizeof(*cfg->global), NULL))) {
  159. ao2_ref(cfg, -1);
  160. return NULL;
  161. }
  162. return cfg;
  163. }
  164. /*! \brief The conf file that's processed for the module. */
  165. static struct aco_file conf_file = {
  166. /*! The config file name. */
  167. .filename = "statsd.conf",
  168. /*! The mapping object types to be processed. */
  169. .types = ACO_TYPES(&global_option),
  170. };
  171. CONFIG_INFO_STANDARD(cfg_info, confs, conf_alloc,
  172. .files = ACO_FILES(&conf_file));
  173. /*! \brief Helper function to check if module is enabled. */
  174. static char is_enabled(void)
  175. {
  176. RAII_VAR(struct conf *, cfg, ao2_global_obj_ref(confs), ao2_cleanup);
  177. return cfg->global->enabled;
  178. }
  179. static int statsd_init(void)
  180. {
  181. RAII_VAR(struct conf *, cfg, ao2_global_obj_ref(confs), ao2_cleanup);
  182. char *server;
  183. struct ast_sockaddr statsd_server;
  184. ast_assert(is_enabled());
  185. ast_debug(3, "Configuring statsd client.\n");
  186. if (socket_fd == -1) {
  187. ast_debug(3, "Creating statsd socket.\n");
  188. socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  189. if (socket_fd == -1) {
  190. perror("Error creating statsd socket");
  191. return -1;
  192. }
  193. }
  194. conf_server(cfg, &statsd_server);
  195. server = ast_sockaddr_stringify_fmt(&statsd_server,
  196. AST_SOCKADDR_STR_DEFAULT);
  197. ast_debug(3, " statsd server = %s.\n", server);
  198. ast_debug(3, " add newline = %s\n", AST_YESNO(cfg->global->add_newline));
  199. ast_debug(3, " prefix = %s\n", cfg->global->prefix);
  200. return 0;
  201. }
  202. static void statsd_shutdown(void)
  203. {
  204. ast_debug(3, "Shutting down statsd client.\n");
  205. if (socket_fd != -1) {
  206. close(socket_fd);
  207. socket_fd = -1;
  208. }
  209. }
  210. static int load_module(void)
  211. {
  212. if (aco_info_init(&cfg_info)) {
  213. aco_info_destroy(&cfg_info);
  214. return AST_MODULE_LOAD_DECLINE;
  215. }
  216. aco_option_register(&cfg_info, "enabled", ACO_EXACT, global_options,
  217. "no", OPT_BOOL_T, 1,
  218. FLDSET(struct conf_global_options, enabled));
  219. aco_option_register(&cfg_info, "add_newline", ACO_EXACT, global_options,
  220. "no", OPT_BOOL_T, 1,
  221. FLDSET(struct conf_global_options, add_newline));
  222. aco_option_register(&cfg_info, "server", ACO_EXACT, global_options,
  223. "127.0.0.1", OPT_SOCKADDR_T, 0,
  224. FLDSET(struct conf_global_options, statsd_server));
  225. aco_option_register(&cfg_info, "prefix", ACO_EXACT, global_options,
  226. "", OPT_CHAR_ARRAY_T, 0,
  227. CHARFLDSET(struct conf_global_options, prefix));
  228. if (aco_process_config(&cfg_info, 0)) {
  229. aco_info_destroy(&cfg_info);
  230. return AST_MODULE_LOAD_DECLINE;
  231. }
  232. if (!is_enabled()) {
  233. return AST_MODULE_LOAD_SUCCESS;
  234. }
  235. if (statsd_init() != 0) {
  236. return AST_MODULE_LOAD_FAILURE;
  237. }
  238. return AST_MODULE_LOAD_SUCCESS;
  239. }
  240. static int unload_module(void)
  241. {
  242. statsd_shutdown();
  243. aco_info_destroy(&cfg_info);
  244. ao2_global_obj_release(confs);
  245. return 0;
  246. }
  247. static int reload_module(void)
  248. {
  249. if (aco_process_config(&cfg_info, 1)) {
  250. return AST_MODULE_LOAD_DECLINE;
  251. }
  252. if (is_enabled()) {
  253. return statsd_init();
  254. } else {
  255. statsd_shutdown();
  256. return AST_MODULE_LOAD_SUCCESS;
  257. }
  258. }
  259. /* The priority of this module is set to be as low as possible, since it could
  260. * be used by any other sort of module.
  261. */
  262. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Statsd client support",
  263. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  264. .load = load_module,
  265. .unload = unload_module,
  266. .reload = reload_module,
  267. .load_pri = 0,
  268. );