res_statsd.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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_string)(const char *metric_name,
  86. const char *metric_type, const char *value, double sample_rate)
  87. {
  88. struct conf *cfg;
  89. struct ast_str *msg;
  90. size_t len;
  91. struct ast_sockaddr statsd_server;
  92. if (socket_fd == -1) {
  93. return;
  94. }
  95. /* Rates <= 0.0 never get logged.
  96. * Rates >= 1.0 always get logged.
  97. * All others leave it to chance.
  98. */
  99. if (sample_rate <= 0.0 ||
  100. (sample_rate < 1.0 && sample_rate < ast_random_double())) {
  101. return;
  102. }
  103. cfg = ao2_global_obj_ref(confs);
  104. conf_server(cfg, &statsd_server);
  105. msg = ast_str_create(40);
  106. if (!msg) {
  107. ao2_cleanup(cfg);
  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:%s|%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, "Sending statistic %s to StatsD server\n", ast_str_buffer(msg));
  122. ast_sendto(socket_fd, ast_str_buffer(msg), len, 0, &statsd_server);
  123. ao2_cleanup(cfg);
  124. ast_free(msg);
  125. }
  126. void AST_OPTIONAL_API_NAME(ast_statsd_log_full)(const char *metric_name,
  127. const char *metric_type, intmax_t value, double sample_rate)
  128. {
  129. char char_value[30];
  130. snprintf(char_value, sizeof(char_value), "%jd", value);
  131. ast_statsd_log_string(metric_name, metric_type, char_value, sample_rate);
  132. }
  133. AST_THREADSTORAGE(statsd_buf);
  134. void AST_OPTIONAL_API_NAME(ast_statsd_log_string_va)(const char *metric_name,
  135. const char *metric_type, const char *value, double sample_rate, ...)
  136. {
  137. struct ast_str *buf;
  138. va_list ap;
  139. int res;
  140. buf = ast_str_thread_get(&statsd_buf, 128);
  141. if (!buf) {
  142. return;
  143. }
  144. va_start(ap, sample_rate);
  145. res = ast_str_set_va(&buf, 0, metric_name, ap);
  146. va_end(ap);
  147. if (res == AST_DYNSTR_BUILD_FAILED) {
  148. return;
  149. }
  150. ast_statsd_log_string(ast_str_buffer(buf), metric_type, value, sample_rate);
  151. }
  152. void AST_OPTIONAL_API_NAME(ast_statsd_log_full_va)(const char *metric_name,
  153. const char *metric_type, intmax_t value, double sample_rate, ...)
  154. {
  155. struct ast_str *buf;
  156. va_list ap;
  157. int res;
  158. buf = ast_str_thread_get(&statsd_buf, 128);
  159. if (!buf) {
  160. return;
  161. }
  162. va_start(ap, sample_rate);
  163. res = ast_str_set_va(&buf, 0, metric_name, ap);
  164. va_end(ap);
  165. if (res == AST_DYNSTR_BUILD_FAILED) {
  166. return;
  167. }
  168. ast_statsd_log_full(ast_str_buffer(buf), metric_type, value, sample_rate);
  169. }
  170. void AST_OPTIONAL_API_NAME(ast_statsd_log)(const char *metric_name,
  171. const char *metric_type, intmax_t value)
  172. {
  173. char char_value[30];
  174. snprintf(char_value, sizeof(char_value), "%jd", value);
  175. ast_statsd_log_string(metric_name, metric_type, char_value, 1.0);
  176. }
  177. void AST_OPTIONAL_API_NAME(ast_statsd_log_sample)(const char *metric_name,
  178. intmax_t value, double sample_rate)
  179. {
  180. char char_value[30];
  181. snprintf(char_value, sizeof(char_value), "%jd", value);
  182. ast_statsd_log_string(metric_name, AST_STATSD_COUNTER, char_value,
  183. sample_rate);
  184. }
  185. /*! \brief Mapping of the statsd conf struct's globals to the
  186. * general context in the config file. */
  187. static struct aco_type global_option = {
  188. .type = ACO_GLOBAL,
  189. .name = "global",
  190. .item_offset = offsetof(struct conf, global),
  191. .category = "general",
  192. .category_match = ACO_WHITELIST_EXACT,
  193. };
  194. static struct aco_type *global_options[] = ACO_TYPES(&global_option);
  195. /*! \brief Disposes of the statsd conf object */
  196. static void conf_destructor(void *obj)
  197. {
  198. struct conf *cfg = obj;
  199. ao2_cleanup(cfg->global);
  200. }
  201. /*! \brief Creates the statis http conf object. */
  202. static void *conf_alloc(void)
  203. {
  204. struct conf *cfg;
  205. if (!(cfg = ao2_alloc(sizeof(*cfg), conf_destructor))) {
  206. return NULL;
  207. }
  208. if (!(cfg->global = ao2_alloc(sizeof(*cfg->global), NULL))) {
  209. ao2_ref(cfg, -1);
  210. return NULL;
  211. }
  212. return cfg;
  213. }
  214. /*! \brief The conf file that's processed for the module. */
  215. static struct aco_file conf_file = {
  216. /*! The config file name. */
  217. .filename = "statsd.conf",
  218. /*! The mapping object types to be processed. */
  219. .types = ACO_TYPES(&global_option),
  220. };
  221. CONFIG_INFO_STANDARD(cfg_info, confs, conf_alloc,
  222. .files = ACO_FILES(&conf_file));
  223. /*! \brief Helper function to check if module is enabled. */
  224. static char is_enabled(void)
  225. {
  226. RAII_VAR(struct conf *, cfg, ao2_global_obj_ref(confs), ao2_cleanup);
  227. return cfg->global->enabled;
  228. }
  229. static int statsd_init(void)
  230. {
  231. RAII_VAR(struct conf *, cfg, ao2_global_obj_ref(confs), ao2_cleanup);
  232. char *server;
  233. struct ast_sockaddr statsd_server;
  234. ast_assert(is_enabled());
  235. ast_debug(3, "Configuring statsd client.\n");
  236. if (socket_fd == -1) {
  237. ast_debug(3, "Creating statsd socket.\n");
  238. socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  239. if (socket_fd == -1) {
  240. perror("Error creating statsd socket");
  241. return -1;
  242. }
  243. }
  244. conf_server(cfg, &statsd_server);
  245. server = ast_sockaddr_stringify_fmt(&statsd_server,
  246. AST_SOCKADDR_STR_DEFAULT);
  247. ast_debug(3, " statsd server = %s.\n", server);
  248. ast_debug(3, " add newline = %s\n", AST_YESNO(cfg->global->add_newline));
  249. ast_debug(3, " prefix = %s\n", cfg->global->prefix);
  250. return 0;
  251. }
  252. static void statsd_shutdown(void)
  253. {
  254. ast_debug(3, "Shutting down statsd client.\n");
  255. if (socket_fd != -1) {
  256. close(socket_fd);
  257. socket_fd = -1;
  258. }
  259. }
  260. static int unload_module(void)
  261. {
  262. statsd_shutdown();
  263. aco_info_destroy(&cfg_info);
  264. ao2_global_obj_release(confs);
  265. return 0;
  266. }
  267. static int load_module(void)
  268. {
  269. if (aco_info_init(&cfg_info)) {
  270. aco_info_destroy(&cfg_info);
  271. return AST_MODULE_LOAD_DECLINE;
  272. }
  273. aco_option_register(&cfg_info, "enabled", ACO_EXACT, global_options,
  274. "no", OPT_BOOL_T, 1,
  275. FLDSET(struct conf_global_options, enabled));
  276. aco_option_register(&cfg_info, "add_newline", ACO_EXACT, global_options,
  277. "no", OPT_BOOL_T, 1,
  278. FLDSET(struct conf_global_options, add_newline));
  279. aco_option_register(&cfg_info, "server", ACO_EXACT, global_options,
  280. "127.0.0.1", OPT_SOCKADDR_T, 0,
  281. FLDSET(struct conf_global_options, statsd_server));
  282. aco_option_register(&cfg_info, "prefix", ACO_EXACT, global_options,
  283. "", OPT_CHAR_ARRAY_T, 0,
  284. CHARFLDSET(struct conf_global_options, prefix));
  285. if (aco_process_config(&cfg_info, 0)) {
  286. aco_info_destroy(&cfg_info);
  287. return AST_MODULE_LOAD_DECLINE;
  288. }
  289. if (!is_enabled()) {
  290. return AST_MODULE_LOAD_SUCCESS;
  291. }
  292. if (statsd_init()) {
  293. unload_module();
  294. return AST_MODULE_LOAD_DECLINE;
  295. }
  296. /* For Optional API. */
  297. ast_module_shutdown_ref(ast_module_info->self);
  298. return AST_MODULE_LOAD_SUCCESS;
  299. }
  300. static int reload_module(void)
  301. {
  302. switch (aco_process_config(&cfg_info, 1)) {
  303. case ACO_PROCESS_OK:
  304. break;
  305. case ACO_PROCESS_UNCHANGED:
  306. return AST_MODULE_LOAD_SUCCESS;
  307. case ACO_PROCESS_ERROR:
  308. default:
  309. return AST_MODULE_LOAD_DECLINE;
  310. }
  311. if (is_enabled()) {
  312. if (statsd_init()) {
  313. return AST_MODULE_LOAD_DECLINE;
  314. }
  315. } else {
  316. statsd_shutdown();
  317. }
  318. return AST_MODULE_LOAD_SUCCESS;
  319. }
  320. /* The priority of this module is set to be as low as possible, since it could
  321. * be used by any other sort of module.
  322. */
  323. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Statsd client support",
  324. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  325. .load = load_module,
  326. .unload = unload_module,
  327. .reload = reload_module,
  328. .load_pri = 0,
  329. );