From 8eb52ceacd307697af962759c2a26f09286867bb Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Fri, 29 Jan 2010 11:21:07 -0700 Subject: [PATCH] Provide command line script to obtain IMAP caching statistics --- imp/docs/CHANGES | 1 + imp/scripts/query-imap-cache.php | 137 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100755 imp/scripts/query-imap-cache.php diff --git a/imp/docs/CHANGES b/imp/docs/CHANGES index b983b55f4..11cfb574e 100644 --- a/imp/docs/CHANGES +++ b/imp/docs/CHANGES @@ -2,6 +2,7 @@ v5.0-git -------- +[mms] Provide command line script to obtain IMAP caching statistics. [mms] Catch flag changes from other IMAP clients when refreshing if CONDSTORE is available on the IMAP server (DIMP). [mms] Improved forwarding options. diff --git a/imp/scripts/query-imap-cache.php b/imp/scripts/query-imap-cache.php new file mode 100755 index 000000000..9302ad17c --- /dev/null +++ b/imp/scripts/query-imap-cache.php @@ -0,0 +1,137 @@ +#!@php_bin@ + + * @package IMP + */ + +require_once dirname(__FILE__) . '/../lib/Application.php'; +Horde_Registry::appInit('imp', array('authentication' => 'none', 'cli' => true)); + +$cli = Horde_Cli::singleton(); + +$c = new Console_Getopt(); +$argv = $c->readPHPArgv(); +array_shift($argv); +$options = $c->getopt2($argv, '', array('user=', 'pass=', 'server=')); +if (PEAR::isError($options)) { + $cli->fatal("Invalid arguments.\n"); +} + +$pass = $server = $user = null; +foreach ($options[0] as $val) { + switch ($val[0]) { + case '--user': + $user = $val[1]; + break; + + case '--pass': + $pass = $val[1]; + break; + + case '--server': + $server = $val[1]; + break; + } +} + +if (is_null($server)) { + $keys = array_keys($imp_imap->loadServerConfig()); + + /* Set first entry to 1, not 0. */ + array_unshift($keys, ''); + unset($keys[0]); + + while (is_null($server)) { + $server = $cli->prompt('Server:', $keys); + } + $server = $keys[$server]; + $cli->message('Using server "' . $server . '"', 'cli.message'); +} + +while (is_null($user)) { + $user = $cli->prompt('Username:'); + if (!strlen($user)) { + $user = null; + } +} +$cli->message('Using username "' . $user . '"', 'cli.message'); + +while (is_null($pass)) { + $pass = $cli->passwordPrompt('Password:'); + if (!strlen($pass)) { + $pass = null; + } +} + +$cli->writeln(); + +$ob = $imp_imap->createImapObject($user, $pass, $server); +if (!$ob) { + $cli->fatal('Could not create Imap Client object.'); +} + +try { + $ob->login(); + $cli->message('Successfully logged in to IMAP server.'); + + $mboxes = $ob->listMailboxes('*', Horde_Imap_Client::MBOX_ALL, array('flat' => true, 'sort' => true)); + $cli->message('User mailbox count: ' . count($mboxes)); +} catch (Horde_Imap_Client_Exception $e) { + $cli->fatal('IMAP error: ' . $e->getMessage()); +} + +$opts = array( + 1 => 'Full Statistics', + 2 => 'Summary', + 3 => 'Exit' +); + +while (true) { + $cli->writeln(); + + $action = $cli->prompt('Action:', $opts); + switch ($action) { + case 1: + case 2: + $mbox_list = array(); + $msg_cnt = $search_cnt = 0; + + foreach ($mboxes as $val) { + if ($res = $ob->cache->get($val)) { + $mbox_list[$val] = array( + 'msgs' => count($res) + ); + $msg_cnt += $mbox_list[$val]['msgs']; + + if ($res = $ob->cache->getMetaData($val, null, array('HICsearch'))) { + $mbox_list[$val]['search'] = count($res['HICsearch']) - 1; + $search_cnt += $mbox_list[$val]['search']; + } + } + } + + $cli->writeln(); + $cli->message($cli->bold('Cached mailboxes:') . ' ' . count($mbox_list), 'cli.message'); + $cli->message($cli->bold('Cached messages:') . ' ' . $msg_cnt, 'cli.message'); + $cli->message($cli->bold('Cached searches:') . ' ' . $search_cnt, 'cli.message'); + + if ($action == 1) { + $cli->writeln(); + foreach ($mbox_list as $key => $val) { + $cli->writeln($cli->indent(sprintf("%s (%d msgs, %d searches)", $key, $val['msgs'], $val['search']))); + } + } + break; + + case 3: + exit; + } +} -- 2.11.0