| 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 |
#include "asterisk.h" |
| 28 |
|
| 29 |
#include "asterisk/stasis_message_router.h" |
| 30 |
#include "asterisk/stasis_system.h" |
| 31 |
#include "asterisk/res_prometheus.h" |
| 32 |
|
| 33 |
#ifdef HAVE_PJPROJECT |
| 34 |
#include "asterisk/res_pjsip.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "prometheus_internal.h" |
| 38 |
|
| 39 |
#ifdef HAVE_PJPROJECT |
| 40 |
|
| 41 |
|
| 42 |
static struct stasis_message_router *router; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
struct prometheus_metric_wrapper { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
struct prometheus_metric *metric; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
char key[128]; |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
static AST_VECTOR(, struct prometheus_metric_wrapper *) metrics; |
| 77 |
|
| 78 |
AST_MUTEX_DEFINE_STATIC(metrics_lock); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
static struct prometheus_metric_wrapper *create_wrapper(const char *key) |
| 90 |
{ |
| 91 |
struct prometheus_metric_wrapper *wrapper; |
| 92 |
|
| 93 |
wrapper = ast_calloc(1, sizeof(*wrapper)); |
| 94 |
if (!wrapper) { |
| 95 |
return NULL; |
| 96 |
} |
| 97 |
|
| 98 |
ast_copy_string(wrapper->key, key, sizeof(wrapper->key)); |
| 99 |
return wrapper; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
static struct prometheus_metric_wrapper *get_wrapper(const char *key) |
| 112 |
{ |
| 113 |
int i; |
| 114 |
SCOPED_MUTEX(lock, &metrics_lock); |
| 115 |
|
| 116 |
for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) { |
| 117 |
struct prometheus_metric_wrapper *wrapper = AST_VECTOR_GET(&metrics, i); |
| 118 |
|
| 119 |
if (!strcmp(wrapper->key, key)) { |
| 120 |
return wrapper; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return NULL; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
static int registration_state_to_int(const char *state) |
| 136 |
{ |
| 137 |
if (!strcasecmp(state, "Registered")) { |
| 138 |
return 1; |
| 139 |
} else if (!strcasecmp(state, "Rejected")) { |
| 140 |
return 2; |
| 141 |
} |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static void registration_deleted_observer(const void *obj) |
| 152 |
{ |
| 153 |
struct ast_variable *fields; |
| 154 |
struct ast_variable *it_fields; |
| 155 |
int i; |
| 156 |
SCOPED_MUTEX(lock, &metrics_lock); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
fields = ast_sorcery_objectset_create(ast_sip_get_sorcery(), obj); |
| 163 |
if (!fields) { |
| 164 |
ast_debug(1, "Unable to convert presumed registry object %p to strings; bailing on delete\n", obj); |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
for (it_fields = fields; it_fields; it_fields = it_fields->next) { |
| 169 |
if (strcasecmp(it_fields->name, "client_uri")) { |
| 170 |
continue; |
| 171 |
} |
| 172 |
|
| 173 |
for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) { |
| 174 |
struct prometheus_metric_wrapper *wrapper = AST_VECTOR_GET(&metrics, i); |
| 175 |
|
| 176 |
if (strcmp(wrapper->key, it_fields->value)) { |
| 177 |
continue; |
| 178 |
} |
| 179 |
|
| 180 |
ast_debug(1, "Registration metric '%s' deleted; purging with prejudice\n", wrapper->key); |
| 181 |
AST_VECTOR_REMOVE(&metrics, i, 1); |
| 182 |
|
| 183 |
prometheus_metric_unregister(wrapper->metric); |
| 184 |
ast_free(wrapper); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
ast_variables_destroy(fields); |
| 189 |
} |
| 190 |
|
| 191 |
static const struct ast_sorcery_observer registration_observer = { |
| 192 |
.deleted = registration_deleted_observer, |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
static void registration_loaded_observer(const char *name, const struct ast_sorcery *sorcery, const char *object_type, int reloaded) |
| 213 |
{ |
| 214 |
SCOPED_MUTEX(lock, &metrics_lock); |
| 215 |
int i; |
| 216 |
|
| 217 |
if (!reloaded) { |
| 218 |
|
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
if (strcmp(object_type, "registration")) { |
| 223 |
|
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) { |
| 228 |
struct prometheus_metric_wrapper *wrapper = AST_VECTOR_GET(&metrics, i); |
| 229 |
struct ast_variable search_fields = { |
| 230 |
.name = "client_uri", |
| 231 |
.value = wrapper->key, |
| 232 |
.next = NULL, |
| 233 |
}; |
| 234 |
void *obj; |
| 235 |
|
| 236 |
ast_debug(1, "Checking for the existance of registration metric %s\n", wrapper->key); |
| 237 |
obj = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), object_type, AST_RETRIEVE_FLAG_DEFAULT, &search_fields); |
| 238 |
if (!obj) { |
| 239 |
ast_debug(1, "Registration metric '%s' not found; purging with prejudice\n", wrapper->key); |
| 240 |
AST_VECTOR_REMOVE(&metrics, i, 1); |
| 241 |
|
| 242 |
prometheus_metric_unregister(wrapper->metric); |
| 243 |
ast_free(wrapper); |
| 244 |
continue; |
| 245 |
} |
| 246 |
ao2_ref(obj, -1); |
| 247 |
} |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
static const struct ast_sorcery_instance_observer observer_callbacks_registrations = { |
| 252 |
.object_type_loaded = registration_loaded_observer, |
| 253 |
}; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
static void registry_message_cb(void *data, struct stasis_subscription *sub, |
| 268 |
struct stasis_message *message) |
| 269 |
{ |
| 270 |
struct ast_json_payload *payload = stasis_message_data(message); |
| 271 |
struct ast_json *json = payload->json; |
| 272 |
const char *username = ast_json_string_get(ast_json_object_get(json, "username")); |
| 273 |
const char *status_str = ast_json_string_get(ast_json_object_get(json, "status")); |
| 274 |
const char *domain = ast_json_string_get(ast_json_object_get(json, "domain")); |
| 275 |
const char *channel_type = ast_json_string_get(ast_json_object_get(json, "channeltype")); |
| 276 |
struct prometheus_metric metric = PROMETHEUS_METRIC_STATIC_INITIALIZATION( |
| 277 |
PROMETHEUS_METRIC_GAUGE, |
| 278 |
"asterisk_pjsip_outbound_registration_status", |
| 279 |
"Current registration status. 0=Unregistered; 1=Registered; 2=Rejected.", |
| 280 |
NULL |
| 281 |
); |
| 282 |
struct prometheus_metric_wrapper *wrapper; |
| 283 |
char eid_str[32]; |
| 284 |
|
| 285 |
ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default); |
| 286 |
|
| 287 |
PROMETHEUS_METRIC_SET_LABEL(&metric, 0, "eid", eid_str); |
| 288 |
PROMETHEUS_METRIC_SET_LABEL(&metric, 1, "username", username); |
| 289 |
PROMETHEUS_METRIC_SET_LABEL(&metric, 2, "domain", domain); |
| 290 |
PROMETHEUS_METRIC_SET_LABEL(&metric, 3, "channel_type", channel_type); |
| 291 |
snprintf(metric.value, sizeof(metric.value), "%d", registration_state_to_int(status_str)); |
| 292 |
|
| 293 |
wrapper = get_wrapper(username); |
| 294 |
if (wrapper) { |
| 295 |
ast_mutex_lock(&wrapper->metric->lock); |
| 296 |
|
| 297 |
strcpy(wrapper->metric->value, metric.value); |
| 298 |
ast_mutex_unlock(&wrapper->metric->lock); |
| 299 |
} else { |
| 300 |
wrapper = create_wrapper(username); |
| 301 |
if (!wrapper) { |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
wrapper->metric = prometheus_gauge_create(metric.name, metric.help); |
| 306 |
if (!wrapper->metric) { |
| 307 |
ast_free(wrapper); |
| 308 |
return; |
| 309 |
} |
| 310 |
*(wrapper->metric) = metric; |
| 311 |
|
| 312 |
prometheus_metric_register(wrapper->metric); |
| 313 |
AST_VECTOR_APPEND(&metrics, wrapper); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
#endif |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
static void pjsip_outbound_registration_metrics_unload_cb(void) |
| 324 |
{ |
| 325 |
#ifdef HAVE_PJPROJECT |
| 326 |
stasis_message_router_unsubscribe_and_join(router); |
| 327 |
router = NULL; |
| 328 |
ast_sorcery_instance_observer_remove(ast_sip_get_sorcery(), &observer_callbacks_registrations); |
| 329 |
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "registration", ®istration_observer); |
| 330 |
#endif |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
static struct prometheus_metrics_provider provider = { |
| 338 |
.name = "pjsip_outbound_registration", |
| 339 |
.unload_cb = pjsip_outbound_registration_metrics_unload_cb, |
| 340 |
}; |
| 341 |
|
| 342 |
int pjsip_outbound_registration_metrics_init(void) |
| 343 |
{ |
| 344 |
prometheus_metrics_provider_register(&provider); |
| 345 |
|
| 346 |
#ifdef HAVE_PJPROJECT |
| 347 |
router = stasis_message_router_create(ast_system_topic()); |
| 348 |
if (!router) { |
| 349 |
goto cleanup; |
| 350 |
} |
| 351 |
|
| 352 |
if (stasis_message_router_add(router, ast_system_registry_type(), registry_message_cb, NULL)) { |
| 353 |
goto cleanup; |
| 354 |
} |
| 355 |
|
| 356 |
if (ast_sorcery_instance_observer_add(ast_sip_get_sorcery(), &observer_callbacks_registrations)) { |
| 357 |
goto cleanup; |
| 358 |
} |
| 359 |
|
| 360 |
if (ast_sorcery_observer_add(ast_sip_get_sorcery(), "registration", ®istration_observer)) { |
| 361 |
goto cleanup; |
| 362 |
} |
| 363 |
#endif |
| 364 |
return 0; |
| 365 |
|
| 366 |
#ifdef HAVE_PJPROJECT |
| 367 |
cleanup: |
| 368 |
ao2_cleanup(router); |
| 369 |
router = NULL; |
| 370 |
ast_sorcery_instance_observer_remove(ast_sip_get_sorcery(), &observer_callbacks_registrations); |
| 371 |
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "registration", ®istration_observer); |
| 372 |
|
| 373 |
return -1; |
| 374 |
#endif |
| 375 |
} |