Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
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 -.
 
static int load_module (void)
 
static int non_neg_value_range (const char *value)
 Check to ensure the value is within the allowed range.
 
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.
 
static int validate_metric_type_counter (const char *statistic_name, const char *value)
 Calls the appropriate functions to validate a counter metric.
 
static int validate_metric_type_gauge (const char *statistic_name, const char *value)
 Calls the appropriate functions to validate a gauge metric.
 
static int validate_metric_type_set (const char *statistic_name, const char *value)
 Calls the appropriate functions to validate a set metric.
 
static int validate_metric_type_timer (const char *statistic_name, const char *value)
 Calls the appropriate functions to validate a timer metric.
 
static int validate_name (const char *name)
 Check to ensure the statistic name is valid.
 
static int validate_numeric (const char *numeric_value)
 Check to ensure that a numeric value is valid.
 
static int value_in_range (const char *value)
 Check to ensure the value is within the allowed range.
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "StatsD Dialplan Application" , .key = ASTERISK_GPL_KEY , .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 432 of file app_statsd.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 432 of file app_statsd.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 432 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 198 of file app_statsd.c.

198 {
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}
static int validate_numeric(const char *numeric_value)
Check to ensure that a numeric value is valid.
Definition app_statsd.c:167
#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 422 of file app_statsd.c.

423{
425}
static int statsd_exec(struct ast_channel *chan, const char *data)
Definition app_statsd.c:351
static const char app[]
Definition app_statsd.c:78
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition module.h:640

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 113 of file app_statsd.c.

113 {
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}
#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 351 of file app_statsd.c.

352{
353 char *stats;
354 double numerical_rate = 1.0;
355
357 AST_APP_ARG(metric_type);
358 AST_APP_ARG(statistic_name);
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);
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}
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:251
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:332
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:305
static int validate_metric(const char *metric)
Check to ensure the metric type is a valid metric type.
Definition app_statsd.c:135
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:278
#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.
static struct @519 args
void AST_OPTIONAL_API_NAME() 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:136

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 417 of file app_statsd.c.

418{
420}
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 135 of file app_statsd.c.

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}
#define ARRAY_LEN(a)
Definition utils.h:706

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 278 of file app_statsd.c.

278 {
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}
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:198
static int validate_name(const char *name)
Check to ensure the statistic name is valid.
Definition app_statsd.c:227
static int value_in_range(const char *value)
Check to ensure the value is within the allowed range.
Definition app_statsd.c:91

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 251 of file app_statsd.c.

251 {
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}

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 332 of file app_statsd.c.

332 {
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}

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 305 of file app_statsd.c.

305 {
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)
314 return 1;
315 }
316
317 return 0;
318}
static int non_neg_value_range(const char *value)
Check to ensure the value is within the allowed range.
Definition app_statsd.c:113

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 227 of file app_statsd.c.

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}
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 167 of file app_statsd.c.

167 {
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}

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 91 of file app_statsd.c.

91 {
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}

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 = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .requires = "res_statsd", }
static

Definition at line 432 of file app_statsd.c.

◆ app

const char app[] = "StatsD"
static

Definition at line 78 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 432 of file app_statsd.c.