Asterisk - The Open Source Telephony Project GIT-master-a358458
Functions | Variables
app_statsd.c File Reference
#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"
Include dependency graph for app_statsd.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int determine_actual_value (const char *raw_value)
 Determines the actual value of a number by looking for a leading + or -. More...
 
static int load_module (void)
 
static int non_neg_value_range (const char *value)
 Check to ensure the value is within the allowed range. More...
 
static int statsd_exec (struct ast_channel *chan, const char *data)
 
static int unload_module (void)
 
static int validate_metric (const char *metric)
 Check to ensure the metric type is a valid metric type. More...
 
static int validate_metric_type_counter (const char *statistic_name, const char *value)
 Calls the appropriate functions to validate a counter metric. More...
 
static int validate_metric_type_gauge (const char *statistic_name, const char *value)
 Calls the appropriate functions to validate a gauge metric. More...
 
static int validate_metric_type_set (const char *statistic_name, const char *value)
 Calls the appropriate functions to validate a set metric. More...
 
static int validate_metric_type_timer (const char *statistic_name, const char *value)
 Calls the appropriate functions to validate a timer metric. More...
 
static int validate_name (const char *name)
 Check to ensure the statistic name is valid. More...
 
static int validate_numeric (const char *numeric_value)
 Check to ensure that a numeric value is valid. More...
 
static int value_in_range (const char *value)
 Check to ensure the value is within the allowed range. More...
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "StatsD Dialplan Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .requires = "res_statsd", }
 
static const char app [] = "StatsD"
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 428 of file app_statsd.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 428 of file app_statsd.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 428 of file app_statsd.c.

◆ determine_actual_value()

static int determine_actual_value ( const char *  raw_value)
static

Determines the actual value of a number by looking for a leading + or -.

Parameters
raw_valueThe 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.

Return values
zeroon success.
1on error.

Definition at line 194 of file app_statsd.c.

194 {
195 const char *actual_value;
196
197 if ((raw_value[0] == '+') || (raw_value[0] == '-')) {
198 actual_value = &raw_value[1];
199 if (ast_strlen_zero(actual_value)) {
200 ast_log(AST_LOG_ERROR, "Value argument %s only contains a sign"
201 " operator.\n", raw_value);
202 return 1;
203 }
204 } else {
205 actual_value = &raw_value[0];
206 }
207
208 return validate_numeric(actual_value);
209}
static int validate_numeric(const char *numeric_value)
Check to ensure that a numeric value is valid.
Definition: app_statsd.c:163
#define ast_log
Definition: astobj2.c:42
#define AST_LOG_ERROR
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65

References ast_log, AST_LOG_ERROR, ast_strlen_zero(), and validate_numeric().

Referenced by validate_metric_type_counter(), and validate_metric_type_gauge().

◆ load_module()

static int load_module ( void  )
static

Definition at line 418 of file app_statsd.c.

419{
421}
static int statsd_exec(struct ast_channel *chan, const char *data)
Definition: app_statsd.c:347
static const char app[]
Definition: app_statsd.c:74
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626

References app, ast_register_application_xml, and statsd_exec().

◆ non_neg_value_range()

static int non_neg_value_range ( const char *  value)
static

Check to ensure the value is within the allowed range.

Parameters
valueThe value of the statistic to be sent to StatsD.

This function checks to see if the value given to the StatsD dailplan application is within the allowed range of [0, 2^64] as specified by StatsD.

Return values
zeroon success.
1on error.

Definition at line 109 of file app_statsd.c.

109 {
110 double numerical_value = strtod(value, NULL);
111
112 if (numerical_value < 0 || numerical_value > pow(2, 64)) {
113 ast_log(AST_LOG_WARNING, "Value %lf out of range!\n", numerical_value);
114 return 1;
115 }
116
117 return 0;
118}
#define AST_LOG_WARNING
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37

References ast_log, AST_LOG_WARNING, NULL, and value.

Referenced by validate_metric_type_timer().

◆ statsd_exec()

static int statsd_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 347 of file app_statsd.c.

