Skip to content
Snippets Groups Projects
app_statsd.c 11.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    /*
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 2015, Digium, Inc.
     *
     * Tyler Cambron <tcambron@digium.com>
     *
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
     * This program is free software, distributed under the terms of
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
     /*** MODULEINFO
    	<depend>res_statsd</depend>
    	<defaultenabled>no</defaultenabled>
    	<support_level>extended</support_level>
     ***/
    
    #include "asterisk.h"
    
    #include <math.h>
    
    #include "asterisk/module.h"
    #include "asterisk/logger.h"
    #include "asterisk/app.h"
    #include "asterisk/pbx.h"
    #include "asterisk/strings.h"
    #include "asterisk/statsd.h"
    
    /*** DOCUMENTATION
    	<application name="StatsD" language="en_US">
    		<synopsis>
    			Allow statistics to be passed to the StatsD server from the dialplan.
    		</synopsis>
    		<syntax>
    			<parameter name="metric_type" required="true">
    				<para>The metric type to be sent to StatsD. Valid metric types
    				are 'g' for gauge, 'c' for counter, 'ms' for timer, and 's' for
    				sets.</para>
    			</parameter>
    			<parameter name="statistic_name" required="true">
    				<para>The name of the variable to be sent to StatsD. Statistic
    				names cannot contain the pipe (|) character.</para>
    			</parameter>
    			<parameter name="value" required="true">
    				<para>The value of the variable to be sent to StatsD. Values
    				must be numeric. Values for gauge and counter metrics can be
    				sent with a '+' or '-' to update a value after the value has
    				been initialized. Only counters can be initialized as negative.
    				Sets can send a string as the value parameter, but the string
    				cannot contain the pipe character.</para>
    			</parameter>
    			<parameter name="sample_rate">
    				<para>The value of the sample rate to be sent to StatsD. Sample
    				rates less than or equal to 0 will never be sent and sample rates
    				greater than or equal to 1 will always be sent. Any rate
    				between 1 and 0 will be compared to a randomly generated value,
    				and if it is greater than the random value, it will be sent.</para>
    			</parameter>
    		</syntax>
    		<description>
    			<para>This dialplan application sends statistics to the StatsD
    			server specified inside of <literal>statsd.conf</literal>.</para>
    		</description>
    	</application>
     ***/
    
    static const char app[] = "StatsD";
    
    /*!
     * \brief Check to ensure the value is within the allowed range.
     *
     * \param value The value of the statistic to be sent to StatsD.
     *
     * This function checks to see if the value given to the StatsD daialplan
     * application is within the allowed range of [-2^63, 2^63] as specified by StatsD.
     *
     * \retval zero on success.
     * \retval 1 on error.
     */
    static int value_in_range(const char *value) {
    	double numerical_value = strtod(value, NULL);
    
    	if (numerical_value < pow(-2, 63) || numerical_value > pow(2, 63)) {
    		ast_log(AST_LOG_WARNING, "Value %lf out of range!\n", numerical_value);
    		return 1;
    	}
    
    	return 0;
    }
    
    /*!
     * \brief Check to ensure the value is within the allowed range.
     *
     * \param value The value of the statistic to be sent to StatsD.
     *
     * This function checks to see if the value given to the StatsD daialplan
     * application is within the allowed range of [0, 2^64] as specified by StatsD.
     *
     * \retval zero on success.
     * \retval 1 on error.
     */
    static int non_neg_value_range(const char *value) {
    	double numerical_value = strtod(value, NULL);
    
    	if (numerical_value < 0 || numerical_value > pow(2, 64)) {
    		ast_log(AST_LOG_WARNING, "Value %lf out of range!\n", numerical_value);
    		return 1;
    	}
    
    	return 0;
    }
    
    /*!
     * \brief Check to ensure the metric type is a valid metric type.
     *
     * \param metric The metric type to be sent to StatsD.
     *
     * This function checks to see if the metric type given to the StatsD dialplan
     * is a valid metric type. Metric types are determined by StatsD.
     *
     * \retval zero on success.
     * \retval 1 on error.
     */
    static int validate_metric(const char *metric)
    {
    	const char *valid_metrics[] = {"g","s","ms","c"};
    	int i;
    
    	if (ast_strlen_zero(metric)) {
    		ast_log(AST_LOG_ERROR, "Missing metric type argument.\n");
    		return 1;
    	}
    
    	for (i = 0; i < ARRAY_LEN(valid_metrics); i++) {
    		if (!strcmp(valid_metrics[i], metric)) {
    			return 0;
    		}
    	}
    
    	ast_log(AST_LOG_ERROR, "Invalid metric type %s.\n", metric);
    
    	return 1;
    }
    
    /*!
     * \brief Check to ensure that a numeric value is valid.
     *
     * \param numeric_value The numeric value to be sent to StatsD.
     *
     * This function checks to see if a number to be sent to StatsD is actually
     * a valid number. One decimal is allowed.
     *
     * \retval zero on success.
     * \retval 1 on error.
     */
    static int validate_numeric(const char *numeric_value) {
    	const char *num;
    	int decimal_counter = 0;
    
    	num = numeric_value;
    	while (*num) {
    		if (!isdigit(*num++)) {
    			if (strstr(numeric_value, ".") != NULL && decimal_counter == 0) {
    				decimal_counter++;
    				continue;
    			}
    			ast_log(AST_LOG_ERROR, "%s is not a number!\n", numeric_value);
    			return 1;
    		}
    	}
    
    	return 0;
    }
    
    /*!
     * \brief Determines the actual value of a number by looking for a leading + or -.
     *
     * \param raw_value The entire numeric string to be sent to StatsD.
     *
     * This function checks to see if the numeric string contains valid characters
     * and then isolates the actual number to be sent for validation. Returns the
     * result of the numeric validation.
     *
     * \retval zero on success.
     * \retval 1 on error.
     */
    static int determine_actual_value(const char *raw_value) {
    	const char *actual_value;
    
    	if ((raw_value[0] == '+') || (raw_value[0] == '-')) {
    		actual_value = &raw_value[1];
    		if (ast_strlen_zero(actual_value)) {
    			ast_log(AST_LOG_ERROR, "Value argument %s only contains a sign"
    				" operator.\n", raw_value);
    			return 1;
    		}
    	} else {
    		actual_value = &raw_value[0];
    	}
    
    	return validate_numeric(actual_value);
    }
    
    /*!
     * \brief Check to ensure the statistic name is valid.
     *
     * \param name The variable name to be sent to StatsD.
     *
     * This function checks to see if the statistic name given to the StatsD
     * dialplan application is valid by ensuring that the name does not have any
     * invalid characters.
     *
     * \retval zero on success.
     * \retval 1 on error.
     */
    static int validate_name(const char *name)
    {
    	if (ast_strlen_zero(name) || (strstr(name, "|") != NULL)) {
    		ast_log(AST_LOG_ERROR, "Statistic name %s is missing or contains a pipe (|)"
    			" character.\n", name);
    		return 1;
    	}
    
    	return 0;
    }
    
    
    /*!
     * \brief Calls the appropriate functions to validate a gauge metric.
     *
     * \param statistic_name The statistic name to be sent to StatsD.
     * \param value The value to be sent to StatsD.
     *
     * This function calls other validating functions to correctly validate each
     * input based on allowable input for a gauge metric.
     *
     * \retval zero on success.
     * \retval 1 on error.
     */
    static int validate_metric_type_gauge(const char *statistic_name, const char *value) {
    
    	if (ast_strlen_zero(value)) {
    		ast_log(AST_LOG_ERROR, "Missing value argument.\n");
    		return 1;
    	}
    
    	if (validate_name(statistic_name) || determine_actual_value(value)
    		|| value_in_range(value)) {
    		return 1;
    	}
    
    	return 0;
    }
    
    /*!
     * \brief Calls the appropriate functions to validate a counter metric.
     *
     * \param statistic_name The statistic name to be sent to StatsD.
     * \param value The value to be sent to StatsD.
     *
     * This function calls other validating functions to correctly validate each
     * input based on allowable input for a counter metric.
     *
     * \retval zero on success.
     * \retval 1 on error.
     */
    static int validate_metric_type_counter(const char *statistic_name, const char *value) {
    
    	if (ast_strlen_zero(value)) {
    		ast_log(AST_LOG_ERROR, "Missing value argument.\n");
    		return 1;
    	}
    
    	if (validate_name(statistic_name) || determine_actual_value(value)
    		|| value_in_range(value)) {
    		return 1;
    	}
    
    	return 0;
    }
    
    /*!
     * \brief Calls the appropriate functions to validate a timer metric.
     *
     * \param statistic_name The statistic name to be sent to StatsD.
     * \param value The value to be sent to StatsD.
     *
     * This function calls other validating functions to correctly validate each
     * input based on allowable input for a timer metric.
     *
     * \retval zero on success.
     * \retval 1 on error.
     */
    static int validate_metric_type_timer(const char *statistic_name, const char *value) {
    
    	if (ast_strlen_zero(value)) {
    		ast_log(AST_LOG_ERROR, "Missing value argument.\n");
    		return 1;
    	}
    
    	if (validate_name(statistic_name) || validate_numeric(value)
    		|| non_neg_value_range(value)) {
    		return 1;
    	}
    
    	return 0;
    }
    
    /*!
     * \brief Calls the appropriate functions to validate a set metric.
     *
     * \param statistic_name The statistic name to be sent to StatsD.
     * \param value The value to be sent to StatsD.
     *
     * This function calls other validating functions to correctly validate each
     * input based on allowable input for a set metric.
     *
     * \retval zero on success.
     * \retval 1 on error.
     */
    static int validate_metric_type_set(const char *statistic_name, const char *value) {
    	if (ast_strlen_zero(value)) {
    		ast_log(AST_LOG_ERROR, "Missing value argument.\n");
    		return 1;
    	}
    
    	if (validate_name(statistic_name)) {
    		return 1;
    	}
    
    	if (strstr(value, "|") != NULL) {
    		ast_log(AST_LOG_ERROR, "Pipe (|) character is not allowed for value %s"
    			" in a set metric.\n", value);
    		return 1;
    	}
    
    	return 0;
    }
    
    static int statsd_exec(struct ast_channel *chan, const char *data)
    {
    	char *stats;
    	double numerical_rate = 1.0;
    
    	AST_DECLARE_APP_ARGS(args,
    			AST_APP_ARG(metric_type);
    			AST_APP_ARG(statistic_name);
    			AST_APP_ARG(value);
    			AST_APP_ARG(sample_rate);
    	);
    
    	if (!data) {
    		ast_log(AST_LOG_ERROR, "No parameters were provided. Correct format is "
    			"StatsD(metric_type,statistic_name,value[,sample_rate]). Sample rate is the "
    			"only optional parameter.\n");
    		return 1;
    	}
    
    	stats = ast_strdupa(data);
    	AST_STANDARD_APP_ARGS(args, stats);
    
    	if (validate_metric(args.metric_type)) {
    		return 1;
    	}
    
    	if (!strcmp(args.metric_type, "g")) {
    		if (validate_metric_type_gauge(args.statistic_name, args.value)) {
    			ast_log(AST_LOG_ERROR, "Invalid input for a gauge metric.\n");
    			return 1;
    		}
    	}
    	else if (!strcmp(args.metric_type, "c")) {
    		if (validate_metric_type_counter(args.statistic_name, args.value)) {
    			ast_log(AST_LOG_ERROR, "Invalid input for a counter metric.\n");
    			return 1;
    		}
    	}
    	else if (!strcmp(args.metric_type, "ms")) {
    		if (validate_metric_type_timer(args.statistic_name, args.value)) {
    			ast_log(AST_LOG_ERROR, "Invalid input for a timer metric.\n");
    			return 1;
    		}
    	}
    	else if (!strcmp(args.metric_type, "s")) {
    		if (validate_metric_type_set(args.statistic_name, args.value)) {
    			ast_log(AST_LOG_ERROR, "Invalid input for a set metric.\n");
    			return 1;
    		}
    	}
    
    	if (args.sample_rate) {
    
    		if (validate_numeric(args.sample_rate)) {
    			return 1;
    		}
    
    		numerical_rate = strtod(args.sample_rate, NULL);
    	}
    
    	ast_statsd_log_string(args.statistic_name, args.metric_type, args.value,
    		numerical_rate);
    
    	return 0;
    }
    
    static int unload_module(void)
    {
    	return ast_unregister_application(app);
    }
    
    static int load_module(void)
    {
    	return ast_register_application_xml(app, statsd_exec);
    }
    
    
    AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "StatsD Dialplan Application",
    	.support_level = AST_MODULE_SUPPORT_EXTENDED,
    	.load = load_module,
    	.unload = unload_module,
    	.requires = "res_statsd",
    );