Asterisk - The Open Source Telephony Project  18.5.0
Functions | Variables
res_endpoint_stats.c File Reference
#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/stasis_endpoints.h"
#include "asterisk/stasis_message_router.h"
#include "asterisk/statsd.h"
Include dependency graph for res_endpoint_stats.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 void cache_update_cb (void *data, struct stasis_subscription *sub, struct stasis_message *message)
 
static int dump_cache_load (void *obj, void *arg, int flags)
 
static int dump_cache_unload (void *obj, void *arg, int flags)
 
static void handle_endpoint_update (struct ast_endpoint_snapshot *old_snapshot, struct ast_endpoint_snapshot *new_snapshot)
 
static int load_module (void)
 
static int unload_module (void)
 
static void update_endpoint_state (struct ast_endpoint_snapshot *snapshot, const char *delta)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Endpoint statistics" , .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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .requires = "res_statsd" }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct stasis_message_routerrouter
 Statsd Endpoint stats. More...
 

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 155 of file res_endpoint_stats.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 155 of file res_endpoint_stats.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 155 of file res_endpoint_stats.c.

◆ cache_update_cb()

static void cache_update_cb ( void *  data,
struct stasis_subscription sub,
struct stasis_message message 
)
static

Definition at line 78 of file res_endpoint_stats.c.

References ast_endpoint_snapshot_type(), handle_endpoint_update(), stasis_cache_update::new_snapshot, stasis_cache_update::old_snapshot, stasis_message_data(), stasis_cache_update::type, and update().

Referenced by load_module().

80 {
82  struct ast_endpoint_snapshot *old_snapshot;
83  struct ast_endpoint_snapshot *new_snapshot;
84 
85  if (ast_endpoint_snapshot_type() != update->type) {
86  return;
87  }
88 
89  old_snapshot = stasis_message_data(update->old_snapshot);
90  new_snapshot = stasis_message_data(update->new_snapshot);
91 
92  handle_endpoint_update(old_snapshot, new_snapshot);
93 }
struct stasis_message * old_snapshot
Old value from the cache.
Definition: stasis.h:971
static void update(int code_size, int y, int wi, int fi, int dq, int sr, int dqsez, struct g726_state *state_ptr)
Definition: codec_g726.c:367
struct stasis_message_type * ast_endpoint_snapshot_type(void)
Message type for ast_endpoint_snapshot.
Cache update message.
Definition: stasis.h:967
A snapshot of an endpoint's state.
struct stasis_message * new_snapshot
New value.
Definition: stasis.h:973
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
struct stasis_message_type * type
Convenience reference to snapshot type.
Definition: stasis.h:969
static void handle_endpoint_update(struct ast_endpoint_snapshot *old_snapshot, struct ast_endpoint_snapshot *new_snapshot)

◆ dump_cache_load()

static int dump_cache_load ( void *  obj,
void *  arg,
int  flags 
)
static

Definition at line 95 of file res_endpoint_stats.c.

References handle_endpoint_update(), NULL, and stasis_message_data().

Referenced by load_module().

96 {
97  struct stasis_message *msg = obj;
98  struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
99 
100  handle_endpoint_update(NULL, snapshot);
101 
102  return 0;
103 }
#define NULL
Definition: resample.c:96
A snapshot of an endpoint's state.
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
static void handle_endpoint_update(struct ast_endpoint_snapshot *old_snapshot, struct ast_endpoint_snapshot *new_snapshot)

◆ dump_cache_unload()

static int dump_cache_unload ( void *  obj,
void *  arg,
int  flags 
)
static

Definition at line 105 of file res_endpoint_stats.c.

References handle_endpoint_update(), NULL, and stasis_message_data().

Referenced by unload_module().

106 {
107  struct stasis_message *msg = obj;
108  struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
109 
110  handle_endpoint_update(snapshot, NULL);
111 
112  return 0;
113 }
#define NULL
Definition: resample.c:96
A snapshot of an endpoint's state.
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
static void handle_endpoint_update(struct ast_endpoint_snapshot *old_snapshot, struct ast_endpoint_snapshot *new_snapshot)

◆ handle_endpoint_update()

static void handle_endpoint_update ( struct ast_endpoint_snapshot old_snapshot,
struct ast_endpoint_snapshot new_snapshot 
)
static

Definition at line 60 of file res_endpoint_stats.c.

References AST_STATSD_GAUGE, ast_statsd_log_full_va(), ast_statsd_log_string(), ast_endpoint_snapshot::num_channels, ast_endpoint_snapshot::resource, ast_endpoint_snapshot::state, ast_endpoint_snapshot::tech, and update_endpoint_state().

Referenced by cache_update_cb(), dump_cache_load(), and dump_cache_unload().

61 {
62  if (!old_snapshot && new_snapshot) {
63  ast_statsd_log_string("endpoints.count", AST_STATSD_GAUGE, "+1", 1.0);
64  update_endpoint_state(new_snapshot, "+1");
65  } else if (old_snapshot && !new_snapshot) {
66  ast_statsd_log_string("endpoints.count", AST_STATSD_GAUGE, "-1", 1.0);
67  update_endpoint_state(old_snapshot, "-1");
68  } else {
69  if (old_snapshot->state != new_snapshot->state) {
70  update_endpoint_state(old_snapshot, "-1");
71  update_endpoint_state(new_snapshot, "+1");
72  }
73  ast_statsd_log_full_va("endpoints.%s.%s.channels", AST_STATSD_GAUGE, new_snapshot->num_channels, 1.0,
74  new_snapshot->tech, new_snapshot->resource);
75  }
76 }
void ast_statsd_log_full_va(const char *metric_name, const char *metric_type, intmax_t value, double sample_rate,...)
Send a stat to the configured statsd server.
Definition: res_statsd.c:199
static void update_endpoint_state(struct ast_endpoint_snapshot *snapshot, const char *delta)
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:111
const ast_string_field resource
const ast_string_field tech
#define AST_STATSD_GAUGE
Support for publishing to a statsd server.
Definition: statsd.h:32
enum ast_endpoint_state state

