stats-plugin.c revision 60a85755d10c082cd416079c40d32977295633d3
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen/* Copyright (c) 2011-2017 Dovecot authors, see the included COPYING file */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen/* If session isn't refreshed every 15 minutes, it's dropped.
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen Must be smaller than MAIL_SESSION_IDLE_TIMEOUT_MSECS in stats server */
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen#define SESSION_STATS_FORCE_REFRESH_SECS (5*60)
1dd054126238349e1a7d3d1ffe7f8bc5fdbacb7aTimo Sirainenconst char *stats_plugin_version = DOVECOT_ABI_VERSION;
5fb3f13537dffd15a31e997da133a721c0728af8Timo Sirainen MODULE_CONTEXT_INIT(&mail_user_module_register);
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainenstruct stats_storage_module stats_storage_module =
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen MODULE_CONTEXT_INIT(&mail_storage_module_register);
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainenstatic struct stats_connection *global_stats_conn = NULL;
1dd054126238349e1a7d3d1ffe7f8bc5fdbacb7aTimo Sirainenstatic struct mail_user *stats_global_user = NULL;
f6aaada6101dd43cd80fe965ff1ab9bfaf776252Timo Sirainenstatic unsigned int stats_user_count = 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void session_stats_refresh_timeout(struct mail_user *user);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void stats_io_activate(struct mail_user *user)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct stats_user *suser = STATS_USER_CONTEXT(user);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* the first user sets the global user. the second user sets
d22301419109ed4a38351715e6760011421dadecTimo Sirainen it to NULL. when we get back to one user we'll need to set
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen the global user again somewhere. do it here. */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* skip time spent waiting in ioloop */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_stats = stats_fill_ptr(suser->pre_io_stats, mail_stats_item);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_user_stats_fill(user, suser->pre_io_stats);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void stats_add_session(struct mail_user *user)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct stats_user *suser = STATS_USER_CONTEXT(user);
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen new_stats = stats_alloc(pool_datastack_create());
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen diff_stats = stats_alloc(pool_datastack_create());
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen /* we'll count new_stats-pre_io_stats and add the changes to
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen session_stats. the new_stats can't be directly copied to
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen session_stats because there are some fields that don't start from
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen zero, like clock_time. (actually with stats_global_user code we're
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen requiring that clock_time is the only such field..) */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (!stats_diff(suser->pre_io_stats, new_stats, diff_stats, &error))
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_error("stats: session stats shrank: %s", error);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* copying is only needed if stats_global_user=NULL */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainensession_stats_need_send(struct stats_user *suser, time_t now,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen bool *changed_r, unsigned int *to_next_secs_r)
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainen unsigned int diff;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen *to_next_secs_r = SESSION_STATS_FORCE_REFRESH_SECS;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (stats_have_changed(suser->last_sent_session_stats,
return TRUE;
return TRUE;
return FALSE;
unsigned int to_next_secs;
bool changed;
static struct mailbox_transaction_context *
const char *reason)
return trans;
bool ret;
return FALSE;
if (*tryagain_r ||
return ret;
unsigned int last_update_secs;
unsigned int refresh_secs;
if (refresh_secs == 0)
if (stats_user_count == 0) {
void old_stats_plugin_deinit(void)