| 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 |
#include "asterisk.h" |
| 36 |
|
| 37 |
#include "asterisk/module.h" |
| 38 |
#include "asterisk/stasis_endpoints.h" |
| 39 |
#include "asterisk/stasis_message_router.h" |
| 40 |
#include "asterisk/statsd.h" |
| 41 |
|
| 42 |
|
| 43 |
static struct stasis_message_router *router; |
| 44 |
|
| 45 |
static void update_endpoint_state(struct ast_endpoint_snapshot *snapshot, const char *delta) |
| 46 |
{ |
| 47 |
switch (snapshot->state) { |
| 48 |
case AST_ENDPOINT_UNKNOWN: |
| 49 |
ast_statsd_log_string("endpoints.state.unknown", AST_STATSD_GAUGE, delta, 1.0); |
| 50 |
break; |
| 51 |
case AST_ENDPOINT_OFFLINE: |
| 52 |
ast_statsd_log_string("endpoints.state.offline", AST_STATSD_GAUGE, delta, 1.0); |
| 53 |
break; |
| 54 |
case AST_ENDPOINT_ONLINE: |
| 55 |
ast_statsd_log_string("endpoints.state.online", AST_STATSD_GAUGE, delta, 1.0); |
| 56 |
break; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
static 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 |
|
| 78 |
static void cache_update_cb(void *data, struct stasis_subscription *sub, |
| 79 |
struct stasis_message *message) |
| 80 |
{ |
| 81 |
struct stasis_cache_update *update = stasis_message_data(message); |
| 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 |
|
| 95 |
static 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 |
|
| 105 |
static 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 |
|
| 115 |
static int load_module(void) |
| 116 |
{ |
| 117 |
struct ao2_container *endpoints; |
| 118 |
|
| 119 |
router = stasis_message_router_create(ast_endpoint_topic_all_cached()); |
| 120 |
if (!router) { |
| 121 |
return AST_MODULE_LOAD_DECLINE; |
| 122 |
} |
| 123 |
stasis_message_router_add(router, stasis_cache_update_type(), cache_update_cb, NULL); |
| 124 |
|
| 125 |
endpoints = stasis_cache_dump(ast_endpoint_cache(), ast_endpoint_snapshot_type()); |
| 126 |
if (endpoints) { |
| 127 |
ao2_callback(endpoints, OBJ_MULTIPLE | OBJ_NODATA | OBJ_NOLOCK, dump_cache_load, NULL); |
| 128 |
ao2_ref(endpoints, -1); |
| 129 |
} |
| 130 |
|
| 131 |
return AST_MODULE_LOAD_SUCCESS; |
| 132 |
} |
| 133 |
|
| 134 |
static int unload_module(void) |
| 135 |
{ |
| 136 |
struct ao2_container *endpoints; |
| 137 |
|
| 138 |
endpoints = stasis_cache_dump(ast_endpoint_cache(), ast_endpoint_snapshot_type()); |
| 139 |
if (endpoints) { |
| 140 |
ao2_callback(endpoints, OBJ_MULTIPLE | OBJ_NODATA | OBJ_NOLOCK, dump_cache_unload, NULL); |
| 141 |
ao2_ref(endpoints, -1); |
| 142 |
} |
| 143 |
|
| 144 |
stasis_message_router_unsubscribe_and_join(router); |
| 145 |
router = NULL; |
| 146 |
|
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Endpoint statistics", |
| 151 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 152 |
.load = load_module, |
| 153 |
.unload = unload_module, |
| 154 |
.requires = "res_statsd" |
| 155 |
); |