From 526afdf95a5ab8a12a96c3095501265077ff24f2 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 1 Feb 2015 22:33:59 +0000 Subject: [PATCH] Return statfiles stats from controller. --- src/client/rspamc.c | 6 ++-- src/controller.c | 19 ++++--------- src/libstat/stat_api.h | 8 ++++++ src/libstat/stat_process.c | 57 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 16 deletions(-) diff --git a/src/client/rspamc.c b/src/client/rspamc.c index 4d137be81..95f04eb5e 100644 --- a/src/client/rspamc.c +++ b/src/client/rspamc.c @@ -679,8 +679,8 @@ rspamc_stat_statfile (const ucl_object_t *obj, GString *out) rspamd_printf_gstring (out, "Statfile: %s ", symbol); } rspamd_printf_gstring (out, "length: %HL; free blocks: %HL; total blocks: %HL; " - "free: %.2f%%\n", size, blocks - used_blocks, blocks, - (blocks - used_blocks) * 100.0 / (gdouble)blocks); + "free: %.2f%%; learned: %L\n", size, blocks - used_blocks, blocks, + (blocks - used_blocks) * 100.0 / (gdouble)blocks, version); } static void @@ -734,6 +734,8 @@ rspamc_stat_output (ucl_object_t *obj) rspamc_stat_statfile (cur, out); } } + rspamd_printf_gstring (out, "Total learns: %L\n", + ucl_object_toint (ucl_object_find_key (obj, "total_learns"))); rspamd_fprintf (stdout, "%v", out); } diff --git a/src/controller.c b/src/controller.c index 16651ea0b..f54d0be03 100644 --- a/src/controller.c +++ b/src/controller.c @@ -26,6 +26,7 @@ #include "libserver/dynamic_cfg.h" #include "libutil/rrd.h" #include "libutil/map.h" +#include "libstat/stat_api.h" #include "main.h" #ifdef WITH_GPERF_TOOLS @@ -1255,12 +1256,8 @@ rspamd_controller_handle_stat_common ( struct rspamd_controller_session *session = conn_ent->ud; ucl_object_t *top, *sub; gint i; - guint64 used, total, rev, ham = 0, spam = 0; - time_t ti; + guint64 learned = 0, spam, ham; rspamd_mempool_stat_t mem_st; - struct rspamd_classifier_config *ccf; - struct rspamd_statfile_config *st; - GList *cur_cl, *cur_st; struct rspamd_stat *stat, stat_copy; rspamd_mempool_stat (&mem_st); @@ -1293,8 +1290,6 @@ rspamd_controller_handle_stat_common ( spam), "spam_count", 0, false); ucl_object_insert_key (top, ucl_object_fromint ( ham), "ham_count", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (stat->messages_learned), "learned", 0, false); ucl_object_insert_key (top, ucl_object_fromint (stat->connections_count), "connections", 0, false); ucl_object_insert_key (top, @@ -1327,15 +1322,11 @@ rspamd_controller_handle_stat_common ( stat->fuzzy_hashes_expired), "fuzzy_expired", 0, false); /* Now write statistics for each statfile */ - cur_cl = g_list_first (session->ctx->cfg->classifiers); - sub = ucl_object_typed_new (UCL_ARRAY); - while (cur_cl) { - ccf = cur_cl->data; - /* XXX: add statistics for the modern system */ - cur_cl = g_list_next (cur_cl); - } + sub = rspamd_stat_statistics (session->ctx->cfg, &learned); ucl_object_insert_key (top, sub, "statfiles", 0, false); + ucl_object_insert_key (top, + ucl_object_fromint (learned), "total_learns", 0, false); if (do_reset) { session->ctx->srv->stat->messages_scanned = 0; diff --git a/src/libstat/stat_api.h b/src/libstat/stat_api.h index 2dbf19372..c0023ffbd 100644 --- a/src/libstat/stat_api.h +++ b/src/libstat/stat_api.h @@ -55,6 +55,14 @@ gboolean rspamd_stat_classify (struct rspamd_task *task, lua_State *L, GError ** gboolean rspamd_stat_learn (struct rspamd_task *task, gboolean spam, lua_State *L, GError **err); +/** + * Get the overall statistics for all statfile backends + * @param cfg configuration + * @param total_learns the total number of learns is stored here + * @return array of statistical information + */ +ucl_object_t * rspamd_stat_statistics (struct rspamd_config *cfg, + guint64 *total_learns); void rspamd_stat_unload (void); diff --git a/src/libstat/stat_process.c b/src/libstat/stat_process.c index 022edde7d..dc58e0ac4 100644 --- a/src/libstat/stat_process.c +++ b/src/libstat/stat_process.c @@ -536,3 +536,60 @@ rspamd_stat_learn (struct rspamd_task *task, gboolean spam, lua_State *L, return ret; } + +ucl_object_t * +rspamd_stat_statistics (struct rspamd_config *cfg, guint64 *total_learns) +{ + struct rspamd_classifier_config *clcf; + struct rspamd_statfile_config *stcf; + struct rspamd_stat_backend *bk; + gpointer backend_runtime; + GList *cur, *st_list = NULL, *curst; + ucl_object_t *res = NULL, *elt; + guint64 learns = 0; + + if (cfg != NULL && cfg->classifiers != NULL) { + res = ucl_object_typed_new (UCL_ARRAY); + + cur = g_list_first (cfg->classifiers); + + while (cur) { + clcf = (struct rspamd_classifier_config *)cur->data; + + st_list = clcf->statfiles; + curst = st_list; + + while (curst != NULL) { + stcf = (struct rspamd_statfile_config *)curst->data; + + bk = rspamd_stat_get_backend (stcf->backend); + + if (bk == NULL) { + msg_warn ("backend of type %s is not defined", stcf->backend); + curst = g_list_next (curst); + continue; + } + + backend_runtime = bk->runtime (stcf, FALSE, bk->ctx); + + learns += bk->total_learns (backend_runtime, bk->ctx); + elt = bk->get_stat (backend_runtime, bk->ctx); + + if (elt != NULL) { + ucl_array_append (res, elt); + } + + curst = g_list_next (curst); + } + + /* Next classifier */ + cur = g_list_next (cur); + } + + if (total_learns != NULL) { + *total_learns = learns; + } + } + + return res; +} -- 2.39.5