Skip to content

Commit 6105901

Browse files
authored
Merge pull request #4278 from darwvin-dev/feature/uac-registrant-statistics
[uac_registrant] Add registration statistics
2 parents ef6ced2 + a2381ff commit 6105901

4 files changed

Lines changed: 168 additions & 1 deletion

File tree

‎modules/uac_registrant/README.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,50 @@ modparam("uac_registrant", "reregister_expiry_percentage", 90)
461461
None to be used in configuration file.
462462

463463

464+
### Exported Statistics
465+
466+
467+
The statistics below are read-only computed gauges. They are calculated from
468+
the current in-memory registrant table when queried, so they stay consistent
469+
across reloads, MI upsert/delete operations, enable/disable operations and
470+
registration state changes.
471+
472+
473+
#### registrants
474+
475+
476+
Total number of registrants currently held in memory, including disabled
477+
registrants. The full statistic name is `uac_registrant:registrants`.
478+
479+
480+
#### enabled_registrants
481+
482+
483+
Number of registrants which are currently enabled. The full statistic name is
484+
`uac_registrant:enabled_registrants`.
485+
486+
487+
#### registered_registrants
488+
489+
490+
Number of enabled registrants currently in `REGISTERED_STATE`. The full
491+
statistic name is `uac_registrant:registered_registrants`.
492+
493+
494+
#### failed_registrants
495+
496+
497+
Number of enabled registrants currently in `REGISTER_TIMEOUT_STATE`,
498+
`INTERNAL_ERROR_STATE`, `WRONG_CREDENTIALS_STATE` or
499+
`REGISTRAR_ERROR_STATE`. The full statistic name is
500+
`uac_registrant:failed_registrants`.
501+
502+
503+
Reading any of these statistics walks the in-memory registrant hash table and
504+
holds only one bucket lock at a time. The cost of each read is therefore
505+
proportional to the number of registrants.
506+
507+
464508
### Exported MI Functions
465509

466510

‎modules/uac_registrant/reg_records.c‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,126 @@ static char call_id_ftag_buf[MD5_LEN];
4848
int send_unregister(unsigned int hash_index, reg_record_t *rec, str *auth_hdr,
4949
unsigned int all_contacts);
5050

