| 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 |
#include "asterisk.h" |
| 26 |
|
| 27 |
#include <math.h> |
| 28 |
|
| 29 |
#include "asterisk/module.h" |
| 30 |
#include "asterisk/logger.h" |
| 31 |
#include "asterisk/app.h" |
| 32 |
#include "asterisk/pbx.h" |
| 33 |
#include "asterisk/strings.h" |
| 34 |
#include "asterisk/statsd.h" |
| 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 |
static const char app[] = "StatsD"; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
static int value_in_range(const char *value) { |
| 92 |
double numerical_value = strtod(value, NULL); |
| 93 |
|
| 94 |
if (numerical_value < pow(-2, 63) || numerical_value > pow(2, 63)) { |
| 95 |
ast_log(AST_LOG_WARNING, "Value %lf out of range!\n", numerical_value); |
| 96 |
return 1; |
| 97 |
} |
| 98 |
|
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
static int non_neg_value_range(const char *value) { |
| 114 |
double numerical_value = strtod(value, NULL); |
| 115 |
|
| 116 |
if (numerical_value < 0 || numerical_value > pow(2, 64)) { |
| 117 |
ast_log(AST_LOG_WARNING, "Value %lf out of range!\n", numerical_value); |
| 118 |
return 1; |
| 119 |
} |
| 120 |
|
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
static int validate_metric(const char *metric) |
| 136 |
{ |
| 137 |
const char *valid_metrics[] = {"g","s","ms","c"}; |
| 138 |
int i; |
| 139 |
|
| 140 |
if (ast_strlen_zero(metric)) { |
| 141 |
ast_log(AST_LOG_ERROR, "Missing metric type argument.\n"); |
| 142 |
return 1; |
| 143 |
} |
| 144 |
|
| 145 |
for (i = 0; i < ARRAY_LEN(valid_metrics); i++) { |
| 146 |
if (!strcmp(valid_metrics[i], metric)) { |
| 147 |
return 0; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
ast_log(AST_LOG_ERROR, "Invalid metric type %s.\n", metric); |
| 152 |
|
| 153 |
return 1; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
static int validate_numeric(const char *numeric_value) { |
| 168 |
const char *num; |
| 169 |
int decimal_counter = 0; |
| 170 |
|
| 171 |
num = numeric_value; |
| 172 |
while (*num) { |
| 173 |
if (!isdigit(*num++)) { |
| 174 |
if (strstr(numeric_value, ".") != NULL && decimal_counter == 0) { |
| 175 |
decimal_counter++; |
| 176 |
continue; |
| 177 |
} |
| 178 |
ast_log(AST_LOG_ERROR, "%s is not a number!\n", numeric_value); |
| 179 |
return 1; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
static int determine_actual_value(const char *raw_value) { |
| 199 |
const char *actual_value; |
| 200 |
|
| 201 |
if ((raw_value[0] == '+') || (raw_value[0] == '-')) { |
| 202 |
actual_value = &raw_value[1]; |
| 203 |
if (ast_strlen_zero(actual_value)) { |
| 204 |
ast_log(AST_LOG_ERROR, "Value argument %s only contains a sign" |
| 205 |
" operator.\n", raw_value); |
| 206 |
return 1; |
| 207 |
} |
| 208 |
} else { |
| 209 |
actual_value = &raw_value[0]; |
| 210 |
} |
| 211 |
|
| 212 |
return validate_numeric(actual_value); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
static int validate_name(const char *name) |
| 228 |
{ |
| 229 |
if (ast_strlen_zero(name) || (strstr(name, "|") != NULL)) { |
| 230 |
ast_log(AST_LOG_ERROR, "Statistic name %s is missing or contains a pipe (|)" |
| 231 |
" character.\n", name); |
| 232 |
return 1; |
| 233 |
} |
| 234 |
|
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
static int validate_metric_type_gauge(const char *statistic_name, const char *value) { |
| 252 |
|
| 253 |
if (ast_strlen_zero(value)) { |
| 254 |
ast_log(AST_LOG_ERROR, "Missing value argument.\n"); |
| 255 |
return 1; |
| 256 |
} |
| 257 |
|
| 258 |
if (validate_name(statistic_name) || determine_actual_value(value) |
| 259 |
|| value_in_range(value)) { |
| 260 |
return 1; |
| 261 |
} |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
static int validate_metric_type_counter(const char *statistic_name, const char *value) { |
| 279 |
|
| 280 |
if (ast_strlen_zero(value)) { |
| 281 |
ast_log(AST_LOG_ERROR, "Missing value argument.\n"); |
| 282 |
return 1; |
| 283 |
} |
| 284 |
|
| 285 |
if (validate_name(statistic_name) || determine_actual_value(value) |
| 286 |
|| value_in_range(value)) { |
| 287 |
return 1; |
| 288 |
} |
| 289 |
|
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
static int validate_metric_type_timer(const char *statistic_name, const char *value) { |
| 306 |
|
| 307 |
if (ast_strlen_zero(value)) { |
| 308 |
ast_log(AST_LOG_ERROR, "Missing value argument.\n"); |
| 309 |
return 1; |
| 310 |
} |
| 311 |
|
| 312 |
if (validate_name(statistic_name) || validate_numeric(value) |
| 313 |
|| non_neg_value_range(value)) { |
| 314 |
return 1; |
| 315 |
} |
| 316 |
|
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
static int validate_metric_type_set(const char *statistic_name, const char *value) { |
| 333 |
if (ast_strlen_zero(value)) { |
| 334 |
ast_log(AST_LOG_ERROR, "Missing value argument.\n"); |
| 335 |
return 1; |
| 336 |
} |
| 337 |
|
| 338 |
if (validate_name(statistic_name)) { |
| 339 |
return 1; |
| 340 |
} |
| 341 |
|
| 342 |
if (strstr(value, "|") != NULL) { |
| 343 |
ast_log(AST_LOG_ERROR, "Pipe (|) character is not allowed for value %s" |
| 344 |
" in a set metric.\n", value); |
| 345 |
return 1; |
| 346 |
} |
| 347 |
|
| 348 |
return 0; |
| 349 |
} |
| 350 |
|
| 351 |
static int statsd_exec(struct ast_channel *chan, const char *data) |
| 352 |
{ |
| 353 |
char *stats; |
| 354 |
double numerical_rate = 1.0; |
| 355 |
|
| 356 |
AST_DECLARE_APP_ARGS(args, |
| 357 |
AST_APP_ARG(metric_type); |
| 358 |
AST_APP_ARG(statistic_name); |
| 359 |
AST_APP_ARG(value); |
| 360 |
AST_APP_ARG(sample_rate); |
| 361 |
); |
| 362 |
|
| 363 |
if (!data) { |
| 364 |
ast_log(AST_LOG_ERROR, "No parameters were provided. Correct format is " |
| 365 |
"StatsD(metric_type,statistic_name,value[,sample_rate]). Sample rate is the " |
| 366 |
"only optional parameter.\n"); |
| 367 |
return 1; |
| 368 |
} |
| 369 |
|
| 370 |
stats = ast_strdupa(data); |
| 371 |
AST_STANDARD_APP_ARGS(args, stats); |
| 372 |
|
| 373 |
if (validate_metric(args.metric_type)) { |
| 374 |
return 1; |
| 375 |
} |
| 376 |
|
| 377 |
if (!strcmp(args.metric_type, "g")) { |
| 378 |
if (validate_metric_type_gauge(args.statistic_name, args.value)) { |
| 379 |
ast_log(AST_LOG_ERROR, "Invalid input for a gauge metric.\n"); |
| 380 |
return 1; |
| 381 |
} |
| 382 |
} |
| 383 |
else if (!strcmp(args.metric_type, "c")) { |
| 384 |
if (validate_metric_type_counter(args.statistic_name, args.value)) { |
| 385 |
ast_log(AST_LOG_ERROR, "Invalid input for a counter metric.\n"); |
| 386 |
return 1; |
| 387 |
} |
| 388 |
} |
| 389 |
else if (!strcmp(args.metric_type, "ms")) { |
| 390 |
if (validate_metric_type_timer(args.statistic_name, args.value)) { |
| 391 |
ast_log(AST_LOG_ERROR, "Invalid input for a timer metric.\n"); |
| 392 |
return 1; |
| 393 |
} |
| 394 |
} |
| 395 |
else if (!strcmp(args.metric_type, "s")) { |
| 396 |
if (validate_metric_type_set(args.statistic_name, args.value)) { |
| 397 |
ast_log(AST_LOG_ERROR, "Invalid input for a set metric.\n"); |
| 398 |
return 1; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
if (args.sample_rate) { |
| 403 |
|
| 404 |
if (validate_numeric(args.sample_rate)) { |
| 405 |
return 1; |
| 406 |
} |
| 407 |
|
| 408 |
numerical_rate = strtod(args.sample_rate, NULL); |
| 409 |
} |
| 410 |
|
| 411 |
ast_statsd_log_string(args.statistic_name, args.metric_type, args.value, |
| 412 |
numerical_rate); |
| 413 |
|
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
static int unload_module(void) |
| 418 |
{ |
| 419 |
return ast_unregister_application(app); |
| 420 |
} |
| 421 |
|
| 422 |
static int load_module(void) |
| 423 |
{ |
| 424 |
return ast_register_application_xml(app, statsd_exec); |
| 425 |
} |
| 426 |
|
| 427 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "StatsD Dialplan Application", |
| 428 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 429 |
.load = load_module, |
| 430 |
.unload = unload_module, |
| 431 |
.requires = "res_statsd", |
| 432 |
); |