diff --git a/src/modules/kex/core_stats.c b/src/modules/kex/core_stats.c index 048b80dc22c..1ebd235752b 100644 --- a/src/modules/kex/core_stats.c +++ b/src/modules/kex/core_stats.c @@ -403,6 +403,7 @@ struct rpc_list_params { rpc_t* rpc; void* ctx; void* hst; + int numeric; int clear; }; @@ -488,32 +489,37 @@ static void rpc_stats_get_statistics(rpc_t* rpc, void* ctx) return; } -/** - * Satistic getter RPC callback with string value. - */ -static void rpc_fetch_grp_vars_cbk(void* p, str* g, str* n, counter_handle_t h) -{ - struct rpc_list_params *packed_params; - rpc_t* rpc; - void* ctx; - void* hst; + +static void rpc_fetch_add_stat(rpc_t* rpc, void* ctx, void* hst, char* g, char* n, unsigned long val, int numeric) { char nbuf[128]; char vbuf[32]; + int res; - packed_params = p; - rpc = packed_params->rpc; - ctx = packed_params->ctx; - hst = packed_params->hst; - - snprintf(nbuf, 127, "%.*s.%.*s", g->len, g->s, n->len, n->s); - snprintf(vbuf, 31, "%lu", counter_get_val(h)); - if(rpc->struct_add(hst, "s", nbuf, vbuf)<0) + snprintf(nbuf, 127, "%s.%s", g, n); + if (numeric) { + res = rpc->struct_add(hst, "d", nbuf, val); + } else { + snprintf(vbuf, 31, "%lu", val); + res = rpc->struct_add(hst, "s", nbuf, vbuf); + } + if (res<0) { rpc->fault(ctx, 500, "Internal error"); return; } } +/** + * Statistic getter RPC callback. + */ +static void rpc_fetch_grp_vars_cbk(void* p, str* g, str* n, counter_handle_t h) +{ + struct rpc_list_params *packed_params = p; + + rpc_fetch_add_stat(packed_params->rpc, packed_params->ctx, packed_params->hst, + g->s, n->s, counter_get_val(h), packed_params->numeric); +} + /** * Group statistic getter RPC callback. */ @@ -525,14 +531,12 @@ static void rpc_fetch_all_grps_cbk(void* p, str* g) /** * All statistic getter RPC callback. */ -static void stats_fetch_all(rpc_t* rpc, void* ctx, void* th, char* stat) +static void stats_fetch_all(rpc_t* rpc, void* ctx, void* th, char* stat, int numeric) { int len = strlen(stat); struct rpc_list_params packed_params; str s_statistic; stat_var *s_stat; - char nbuf[128]; - char vbuf[32]; char *m; char *n; int i; @@ -541,12 +545,14 @@ static void stats_fetch_all(rpc_t* rpc, void* ctx, void* th, char* stat) packed_params.rpc = rpc; packed_params.ctx = ctx; packed_params.hst = th; + packed_params.numeric = numeric; counter_iterate_grp_names(rpc_fetch_all_grps_cbk, &packed_params); } else if (stat[len-1]==':') { packed_params.rpc = rpc; packed_params.ctx = ctx; packed_params.hst = th; + packed_params.numeric = numeric; stat[len-1] = '\0'; counter_iterate_grp_vars(stat, rpc_fetch_grp_vars_cbk, &packed_params); stat[len-1] = ':'; @@ -556,14 +562,8 @@ static void stats_fetch_all(rpc_t* rpc, void* ctx, void* th, char* stat) s_statistic.len = strlen(stat); s_stat = get_stat(&s_statistic); if (s_stat) { - snprintf(nbuf, 127, "%s.%s", - ZSW(get_stat_module(s_stat)), ZSW(get_stat_name(s_stat))); - snprintf(vbuf, 31, "%lu", get_stat_val(s_stat)); - if(rpc->struct_add(th, "s", nbuf, vbuf)<0) - { - rpc->fault(ctx, 500, "Internal error"); - return; - } + rpc_fetch_add_stat(rpc, ctx, th, + ZSW(get_stat_module(s_stat)), ZSW(get_stat_name(s_stat)), get_stat_val(s_stat), numeric); } else { n = strchr(stat, '.'); if(n==NULL) { @@ -592,23 +592,17 @@ static void stats_fetch_all(rpc_t* rpc, void* ctx, void* th, char* stat) return; } - snprintf(nbuf, 127, "%s.%s", - m, ZSW(get_stat_name(s_stat))); - snprintf(vbuf, 31, "%lu", get_stat_val(s_stat)); - if(rpc->struct_add(th, "s", nbuf, vbuf)<0) - { - rpc->fault(ctx, 500, "Internal error"); - return; - } + rpc_fetch_add_stat(rpc, ctx, th, + ZSW(m), ZSW(get_stat_name(s_stat)), get_stat_val(s_stat), numeric); } } } } /** - * RPC statistics getter with string values. + * RPC statistics getter */ -static void rpc_stats_fetch_statistics(rpc_t* rpc, void* ctx) +static void rpc_stats_fetch_statistics(rpc_t* rpc, void* ctx, int numeric) { char* stat; void *th; @@ -625,148 +619,29 @@ static void rpc_stats_fetch_statistics(rpc_t* rpc, void* ctx) rpc->fault(ctx, 500, "Internal error creating root struct"); return; } - stats_fetch_all(rpc, ctx, th, stat); + stats_fetch_all(rpc, ctx, th, stat, numeric); while((rpc->scan(ctx, "*s", &stat)>0)) { - stats_fetch_all(rpc, ctx, th, stat); + stats_fetch_all(rpc, ctx, th, stat, numeric); } return; } -/** - * Satistic getter RPC callback with number value. - */ -static void rpc_fetchn_grp_vars_cbk(void* p, str* g, str* n, counter_handle_t h) -{ - struct rpc_list_params *packed_params; - rpc_t* rpc; - void* ctx; - void* hst; - char nbuf[128]; - - packed_params = p; - rpc = packed_params->rpc; - ctx = packed_params->ctx; - hst = packed_params->hst; - - snprintf(nbuf, 127, "%.*s.%.*s", g->len, g->s, n->len, n->s); - if(rpc->struct_add(hst, "d", nbuf, (int)counter_get_val(h))<0) - { - rpc->fault(ctx, 500, "Internal error"); - return; - } -} /** - * Group statistic getter RPC callback. - */ -static void rpc_fetchn_all_grps_cbk(void* p, str* g) -{ - counter_iterate_grp_vars(g->s, rpc_fetchn_grp_vars_cbk, p); -} - -/** - * All statistic getter RPC callback with number value. + * RPC statistics getter with string values. */ -static void stats_fetchn_all(rpc_t* rpc, void* ctx, void* th, char* stat) +static void rpc_stats_fetchs_statistics(rpc_t* rpc, void* ctx) { - int len = strlen(stat); - struct rpc_list_params packed_params; - str s_statistic; - stat_var *s_stat; - char nbuf[128]; - char *m; - char *n; - int i; - - if (len==3 && strcmp("all", stat)==0) { - packed_params.rpc = rpc; - packed_params.ctx = ctx; - packed_params.hst = th; - counter_iterate_grp_names(rpc_fetchn_all_grps_cbk, &packed_params); - } - else if (stat[len-1]==':') { - packed_params.rpc = rpc; - packed_params.ctx = ctx; - packed_params.hst = th; - stat[len-1] = '\0'; - counter_iterate_grp_vars(stat, rpc_fetchn_grp_vars_cbk, &packed_params); - stat[len-1] = ':'; - } - else { - s_statistic.s = stat; - s_statistic.len = strlen(stat); - s_stat = get_stat(&s_statistic); - if (s_stat) { - snprintf(nbuf, 127, "%s.%s", - ZSW(get_stat_module(s_stat)), ZSW(get_stat_name(s_stat))); - if(rpc->struct_add(th, "d", nbuf, (int)get_stat_val(s_stat))<0) { - rpc->fault(ctx, 500, "Internal error"); - return; - } - } else { - n = strchr(stat, '.'); - if(n==NULL) { - n =strchr(stat, ':'); - } - if(n==NULL) { - return; - } - n++; - s_statistic.s = n; - s_statistic.len = strlen(n); - s_stat = get_stat(&s_statistic); - if (s_stat) { - m = get_stat_module(s_stat); - if(m==NULL) { - return; - } - for(i=0; m[i]!=0 && stat[i]!=0; i++) { - if(stat[i]!=m[i]) { - /* module name mismatch */ - return; - } - } - if(m[i]!=0 || (stat[i]!='.' && stat[i]!=':')) { - /* module name mismatch */ - return; - } - - snprintf(nbuf, 127, "%s.%s", - m, ZSW(get_stat_name(s_stat))); - if(rpc->struct_add(th, "d", nbuf, (int)get_stat_val(s_stat))<0) { - rpc->fault(ctx, 500, "Internal error"); - return; - } - } - } - } + rpc_stats_fetch_statistics(rpc, ctx, 0); + return; } - /** * RPC statistics getter with number values. */ static void rpc_stats_fetchn_statistics(rpc_t* rpc, void* ctx) { - char* stat; - void *th; - - if (stats_support()==0) { - rpc->fault(ctx, 400, "stats support not enabled"); - return; - } - if (rpc->scan(ctx, "s", &stat) < 1) { - rpc->fault(ctx, 400, "Please provide which stats to retrieve"); - return; - } - if (rpc->add(ctx, "{", &th) < 0) { - rpc->fault(ctx, 500, "Internal error creating root struct"); - return; - } - stats_fetchn_all(rpc, ctx, th, stat); - while((rpc->scan(ctx, "*s", &stat)>0)) { - stats_fetchn_all(rpc, ctx, th, stat); - } + rpc_stats_fetch_statistics(rpc, ctx, 1); return; } @@ -922,7 +797,7 @@ static const char* rpc_stats_get_statistics_doc[2] = /** * RPC statistics getter doc. */ -static const char* rpc_stats_fetch_statistics_doc[2] = +static const char* rpc_stats_fetchs_statistics_doc[2] = {"fetch core and modules stats as string values", 0}; /** @@ -951,8 +826,8 @@ rpc_export_t kex_stats_rpc[] = { {"stats.get_statistics", rpc_stats_get_statistics, rpc_stats_get_statistics_doc, RET_ARRAY}, - {"stats.fetch", rpc_stats_fetch_statistics, - rpc_stats_fetch_statistics_doc, 0}, + {"stats.fetch", rpc_stats_fetchs_statistics, + rpc_stats_fetchs_statistics_doc, 0}, {"stats.fetchn", rpc_stats_fetchn_statistics, rpc_stats_fetchn_statistics_doc, 0}, {"stats.reset_statistics", rpc_stats_reset_statistics,