348{
349 char *stats;
350 double numerical_rate = 1.0;
351
353 AST_APP_ARG(metric_type);
354 AST_APP_ARG(statistic_name);
356 AST_APP_ARG(sample_rate);
357 );
358
359 if (!data) {
360 ast_log(AST_LOG_ERROR, "No parameters were provided. Correct format is "
361 "StatsD(metric_type,statistic_name,value[,sample_rate]). Sample rate is the "
362 "only optional parameter.\n");
363 return 1;
364 }
365
366 stats = ast_strdupa(data);
368
369 if (validate_metric(args.metric_type)) {
370 return 1;
371 }
372
373 if (!strcmp(args.metric_type, "g")) {
374 if (validate_metric_type_gauge(args.statistic_name, args.value)) {
375 ast_log(AST_LOG_ERROR, "Invalid input for a gauge metric.\n");
376 return 1;
377 }
378 }
379 else if (!strcmp(args.metric_type, "c")) {
380 if (validate_metric_type_counter(args.statistic_name, args.value)) {
381 ast_log(AST_LOG_ERROR, "Invalid input for a counter metric.\n");
382 return 1;
383 }
384 }
385 else if (!strcmp(args.metric_type, "ms")) {
386 if (validate_metric_type_timer(args.statistic_name, args.value)) {
387 ast_log(AST_LOG_ERROR, "Invalid input for a timer metric.\n");
388 return 1;
389 }
390 }
391 else if (!strcmp(args.metric_type, "s")) {
392 if (validate_metric_type_set(args.statistic_name, args.value)) {
393 ast_log(AST_LOG_ERROR, "Invalid input for a set metric.\n");
394 return 1;
395 }
396 }
397
398 if (args.sample_rate) {
399
400 if (validate_numeric(args.sample_rate)) {
401 return 1;
402 }
403
404 numerical_rate = strtod(args.sample_rate, NULL);
405 }
406
407 ast_statsd_log_string(args.statistic_name, args.metric_type, args.value,
408 numerical_rate);
409
410 return 0;
411}
static int validate_metric_type_gauge(const char *statistic_name, const char *value)
Calls the appropriate functions to validate a gauge metric.
Definition: app_statsd.c:247
static int validate_metric_type_set(const char *statistic_name, const char *value)
Calls the appropriate functions to validate a set metric.
Definition: app_statsd.c:328
static int validate_metric_type_timer(const char *statistic_name, const char *value)
Calls the appropriate functions to validate a timer metric.
Definition: app_statsd.c:301
static int validate_metric(const char *metric)
Check to ensure the metric type is a valid metric type.
Definition: app_statsd.c:131
static int validate_metric_type_counter(const char *statistic_name, const char *value)
Calls the appropriate functions to validate a counter metric.
Definition: app_statsd.c:274
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
void ast_statsd_log_string(const char *metric_name, const char *metric_type, const char *value, double sample_rate)
Send a stat to the configured statsd server.
Definition: res_statsd.c:117
const char * args

References args, AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log, AST_LOG_ERROR, AST_STANDARD_APP_ARGS, ast_statsd_log_string(), ast_strdupa, NULL, validate_metric(), validate_metric_type_counter(), validate_metric_type_gauge(), validate_metric_type_set(), validate_metric_type_timer(), validate_numeric(), and value.

Referenced by load_module().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 413 of file app_statsd.c.

414{
416}
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392

References app, and ast_unregister_application().

◆ validate_metric()

static int validate_metric ( const char *  metric)
static

Check to ensure the metric type is a valid metric type.

Parameters
metricThe 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.

Return values
zeroon success.
1on error.

Definition at line 131 of file app_statsd.c.

132{
133 const char *valid_metrics[] = {"g","s","ms","c"};
134 int i;
135
136 if (ast_strlen_zero(metric)) {
137 ast_log(AST_LOG_ERROR, "Missing metric type argument.\n");
138 return 1;
139 }
140
141 for (i = 0; i < ARRAY_LEN(valid_metrics); i++) {
142 if (!strcmp(valid_metrics[i], metric)) {
143 return 0;
144 }
145 }
146
147 ast_log(AST_LOG_ERROR, "Invalid metric type %s.\n", metric);
148
149 return 1;
150}
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, ast_log, AST_LOG_ERROR, and ast_strlen_zero().

Referenced by statsd_exec().

◆ validate_metric_type_counter()

static int validate_metric_type_counter ( const char *  statistic_name,
const char *  value 
)
static

Calls the appropriate functions to validate a counter metric.

Parameters
statistic_nameThe statistic name to be sent to StatsD.
valueThe 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.

Return values
zeroon success.
1on error.

Definition at line 274 of file app_statsd.c.

274 {
275
276 if (ast_strlen_zero(value)) {
277 ast_log(AST_LOG_ERROR, "Missing value argument.\n");
278 return 1;
279 }
280
281 if (validate_name(statistic_name) || determine_actual_value(value)
282 || value_in_range(value)) {
283 return 1;
284 }
285
286 return 0;
287}
static int determine_actual_value(const char *raw_value)
Determines the actual value of a number by looking for a leading + or -.
Definition: app_statsd.c:194
static int validate_name(const char *name)
Check to ensure the statistic name is valid.
Definition: app_statsd.c:223
static int value_in_range(const char *value)
Check to ensure the value is within the allowed range.
Definition: app_statsd.c:87

References ast_log, AST_LOG_ERROR, ast_strlen_zero(), determine_actual_value(), validate_name(), value, and value_in_range().

Referenced by statsd_exec().

◆ validate_metric_type_gauge()

static int validate_metric_type_gauge ( const char *  statistic_name,
const char *  value 
)
static

Calls the appropriate functions to validate a gauge metric.

Parameters
statistic_nameThe statistic name to be sent to StatsD.
valueThe 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.

Return values
zeroon success.
1on error.

Definition at line 247 of file app_statsd.c.

247 {
248
249 if (ast_strlen_zero(value)) {
250 ast_log(AST_LOG_ERROR, "Missing value argument.\n");
251 return 1;
252 }
253
254 if (validate_name(statistic_name) || determine_actual_value(value)
255 || value_in_range(value)) {
256 return 1;
257 }
258
259 return 0;
260}

