#!C:\Program Files (x86)\Plesk\Additional\PleskPHP74\\php.exe
<?php
/**
 * This script outputs statistics on the current memcache pool.
 *
 * Copyright 2007-2017 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (LGPL-2). If you
 * did not receive this file, see http://www.horde.org/licenses/lgpl.
 *
 * @author   Michael Slusarz <slusarz@horde.org>
 * @category Horde
 * @license  http://www.horde.org/licenses/lgpl LGPL-2
 * @package  Horde
 */

$baseFile = __DIR__ . '/../lib/Application.php';
if (file_exists($baseFile)) {
    require_once $baseFile;
} else {
    require_once 'PEAR/Config.php';
    require_once PEAR_Config::singleton()
        ->get('horde_dir', null, 'pear.horde.org') . '/lib/Application.php';
}
Horde_Registry::appInit('horde', array(
    'authentication' => 'none',
    'cli' => true
));

$hashtable = $injector->getInstance('Horde_HashTable');
if (!($hashtable instanceof Horde_HashTable_Memcache)) {
    $cli->fatal('Memcache HashTable driver not enabled.');
}

$parser = new Horde_Argv_Parser();
$parser->addOption('-a', '--all', array(
    'action' => 'store_true',
    'dest' => 'all',
    'help' => 'Show all servers'
));
$parser->addOption('--flush', array(
    'action' => 'store_true',
    'dest' => 'flush',
    'help' => 'Flush all data'
));
$parser->addOption('-l', '--lookup', array(
    'dest' => 'lookup',
    'help' => 'Key to lookup'
));
$parser->addOption('-r', '--raw', array(
    'action' => 'store_true',
    'dest' => 'raw',
    'help' => 'Display raw data'
));
$parser->addOption('-s', '--summary', array(
    'action' => 'store_true',
    'dest' => 'summary',
    'help' => 'Display summary'
));
list($values,) = $parser->parseArgs();

if ($values->flush) {
    if ($cli->prompt($cli->red('Are you sure you want to flush all data?'), array('y' => 'Yes', 'n' => 'No'), 'n') == 'y') {
        $hashtable->clear();
        $cli->writeln($cli->green('Done.'));
    }
    exit;
}

if ($values->lookup) {
    $data = $hashtable->get($values->lookup);
    $cli->writeln(empty($data) ? '[Key not found.]' : print_r($data, true));
    exit;
}

$stats = $hashtable->stats();

if ($values->raw) {
    $cli->writeln(print_r($stats, true));
} elseif (!$values->summary) {
    $values->all = true;
}

if ($values->all || $values->summary) {
    if ($values->summary) {
        $total = array();
        $total_keys = array('bytes', 'limit_maxbytes', 'curr_items', 'total_items', 'get_hits', 'get_misses', 'curr_connections', 'bytes_read', 'bytes_written');
        foreach ($total_keys as $key) {
            $total[$key] = 0;
        }
    }

    $i = count($stats);
    $s_count = 0;

    foreach ($stats as $key => $val) {
        if ($val === false) {
            $cli->message('Could not connect to server: ' . $key, 'cli.warning');
            continue;
        }

        ++$s_count;

        if ($values->summary) {
            foreach ($total_keys as $k) {
                $total[$k] += $val[$k];
            }
        }

        if ($values->all) {
            $cli->writeln($cli->green('Server: ' . $key . ' (Version: ' . $val['version'] . ' - ' . $val['threads'] . ' thread(s))'));
            _outputInfo($val, $cli);
            if (--$i || $values->summary) {
                $cli->writeln();
            }
        }
    }

    if ($values->summary) {
        $cli->writeln($cli->green('Memcache pool (' . $s_count . ' active server(s))'));
        if ($s_count) {
            _outputInfo($total, $cli);
        }
    }
}

function _outputInfo($val, $cli)
{
    $cli->writeln($cli->indent('Size:          ' . sprintf("%0.2f", $val['bytes'] / 1048576) . ' MB (Max: ' . sprintf("%0.2f", ($val['limit_maxbytes']) / 1048576) . ' MB - ' . ((!empty($val['limit_maxbytes']) ? round(($val['bytes'] / $val['limit_maxbytes']) * 100, 1) : 'N/A')) . '% used)'));
    $cli->writeln($cli->indent('Items:         ' . $val['curr_items'] . ' (Total: ' . $val['total_items'] . ')'));
    $cli->writeln($cli->indent('Cache Ratio:   ' . $val['get_hits'] . ' hits, ' . $val['get_misses'] . ' misses'));
    $cli->writeln($cli->indent('Connections:   ' . $val['curr_connections']));
    $cli->writeln($cli->indent('Traffic:       ' . sprintf("%0.2f", $val['bytes_read'] / 1048576) . ' MB in, ' . sprintf("%0.2f", $val['bytes_written'] / 1048576) . ' MB out'));
}
