| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
#include "asterisk.h" |
| 90 |
|
| 91 |
#include "asterisk/config_options.h" |
| 92 |
#include "asterisk/module.h" |
| 93 |
#include "asterisk/netsock2.h" |
| 94 |
|
| 95 |
#define AST_API_MODULE |
| 96 |
#include "asterisk/statsd.h" |
| 97 |
|
| 98 |
#define DEFAULT_STATSD_PORT 8125 |
| 99 |
|
| 100 |
#define MAX_PREFIX 40 |
| 101 |
|
| 102 |
|
| 103 |
static int socket_fd = -1; |
| 104 |
|
| 105 |
|
| 106 |
struct conf_global_options { |
| 107 |
|
| 108 |
int enabled; |
| 109 |
|
| 110 |
int add_newline; |
| 111 |
|
| 112 |
struct ast_sockaddr statsd_server; |
| 113 |
|
| 114 |
char prefix[MAX_PREFIX + 1]; |
| 115 |
|
| 116 |
int meter_support; |
| 117 |
}; |
| 118 |
|
| 119 |
|
| 120 |
struct conf { |
| 121 |
|
| 122 |
struct conf_global_options *global; |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
static AO2_GLOBAL_OBJ_STATIC(confs); |
| 127 |
|
| 128 |
static void conf_server(const struct conf *cfg, struct ast_sockaddr *addr) |
| 129 |
{ |
| 130 |
*addr = cfg->global->statsd_server; |
| 131 |
if (ast_sockaddr_port(addr) == 0) { |
| 132 |
ast_sockaddr_set_port(addr, DEFAULT_STATSD_PORT); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
void AST_OPTIONAL_API_NAME(ast_statsd_log_string)(const char *metric_name, |
| 137 |
const char *metric_type, const char *value, double sample_rate) |
| 138 |
{ |
| 139 |
struct conf *cfg; |
| 140 |
struct ast_str *msg; |
| 141 |
size_t len; |
| 142 |
struct ast_sockaddr statsd_server; |
| 143 |
|
| 144 |
if (socket_fd == -1) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
if (sample_rate <= 0.0 || |
| 153 |
(sample_rate < 1.0 && sample_rate < ast_random_double())) { |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
cfg = ao2_global_obj_ref(confs); |
| 158 |
conf_server(cfg, &statsd_server); |
| 159 |
|
| 160 |
msg = ast_str_create(40); |
| 161 |
if (!msg) { |
| 162 |
ao2_cleanup(cfg); |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
if (!ast_strlen_zero(cfg->global->prefix)) { |
| 167 |
ast_str_append(&msg, 0, "%s.", cfg->global->prefix); |
| 168 |
} |
| 169 |
|
| 170 |
if (!cfg->global->meter_support && strcmp(metric_type, AST_STATSD_METER)) { |
| 171 |
ast_str_append(&msg, 0, "%s_meter:%s|%s", metric_name, value, AST_STATSD_COUNTER); |
| 172 |
} else { |
| 173 |
ast_str_append(&msg, 0, "%s:%s|%s", metric_name, value, metric_type); |
| 174 |
} |
| 175 |
|
| 176 |
if (sample_rate < 1.0) { |
| 177 |
ast_str_append(&msg, 0, "|@%.2f", sample_rate); |
| 178 |
} |
| 179 |
|
| 180 |
if (cfg->global->add_newline) { |
| 181 |
ast_str_append(&msg, 0, "\n"); |
| 182 |
} |
| 183 |
|
| 184 |
len = ast_str_strlen(msg); |
| 185 |
|
| 186 |
ast_debug(6, "Sending statistic %s to StatsD server\n", ast_str_buffer(msg)); |
| 187 |
ast_sendto(socket_fd, ast_str_buffer(msg), len, 0, &statsd_server); |
| 188 |
|
| 189 |
ao2_cleanup(cfg); |
| 190 |
ast_free(msg); |
| 191 |
} |
| 192 |
|
| 193 |
void AST_OPTIONAL_API_NAME(ast_statsd_log_full)(const char *metric_name, |
| 194 |
const char *metric_type, intmax_t value, double sample_rate) |
| 195 |
{ |
| 196 |
char char_value[30]; |
| 197 |
snprintf(char_value, sizeof(char_value), "%jd", value); |
| 198 |
|
| 199 |
ast_statsd_log_string(metric_name, metric_type, char_value, sample_rate); |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
AST_THREADSTORAGE(statsd_buf); |
| 204 |
|
| 205 |
void AST_OPTIONAL_API_NAME(ast_statsd_log_string_va)(const char *metric_name, |
| 206 |
const char *metric_type, const char *value, double sample_rate, ...) |
| 207 |
{ |
| 208 |
struct ast_str *buf; |
| 209 |
va_list ap; |
| 210 |
int res; |
| 211 |
|
| 212 |
buf = ast_str_thread_get(&statsd_buf, 128); |
| 213 |
if (!buf) { |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
va_start(ap, sample_rate); |
| 218 |
res = ast_str_set_va(&buf, 0, metric_name, ap); |
| 219 |
va_end(ap); |
| 220 |
|
| 221 |
if (res == AST_DYNSTR_BUILD_FAILED) { |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
ast_statsd_log_string(ast_str_buffer(buf), metric_type, value, sample_rate); |
| 226 |
} |
| 227 |
|
| 228 |
void AST_OPTIONAL_API_NAME(ast_statsd_log_full_va)(const char *metric_name, |
| 229 |
const char *metric_type, intmax_t value, double sample_rate, ...) |
| 230 |
{ |
| 231 |
struct ast_str *buf; |
| 232 |
va_list ap; |
| 233 |
int res; |
| 234 |
|
| 235 |
buf = ast_str_thread_get(&statsd_buf, 128); |
| 236 |
if (!buf) { |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
va_start(ap, sample_rate); |
| 241 |
res = ast_str_set_va(&buf, 0, metric_name, ap); |
| 242 |
va_end(ap); |
| 243 |
|
| 244 |
if (res == AST_DYNSTR_BUILD_FAILED) { |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
ast_statsd_log_full(ast_str_buffer(buf), metric_type, value, sample_rate); |
| 249 |
} |
| 250 |
|
| 251 |
void AST_OPTIONAL_API_NAME(ast_statsd_log)(const char *metric_name, |
| 252 |
const char *metric_type, intmax_t value) |
| 253 |
{ |
| 254 |
char char_value[30]; |
| 255 |
snprintf(char_value, sizeof(char_value), "%jd", value); |
| 256 |
|
| 257 |
ast_statsd_log_string(metric_name, metric_type, char_value, 1.0); |
| 258 |
} |
| 259 |
|
| 260 |
void AST_OPTIONAL_API_NAME(ast_statsd_log_sample)(const char *metric_name, |
| 261 |
intmax_t value, double sample_rate) |
| 262 |
{ |
| 263 |
char char_value[30]; |
| 264 |
snprintf(char_value, sizeof(char_value), "%jd", value); |
| 265 |
|
| 266 |
ast_statsd_log_string(metric_name, AST_STATSD_COUNTER, char_value, |
| 267 |
sample_rate); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
static struct aco_type global_option = { |
| 273 |
.type = ACO_GLOBAL, |
| 274 |
.name = "global", |
| 275 |
.item_offset = offsetof(struct conf, global), |
| 276 |
.category = "general", |
| 277 |
.category_match = ACO_WHITELIST_EXACT, |
| 278 |
}; |
| 279 |
|
| 280 |
static struct aco_type *global_options[] = ACO_TYPES(&global_option); |
| 281 |
|
| 282 |
|
| 283 |
static void conf_destructor(void *obj) |
| 284 |
{ |
| 285 |
struct conf *cfg = obj; |
| 286 |
ao2_cleanup(cfg->global); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
static void *conf_alloc(void) |
| 291 |
{ |
| 292 |
struct conf *cfg; |
| 293 |
|
| 294 |
if (!(cfg = ao2_alloc(sizeof(*cfg), conf_destructor))) { |
| 295 |
return NULL; |
| 296 |
} |
| 297 |
|
| 298 |
if (!(cfg->global = ao2_alloc(sizeof(*cfg->global), NULL))) { |
| 299 |
ao2_ref(cfg, -1); |
| 300 |
return NULL; |
| 301 |
} |
| 302 |
return cfg; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
static struct aco_file conf_file = { |
| 307 |
|
| 308 |
.filename = "statsd.conf", |
| 309 |
|
| 310 |
.types = ACO_TYPES(&global_option), |
| 311 |
}; |
| 312 |
|
| 313 |
CONFIG_INFO_STANDARD(cfg_info, confs, conf_alloc, |
| 314 |
.files = ACO_FILES(&conf_file)); |
| 315 |
|
| 316 |
|
| 317 |
static char is_enabled(void) |
| 318 |
{ |
| 319 |
RAII_VAR(struct conf *, cfg, ao2_global_obj_ref(confs), ao2_cleanup); |
| 320 |
return cfg->global->enabled; |
| 321 |
} |
| 322 |
|
| 323 |
static int statsd_init(void) |
| 324 |
{ |
| 325 |
RAII_VAR(struct conf *, cfg, ao2_global_obj_ref(confs), ao2_cleanup); |
| 326 |
char *server; |
| 327 |
struct ast_sockaddr statsd_server; |
| 328 |
|
| 329 |
ast_assert(is_enabled()); |
| 330 |
|
| 331 |
ast_debug(3, "Configuring StatsD client.\n"); |
| 332 |
|
| 333 |
if (socket_fd == -1) { |
| 334 |
ast_debug(3, "Creating StatsD socket.\n"); |
| 335 |
socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 336 |
if (socket_fd == -1) { |
| 337 |
perror("Error creating StatsD socket"); |
| 338 |
return -1; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
conf_server(cfg, &statsd_server); |
| 343 |
server = ast_sockaddr_stringify_fmt(&statsd_server, |
| 344 |
AST_SOCKADDR_STR_DEFAULT); |
| 345 |
ast_debug(3, " StatsD server = %s.\n", server); |
| 346 |
ast_debug(3, " add newline = %s\n", AST_YESNO(cfg->global->add_newline)); |
| 347 |
ast_debug(3, " prefix = %s\n", cfg->global->prefix); |
| 348 |
|
| 349 |
return 0; |
| 350 |
} |
| 351 |
|
| 352 |
static void statsd_shutdown(void) |
| 353 |
{ |
| 354 |
ast_debug(3, "Shutting down StatsD client.\n"); |
| 355 |
if (socket_fd != -1) { |
| 356 |
close(socket_fd); |
| 357 |
socket_fd = -1; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
static int unload_module(void) |
| 362 |
{ |
| 363 |
statsd_shutdown(); |
| 364 |
aco_info_destroy(&cfg_info); |
| 365 |
ao2_global_obj_release(confs); |
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
static int load_module(void) |
| 370 |
{ |
| 371 |
if (aco_info_init(&cfg_info)) { |
| 372 |
aco_info_destroy(&cfg_info); |
| 373 |
return AST_MODULE_LOAD_DECLINE; |
| 374 |
} |
| 375 |
|
| 376 |
aco_option_register(&cfg_info, "enabled", ACO_EXACT, global_options, |
| 377 |
"no", OPT_BOOL_T, 1, |
| 378 |
FLDSET(struct conf_global_options, enabled)); |
| 379 |
|
| 380 |
aco_option_register(&cfg_info, "add_newline", ACO_EXACT, global_options, |
| 381 |
"no", OPT_BOOL_T, 1, |
| 382 |
FLDSET(struct conf_global_options, add_newline)); |
| 383 |
|
| 384 |
aco_option_register(&cfg_info, "server", ACO_EXACT, global_options, |
| 385 |
"127.0.0.1", OPT_SOCKADDR_T, 0, |
| 386 |
FLDSET(struct conf_global_options, statsd_server)); |
| 387 |
|
| 388 |
aco_option_register(&cfg_info, "prefix", ACO_EXACT, global_options, |
| 389 |
"", OPT_CHAR_ARRAY_T, 0, |
| 390 |
CHARFLDSET(struct conf_global_options, prefix)); |
| 391 |
|
| 392 |
aco_option_register(&cfg_info, "meter_support", ACO_EXACT, global_options, |
| 393 |
"yes", OPT_BOOL_T, 1, |
| 394 |
FLDSET(struct conf_global_options, meter_support)); |
| 395 |
|
| 396 |
if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) { |
| 397 |
struct conf *cfg; |
| 398 |
|
| 399 |
ast_log(LOG_NOTICE, "Could not load statsd config; using defaults\n"); |
| 400 |
cfg = conf_alloc(); |
| 401 |
if (!cfg) { |
| 402 |
aco_info_destroy(&cfg_info); |
| 403 |
return AST_MODULE_LOAD_DECLINE; |
| 404 |
} |
| 405 |
|
| 406 |
if (aco_set_defaults(&global_option, "general", cfg->global)) { |
| 407 |
ast_log(LOG_ERROR, "Failed to initialize statsd defaults.\n"); |
| 408 |
ao2_ref(cfg, -1); |
| 409 |
aco_info_destroy(&cfg_info); |
| 410 |
return AST_MODULE_LOAD_DECLINE; |
| 411 |
} |
| 412 |
|
| 413 |
ao2_global_obj_replace_unref(confs, cfg); |
| 414 |
ao2_ref(cfg, -1); |
| 415 |
} |
| 416 |
|
| 417 |
if (!is_enabled()) { |
| 418 |
return AST_MODULE_LOAD_SUCCESS; |
| 419 |
} |
| 420 |
|
| 421 |
if (statsd_init()) { |
| 422 |
unload_module(); |
| 423 |
return AST_MODULE_LOAD_DECLINE; |
| 424 |
} |
| 425 |
|
| 426 |
return AST_MODULE_LOAD_SUCCESS; |
| 427 |
} |
| 428 |
|
| 429 |
static int reload_module(void) |
| 430 |
{ |
| 431 |
switch (aco_process_config(&cfg_info, 1)) { |
| 432 |
case ACO_PROCESS_OK: |
| 433 |
break; |
| 434 |
case ACO_PROCESS_UNCHANGED: |
| 435 |
return AST_MODULE_LOAD_SUCCESS; |
| 436 |
case ACO_PROCESS_ERROR: |
| 437 |
default: |
| 438 |
return AST_MODULE_LOAD_DECLINE; |
| 439 |
} |
| 440 |
|
| 441 |
if (is_enabled()) { |
| 442 |
if (statsd_init()) { |
| 443 |
return AST_MODULE_LOAD_DECLINE; |
| 444 |
} |
| 445 |
} else { |
| 446 |
statsd_shutdown(); |
| 447 |
} |
| 448 |
return AST_MODULE_LOAD_SUCCESS; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "StatsD client support", |
| 455 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 456 |
.load = load_module, |
| 457 |
.unload = unload_module, |
| 458 |
.reload = reload_module, |
| 459 |
.load_pri = AST_MODPRI_REALTIME_DRIVER + 5, |
| 460 |
); |