References ast_log, AST_LOG_ERROR, ast_strlen_zero(), determine_actual_value(), validate_name(), value, and value_in_range().

Referenced by statsd_exec().

◆ validate_metric_type_set()

static int validate_metric_type_set ( const char *  statistic_name,
const char *  value 
)
static

Calls the appropriate functions to validate a set metric.

Parameters
statistic_nameThe statistic name to be sent to StatsD.
valueThe 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.

Return values
zeroon success.
1on error.

Definition at line 328 of file app_statsd.c.

328 {
329 if (ast_strlen_zero(value)) {
330 ast_log(AST_LOG_ERROR, "Missing value argument.\n");
331 return 1;
332 }
333
334 if (validate_name(statistic_name)) {
335 return 1;
336 }
337
338 if (strstr(value, "|") != NULL) {
339 ast_log(AST_LOG_ERROR, "Pipe (|) character is not allowed for value %s"
340 " in a set metric.\n", value);
341 return 1;
342 }
343
344 return 0;
345}

References ast_log, AST_LOG_ERROR, ast_strlen_zero(), NULL, validate_name(), and value.

Referenced by statsd_exec().

◆ validate_metric_type_timer()

static int validate_metric_type_timer ( const char *  statistic_name,
const char *  value 
)
static

Calls the appropriate functions to validate a timer metric.

Parameters
statistic_nameThe statistic name to be sent to StatsD.
valueThe 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.

Return values
zeroon success.
1on error.

Definition at line 301 of file app_statsd.c.

301 {
302
303 if (ast_strlen_zero(value)) {
304 ast_log(AST_LOG_ERROR, "Missing value argument.\n");
305 return 1;
306 }
307
308 if (validate_name(statistic_name) || validate_numeric(value)
310 return 1;
311 }
312
313 return 0;
314}
static int non_neg_value_range(const char *value)
Check to ensure the value is within the allowed range.
Definition: app_statsd.c:109

References ast_log, AST_LOG_ERROR, ast_strlen_zero(), non_neg_value_range(), validate_name(), validate_numeric(), and value.

Referenced by statsd_exec().

◆ validate_name()

static int validate_name ( const char *  name)
static

Check to ensure the statistic name is valid.

Parameters
nameThe 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.

Return values
zeroon success.
1on error.

Definition at line 223 of file app_statsd.c.

224{
225 if (ast_strlen_zero(name) || (strstr(name, "|") != NULL)) {
226 ast_log(AST_LOG_ERROR, "Statistic name %s is missing or contains a pipe (|)"
227 " character.\n", name);
228 return 1;
229 }
230
231 return 0;
232}
static const char name[]
Definition: format_mp3.c:68

References ast_log, AST_LOG_ERROR, ast_strlen_zero(), name, and NULL.

Referenced by validate_metric_type_counter(), validate_metric_type_gauge(), validate_metric_type_set(), and validate_metric_type_timer().

◆ validate_numeric()

static int validate_numeric ( const char *  numeric_value)
static

Check to ensure that a numeric value is valid.

Parameters
numeric_valueThe 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.

Return values
zeroon success.
1on error.

Definition at line 163 of file app_statsd.c.

163 {
164 const char *num;
165 int decimal_counter = 0;
166
167 num = numeric_value;
168 while (*num) {
169 if (!isdigit(*num++)) {
170 if (strstr(numeric_value, ".") != NULL && decimal_counter == 0) {
171 decimal_counter++;
172 continue;
173 }
174 ast_log(AST_LOG_ERROR, "%s is not a number!\n", numeric_value);
175 return 1;
176 }
177 }
178
179 return 0;
180}

References ast_log, AST_LOG_ERROR, and NULL.

Referenced by determine_actual_value(), statsd_exec(), and validate_metric_type_timer().

◆ value_in_range()

static int value_in_range ( const char *  value)
static

Check to ensure the value is within the allowed range.

Parameters
valueThe value of the statistic to be sent to StatsD.

This function checks to see if the value given to the StatsD dailplan application is within the allowed range of [-2^63, 2^63] as specified by StatsD.

Return values
zeroon success.
1on error.

Definition at line 87 of file app_statsd.c.

87 {
88 double numerical_value = strtod(value, NULL);
89
90 if (numerical_value < pow(-2, 63) || numerical_value > pow(2, 63)) {
91 ast_log(AST_LOG_WARNING, "Value %lf out of range!\n", numerical_value);
92 return 1;
93 }
94
95 return 0;
96}

References ast_log, AST_LOG_WARNING, NULL, and value.

Referenced by validate_metric_type_counter(), and validate_metric_type_gauge().

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "StatsD Dialplan Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .requires = "res_statsd", }
static

Definition at line 428 of file app_statsd.c.

◆ app

const char app[] = "StatsD"
static

Definition at line 74 of file app_statsd.c.

Referenced by load_module(), and unload_module().

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 428 of file app_statsd.c.