◆ load_module()

static int load_module ( void  )
static

Definition at line 115 of file res_endpoint_stats.c.

References ao2_callback, ao2_ref, ast_endpoint_cache(), ast_endpoint_snapshot_type(), ast_endpoint_topic_all_cached(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, cache_update_cb(), dump_cache_load(), endpoints, NULL, OBJ_MULTIPLE, OBJ_NODATA, OBJ_NOLOCK, stasis_cache_dump(), stasis_cache_update_type(), stasis_message_router_add(), and stasis_message_router_create.

Referenced by unload_module().

116 {
117  struct ao2_container *endpoints;
118 
120  if (!router) {
122  }
124 
126  if (endpoints) {
128  ao2_ref(endpoints, -1);
129  }
130 
132 }
int stasis_message_router_add(struct stasis_message_router *router, struct stasis_message_type *message_type, stasis_subscription_cb callback, void *data)
Add a route to a message router.
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
struct stasis_message_type * ast_endpoint_snapshot_type(void)
Message type for ast_endpoint_snapshot.
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
struct ao2_container * stasis_cache_dump(struct stasis_cache *cache, struct stasis_message_type *type)
Dump cached items to a subscription for the ast_eid_default entity.
Definition: stasis_cache.c:736
#define NULL
Definition: resample.c:96
static void cache_update_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
struct stasis_topic * ast_endpoint_topic_all_cached(void)
Cached topic for all endpoint related messages.
static struct ao2_container * endpoints
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct stasis_message_type * stasis_cache_update_type(void)
Message type for cache update messages.
#define stasis_message_router_create(topic)
static struct stasis_message_router * router
Statsd Endpoint stats.
static int dump_cache_load(void *obj, void *arg, int flags)
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
Generic container type.
struct stasis_cache * ast_endpoint_cache(void)
Backend cache for ast_endpoint_topic_all_cached().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 134 of file res_endpoint_stats.c.

References ao2_callback, ao2_ref, ast_endpoint_cache(), ast_endpoint_snapshot_type(), AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_SUPPORT_EXTENDED, ASTERISK_GPL_KEY, dump_cache_unload(), endpoints, load_module(), NULL, OBJ_MULTIPLE, OBJ_NODATA, OBJ_NOLOCK, stasis_cache_dump(), and stasis_message_router_unsubscribe_and_join().

135 {
136  struct ao2_container *endpoints;
137 
139  if (endpoints) {
141  ao2_ref(endpoints, -1);
142  }
143 
145  router = NULL;
146 
147  return 0;
148 }
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
struct stasis_message_type * ast_endpoint_snapshot_type(void)
Message type for ast_endpoint_snapshot.
void stasis_message_router_unsubscribe_and_join(struct stasis_message_router *router)
Unsubscribe the router from the upstream topic, blocking until the final message has been processed...
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
struct ao2_container * stasis_cache_dump(struct stasis_cache *cache, struct stasis_message_type *type)
Dump cached items to a subscription for the ast_eid_default entity.
Definition: stasis_cache.c:736
#define NULL
Definition: resample.c:96
static struct ao2_container * endpoints
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static struct stasis_message_router * router
Statsd Endpoint stats.
Generic container type.
static int dump_cache_unload(void *obj, void *arg, int flags)
struct stasis_cache * ast_endpoint_cache(void)
Backend cache for ast_endpoint_topic_all_cached().

◆ update_endpoint_state()

static void update_endpoint_state ( struct ast_endpoint_snapshot snapshot,
const char *  delta 
)
static

Definition at line 45 of file res_endpoint_stats.c.

References AST_ENDPOINT_OFFLINE, AST_ENDPOINT_ONLINE, AST_ENDPOINT_UNKNOWN, AST_STATSD_GAUGE, ast_statsd_log_string(), and ast_endpoint_snapshot::state.

Referenced by handle_endpoint_update().

46 {
47  switch (snapshot->state) {
49  ast_statsd_log_string("endpoints.state.unknown", AST_STATSD_GAUGE, delta, 1.0);
50  break;
52  ast_statsd_log_string("endpoints.state.offline", AST_STATSD_GAUGE, delta, 1.0);
53  break;
55  ast_statsd_log_string("endpoints.state.online", AST_STATSD_GAUGE, delta, 1.0);
56  break;
57  }
58 }
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:111
#define AST_STATSD_GAUGE
Support for publishing to a statsd server.
Definition: statsd.h:32
enum ast_endpoint_state state

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Endpoint statistics" , .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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .requires = "res_statsd" }
static

Definition at line 155 of file res_endpoint_stats.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 155 of file res_endpoint_stats.c.

◆ router

struct stasis_message_router* router
static

Statsd Endpoint stats.

This module subscribes to Stasis endpoints and send statistics based on their state.

Author
Matthew Jordan mjord.nosp@m.an@d.nosp@m.igium.nosp@m..com
Since
13.7.0

Stasis message router

Definition at line 43 of file res_endpoint_stats.c.