51+
enum reg_stat_filter {
52+
REG_STAT_ALL,
53+
REG_STAT_ENABLED,
54+
REG_STAT_REGISTERED,
55+
REG_STAT_FAILED
56+
};
57+
58+
static int count_registrant(void *e_data, void *data, void *r_data)
59+
{
60+
unsigned long *count = (unsigned long *)data;
61+
62+
(*count)++;
63+
return 0;
64+
}
65+
66+
static int count_enabled_registrant(void *e_data, void *data, void *r_data)
67+
{
68+
reg_record_t *rec = (reg_record_t *)e_data;
69+
unsigned long *count = (unsigned long *)data;
70+
71+
if (rec->flags & REG_ENABLED)
72+
(*count)++;
73+
return 0;
74+
}
75+
76+
static int count_registered_registrant(void *e_data, void *data, void *r_data)
77+
{
78+
reg_record_t *rec = (reg_record_t *)e_data;
79+
unsigned long *count = (unsigned long *)data;
80+
81+
if ((rec->flags & REG_ENABLED) && rec->state == REGISTERED_STATE)
82+
(*count)++;
83+
return 0;
84+
}
85+
86+
static int count_failed_registrant(void *e_data, void *data, void *r_data)
87+
{
88+
reg_record_t *rec = (reg_record_t *)e_data;
89+
unsigned long *count = (unsigned long *)data;
90+
91+
if ((rec->flags & REG_ENABLED) &&
92+
(rec->state == REGISTER_TIMEOUT_STATE ||
93+
rec->state == INTERNAL_ERROR_STATE ||
94+
rec->state == WRONG_CREDENTIALS_STATE ||
95+
rec->state == REGISTRAR_ERROR_STATE))
96+
(*count)++;
97+
return 0;
98+
}
99+
100+
static unsigned long get_registrant_count(enum reg_stat_filter filter)
101+
{
102+
slinkedl_run_data_f *count_handler;
103+
unsigned long count = 0;
104+
unsigned int i;
105+
int ret;
106+
107+
if (!reg_htable)
108+
return 0;
109+
110+
switch (filter) {
111+
case REG_STAT_ALL:
112+
count_handler = count_registrant;
113+
break;
114+
case REG_STAT_ENABLED:
115+
count_handler = count_enabled_registrant;
116+
break;
117+
case REG_STAT_REGISTERED:
118+
count_handler = count_registered_registrant;
119+
break;
120+
case REG_STAT_FAILED:
121+
count_handler = count_failed_registrant;
122+
break;
123+
default:
124+
LM_ERR("invalid registrant statistic filter %d\n", filter);
125+
return 0;
126+
}
127+
128+
for (i = 0; i < reg_hsize; i++) {
129+
lock_get(&reg_htable[i].lock);
130+
ret = slinkedl_traverse(reg_htable[i].p_list,
131+
count_handler, &count, NULL);
132+
lock_release(&reg_htable[i].lock);
133+
134+
if (ret < 0) {
135+
LM_ERR("failed to count registrants in bucket %u\n", i);
136+
break;
137+
}
138+
}
139+
140+
return count;
141+
}
142+
143+
static unsigned long get_total_registrants(void *unused)
144+
{
145+
return get_registrant_count(REG_STAT_ALL);
146+
}
147+
148+
static unsigned long get_enabled_registrants(void *unused)
149+
{
150+
return get_registrant_count(REG_STAT_ENABLED);
151+
}
152+
153+
static unsigned long get_registered_registrants(void *unused)
154+
{
155+
return get_registrant_count(REG_STAT_REGISTERED);
156+
}
157+
158+
static unsigned long get_failed_registrants(void *unused)
159+
{
160+
return get_registrant_count(REG_STAT_FAILED);
161+
}
162+
163+
const stat_export_t reg_stats[] = {
164+
{"registrants", STAT_IS_FUNC, (stat_var **)get_total_registrants},
165+
{"enabled_registrants", STAT_IS_FUNC, (stat_var **)get_enabled_registrants},
166+
{"registered_registrants", STAT_IS_FUNC, (stat_var **)get_registered_registrants},
167+
{"failed_registrants", STAT_IS_FUNC, (stat_var **)get_failed_registrants},
168+
{0, 0, 0}
169+
};
170+
51171
int reg_build_sr_identifier(const str *aor, const str *contact,
52172
const str *registrar, str *ident)
53173
{
@@ -411,6 +531,7 @@ int init_reg_htable(void) {
411531
}
412532
reg_htable[i].s_list = NULL;
413533
}
534+
414535
return 0;
415536
}
416537

‎modules/uac_registrant/reg_records.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <stdlib.h>
3232

3333
#include "../../lock_ops.h"
34+
#include "../../statistics.h"
3435
#include "../../ut.h"
3536
#include "../../mem/shm_mem.h"
3637
#include "../tm/dlg.h"
@@ -112,6 +113,7 @@ typedef struct record_coords {
112113

113114
extern reg_table_t reg_htable;
114115
extern unsigned int reg_hsize;
116+
extern const stat_export_t reg_stats[];
115117

116118
void *reg_alloc(size_t size);
117119
void reg_free(void *ptr);

‎modules/uac_registrant/registrant.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ struct module_exports exports= {
232232
0, /* exported functions */
233233
0, /* exported async functions */
234234
params, /* exported parameters */
235-
NULL, /* exported statistics */
235+
reg_stats, /* exported statistics */
236236
mi_cmds, /* exported MI functions */
237237
NULL, /* exported pseudo-variables */
238238
0, /* exported transformations */

0 commit comments

Comments
 (0)