Asterisk - The Open Source Telephony Project GIT-master-a358458
res_endpoint_stats.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2015, Digium, Inc.
5 *
6 * Matthew Jordan <mjordan@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*!
20 * \brief Statsd Endpoint stats.
21 *
22 * This module subscribes to Stasis endpoints and send statistics
23 * based on their state.
24 *
25 * \author Matthew Jordan <mjordan@digium.com>
26 * \since 13.7.0
27 */
28
29/*** MODULEINFO
30 <depend>res_statsd</depend>
31 <defaultenabled>no</defaultenabled>
32 <support_level>extended</support_level>
33 ***/
34
35#include "asterisk.h"
36
37#include "asterisk/module.h"
40#include "asterisk/statsd.h"
41
42/*! Stasis message router */
44
45static void update_endpoint_state(struct ast_endpoint_snapshot *snapshot, const char *delta)
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}
59
60static void handle_endpoint_update(struct ast_endpoint_snapshot *old_snapshot, struct ast_endpoint_snapshot *new_snapshot)
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}
77
78static void cache_update_cb(void *data, struct stasis_subscription *sub,
79 struct stasis_message *message)
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}
94
95static int dump_cache_load(void *obj, void *arg, int flags)
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}
104
105static int dump_cache_unload(void *obj, void *arg, int flags)
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}
114
115static int load_module(void)
116{
117 struct ao2_container *endpoints;
118
120 if (!router) {
122 }
124
126 if (endpoints) {
128 ao2_ref(endpoints, -1);
129 }
130
132}
133
134static int unload_module(void)
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}
149
151 .support_level = AST_MODULE_SUPPORT_EXTENDED,
152 .load = load_module,
153 .unload = unload_module,
154 .requires = "res_statsd"
155 );
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition: astobj2.h:1693
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
@ OBJ_NOLOCK
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063
@ OBJ_NODATA
Definition: astobj2.h:1044
@ OBJ_MULTIPLE
Definition: astobj2.h:1049
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
@ AST_ENDPOINT_OFFLINE
Definition: endpoints.h:55
@ AST_ENDPOINT_ONLINE
Definition: endpoints.h:57
@ AST_ENDPOINT_UNKNOWN
Definition: endpoints.h:53
struct stasis_message_type * ast_endpoint_snapshot_type(void)
Message type for ast_endpoint_snapshot.
struct stasis_topic * ast_endpoint_topic_all_cached(void)
Cached topic for all endpoint related messages.
struct stasis_message_type * stasis_cache_update_type(void)
Message type for cache update messages.
struct stasis_cache * ast_endpoint_cache(void)
Backend cache for ast_endpoint_topic_all_cached().
static struct ao2_container * endpoints
Asterisk module definitions.
@ AST_MODFLAG_DEFAULT
Definition: module.h:315
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODULE_SUPPORT_EXTENDED
Definition: module.h:122
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
struct stasis_forward * sub
Definition: res_corosync.c:240
static int dump_cache_load(void *obj, void *arg, int flags)
static struct stasis_message_router * router
Statsd Endpoint stats.
static void update_endpoint_state(struct ast_endpoint_snapshot *snapshot, const char *delta)
static int dump_cache_unload(void *obj, void *arg, int flags)
static int load_module(void)
static void handle_endpoint_update(struct ast_endpoint_snapshot *old_snapshot, struct ast_endpoint_snapshot *new_snapshot)
static int unload_module(void)
static void cache_update_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
#define NULL
Definition: resample.c:96
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
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
Endpoint abstractions.
#define stasis_message_router_create(topic)
Create a new message router object.
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.
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.
#define AST_STATSD_GAUGE
Support for publishing to a statsd server.
Definition: statsd.h:32
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
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:209
Generic container type.
A snapshot of an endpoint's state.
const ast_string_field tech
enum ast_endpoint_state state
const ast_string_field resource
Cache update message.
Definition: stasis.h:965