From e221e10c39e47a569a8e33ff79a53838e844c414 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 7 Jun 2013 14:52:25 +0100 Subject: [PATCH] Improve counters output in the controller. --- src/controller.c | 61 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/src/controller.c b/src/controller.c index f633f5b01..865f8f1de 100644 --- a/src/controller.c +++ b/src/controller.c @@ -268,21 +268,6 @@ check_auth (struct controller_command *cmd, struct controller_session *session) return 1; } -static void -counter_write_callback (gpointer key, gpointer value, void *data) -{ - struct controller_session *session = data; - struct counter_data *cd = value; - gchar *name = key; - gchar out_buf[128]; - gint r; - - r = rspamd_snprintf (out_buf, sizeof (out_buf), "%s: %uD" CRLF, name, (guint32)cd->value); - if (! rspamd_dispatcher_write (session->dispatcher, out_buf, r, TRUE, FALSE)) { - msg_warn ("cannot write to socket"); - } -} - static gboolean write_whole_statfile (struct controller_session *session, gchar *symbol, struct classifier_config *ccf) { @@ -437,6 +422,50 @@ process_sync_command (struct controller_session *session, gchar **args) return TRUE; } +static gboolean +process_counters_command (struct controller_session *session) +{ + gchar out_buf[BUFSIZ]; + GList *cur; + struct cache_item *item; + struct symbols_cache *cache; + gint r; + + cache = session->cfg->cache; + + r = rspamd_snprintf (out_buf, sizeof (out_buf), "Rspamd counters." CRLF); + + if (cache != NULL) { + cur = cache->negative_items; + while (cur) { + item = cur->data; + if (!item->is_callback) { + r += rspamd_snprintf (out_buf + r, sizeof (out_buf) - r, "%s %.2f %d %.3f" CRLF, + item->s->symbol, item->s->weight, + item->s->frequency, item->s->avg_time); + } + cur = g_list_next (cur); + } + cur = cache->static_items; + while (cur) { + item = cur->data; + if (!item->is_callback) { + r += rspamd_snprintf (out_buf + r, sizeof (out_buf) - r, "%s %.2f %d %.3f" CRLF, + item->s->symbol, item->s->weight, + item->s->frequency, item->s->avg_time); + } + cur = g_list_next (cur); + } + } + + if (!session->restful) { + return rspamd_dispatcher_write (session->dispatcher, out_buf, r, FALSE, FALSE); + } + else { + return restful_write_reply (200, NULL, out_buf, r, session->dispatcher); + } +} + static gboolean process_stat_command (struct controller_session *session) { @@ -1078,7 +1107,7 @@ process_command (struct controller_command *cmd, gchar **cmd_args, struct contro } break; case COMMAND_COUNTERS: - rspamd_hash_foreach (rspamd_main->counters, counter_write_callback, session); + process_counters_command (session); break; case COMMAND_ADD_ACTION: if (check_auth (cmd, session)) { -- 2.39.5